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-2024, 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 *trigname, bool missing_ok);
170 
172 
173 extern void EnableDisableTrigger(Relation rel, const char *tgname, Oid tgparent,
174  char fires_when, bool skip_system, bool recurse,
175  LOCKMODE lockmode);
176 
177 extern void RelationBuildTriggers(Relation relation);
178 
179 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
180 
181 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
182 
185 
186 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
187 
188 extern void ExecBSInsertTriggers(EState *estate,
189  ResultRelInfo *relinfo);
190 extern void ExecASInsertTriggers(EState *estate,
191  ResultRelInfo *relinfo,
192  TransitionCaptureState *transition_capture);
193 extern bool ExecBRInsertTriggers(EState *estate,
194  ResultRelInfo *relinfo,
195  TupleTableSlot *slot);
196 extern void ExecARInsertTriggers(EState *estate,
197  ResultRelInfo *relinfo,
198  TupleTableSlot *slot,
199  List *recheckIndexes,
200  TransitionCaptureState *transition_capture);
201 extern bool ExecIRInsertTriggers(EState *estate,
202  ResultRelInfo *relinfo,
203  TupleTableSlot *slot);
204 extern void ExecBSDeleteTriggers(EState *estate,
205  ResultRelInfo *relinfo);
206 extern void ExecASDeleteTriggers(EState *estate,
207  ResultRelInfo *relinfo,
208  TransitionCaptureState *transition_capture);
209 extern bool ExecBRDeleteTriggers(EState *estate,
210  EPQState *epqstate,
211  ResultRelInfo *relinfo,
212  ItemPointer tupleid,
213  HeapTuple fdw_trigtuple,
214  TupleTableSlot **epqslot,
215  TM_Result *tmresult,
216  TM_FailureData *tmfd);
217 extern void ExecARDeleteTriggers(EState *estate,
218  ResultRelInfo *relinfo,
219  ItemPointer tupleid,
220  HeapTuple fdw_trigtuple,
221  TransitionCaptureState *transition_capture,
222  bool is_crosspart_update);
223 extern bool ExecIRDeleteTriggers(EState *estate,
224  ResultRelInfo *relinfo,
225  HeapTuple trigtuple);
226 extern void ExecBSUpdateTriggers(EState *estate,
227  ResultRelInfo *relinfo);
228 extern void ExecASUpdateTriggers(EState *estate,
229  ResultRelInfo *relinfo,
230  TransitionCaptureState *transition_capture);
231 extern bool ExecBRUpdateTriggers(EState *estate,
232  EPQState *epqstate,
233  ResultRelInfo *relinfo,
234  ItemPointer tupleid,
235  HeapTuple fdw_trigtuple,
236  TupleTableSlot *newslot,
237  TM_Result *tmresult,
238  TM_FailureData *tmfd);
239 extern void ExecARUpdateTriggers(EState *estate,
240  ResultRelInfo *relinfo,
241  ResultRelInfo *src_partinfo,
242  ResultRelInfo *dst_partinfo,
243  ItemPointer tupleid,
244  HeapTuple fdw_trigtuple,
245  TupleTableSlot *newslot,
246  List *recheckIndexes,
247  TransitionCaptureState *transition_capture,
248  bool is_crosspart_update);
249 extern bool ExecIRUpdateTriggers(EState *estate,
250  ResultRelInfo *relinfo,
251  HeapTuple trigtuple,
252  TupleTableSlot *newslot);
253 extern void ExecBSTruncateTriggers(EState *estate,
254  ResultRelInfo *relinfo);
255 extern void ExecASTruncateTriggers(EState *estate,
256  ResultRelInfo *relinfo);
257 
258 extern void AfterTriggerBeginXact(void);
259 extern void AfterTriggerBeginQuery(void);
260 extern void AfterTriggerEndQuery(EState *estate);
261 extern void AfterTriggerFireDeferred(void);
262 extern void AfterTriggerEndXact(bool isCommit);
263 extern void AfterTriggerBeginSubXact(void);
264 extern void AfterTriggerEndSubXact(bool isCommit);
266 extern bool AfterTriggerPendingOnRel(Oid relid);
267 
268 
269 /*
270  * in utils/adt/ri_triggers.c
271  */
272 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
273  TupleTableSlot *oldslot, TupleTableSlot *newslot);
274 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
275  TupleTableSlot *oldslot, TupleTableSlot *newslot);
276 extern bool RI_Initial_Check(Trigger *trigger,
277  Relation fk_rel, Relation pk_rel);
278 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
279  Relation pk_rel);
280 
281 /* result values for RI_FKey_trigger_type: */
282 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
283 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
284 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
285 
286 extern int RI_FKey_trigger_type(Oid tgfoid);
287 
288 #endif /* TRIGGER_H */
unsigned int uint32
Definition: c.h:506
#define PGDLLIMPORT
Definition: c.h:1307
#define stmt
Definition: indent_codes.h:59
int LOCKMODE
Definition: lockdefs.h:26
CmdType
Definition: nodes.h:263
NodeTag
Definition: nodes.h:27
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:54
Definition: nodes.h:129
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
TM_Result
Definition: tableam.h:79
uint32 TriggerEvent
Definition: trigger.h:29
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2935
void AfterTriggerBeginXact(void)
Definition: trigger.c:4992
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:2775
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2395
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2459
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5355
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
Definition: ri_triggers.c:1305
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3223
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:63
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2141
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2812
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2613
void EnableDisableTrigger(Relation rel, const char *tgname, Oid tgparent, char fires_when, bool skip_system, bool recurse, LOCKMODE lockmode)
Definition: trigger.c:1722
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2552
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2086
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3270
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2273
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ResultRelInfo *src_partinfo, ResultRelInfo *dst_partinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, List *recheckIndexes, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:3098
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:5665
Oid get_trigger_oid(Oid relid, const char *trigname, bool missing_ok)
Definition: trigger.c:1366
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:158
struct TriggerData TriggerData
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2917
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2664
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2684
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1463
void AfterTriggerFireDeferred(void)
Definition: trigger.c:5203
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2535
void TriggerSetParentTrigger(Relation trigRel, Oid childTrigId, Oid parentTrigId, Oid childTableId)
Definition: trigger.c:1216
struct TransitionCaptureState TransitionCaptureState
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4888
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2446
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:175
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *newslot)
Definition: trigger.c:3157
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5259
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5980
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1438
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1857
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:5307
void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1732
bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
Definition: ri_triggers.c:1337
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:3116
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:5044
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1287
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2859
void AfterTriggerBeginQuery(void)
Definition: trigger.c:5024