1997-09-24 16:17:14 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/spi/timetravel.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
1997-09-24 16:17:14 +08:00
|
|
|
* timetravel.c -- function to get time travel feature
|
|
|
|
* using general triggers.
|
2009-01-07 21:44:37 +08:00
|
|
|
*
|
2011-06-01 04:10:05 +08:00
|
|
|
* Modified by BÖJTHE Zoltán, Hungary, mailto:urdesobt@axelero.hu
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
2009-01-07 21:44:37 +08:00
|
|
|
#include "postgres.h"
|
1997-09-24 16:17:14 +08:00
|
|
|
|
2009-01-07 21:44:37 +08:00
|
|
|
#include <ctype.h>
|
2003-07-27 12:51:45 +08:00
|
|
|
|
2009-01-07 21:44:37 +08:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
2005-06-30 06:51:57 +08:00
|
|
|
#include "utils/nabstime.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
2005-06-30 06:51:57 +08:00
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
/* AbsoluteTime currabstime(void); */
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum timetravel(PG_FUNCTION_ARGS);
|
|
|
|
Datum set_timetravel(PG_FUNCTION_ARGS);
|
2003-07-27 12:59:29 +08:00
|
|
|
Datum get_timetravel(PG_FUNCTION_ARGS);
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
char *ident;
|
2007-03-16 07:12:07 +08:00
|
|
|
SPIPlanPtr splan;
|
2009-06-11 22:49:15 +08:00
|
|
|
} EPlan;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
static EPlan *Plans = NULL; /* for UPDATE/DELETE */
|
|
|
|
static int nPlans = 0;
|
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
typedef struct _TTOffList
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
struct _TTOffList *next;
|
|
|
|
char name[1];
|
2009-06-11 22:49:15 +08:00
|
|
|
} TTOffList;
|
2003-07-27 12:51:45 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static TTOffList TTOff = {NULL, {0}};
|
1997-09-24 16:17:14 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static int findTTStatus(char *name);
|
2009-06-11 22:49:15 +08:00
|
|
|
static EPlan *find_plan(char *ident, EPlan **eplan, int *nplans);
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
/*
|
1998-02-26 12:46:47 +08:00
|
|
|
* timetravel () --
|
|
|
|
* 1. IF an update affects tuple with stop_date eq INFINITY
|
2003-07-27 12:51:45 +08:00
|
|
|
* then form (and return) new tuple with start_date eq current date
|
|
|
|
* and stop_date eq INFINITY [ and update_user eq current user ]
|
|
|
|
* and all other column values as in new tuple, and insert tuple
|
|
|
|
* with old data and stop_date eq current date
|
1998-02-26 12:46:47 +08:00
|
|
|
* 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
|
2003-07-27 12:51:45 +08:00
|
|
|
* [ and delete_user eq current user ]
|
1998-02-26 12:46:47 +08:00
|
|
|
* 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.
|
2003-07-27 12:51:45 +08:00
|
|
|
* [ and insert_user eq current user, update_user and delete_user
|
|
|
|
* eq NULL ]
|
1998-02-26 12:46:47 +08:00
|
|
|
*
|
1997-09-24 16:17:14 +08:00
|
|
|
* In CREATE TRIGGER you are to specify start_date and stop_date column
|
|
|
|
* names:
|
|
|
|
* EXECUTE PROCEDURE
|
2003-07-27 12:51:45 +08:00
|
|
|
* timetravel ('date_on', 'date_off' [,'insert_user', 'update_user', 'delete_user' ] ).
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
#define MaxAttrNum 5
|
|
|
|
#define MinAttrNum 2
|
|
|
|
|
|
|
|
#define a_time_on 0
|
|
|
|
#define a_time_off 1
|
|
|
|
#define a_ins_user 2
|
|
|
|
#define a_upd_user 3
|
|
|
|
#define a_del_user 4
|
|
|
|
|
2000-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(timetravel);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
Datum /* have to return HeapTuple to Executor */
|
2000-05-29 09:59:17 +08:00
|
|
|
timetravel(PG_FUNCTION_ARGS)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int argc;
|
|
|
|
char **args; /* arguments */
|
|
|
|
int attnum[MaxAttrNum]; /* fnumbers of start/stop columns */
|
2003-07-27 12:51:45 +08:00
|
|
|
Datum oldtimeon,
|
2003-08-04 08:43:34 +08:00
|
|
|
oldtimeoff;
|
2003-07-27 12:51:45 +08:00
|
|
|
Datum newtimeon,
|
2003-08-04 08:43:34 +08:00
|
|
|
newtimeoff,
|
|
|
|
newuser,
|
|
|
|
nulltext;
|
|
|
|
Datum *cvals; /* column values */
|
|
|
|
char *cnulls; /* column nulls */
|
|
|
|
char *relname; /* triggered relation name */
|
1997-09-24 16:17:14 +08:00
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple trigtuple;
|
|
|
|
HeapTuple newtuple = NULL;
|
|
|
|
HeapTuple rettuple;
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
2003-08-04 08:43:34 +08:00
|
|
|
int natts; /* # of attributes */
|
|
|
|
EPlan *plan; /* prepared plan */
|
1997-09-24 16:17:14 +08:00
|
|
|
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;
|
2003-08-04 08:43:34 +08:00
|
|
|
int ret;
|
|
|
|
int i;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some checks first...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Called by trigger manager ? */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2000-05-29 09:59:17 +08:00
|
|
|
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 */
|
2010-10-09 01:27:31 +08:00
|
|
|
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
|
|
|
|
elog(ERROR, "timetravel: must be fired for row");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Should be called BEFORE */
|
2010-10-09 01:27:31 +08:00
|
|
|
if (!TRIGGER_FIRED_BEFORE(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 ? */
|
2003-08-04 08:43:34 +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
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
2000-05-29 09:59:17 +08:00
|
|
|
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 */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (0 == findTTStatus(relname))
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-07-27 12:51:45 +08:00
|
|
|
/* 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
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
argc = trigger->tgnargs;
|
2003-08-04 08:43:34 +08:00
|
|
|
if (argc != MinAttrNum && argc != MaxAttrNum)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): invalid (!= %d or %d) number of arguments %d",
|
|
|
|
relname, MinAttrNum, MaxAttrNum, trigger->tgnargs);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
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
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < MinAttrNum; i++)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
attnum[i] = SPI_fnumber(tupdesc, args[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (attnum[i] < 0)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): there is no attribute %s", relname, args[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum[i]) != ABSTIMEOID)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): attribute %s must be of abstime type",
|
|
|
|
relname, args[i]);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
for (; i < argc; i++)
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
2003-07-27 12:51:45 +08:00
|
|
|
attnum[i] = SPI_fnumber(tupdesc, args[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (attnum[i] < 0)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): there is no attribute %s", relname, args[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum[i]) != TEXTOID)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): attribute %s must be of text type",
|
|
|
|
relname, args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create fields containing name */
|
2008-03-26 06:42:46 +08:00
|
|
|
newuser = CStringGetTextDatum(GetUserNameFromId(GetUserId()));
|
2003-07-27 12:51:45 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
nulltext = (Datum) NULL;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isinsert)
|
|
|
|
{ /* INSERT */
|
|
|
|
int chnattrs = 0;
|
|
|
|
int chattrs[MaxAttrNum];
|
|
|
|
Datum newvals[MaxAttrNum];
|
|
|
|
char newnulls[MaxAttrNum];
|
2003-07-27 12:51:45 +08:00
|
|
|
|
|
|
|
oldtimeon = SPI_getbinval(trigtuple, tupdesc, attnum[a_time_on], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
newvals[chnattrs] = GetCurrentAbsoluteTime();
|
2003-07-27 12:51:45 +08:00
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_time_on];
|
1997-10-03 02:01:57 +08:00
|
|
|
chnattrs++;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
oldtimeoff = SPI_getbinval(trigtuple, tupdesc, attnum[a_time_off], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
if ((chnattrs == 0 && DatumGetInt32(oldtimeon) >= NOEND_ABSTIME) ||
|
|
|
|
(chnattrs > 0 && DatumGetInt32(newvals[a_time_on]) >= NOEND_ABSTIME))
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s is infinity", relname, args[a_time_on]);
|
1997-10-03 02:01:57 +08:00
|
|
|
newvals[chnattrs] = NOEND_ABSTIME;
|
2003-07-27 12:51:45 +08:00
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_time_off];
|
1997-10-03 02:01:57 +08:00
|
|
|
chnattrs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
if ((chnattrs == 0 && DatumGetInt32(oldtimeon) > DatumGetInt32(oldtimeoff)) ||
|
|
|
|
(chnattrs > 0 && DatumGetInt32(newvals[a_time_on]) > DatumGetInt32(oldtimeoff)))
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s gt %s", relname, args[a_time_on], args[a_time_off]);
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
pfree(relname);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (chnattrs <= 0)
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(trigtuple);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (argc == MaxAttrNum)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
|
|
|
/* clear update_user value */
|
|
|
|
newvals[chnattrs] = nulltext;
|
|
|
|
newnulls[chnattrs] = 'n';
|
|
|
|
chattrs[chnattrs] = attnum[a_upd_user];
|
|
|
|
chnattrs++;
|
|
|
|
/* clear delete_user value */
|
|
|
|
newvals[chnattrs] = nulltext;
|
|
|
|
newnulls[chnattrs] = 'n';
|
|
|
|
chattrs[chnattrs] = attnum[a_del_user];
|
|
|
|
chnattrs++;
|
|
|
|
/* set insert_user value */
|
|
|
|
newvals[chnattrs] = newuser;
|
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_ins_user];
|
|
|
|
chnattrs++;
|
|
|
|
}
|
|
|
|
rettuple = SPI_modifytuple(rel, trigtuple, chnattrs, chattrs, newvals, newnulls);
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
2003-07-27 12:51:45 +08:00
|
|
|
/* end of INSERT */
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2004-10-07 23:21:58 +08:00
|
|
|
/* UPDATE/DELETE: */
|
2003-07-27 12:51:45 +08:00
|
|
|
oldtimeon = SPI_getbinval(trigtuple, tupdesc, attnum[a_time_on], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[a_time_on]);
|
|
|
|
|
|
|
|
oldtimeoff = SPI_getbinval(trigtuple, tupdesc, attnum[a_time_off], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[a_time_off]);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +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
|
|
|
*/
|
2003-08-04 08:43:34 +08:00
|
|
|
if (newtuple != NULL)
|
|
|
|
{ /* UPDATE */
|
2003-07-27 12:51:45 +08:00
|
|
|
newtimeon = SPI_getbinval(newtuple, tupdesc, attnum[a_time_on], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[a_time_on]);
|
|
|
|
|
|
|
|
newtimeoff = SPI_getbinval(newtuple, tupdesc, attnum[a_time_off], &isnull);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isnull)
|
2003-07-27 12:51:45 +08:00
|
|
|
elog(ERROR, "timetravel (%s): %s must be NOT NULL", relname, args[a_time_off]);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (oldtimeon != newtimeon || oldtimeoff != newtimeoff)
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-02 03:10:30 +08:00
|
|
|
elog(ERROR, "timetravel (%s): you cannot change %s and/or %s columns (use set_timetravel)",
|
2003-07-27 12:51:45 +08:00
|
|
|
relname, args[a_time_on], args[a_time_off]);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
if (oldtimeoff != NOEND_ABSTIME)
|
2005-10-15 10:49:52 +08:00
|
|
|
{ /* current record is a deleted/updated record */
|
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
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
newtimeoff = GetCurrentAbsoluteTime();
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Connect to SPI manager */
|
2003-08-04 08:43:34 +08:00
|
|
|
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));
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < natts; i++)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-07-27 12:51:45 +08:00
|
|
|
cvals[i] = SPI_getbinval(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) */
|
2005-10-15 10:49:52 +08:00
|
|
|
cvals[attnum[a_time_off] - 1] = newtimeoff; /* stop_date eq current date */
|
2003-07-27 12:51:45 +08:00
|
|
|
cnulls[attnum[a_time_off] - 1] = ' ';
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!newtuple)
|
|
|
|
{ /* DELETE */
|
|
|
|
if (argc == MaxAttrNum)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
cvals[attnum[a_del_user] - 1] = newuser; /* set delete user */
|
2003-07-27 12:51:45 +08:00
|
|
|
cnulls[attnum[a_del_user] - 1] = ' ';
|
|
|
|
}
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Construct ident string as TriggerName $ TriggeredRelationId and try to
|
|
|
|
* find prepared execution plan.
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
|
1997-09-24 16:17:14 +08:00
|
|
|
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 ... */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (plan->splan == NULL)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2007-03-16 07:12:07 +08:00
|
|
|
SPIPlanPtr pplan;
|
2003-08-04 08:43:34 +08:00
|
|
|
Oid *ctypes;
|
|
|
|
char sql[8192];
|
2004-08-29 13:07:03 +08:00
|
|
|
char separ = ' ';
|
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
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "INSERT INTO %s VALUES (", relname);
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 1; i <= natts; i++)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
|
|
|
ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!(tupdesc->attrs[i - 1]->attisdropped)) /* skip dropped columns */
|
2003-09-12 01:25:14 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%c$%d", separ, i);
|
|
|
|
separ = ',';
|
2003-09-12 01:25:14 +08:00
|
|
|
}
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-09-12 01:25:14 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ")");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-07-28 00:32:34 +08:00
|
|
|
elog(DEBUG4, "timetravel (%s) update: sql: %s", relname, sql);
|
2003-07-27 12:51:45 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/* Prepare plan for query */
|
|
|
|
pplan = SPI_prepare(sql, natts, ctypes);
|
2003-08-04 08:43:34 +08:00
|
|
|
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
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Remember that SPI_prepare places plan in current memory context -
|
2011-09-16 12:42:53 +08:00
|
|
|
* so, we have to save plan in Top memory context for later use.
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
2011-09-16 12:42:53 +08:00
|
|
|
if (SPI_keepplan(pplan))
|
|
|
|
elog(ERROR, "timetravel (%s): SPI_keepplan failed", relname);
|
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
|
|
|
|
2003-08-04 08:43:34 +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 ... */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (newtuple)
|
|
|
|
{ /* UPDATE */
|
|
|
|
int chnattrs = 0;
|
|
|
|
int chattrs[MaxAttrNum];
|
2003-07-27 12:51:45 +08:00
|
|
|
Datum newvals[MaxAttrNum];
|
|
|
|
char newnulls[MaxAttrNum];
|
|
|
|
|
|
|
|
newvals[chnattrs] = newtimeoff;
|
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_time_on];
|
|
|
|
chnattrs++;
|
|
|
|
|
|
|
|
newvals[chnattrs] = NOEND_ABSTIME;
|
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_time_off];
|
|
|
|
chnattrs++;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (argc == MaxAttrNum)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
|
|
|
/* set update_user value */
|
|
|
|
newvals[chnattrs] = newuser;
|
|
|
|
newnulls[chnattrs] = ' ';
|
|
|
|
chattrs[chnattrs] = attnum[a_upd_user];
|
|
|
|
chnattrs++;
|
|
|
|
/* clear delete_user value */
|
|
|
|
newvals[chnattrs] = nulltext;
|
|
|
|
newnulls[chnattrs] = 'n';
|
|
|
|
chattrs[chnattrs] = attnum[a_del_user];
|
|
|
|
chnattrs++;
|
|
|
|
/* set insert_user value */
|
|
|
|
newvals[chnattrs] = nulltext;
|
|
|
|
newnulls[chnattrs] = 'n';
|
|
|
|
chattrs[chnattrs] = attnum[a_ins_user];
|
|
|
|
chnattrs++;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
rettuple = SPI_modifytuple(rel, newtuple, chnattrs, chattrs, newvals, newnulls);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-09-24 16:17:14 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* SPI_copytuple allocates tmptuple in upper executor context - have
|
|
|
|
* to free allocation using SPI_pfree
|
1997-09-24 16:17:14 +08:00
|
|
|
*/
|
2003-07-28 00:32:34 +08:00
|
|
|
/* SPI_pfree(tmptuple); */
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
else
|
2004-10-07 23:21:58 +08:00
|
|
|
/* DELETE case */
|
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);
|
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-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(set_timetravel);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
set_timetravel(PG_FUNCTION_ARGS)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
Name relname = PG_GETARG_NAME(0);
|
|
|
|
int32 on = PG_GETARG_INT32(1);
|
|
|
|
char *rname;
|
|
|
|
char *d;
|
|
|
|
char *s;
|
2003-07-27 12:51:45 +08:00
|
|
|
int32 ret;
|
2003-08-04 08:43:34 +08:00
|
|
|
TTOffList *p,
|
|
|
|
*pp;
|
2003-07-27 12:51:45 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (pp = (p = &TTOff)->next; pp; pp = (p = pp)->next)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
if (namestrcmp(relname, pp->name) == 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
break;
|
2003-07-27 12:51:45 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
if (pp)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-07-27 12:51:45 +08:00
|
|
|
/* OFF currently */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (on != 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-07-27 12:51:45 +08:00
|
|
|
/* turn ON */
|
|
|
|
p->next = pp->next;
|
|
|
|
free(pp);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-07-27 12:51:45 +08:00
|
|
|
ret = 0;
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
2003-07-27 12:51:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ON currently */
|
2003-08-04 08:43:34 +08:00
|
|
|
if (on == 0)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
|
|
|
/* turn OFF */
|
|
|
|
s = rname = DatumGetCString(DirectFunctionCall1(nameout, NameGetDatum(relname)));
|
2003-08-04 08:43:34 +08:00
|
|
|
if (s)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
pp = malloc(sizeof(TTOffList) + strlen(rname));
|
|
|
|
if (pp)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
|
|
|
pp->next = NULL;
|
|
|
|
p->next = pp;
|
|
|
|
d = pp->name;
|
|
|
|
while (*s)
|
2003-08-04 08:43:34 +08:00
|
|
|
*d++ = tolower((unsigned char) *s++);
|
2003-07-27 12:51:45 +08:00
|
|
|
*d = '\0';
|
|
|
|
}
|
|
|
|
pfree(rname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
PG_RETURN_INT32(ret);
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
/*
|
|
|
|
* get_timetravel (relname) --
|
2003-08-04 08:43:34 +08:00
|
|
|
* get timetravel status for specified relation (ON/OFF)
|
2003-07-27 12:51:45 +08:00
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(get_timetravel);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
Datum
|
|
|
|
get_timetravel(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-07-27 12:59:29 +08:00
|
|
|
Name relname = PG_GETARG_NAME(0);
|
2003-08-04 08:43:34 +08:00
|
|
|
TTOffList *pp;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (pp = TTOff.next; pp; pp = pp->next)
|
2003-07-27 12:51:45 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
if (namestrcmp(relname, pp->name) == 0)
|
2003-07-27 12:51:45 +08:00
|
|
|
PG_RETURN_INT32(0);
|
|
|
|
}
|
2000-05-29 09:59:17 +08:00
|
|
|
PG_RETURN_INT32(1);
|
1997-09-24 16:17:14 +08:00
|
|
|
}
|
|
|
|
|
2003-07-27 12:51:45 +08:00
|
|
|
static int
|
|
|
|
findTTStatus(char *name)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
TTOffList *pp;
|
|
|
|
|
|
|
|
for (pp = TTOff.next; pp; pp = pp->next)
|
2004-05-07 08:24:59 +08:00
|
|
|
if (pg_strcasecmp(name, pp->name) == 0)
|
2003-07-27 12:51:45 +08:00
|
|
|
return 0;
|
|
|
|
return 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
|
|
|
}
|
2003-07-27 12:51:45 +08:00
|
|
|
*/
|
1997-09-24 16:17:14 +08:00
|
|
|
|
|
|
|
static EPlan *
|
2009-06-11 22:49:15 +08:00
|
|
|
find_plan(char *ident, EPlan **eplan, int *nplans)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
EPlan *newp;
|
|
|
|
int i;
|
1997-09-24 16:17:14 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (*nplans > 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < *nplans; i++)
|
1997-09-24 16:17:14 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
if (strcmp((*eplan)[i].ident, ident) == 0)
|
1997-09-24 16:17:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
if (i != *nplans)
|
1997-09-24 16:17:14 +08:00
|
|
|
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);
|
|
|
|
}
|