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. copy.c).
48  *
49  * This is per-caller to avoid conflicts in setting tcs_map or
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 UPDATE and DELETE, AfterTriggerSaveEvent may need to convert the
70  * new and old tuples from a child table's format to the format of the
71  * relation named in a query so that it is compatible with the transition
72  * tuplestores. The caller must store the conversion map here if so.
73  */
75 
76  /*
77  * For INSERT and COPY, it would be wasteful to convert tuples from child
78  * format to parent format after they have already been converted in the
79  * opposite direction during routing. In that case we bypass conversion
80  * and allow the inserting code (copy.c and nodeModifyTable.c) to provide
81  * a slot containing the original tuple directly.
82  */
84 
85  /*
86  * Private data including the tuplestore(s) into which to insert tuples.
87  */
90 
91 /*
92  * TriggerEvent bit flags
93  *
94  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
95  * can't be OR'd together in a single TriggerEvent. This is unlike the
96  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
97  * representation!
98  */
99 #define TRIGGER_EVENT_INSERT 0x00000000
100 #define TRIGGER_EVENT_DELETE 0x00000001
101 #define TRIGGER_EVENT_UPDATE 0x00000002
102 #define TRIGGER_EVENT_TRUNCATE 0x00000003
103 #define TRIGGER_EVENT_OPMASK 0x00000003
104 
105 #define TRIGGER_EVENT_ROW 0x00000004
106 
107 #define TRIGGER_EVENT_BEFORE 0x00000008
108 #define TRIGGER_EVENT_AFTER 0x00000000
109 #define TRIGGER_EVENT_INSTEAD 0x00000010
110 #define TRIGGER_EVENT_TIMINGMASK 0x00000018
111 
112 /* More TriggerEvent flags, used only within trigger.c */
113 
114 #define AFTER_TRIGGER_DEFERRABLE 0x00000020
115 #define AFTER_TRIGGER_INITDEFERRED 0x00000040
116 
117 #define TRIGGER_FIRED_BY_INSERT(event) \
118  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
119 
120 #define TRIGGER_FIRED_BY_DELETE(event) \
121  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
122 
123 #define TRIGGER_FIRED_BY_UPDATE(event) \
124  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
125 
126 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
127  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
128 
129 #define TRIGGER_FIRED_FOR_ROW(event) \
130  ((event) & TRIGGER_EVENT_ROW)
131 
132 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
133  (!TRIGGER_FIRED_FOR_ROW(event))
134 
135 #define TRIGGER_FIRED_BEFORE(event) \
136  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
137 
138 #define TRIGGER_FIRED_AFTER(event) \
139  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
140 
141 #define TRIGGER_FIRED_INSTEAD(event) \
142  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
143 
144 /*
145  * Definitions for replication role based firing.
146  */
147 #define SESSION_REPLICATION_ROLE_ORIGIN 0
148 #define SESSION_REPLICATION_ROLE_REPLICA 1
149 #define SESSION_REPLICATION_ROLE_LOCAL 2
151 
152 /*
153  * States at which a trigger can be fired. These are the
154  * possible values for pg_trigger.tgenabled.
155  */
156 #define TRIGGER_FIRES_ON_ORIGIN 'O'
157 #define TRIGGER_FIRES_ALWAYS 'A'
158 #define TRIGGER_FIRES_ON_REPLICA 'R'
159 #define TRIGGER_DISABLED 'D'
160 
161 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
162  Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
163  Oid funcoid, Oid parentTriggerOid, Node *whenClause,
164  bool isInternal, bool in_partition);
165 
166 extern void RemoveTriggerById(Oid trigOid);
167 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
168 
169 extern ObjectAddress renametrig(RenameStmt *stmt);
170 
171 extern void EnableDisableTrigger(Relation rel, const char *tgname,
172  char fires_when, bool skip_system, LOCKMODE lockmode);
173 
174 extern void RelationBuildTriggers(Relation relation);
175 
176 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
177 
178 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
179 
182 
183 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
184 
185 extern void ExecBSInsertTriggers(EState *estate,
186  ResultRelInfo *relinfo);
187 extern void ExecASInsertTriggers(EState *estate,
188  ResultRelInfo *relinfo,
189  TransitionCaptureState *transition_capture);
190 extern bool ExecBRInsertTriggers(EState *estate,
191  ResultRelInfo *relinfo,
192  TupleTableSlot *slot);
193 extern void ExecARInsertTriggers(EState *estate,
194  ResultRelInfo *relinfo,
195  TupleTableSlot *slot,
196  List *recheckIndexes,
197  TransitionCaptureState *transition_capture);
198 extern bool ExecIRInsertTriggers(EState *estate,
199  ResultRelInfo *relinfo,
200  TupleTableSlot *slot);
201 extern void ExecBSDeleteTriggers(EState *estate,
202  ResultRelInfo *relinfo);
203 extern void ExecASDeleteTriggers(EState *estate,
204  ResultRelInfo *relinfo,
205  TransitionCaptureState *transition_capture);
206 extern bool ExecBRDeleteTriggers(EState *estate,
207  EPQState *epqstate,
208  ResultRelInfo *relinfo,
209  ItemPointer tupleid,
210  HeapTuple fdw_trigtuple,
211  TupleTableSlot **epqslot);
212 extern void ExecARDeleteTriggers(EState *estate,
213  ResultRelInfo *relinfo,
214  ItemPointer tupleid,
215  HeapTuple fdw_trigtuple,
216  TransitionCaptureState *transition_capture);
217 extern bool ExecIRDeleteTriggers(EState *estate,
218  ResultRelInfo *relinfo,
219  HeapTuple trigtuple);
220 extern void ExecBSUpdateTriggers(EState *estate,
221  ResultRelInfo *relinfo);
222 extern void ExecASUpdateTriggers(EState *estate,
223  ResultRelInfo *relinfo,
224  TransitionCaptureState *transition_capture);
225 extern bool ExecBRUpdateTriggers(EState *estate,
226  EPQState *epqstate,
227  ResultRelInfo *relinfo,
228  ItemPointer tupleid,
229  HeapTuple fdw_trigtuple,
230  TupleTableSlot *slot);
231 extern void ExecARUpdateTriggers(EState *estate,
232  ResultRelInfo *relinfo,
233  ItemPointer tupleid,
234  HeapTuple fdw_trigtuple,
235  TupleTableSlot *slot,
236  List *recheckIndexes,
237  TransitionCaptureState *transition_capture);
238 extern bool ExecIRUpdateTriggers(EState *estate,
239  ResultRelInfo *relinfo,
240  HeapTuple trigtuple,
241  TupleTableSlot *slot);
242 extern void ExecBSTruncateTriggers(EState *estate,
243  ResultRelInfo *relinfo);
244 extern void ExecASTruncateTriggers(EState *estate,
245  ResultRelInfo *relinfo);
246 
247 extern void AfterTriggerBeginXact(void);
248 extern void AfterTriggerBeginQuery(void);
249 extern void AfterTriggerEndQuery(EState *estate);
250 extern void AfterTriggerFireDeferred(void);
251 extern void AfterTriggerEndXact(bool isCommit);
252 extern void AfterTriggerBeginSubXact(void);
253 extern void AfterTriggerEndSubXact(bool isCommit);
254 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
255 extern bool AfterTriggerPendingOnRel(Oid relid);
256 
257 
258 /*
259  * in utils/adt/ri_triggers.c
260  */
261 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
262  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
263 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
264  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
265 extern bool RI_Initial_Check(Trigger *trigger,
266  Relation fk_rel, Relation pk_rel);
267 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
268  Relation pk_rel);
269 
270 /* result values for RI_FKey_trigger_type: */
271 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
272 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
273 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
274 
275 extern int RI_FKey_trigger_type(Oid tgfoid);
276 
277 #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:2344
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:4700
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:1469
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:2643
const Bitmapset * tg_updatedcols
Definition: trigger.h:42
Definition: nodes.h:529
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot)
Definition: trigger.c:2413
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
#define PGDLLIMPORT
Definition: c.h:1257
HeapTuple tg_trigtuple
Definition: trigger.h:35
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2130
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1239
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4652
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5287
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2395
struct AfterTriggersTableData * tcs_private
Definition: trigger.h:88
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2783
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1592
TupleConversionMap * tcs_map
Definition: trigger.h:74
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2891
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2938
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:157
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4432
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:67
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2630
unsigned int uint32
Definition: c.h:374
TupleTableSlot * tg_newslot
Definition: trigger.h:39
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2873
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4748
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1821
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1161
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4310
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4452
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:2825
struct TransitionCaptureState TransitionCaptureState
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2192
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1876
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1335
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2575
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:4596
NodeTag type
Definition: trigger.h:32
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2181
Tuplestorestate * tg_newtable
Definition: trigger.h:41
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2528
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2008
void AfterTriggerBeginXact(void)
Definition: trigger.c:4400
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2268
Definition: pg_list.h:50
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture)
Definition: trigger.c:2498
CmdType
Definition: nodes.h:672
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2283
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:4972
Relation tg_relation
Definition: trigger.h:34
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:83