2 37.3. Writing Trigger Functions in C #
4 This section describes the low-level details of the interface to a
5 trigger function. This information is only needed when writing trigger
6 functions in C. If you are using a higher-level language then these
7 details are handled for you. In most cases you should consider using a
8 procedural language before writing your triggers in C. The
9 documentation of each procedural language explains how to write a
10 trigger in that language.
12 Trigger functions must use the “version 1” function manager interface.
14 When a function is called by the trigger manager, it is not passed any
15 normal arguments, but it is passed a “context” pointer pointing to a
16 TriggerData structure. C functions can check whether they were called
17 from the trigger manager or not by executing the macro:
18 CALLED_AS_TRIGGER(fcinfo)
21 ((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
23 If this returns true, then it is safe to cast fcinfo->context to type
24 TriggerData * and make use of the pointed-to TriggerData structure. The
25 function must not alter the TriggerData structure or any of the data it
28 struct TriggerData is defined in commands/trigger.h:
29 typedef struct TriggerData
32 TriggerEvent tg_event;
34 HeapTuple tg_trigtuple;
35 HeapTuple tg_newtuple;
37 TupleTableSlot *tg_trigslot;
38 TupleTableSlot *tg_newslot;
39 Tuplestorestate *tg_oldtable;
40 Tuplestorestate *tg_newtable;
41 const Bitmapset *tg_updatedcols;
44 where the members are defined as follows:
50 Describes the event for which the function is called. You can
51 use the following macros to examine tg_event:
53 TRIGGER_FIRED_BEFORE(tg_event)
54 Returns true if the trigger fired before the operation.
56 TRIGGER_FIRED_AFTER(tg_event)
57 Returns true if the trigger fired after the operation.
59 TRIGGER_FIRED_INSTEAD(tg_event)
60 Returns true if the trigger fired instead of the
63 TRIGGER_FIRED_FOR_ROW(tg_event)
64 Returns true if the trigger fired for a row-level event.
66 TRIGGER_FIRED_FOR_STATEMENT(tg_event)
67 Returns true if the trigger fired for a statement-level
70 TRIGGER_FIRED_BY_INSERT(tg_event)
71 Returns true if the trigger was fired by an INSERT
74 TRIGGER_FIRED_BY_UPDATE(tg_event)
75 Returns true if the trigger was fired by an UPDATE
78 TRIGGER_FIRED_BY_DELETE(tg_event)
79 Returns true if the trigger was fired by a DELETE command.
81 TRIGGER_FIRED_BY_TRUNCATE(tg_event)
82 Returns true if the trigger was fired by a TRUNCATE
86 A pointer to a structure describing the relation that the
87 trigger fired for. Look at utils/rel.h for details about this
88 structure. The most interesting things are tg_relation->rd_att
89 (descriptor of the relation tuples) and
90 tg_relation->rd_rel->relname (relation name; the type is not
91 char* but NameData; use SPI_getrelname(tg_relation) to get a
92 char* if you need a copy of the name).
95 A pointer to the row for which the trigger was fired. This is
96 the row being inserted, updated, or deleted. If this trigger was
97 fired for an INSERT or DELETE then this is what you should
98 return from the function if you don't want to replace the row
99 with a different one (in the case of INSERT) or skip the
100 operation. For triggers on foreign tables, values of system
101 columns herein are unspecified.
104 A pointer to the new version of the row, if the trigger was
105 fired for an UPDATE, and NULL if it is for an INSERT or a
106 DELETE. This is what you have to return from the function if the
107 event is an UPDATE and you don't want to replace this row by a
108 different one or skip the operation. For triggers on foreign
109 tables, values of system columns herein are unspecified.
112 A pointer to a structure of type Trigger, defined in
115 typedef struct Trigger
138 where tgname is the trigger's name, tgnargs is the number of
139 arguments in tgargs, and tgargs is an array of pointers to the
140 arguments specified in the CREATE TRIGGER statement. The other
141 members are for internal use only.
144 The slot containing tg_trigtuple, or a NULL pointer if there is
148 The slot containing tg_newtuple, or a NULL pointer if there is
152 A pointer to a structure of type Tuplestorestate containing zero
153 or more rows in the format specified by tg_relation, or a NULL
154 pointer if there is no OLD TABLE transition relation.
157 A pointer to a structure of type Tuplestorestate containing zero
158 or more rows in the format specified by tg_relation, or a NULL
159 pointer if there is no NEW TABLE transition relation.
162 For UPDATE triggers, a bitmap set indicating the columns that
163 were updated by the triggering command. Generic trigger
164 functions can use this to optimize actions by not having to deal
165 with columns that were not changed.
167 As an example, to determine whether a column with attribute
168 number attnum (1-based) is a member of this bitmap set, call
169 bms_is_member(attnum - FirstLowInvalidHeapAttributeNumber,
170 trigdata->tg_updatedcols)).
172 For triggers other than UPDATE triggers, this will be NULL.
174 To allow queries issued through SPI to reference transition tables, see
175 SPI_register_trigger_data.
177 A trigger function must return either a HeapTuple pointer or a NULL
178 pointer (not an SQL null value, that is, do not set isNull true). Be
179 careful to return either tg_trigtuple or tg_newtuple, as appropriate,
180 if you don't want to modify the row being operated on.