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 *trigname, 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, 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 extern void ExecARDeleteTriggers(EState *estate,
216  ResultRelInfo *relinfo,
217  ItemPointer tupleid,
218  HeapTuple fdw_trigtuple,
219  TransitionCaptureState *transition_capture,
220  bool is_crosspart_update);
221 extern bool ExecIRDeleteTriggers(EState *estate,
222  ResultRelInfo *relinfo,
223  HeapTuple trigtuple);
224 extern void ExecBSUpdateTriggers(EState *estate,
225  ResultRelInfo *relinfo);
226 extern void ExecASUpdateTriggers(EState *estate,
227  ResultRelInfo *relinfo,
228  TransitionCaptureState *transition_capture);
229 extern bool ExecBRUpdateTriggers(EState *estate,
230  EPQState *epqstate,
231  ResultRelInfo *relinfo,
232  ItemPointer tupleid,
233  HeapTuple fdw_trigtuple,
234  TupleTableSlot *newslot,
235  TM_FailureData *tmfd);
236 extern void ExecARUpdateTriggers(EState *estate,
237  ResultRelInfo *relinfo,
238  ResultRelInfo *src_partinfo,
239  ResultRelInfo *dst_partinfo,
240  ItemPointer tupleid,
241  HeapTuple fdw_trigtuple,
242  TupleTableSlot *newslot,
243  List *recheckIndexes,
244  TransitionCaptureState *transition_capture,
245  bool is_crosspart_update);
246 extern bool ExecIRUpdateTriggers(EState *estate,
247  ResultRelInfo *relinfo,
248  HeapTuple trigtuple,
249  TupleTableSlot *newslot);
250 extern void ExecBSTruncateTriggers(EState *estate,
251  ResultRelInfo *relinfo);
252 extern void ExecASTruncateTriggers(EState *estate,
253  ResultRelInfo *relinfo);
254 
255 extern void AfterTriggerBeginXact(void);
256 extern void AfterTriggerBeginQuery(void);
257 extern void AfterTriggerEndQuery(EState *estate);
258 extern void AfterTriggerFireDeferred(void);
259 extern void AfterTriggerEndXact(bool isCommit);
260 extern void AfterTriggerBeginSubXact(void);
261 extern void AfterTriggerEndSubXact(bool isCommit);
262 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
263 extern bool AfterTriggerPendingOnRel(Oid relid);
264 
265 
266 /*
267  * in utils/adt/ri_triggers.c
268  */
269 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
270  TupleTableSlot *oldslot, TupleTableSlot *newslot);
271 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
272  TupleTableSlot *oldslot, TupleTableSlot *newslot);
273 extern bool RI_Initial_Check(Trigger *trigger,
274  Relation fk_rel, Relation pk_rel);
275 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
276  Relation pk_rel);
277 
278 /* result values for RI_FKey_trigger_type: */
279 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
280 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
281 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
282 
283 extern int RI_FKey_trigger_type(Oid tgfoid);
284 
285 #endif /* TRIGGER_H */
unsigned int uint32
Definition: c.h:442
#define PGDLLIMPORT
Definition: c.h:1288
int LOCKMODE
Definition: lockdefs.h:26
CmdType
Definition: nodes.h:257
NodeTag
Definition: nodes.h:27
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:52
Definition: nodes.h:112
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
uint32 TriggerEvent
Definition: trigger.h:29
void AfterTriggerBeginXact(void)
Definition: trigger.c:4922
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:2777
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2399
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2463
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5285
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
Definition: ri_triggers.c:1230
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3204
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:70
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2145
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2813
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2617
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2556
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2090
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3251
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2277
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:3080
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:5595
Oid get_trigger_oid(Oid relid, const char *trigname, bool missing_ok)
Definition: trigger.c:1376
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:164
struct TriggerData TriggerData
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, TM_FailureData *tmfd)
Definition: trigger.c:2936
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2918
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2668
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1473
void AfterTriggerFireDeferred(void)
Definition: trigger.c:5133
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2539
void TriggerSetParentTrigger(Relation trigRel, Oid childTrigId, Oid parentTrigId, Oid childTableId)
Definition: trigger.c:1226
struct TransitionCaptureState TransitionCaptureState
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4818
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2450
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:181
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *newslot)
Definition: trigger.c:3138
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5189
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5910
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1369
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1861
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:5237
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:2688
bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
Definition: ri_triggers.c:1262
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2998
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4974
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1297
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system, bool recurse, LOCKMODE lockmode)
Definition: trigger.c:1730
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2860
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4954