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-2020, 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 "catalog/objectaddress.h"
17 #include "nodes/execnodes.h"
18 #include "nodes/parsenodes.h"
19 
20 /*
21  * TriggerData is the node type that is passed as fmgr "context" info
22  * when a function is called by the trigger manager.
23  */
24 
25 #define CALLED_AS_TRIGGER(fcinfo) \
26  ((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
27 
29 
30 typedef struct TriggerData
31 {
43 } TriggerData;
44 
45 /*
46  * The state for capturing old and new tuples into transition tables for a
47  * single ModifyTable node (or other operation source, e.g. copyfrom.c).
48  *
49  * This is per-caller to avoid conflicts in setting
50  * tcs_original_insert_tuple. Note, however, that the pointed-to
51  * private data may be shared across multiple callers.
52  */
53 struct AfterTriggersTableData; /* private in trigger.c */
54 
55 typedef struct TransitionCaptureState
56 {
57  /*
58  * Is there at least one trigger specifying each transition relation on
59  * the relation explicitly named in the DML statement or COPY command?
60  * Note: in current usage, these flags could be part of the private state,
61  * but it seems possibly useful to let callers see them.
62  */
67 
68  /*
69  * For INSERT and COPY, it would be wasteful to convert tuples from child
70  * format to parent format after they have already been converted in the
71  * opposite direction during routing. In that case we bypass conversion
72  * and allow the inserting code (copyfrom.c and nodeModifyTable.c) to
73  * provide a slot containing the original tuple directly.
74  */
76 
77  /*
78  * Private data including the tuplestore(s) into which to insert tuples.
79  */
82 
83 /*
84  * TriggerEvent bit flags
85  *
86  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
87  * can't be OR'd together in a single TriggerEvent. This is unlike the
88  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
89  * representation!
90  */
91 #define TRIGGER_EVENT_INSERT 0x00000000
92 #define TRIGGER_EVENT_DELETE 0x00000001
93 #define TRIGGER_EVENT_UPDATE 0x00000002
94 #define TRIGGER_EVENT_TRUNCATE 0x00000003
95 #define TRIGGER_EVENT_OPMASK 0x00000003
96 
97 #define TRIGGER_EVENT_ROW 0x00000004
98 
99 #define TRIGGER_EVENT_BEFORE 0x00000008
100 #define TRIGGER_EVENT_AFTER 0x00000000
101 #define TRIGGER_EVENT_INSTEAD 0x00000010
102 #define TRIGGER_EVENT_TIMINGMASK 0x00000018
103 
104 /* More TriggerEvent flags, used only within trigger.c */
105 
106 #define AFTER_TRIGGER_DEFERRABLE 0x00000020
107 #define AFTER_TRIGGER_INITDEFERRED 0x00000040
108 
109 #define TRIGGER_FIRED_BY_INSERT(event) \
110  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
111 
112 #define TRIGGER_FIRED_BY_DELETE(event) \
113  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
114 
115 #define TRIGGER_FIRED_BY_UPDATE(event) \
116  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
117 
118 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
119  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
120 
121 #define TRIGGER_FIRED_FOR_ROW(event) \
122  ((event) & TRIGGER_EVENT_ROW)
123 
124 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
125  (!TRIGGER_FIRED_FOR_ROW(event))
126 
127 #define TRIGGER_FIRED_BEFORE(event) \
128  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
129 
130 #define TRIGGER_FIRED_AFTER(event) \
131  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
132 
133 #define TRIGGER_FIRED_INSTEAD(event) \
134  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
135 
136 /*
137  * Definitions for replication role based firing.
138  */
139 #define SESSION_REPLICATION_ROLE_ORIGIN 0
140 #define SESSION_REPLICATION_ROLE_REPLICA 1
141 #define SESSION_REPLICATION_ROLE_LOCAL 2
143 
144 /*
145  * States at which a trigger can be fired. These are the
146  * possible values for pg_trigger.tgenabled.
147  */
148 #define TRIGGER_FIRES_ON_ORIGIN 'O'
149 #define TRIGGER_FIRES_ALWAYS 'A'
150 #define TRIGGER_FIRES_ON_REPLICA 'R'
151 #define TRIGGER_DISABLED 'D'
152 
153 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
154  Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
155  Oid funcoid, Oid parentTriggerOid, Node *whenClause,
156  bool isInternal, bool in_partition);
157 
158 extern void RemoveTriggerById(Oid trigOid);
159 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
160 
161 extern ObjectAddress renametrig(RenameStmt *stmt);
162 
163 extern void EnableDisableTrigger(Relation rel, const char *tgname,
164  char fires_when, bool skip_system, LOCKMODE lockmode);
165 
166 extern void RelationBuildTriggers(Relation relation);
167 
168 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
169 
170 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
171 
174 
175 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
176 
177 extern void ExecBSInsertTriggers(EState *estate,
178  ResultRelInfo *relinfo);
179 extern void ExecASInsertTriggers(EState *estate,
180  ResultRelInfo *relinfo,
181  TransitionCaptureState *transition_capture);
182 extern bool ExecBRInsertTriggers(EState *estate,
183  ResultRelInfo *relinfo,
184  TupleTableSlot *slot);
185 extern void ExecARInsertTriggers(EState *estate,
186  ResultRelInfo *relinfo,
187  TupleTableSlot *slot,
188  List *recheckIndexes,
189  TransitionCaptureState *transition_capture);
190 extern bool ExecIRInsertTriggers(EState *estate,
191  ResultRelInfo *relinfo,
192  TupleTableSlot *slot);
193 extern void ExecBSDeleteTriggers(EState *estate,
194  ResultRelInfo *relinfo);
195 extern void ExecASDeleteTriggers(EState *estate,
196  ResultRelInfo *relinfo,
197  TransitionCaptureState *transition_capture);
198 extern bool ExecBRDeleteTriggers(EState *estate,
199  EPQState *epqstate,
200  ResultRelInfo *relinfo,
201  ItemPointer tupleid,
202  HeapTuple fdw_trigtuple,
203  TupleTableSlot **epqslot);
204 extern void ExecARDeleteTriggers(EState *estate,
205  ResultRelInfo *relinfo,
206  ItemPointer tupleid,
207  HeapTuple fdw_trigtuple,
208  TransitionCaptureState *transition_capture);
209 extern bool ExecIRDeleteTriggers(EState *estate,
210  ResultRelInfo *relinfo,
211  HeapTuple trigtuple);
212 extern void ExecBSUpdateTriggers(EState *estate,
213  ResultRelInfo *relinfo);
214 extern void ExecASUpdateTriggers(EState *estate,
215  ResultRelInfo *relinfo,
216  TransitionCaptureState *transition_capture);
217 extern bool ExecBRUpdateTriggers(EState *estate,
218  EPQState *epqstate,
219  ResultRelInfo *relinfo,
220  ItemPointer tupleid,
221  HeapTuple fdw_trigtuple,
222  TupleTableSlot *slot);
223 extern void ExecARUpdateTriggers(EState *estate,
224  ResultRelInfo *relinfo,
225  ItemPointer tupleid,
226  HeapTuple fdw_trigtuple,
227  TupleTableSlot *slot,
228  List *recheckIndexes,
229  TransitionCaptureState *transition_capture);
230 extern bool ExecIRUpdateTriggers(EState *estate,
231  ResultRelInfo *relinfo,
232  HeapTuple trigtuple,
233  TupleTableSlot *slot);
234 extern void ExecBSTruncateTriggers(EState *estate,
235  ResultRelInfo *relinfo);
236 extern void ExecASTruncateTriggers(EState *estate,
237  ResultRelInfo *relinfo);
238 
239 extern void AfterTriggerBeginXact(void);
240 extern void AfterTriggerBeginQuery(void);
241 extern void AfterTriggerEndQuery(EState *estate);
242 extern void AfterTriggerFireDeferred(void);
243 extern void AfterTriggerEndXact(bool isCommit);
244 extern void AfterTriggerBeginSubXact(void);
245 extern void AfterTriggerEndSubXact(bool isCommit);
246 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
247 extern bool AfterTriggerPendingOnRel(Oid relid);
248 
249 
250 /*
251  * in utils/adt/ri_triggers.c
252  */
253 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
254  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
255 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
256  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
257 extern bool RI_Initial_Check(Trigger *trigger,
258  Relation fk_rel, Relation pk_rel);
259 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
260  Relation pk_rel);
261 
262 /* result values for RI_FKey_trigger_type: */
263 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
264 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
265 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
266 
267 extern int RI_FKey_trigger_type(Oid tgfoid);
268 
269 #endif /* TRIGGER_H */
void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1575
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2398
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:4770
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel, TupleTableSlot *old_slot, TupleTableSlot *new_slot)
Definition: ri_triggers.c:1194
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system, LOCKMODE lockmode)
Definition: trigger.c:1544
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1295
int LOCKMODE
Definition: lockdefs.h:26
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2697
const Bitmapset * tg_updatedcols
Definition: trigger.h:42
Definition: nodes.h:527
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot)
Definition: trigger.c:2467
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
#define PGDLLIMPORT
Definition: c.h:1313
HeapTuple tg_trigtuple
Definition: trigger.h:35
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2184
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1314
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4722
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5357
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2449
struct AfterTriggersTableData * tcs_private
Definition: trigger.h:80
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2837
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1646
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2945
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2992
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:160
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4502
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:68
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2684
unsigned int uint32
Definition: c.h:429
TupleTableSlot * tg_newslot
Definition: trigger.h:39
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2870
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4818
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1875
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1236
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4380
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4522
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, TupleTableSlot *old_slot, TupleTableSlot *new_slot)
Definition: ri_triggers.c:1162
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2879
struct TransitionCaptureState TransitionCaptureState
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2246
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1930
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1410
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2629
uint32 TriggerEvent
Definition: trigger.h:28
TriggerEvent tg_event
Definition: trigger.h:33
const char * name
Definition: encode.c:561
Tuplestorestate * tg_oldtable
Definition: trigger.h:40
struct TriggerData TriggerData
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4666
NodeTag type
Definition: trigger.h:32
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2235
Tuplestorestate * tg_newtable
Definition: trigger.h:41
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2582
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2062
void AfterTriggerBeginXact(void)
Definition: trigger.c:4470
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2322
Definition: pg_list.h:50
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture)
Definition: trigger.c:2552
CmdType
Definition: nodes.h:670
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2337
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:5042
Relation tg_relation
Definition: trigger.h:34
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:75