PostgreSQL Source Code git master
moddatetime.c
Go to the documentation of this file.
1/*
2moddatetime.c
3
4contrib/spi/moddatetime.c
5
6What is this?
7It is a function to be called from a trigger for the purpose of updating
8a modification datetime stamp in a record when that record is UPDATEd.
9
10Credits
11This is 95%+ based on autoinc.c, which I used as a starting point as I do
12not really know what I am doing. I also had help from
13Jan Wieck <[email protected]> who told me about the timestamp_in("now") function.
14OH, me, I'm Terry Mackintosh <[email protected]>
15*/
16#include "postgres.h"
17
18#include "access/htup_details.h"
19#include "catalog/pg_type.h"
20#include "commands/trigger.h"
21#include "executor/spi.h"
22#include "utils/fmgrprotos.h"
23#include "utils/rel.h"
24
26 .name = "moddatetime",
27 .version = PG_VERSION
28);
29
31
34{
35 TriggerData *trigdata = (TriggerData *) fcinfo->context;
36 Trigger *trigger; /* to get trigger name */
37 int nargs; /* # of arguments */
38 int attnum; /* positional number of field to change */
39 Oid atttypid; /* type OID of field to change */
40 Datum newdt; /* The current datetime. */
41 bool newdtnull; /* null flag for it */
42 char **args; /* arguments */
43 char *relname; /* triggered relation name */
44 Relation rel; /* triggered relation */
45 HeapTuple rettuple = NULL;
46 TupleDesc tupdesc; /* tuple description */
47
48 if (!CALLED_AS_TRIGGER(fcinfo))
49 /* internal error */
50 elog(ERROR, "moddatetime: not fired by trigger manager");
51
52 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
53 /* internal error */
54 elog(ERROR, "moddatetime: must be fired for row");
55
56 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
57 /* internal error */
58 elog(ERROR, "moddatetime: must be fired before event");
59
60 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
61 /* internal error */
62 elog(ERROR, "moddatetime: cannot process INSERT events");
63 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
64 rettuple = trigdata->tg_newtuple;
65 else
66 /* internal error */
67 elog(ERROR, "moddatetime: cannot process DELETE events");
68
69 rel = trigdata->tg_relation;
71
72 trigger = trigdata->tg_trigger;
73
74 nargs = trigger->tgnargs;
75
76 if (nargs != 1)
77 /* internal error */
78 elog(ERROR, "moddatetime (%s): A single argument was expected", relname);
79
80 args = trigger->tgargs;
81 /* must be the field layout? */
82 tupdesc = rel->rd_att;
83
84 /*
85 * This gets the position in the tuple of the field we want. args[0] being
86 * the name of the field to update, as passed in from the trigger.
87 */
88 attnum = SPI_fnumber(tupdesc, args[0]);
89
90 /*
91 * This is where we check to see if the field we are supposed to update
92 * even exists.
93 */
94 if (attnum <= 0)
96 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
97 errmsg("\"%s\" has no attribute \"%s\"",
98 relname, args[0])));
99
100 /*
101 * Check the target field has an allowed type, and get the current
102 * datetime as a value of that type.
103 */
104 atttypid = SPI_gettypeid(tupdesc, attnum);
105 if (atttypid == TIMESTAMPOID)
107 CStringGetDatum("now"),
109 Int32GetDatum(-1));
110 else if (atttypid == TIMESTAMPTZOID)
112 CStringGetDatum("now"),
114 Int32GetDatum(-1));
115 else
116 {
118 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
119 errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP or TIMESTAMPTZ",
120 args[0], relname)));
121 newdt = (Datum) 0; /* keep compiler quiet */
122 }
123 newdtnull = false;
124
125 /* Replace the attnum'th column with newdt */
126 rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
127 1, &attnum, &newdt, &newdtnull);
128
129 /* Clean up */
130 pfree(relname);
131
132 return PointerGetDatum(rettuple);
133}
Datum timestamp_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:166
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:418
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:686
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
Definition: heaptuple.c:1278
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void pfree(void *pointer)
Definition: mcxt.c:2152
PG_MODULE_MAGIC_EXT(.name="moddatetime",.version=PG_VERSION)
PG_FUNCTION_INFO_V1(moddatetime)
Datum moddatetime(PG_FUNCTION_ARGS)
Definition: moddatetime.c:33
int16 attnum
Definition: pg_attribute.h:74
NameData relname
Definition: pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1176
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1309
char * SPI_getrelname(Relation rel)
Definition: spi.c:1327
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
int16 tgnargs
Definition: reltrigger.h:38
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116
const char * name