PostgreSQL Source Code  git master
trigger.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * trigger.h
4  * Declarations for trigger handling.
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/commands/trigger.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef TRIGGER_H
14 #define TRIGGER_H
15 
16 #include "access/tableam.h"
17 #include "catalog/objectaddress.h"
18 #include "nodes/execnodes.h"
19 #include "nodes/parsenodes.h"
20 
21 /*
22  * TriggerData is the node type that is passed as fmgr "context" info
23  * when a function is called by the trigger manager.
24  */
25 
26 #define CALLED_AS_TRIGGER(fcinfo) \
27  ((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
28 
30 
31 typedef struct TriggerData
32 {
45 
46 /*
47  * The state for capturing old and new tuples into transition tables for a
48  * single ModifyTable node (or other operation source, e.g. copyfrom.c).
49  *
50  * This is per-caller to avoid conflicts in setting
51  * tcs_original_insert_tuple. Note, however, that the pointed-to
52  * private data may be shared across multiple callers.
53  */
54 struct AfterTriggersTableData; /* private in trigger.c */
55 
56 typedef struct TransitionCaptureState
57 {
58  /*
59  * Is there at least one trigger specifying each transition relation on
60  * the relation explicitly named in the DML statement or COPY command?
61  * Note: in current usage, these flags could be part of the private state,
62  * but it seems possibly useful to let callers see them.
63  */
68 
69  /*
70  * For INSERT and COPY, it would be wasteful to convert tuples from child
71  * format to parent format after they have already been converted in the
72  * opposite direction during routing. In that case we bypass conversion
73  * and allow the inserting code (copyfrom.c and nodeModifyTable.c) to
74  * provide a slot containing the original tuple directly.
75  */
77 
78  /*
79  * Private data including the tuplestore(s) into which to insert tuples.
80  */
83 
84 /*
85  * TriggerEvent bit flags
86  *
87  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
88  * can't be OR'd together in a single TriggerEvent. This is unlike the
89  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
90  * representation!
91  */
92 #define TRIGGER_EVENT_INSERT 0x00000000
93 #define TRIGGER_EVENT_DELETE 0x00000001
94 #define TRIGGER_EVENT_UPDATE 0x00000002
95 #define TRIGGER_EVENT_TRUNCATE 0x00000003
96 #define TRIGGER_EVENT_OPMASK 0x00000003
97 
98 #define TRIGGER_EVENT_ROW 0x00000004
99 
100 #define TRIGGER_EVENT_BEFORE 0x00000008
101 #define TRIGGER_EVENT_AFTER 0x00000000
102 #define TRIGGER_EVENT_INSTEAD 0x00000010
103 #define TRIGGER_EVENT_TIMINGMASK 0x00000018
104 
105 /* More TriggerEvent flags, used only within trigger.c */
106 
107 #define AFTER_TRIGGER_DEFERRABLE 0x00000020
108 #define AFTER_TRIGGER_INITDEFERRED 0x00000040
109 
110 #define TRIGGER_FIRED_BY_INSERT(event) \
111  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
112 
113 #define TRIGGER_FIRED_BY_DELETE(event) \
114  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
115 
116 #define TRIGGER_FIRED_BY_UPDATE(event) \
117  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
118 
119 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
120  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
121 
122 #define TRIGGER_FIRED_FOR_ROW(event) \
123  ((event) & TRIGGER_EVENT_ROW)
124 
125 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
126  (!TRIGGER_FIRED_FOR_ROW(event))
127 
128 #define TRIGGER_FIRED_BEFORE(event) \
129  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
130 
131 #define TRIGGER_FIRED_AFTER(event) \
132  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
133 
134 #define TRIGGER_FIRED_INSTEAD(event) \
135  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
136 
137 /*
138  * Definitions for replication role based firing.
139  */
140 #define SESSION_REPLICATION_ROLE_ORIGIN 0
141 #define SESSION_REPLICATION_ROLE_REPLICA 1
142 #define SESSION_REPLICATION_ROLE_LOCAL 2
144 
145 /*
146  * States at which a trigger can be fired. These are the
147  * possible values for pg_trigger.tgenabled.
148  */
149 #define TRIGGER_FIRES_ON_ORIGIN 'O'
150 #define TRIGGER_FIRES_ALWAYS 'A'
151 #define TRIGGER_FIRES_ON_REPLICA 'R'
152 #define TRIGGER_DISABLED 'D'
153 
154 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
155  Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
156  Oid funcoid, Oid parentTriggerOid, Node *whenClause,
157  bool isInternal, bool in_partition);
158 extern ObjectAddress CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
159  Oid relOid, Oid refRelOid, Oid constraintOid,
160  Oid indexOid, Oid funcoid, Oid parentTriggerOid,
161  Node *whenClause, bool isInternal, bool in_partition,
162  char trigger_fires_when);
163 
164 extern void TriggerSetParentTrigger(Relation trigRel,
165  Oid childTrigId,
166  Oid parentTrigId,
167  Oid childTableId);
168 extern void RemoveTriggerById(Oid trigOid);
169 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
170 
171 extern ObjectAddress renametrig(RenameStmt *stmt);
172 
173 extern void EnableDisableTrigger(Relation rel, const char *tgname,
174  char fires_when, bool skip_system, LOCKMODE lockmode);
175 
176 extern void RelationBuildTriggers(Relation relation);
177 
178 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
179 
180 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
181 
184 
185 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
186 
187 extern void ExecBSInsertTriggers(EState *estate,
188  ResultRelInfo *relinfo);
189 extern void ExecASInsertTriggers(EState *estate,
190  ResultRelInfo *relinfo,
191  TransitionCaptureState *transition_capture);
192 extern bool ExecBRInsertTriggers(EState *estate,
193  ResultRelInfo *relinfo,
194  TupleTableSlot *slot);
195 extern void ExecARInsertTriggers(EState *estate,
196  ResultRelInfo *relinfo,
197  TupleTableSlot *slot,
198  List *recheckIndexes,
199  TransitionCaptureState *transition_capture);
200 extern bool ExecIRInsertTriggers(EState *estate,
201  ResultRelInfo *relinfo,
202  TupleTableSlot *slot);
203 extern void ExecBSDeleteTriggers(EState *estate,
204  ResultRelInfo *relinfo);
205 extern void ExecASDeleteTriggers(EState *estate,
206  ResultRelInfo *relinfo,
207  TransitionCaptureState *transition_capture);
208 extern bool ExecBRDeleteTriggers(EState *estate,
209  EPQState *epqstate,
210  ResultRelInfo *relinfo,
211  ItemPointer tupleid,
212  HeapTuple fdw_trigtuple,
213  TupleTableSlot **epqslot);
214 extern void ExecARDeleteTriggers(EState *estate,
215  ResultRelInfo *relinfo,
216  ItemPointer tupleid,
217  HeapTuple fdw_trigtuple,
218  TransitionCaptureState *transition_capture,
219  bool is_crosspart_update);
220 extern bool ExecIRDeleteTriggers(EState *estate,
221  ResultRelInfo *relinfo,
222  HeapTuple trigtuple);
223 extern void ExecBSUpdateTriggers(EState *estate,
224  ResultRelInfo *relinfo);
225 extern void ExecASUpdateTriggers(EState *estate,
226  ResultRelInfo *relinfo,
227  TransitionCaptureState *transition_capture);
228 extern bool ExecBRUpdateTriggers(EState *estate,
229  EPQState *epqstate,
230  ResultRelInfo *relinfo,
231  ItemPointer tupleid,
232  HeapTuple fdw_trigtuple,
233  TupleTableSlot *slot,
234  TM_FailureData *tmfdp);
235 extern void ExecARUpdateTriggers(EState *estate,
236  ResultRelInfo *relinfo,
237  ResultRelInfo *src_partinfo,
238  ResultRelInfo *dst_partinfo,
239  ItemPointer tupleid,
240  HeapTuple fdw_trigtuple,
241  TupleTableSlot *slot,
242  List *recheckIndexes,
243  TransitionCaptureState *transition_capture,
244  bool is_crosspart_update);
245 extern bool ExecIRUpdateTriggers(EState *estate,
246  ResultRelInfo *relinfo,
247  HeapTuple trigtuple,
248  TupleTableSlot *slot);
249 extern void ExecBSTruncateTriggers(EState *estate,
250  ResultRelInfo *relinfo);
251 extern void ExecASTruncateTriggers(EState *estate,
252  ResultRelInfo *relinfo);
253 
254 extern void AfterTriggerBeginXact(void);
255 extern void AfterTriggerBeginQuery(void);
256 extern void AfterTriggerEndQuery(EState *estate);
257 extern void AfterTriggerFireDeferred(void);
258 extern void AfterTriggerEndXact(bool isCommit);
259 extern void AfterTriggerBeginSubXact(void);
260 extern void AfterTriggerEndSubXact(bool isCommit);
261 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
262 extern bool AfterTriggerPendingOnRel(Oid relid);
263 
264 
265 /*
266  * in utils/adt/ri_triggers.c
267  */
268 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
269  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
270 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
271  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
272 extern bool RI_Initial_Check(Trigger *trigger,
273  Relation fk_rel, Relation pk_rel);
274 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
275  Relation pk_rel);
276 
277 /* result values for RI_FKey_trigger_type: */
278 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
279 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
280 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
281 
282 extern int RI_FKey_trigger_type(Oid tgfoid);
283 
284 #endif /* TRIGGER_H */
unsigned int uint32
Definition: c.h:441
#define PGDLLIMPORT
Definition: c.h:1331
const char * name
Definition: encode.c:561
int LOCKMODE
Definition: lockdefs.h:26
CmdType
Definition: nodes.h:719
NodeTag
Definition: nodes.h:27
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:51
Definition: nodes.h:574
struct AfterTriggersTableData * tcs_private
Definition: trigger.h:81
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:76
Tuplestorestate * tg_oldtable
Definition: trigger.h:41
NodeTag type
Definition: trigger.h:33
Relation tg_relation
Definition: trigger.h:35
const Bitmapset * tg_updatedcols
Definition: trigger.h:43
Tuplestorestate * tg_newtable
Definition: trigger.h:42
TriggerEvent tg_event
Definition: trigger.h:34
TupleTableSlot * tg_trigslot
Definition: trigger.h:39
HeapTuple tg_newtuple
Definition: trigger.h:37
TupleTableSlot * tg_newslot
Definition: trigger.h:40
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel, TupleTableSlot *old_slot, TupleTableSlot *new_slot)
Definition: ri_triggers.c:1262
uint32 TriggerEvent
Definition: trigger.h:29
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot, TM_FailureData *tmfdp)
Definition: trigger.c:2945
void AfterTriggerBeginXact(void)
Definition: trigger.c:4929
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:2786
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2408
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2472
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5287
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3213
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:68
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2154
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2822
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2626
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2565
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2099
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, TupleTableSlot *old_slot, TupleTableSlot *new_slot)
Definition: ri_triggers.c:1230
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3260
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1415
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2286
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:5597
ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString, Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid, Oid funcoid, Oid parentTriggerOid, Node *whenClause, bool isInternal, bool in_partition)
Definition: trigger.c:162
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:3147
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ResultRelInfo *src_partinfo, ResultRelInfo *dst_partinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:3089
struct TriggerData TriggerData
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2927
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2677
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1512
void AfterTriggerFireDeferred(void)
Definition: trigger.c:5135
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2548
void TriggerSetParentTrigger(Relation trigRel, Oid childTrigId, Oid parentTrigId, Oid childTableId)
Definition: trigger.c:1265
struct TransitionCaptureState TransitionCaptureState
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system, LOCKMODE lockmode)
Definition: trigger.c:1768
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4825
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2459
ObjectAddress CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString, Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid, Oid funcoid, Oid parentTriggerOid, Node *whenClause, bool isInternal, bool in_partition, char trigger_fires_when)
Definition: trigger.c:179
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5191
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5912
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1369
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1870
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:5239
void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1649
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot)
Definition: trigger.c:2697
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2998
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4981
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1336
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2869
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4961