1997-09-24 16:17:14 +08:00
|
|
|
/*
|
|
|
|
* timetravel.c -- function to get time travel feature
|
|
|
|
* using general triggers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "executor/spi.h" /* this is what you need to work with SPI */
|
|
|
|
#include "commands/trigger.h" /* -"- and triggers */
|
|
|
|
#include <ctype.h> /* tolower () */
|
|
|
|
|
|
|
|
#define ABSTIMEOID 702 /* it should be in pg_type.h */
|
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
AbsoluteTime currabstime(void);
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum timetravel(PG_FUNCTION_ARGS);
|
|
|
|
Datum set_timetravel(PG_FUNCTION_ARGS);
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
char *ident;
|
|
|
|
void *splan;
|
|
|
|
} EPlan;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
static EPlan *Plans = NULL; /* for UPDATE/DELETE */
|
|
|
|
static int nPlans = 0;
|
|
|
|
|
1998-02-26 12:46:47 +08:00
|
|
|
static char **TTOff = NULL;
|
|
|
|
static int nTTOff = 0;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
static EPlan *find_plan(char *ident, EPlan ** eplan, int *nplans);
|
|
|
|
|
|
|
|
/*
|
1998-02-26 12:46:47 +08:00
|
|
|
* timetravel () --
|
|
|
|
* 1. IF an update affects tuple with stop_date eq INFINITY
|
|
|
|
* then form (and return) new tuple with stop_date eq current date
|
|
|
|
* and all other column values as in old tuple, and insert tuple
|
|
|
|
* with new data and start_date eq current date and
|
|
|
|
* stop_date eq INFINITY
|
|
|
|
* ELSE - skip updation of tuple.
|
|
|
|
* 2. IF an delete affects tuple with stop_date eq INFINITY
|
|
|
|
* then insert the same tuple with stop_date eq current date
|
|
|
|
* ELSE - skip deletion of tuple.
|
|
|
|
* 3. On INSERT, if start_date is NULL then current date will be
|
|
|
|
* inserted, if stop_date is NULL then INFINITY will be inserted.
|
|
|
|
*
|
1997-09-24 16:17:14 +08:00
|
|
|
* In CREATE TRIGGER you are to specify start_date and stop_date column
|
|
|
|
* names:
|
|
|
|
* EXECUTE PROCEDURE
|
|
|
|
* timetravel ('date_on', 'date_off').
|
|
|
|
*/
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
timetravel(PG_FUNCTION_ARGS)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1997-09-24 16:17:14 +08:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
char **args; /* arguments */
|
|
|
|
int attnum[2]; /* fnumbers of start/stop columns */
|
1998-02-26 12:46:47 +08:00
|
|
|
Datum oldon,
|
|
|
|
oldoff;
|
|
|
|
Datum newon,
|
|
|
|
newoff;
|
1997-09-24 16:17:14 +08:00
|
|
|
Datum *cvals; /* column values */
|
|
|
|
char *cnulls; /* column nulls */
|
|
|
|
char *relname; /* triggered relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple trigtuple;
|
|
|
|
HeapTuple newtuple = NULL;
|
|
|
|
HeapTuple rettuple;
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
int natts; /* # of attributes */
|
|
|
|
EPlan *plan; /* prepared plan */
|
|
|
|
char ident[2 * NAMEDATALEN];
|
|
|
|
bool isnull; /* to know is some column NULL or not */
|
1997-10-03 02:01:57 +08:00
|
|
|
bool isinsert = false;
|
1997-09-24 16:17:14 +08:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some checks first...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Called by trigger manager ? */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
elog(ERROR, "timetravel: not fired by trigger manager");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Should be called for ROW trigger */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel: can't process STATEMENT events");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Should be called BEFORE */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel: must be fired before event");
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
/* INSERT ? */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
1997-10-03 02:01:57 +08:00
|
|
|
isinsert = true;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
|
|
|
newtuple = trigdata->tg_newtuple;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
trigtuple = trigdata->tg_trigtuple;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
rel = trigdata->tg_relation;
|
1997-09-24 16:17:14 +08:00
|
|
|
relname = SPI_getrelname(rel);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* check if TT is OFF for this relation */
|
|
|
|
for (i = 0; i < nTTOff; i++)
|
1998-02-26 12:46:47 +08:00
|
|
|
if (strcasecmp(TTOff[i], relname) == 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
break;
|
|
|
|
if (i < nTTOff) /* OFF - nothing to do */
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
pfree(relname);
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum((newtuple != NULL) ? newtuple : trigtuple);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
trigger = trigdata->tg_trigger;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
if (trigger->tgnargs != 2)
|
1998-02-26 12:46:47 +08:00
|
|
|
elog(ERROR, "timetravel (%s): invalid (!= 2) number of arguments %d",
|
|
|
|
relname, trigger->tgnargs);
|
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
args = trigger->tgargs;
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
attnum[i] = SPI_fnumber(tupdesc, args[i]);
|
|
|
|
if (attnum[i] < 0)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): there is no attribute %s", relname, args[i]);
|
1998-02-26 12:46:47 +08:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum[i]) != ABSTIMEOID)
|
|
|
|
elog(ERROR, "timetravel (%s): attributes %s and %s must be of abstime type",
|
|
|
|
relname, args[0], args[1]);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
if (isinsert) /* INSERT */
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
int chnattrs = 0;
|
|
|
|
int chattrs[2];
|
|
|
|
Datum newvals[2];
|
|
|
|
|
|
|
|
oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
|
1997-10-03 02:01:57 +08:00
|
|
|
if (isnull)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
newvals[chnattrs] = GetCurrentAbsoluteTime();
|
1997-10-03 02:01:57 +08:00
|
|
|
chattrs[chnattrs] = attnum[0];
|
|
|
|
chnattrs++;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
|
1997-10-03 02:01:57 +08:00
|
|
|
if (isnull)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
if ((chnattrs == 0 && DatumGetInt32(oldon) >= NOEND_ABSTIME) ||
|
|
|
|
(chnattrs > 0 && DatumGetInt32(newvals[0]) >= NOEND_ABSTIME))
|
|
|
|
elog(ERROR, "timetravel (%s): %s ge %s",
|
|
|
|
relname, args[0], args[1]);
|
1997-10-03 02:01:57 +08:00
|
|
|
newvals[chnattrs] = NOEND_ABSTIME;
|
|
|
|
chattrs[chnattrs] = attnum[1];
|
|
|
|
chnattrs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
if ((chnattrs == 0 && DatumGetInt32(oldon) >=
|
|
|
|
DatumGetInt32(oldoff)) ||
|
|
|
|
(chnattrs > 0 && DatumGetInt32(newvals[0]) >=
|
|
|
|
DatumGetInt32(oldoff)))
|
|
|
|
elog(ERROR, "timetravel (%s): %s ge %s",
|
|
|
|
relname, args[0], args[1]);
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
pfree(relname);
|
|
|
|
if (chnattrs <= 0)
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(trigtuple);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
rettuple = SPI_modifytuple(rel, trigtuple, chnattrs,
|
|
|
|
chattrs, newvals, NULL);
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
oldon = SPI_getbinval(trigtuple, tupdesc, attnum[0], &isnull);
|
1997-09-24 16:17:14 +08:00
|
|
|
if (isnull)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[0]);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
oldoff = SPI_getbinval(trigtuple, tupdesc, attnum[1], &isnull);
|
1997-09-24 16:17:14 +08:00
|
|
|
if (isnull)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[1]);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
1998-02-26 12:46:47 +08:00
|
|
|
* If DELETE/UPDATE of tuple with stop_date neq INFINITY then say
|
|
|
|
* upper Executor to skip operation for this tuple
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
1998-02-26 12:46:47 +08:00
|
|
|
if (newtuple != NULL) /* UPDATE */
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
newon = SPI_getbinval(newtuple, tupdesc, attnum[0], &isnull);
|
1997-09-24 16:17:14 +08:00
|
|
|
if (isnull)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[0]);
|
1998-02-26 12:46:47 +08:00
|
|
|
newoff = SPI_getbinval(newtuple, tupdesc, attnum[1], &isnull);
|
1997-09-24 16:17:14 +08:00
|
|
|
if (isnull)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[1]);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
if (oldon != newon || oldoff != newoff)
|
|
|
|
elog(ERROR, "timetravel (%s): you can't change %s and/or %s columns (use set_timetravel)",
|
|
|
|
relname, args[0], args[1]);
|
|
|
|
|
|
|
|
if (newoff != NOEND_ABSTIME)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
pfree(relname); /* allocated in upper executor context */
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(NULL);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
else if (oldoff != NOEND_ABSTIME) /* DELETE */
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
pfree(relname);
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(NULL);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
newoff = GetCurrentAbsoluteTime();
|
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Connect to SPI manager */
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): SPI_connect returned %d", relname, ret);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Fetch tuple values and nulls */
|
1998-02-26 12:46:47 +08:00
|
|
|
cvals = (Datum *) palloc(natts * sizeof(Datum));
|
|
|
|
cnulls = (char *) palloc(natts * sizeof(char));
|
1997-09-24 16:17:14 +08:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
cvals[i] = SPI_getbinval((newtuple != NULL) ? newtuple : trigtuple,
|
|
|
|
tupdesc, i + 1, &isnull);
|
1997-09-24 16:17:14 +08:00
|
|
|
cnulls[i] = (isnull) ? 'n' : ' ';
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* change date column(s) */
|
1998-02-26 12:46:47 +08:00
|
|
|
if (newtuple) /* UPDATE */
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
cvals[attnum[0] - 1] = newoff; /* start_date eq current date */
|
1997-09-24 16:17:14 +08:00
|
|
|
cnulls[attnum[0] - 1] = ' ';
|
|
|
|
cvals[attnum[1] - 1] = NOEND_ABSTIME; /* stop_date eq INFINITY */
|
|
|
|
cnulls[attnum[1] - 1] = ' ';
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
else
|
|
|
|
/* DELETE */
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
cvals[attnum[1] - 1] = newoff; /* stop_date eq current date */
|
1997-09-24 16:17:14 +08:00
|
|
|
cnulls[attnum[1] - 1] = ' ';
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
1998-02-26 12:46:47 +08:00
|
|
|
* Construct ident string as TriggerName $ TriggeredRelationId and try
|
|
|
|
* to find prepared execution plan.
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
|
|
|
sprintf(ident, "%s$%u", trigger->tgname, rel->rd_id);
|
|
|
|
plan = find_plan(ident, &Plans, &nPlans);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* if there is no plan ... */
|
|
|
|
if (plan->splan == NULL)
|
|
|
|
{
|
|
|
|
void *pplan;
|
|
|
|
Oid *ctypes;
|
|
|
|
char sql[8192];
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* allocate ctypes for preparation */
|
|
|
|
ctypes = (Oid *) palloc(natts * sizeof(Oid));
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
1998-02-26 12:46:47 +08:00
|
|
|
* Construct query: INSERT INTO _relation_ VALUES ($1, ...)
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
|
|
|
sprintf(sql, "INSERT INTO %s VALUES (", relname);
|
|
|
|
for (i = 1; i <= natts; i++)
|
|
|
|
{
|
|
|
|
sprintf(sql + strlen(sql), "$%d%s",
|
1998-02-26 12:46:47 +08:00
|
|
|
i, (i < natts) ? ", " : ")");
|
1997-09-24 16:17:14 +08:00
|
|
|
ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Prepare plan for query */
|
|
|
|
pplan = SPI_prepare(sql, natts, ctypes);
|
|
|
|
if (pplan == NULL)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): SPI_prepare returned %d", relname, SPI_result);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
|
|
|
* Remember that SPI_prepare places plan in current memory context
|
|
|
|
* - so, we have to save plan in Top memory context for latter
|
|
|
|
* use.
|
|
|
|
*/
|
|
|
|
pplan = SPI_saveplan(pplan);
|
|
|
|
if (pplan == NULL)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): SPI_saveplan returned %d", relname, SPI_result);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
plan->splan = pplan;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
|
|
|
* Ok, execute prepared plan.
|
|
|
|
*/
|
|
|
|
ret = SPI_execp(plan->splan, cvals, cnulls, 0);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
if (ret < 0)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "timetravel (%s): SPI_execp returned %d", relname, ret);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Tuple to return to upper Executor ... */
|
1998-02-26 12:46:47 +08:00
|
|
|
if (newtuple) /* UPDATE */
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
|
|
|
HeapTuple tmptuple;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
tmptuple = SPI_copytuple(trigtuple);
|
|
|
|
rettuple = SPI_modifytuple(rel, tmptuple, 1, &(attnum[1]), &newoff, NULL);
|
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
|
|
|
* SPI_copytuple allocates tmptuple in upper executor context -
|
|
|
|
* have to free allocation using SPI_pfree
|
|
|
|
*/
|
1998-02-26 12:46:47 +08:00
|
|
|
SPI_pfree(tmptuple);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
else
|
|
|
|
/* DELETE */
|
1997-09-24 16:17:14 +08:00
|
|
|
rettuple = trigtuple;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
SPI_finish(); /* don't forget say Bye to SPI mgr */
|
|
|
|
|
|
|
|
pfree(relname);
|
1997-09-24 16:17:14 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-05-29 09:59:17 +08:00
|
|
|
* set_timetravel (relname, on) --
|
1998-02-26 12:46:47 +08:00
|
|
|
* turn timetravel for specified relation ON/OFF
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
set_timetravel(PG_FUNCTION_ARGS)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
Name relname = PG_GETARG_NAME(0);
|
|
|
|
int32 on = PG_GETARG_INT32(1);
|
1998-02-26 12:46:47 +08:00
|
|
|
char *rname;
|
|
|
|
char *d;
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
for (i = 0; i < nTTOff; i++)
|
1998-02-26 12:46:47 +08:00
|
|
|
if (namestrcmp(relname, TTOff[i]) == 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
break;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
if (i < nTTOff) /* OFF currently */
|
|
|
|
{
|
|
|
|
if (on == 0)
|
2000-05-29 09:59:17 +08:00
|
|
|
PG_RETURN_INT32(0);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* turn ON */
|
1998-02-26 12:46:47 +08:00
|
|
|
free(TTOff[i]);
|
1997-09-24 16:17:14 +08:00
|
|
|
if (nTTOff == 1)
|
1998-02-26 12:46:47 +08:00
|
|
|
free(TTOff);
|
1997-09-24 16:17:14 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i < nTTOff - 1)
|
1998-02-26 12:46:47 +08:00
|
|
|
memcpy(&(TTOff[i]), &(TTOff[i + 1]), (nTTOff - i) * sizeof(char *));
|
|
|
|
TTOff = realloc(TTOff, (nTTOff - 1) * sizeof(char *));
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
nTTOff--;
|
2000-05-29 09:59:17 +08:00
|
|
|
PG_RETURN_INT32(0);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* ON currently */
|
|
|
|
if (on != 0)
|
2000-05-29 09:59:17 +08:00
|
|
|
PG_RETURN_INT32(1);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* turn OFF */
|
|
|
|
if (nTTOff == 0)
|
1998-02-26 12:46:47 +08:00
|
|
|
TTOff = malloc(sizeof(char *));
|
1997-09-24 16:17:14 +08:00
|
|
|
else
|
1998-02-26 12:46:47 +08:00
|
|
|
TTOff = realloc(TTOff, (nTTOff + 1) * sizeof(char *));
|
2000-08-04 00:35:08 +08:00
|
|
|
s = rname = DatumGetCString(DirectFunctionCall1(nameout,
|
|
|
|
NameGetDatum(relname)));
|
1998-02-26 12:46:47 +08:00
|
|
|
d = TTOff[nTTOff] = malloc(strlen(rname) + 1);
|
1997-09-24 16:17:14 +08:00
|
|
|
while (*s)
|
1998-02-26 12:46:47 +08:00
|
|
|
*d++ = tolower(*s++);
|
1997-09-24 16:17:14 +08:00
|
|
|
*d = 0;
|
1998-02-26 12:46:47 +08:00
|
|
|
pfree(rname);
|
1997-09-24 16:17:14 +08:00
|
|
|
nTTOff++;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
PG_RETURN_INT32(1);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AbsoluteTime
|
1998-02-26 12:46:47 +08:00
|
|
|
currabstime()
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
return (GetCurrentAbsoluteTime());
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static EPlan *
|
|
|
|
find_plan(char *ident, EPlan ** eplan, int *nplans)
|
|
|
|
{
|
|
|
|
EPlan *newp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*nplans > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < *nplans; i++)
|
|
|
|
{
|
|
|
|
if (strcmp((*eplan)[i].ident, ident) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i != *nplans)
|
|
|
|
return (*eplan + i);
|
|
|
|
*eplan = (EPlan *) realloc(*eplan, (i + 1) * sizeof(EPlan));
|
|
|
|
newp = *eplan + i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newp = *eplan = (EPlan *) malloc(sizeof(EPlan));
|
|
|
|
(*nplans) = i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
newp->ident = (char *) malloc(strlen(ident) + 1);
|
|
|
|
strcpy(newp->ident, ident);
|
|
|
|
newp->splan = NULL;
|
|
|
|
(*nplans)++;
|
|
|
|
|
|
|
|
return (newp);
|
|
|
|
}
|