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-2019, 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 {
42 } TriggerData;
43 
44 /*
45  * The state for capturing old and new tuples into transition tables for a
46  * single ModifyTable node (or other operation source, e.g. copy.c).
47  *
48  * This is per-caller to avoid conflicts in setting tcs_map or
49  * tcs_original_insert_tuple. Note, however, that the pointed-to
50  * private data may be shared across multiple callers.
51  */
52 struct AfterTriggersTableData; /* private in trigger.c */
53 
54 typedef struct TransitionCaptureState
55 {
56  /*
57  * Is there at least one trigger specifying each transition relation on
58  * the relation explicitly named in the DML statement or COPY command?
59  * Note: in current usage, these flags could be part of the private state,
60  * but it seems possibly useful to let callers see them.
61  */
66 
67  /*
68  * For UPDATE and DELETE, AfterTriggerSaveEvent may need to convert the
69  * new and old tuples from a child table's format to the format of the
70  * relation named in a query so that it is compatible with the transition
71  * tuplestores. The caller must store the conversion map here if so.
72  */
74 
75  /*
76  * For INSERT and COPY, it would be wasteful to convert tuples from child
77  * format to parent format after they have already been converted in the
78  * opposite direction during routing. In that case we bypass conversion
79  * and allow the inserting code (copy.c and nodeModifyTable.c) to provide
80  * a slot containing the original tuple directly.
81  */
83 
84  /*
85  * Private data including the tuplestore(s) into which to insert tuples.
86  */
89 
90 /*
91  * TriggerEvent bit flags
92  *
93  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
94  * can't be OR'd together in a single TriggerEvent. This is unlike the
95  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
96  * representation!
97  */
98 #define TRIGGER_EVENT_INSERT 0x00000000
99 #define TRIGGER_EVENT_DELETE 0x00000001
100 #define TRIGGER_EVENT_UPDATE 0x00000002
101 #define TRIGGER_EVENT_TRUNCATE 0x00000003
102 #define TRIGGER_EVENT_OPMASK 0x00000003
103 
104 #define TRIGGER_EVENT_ROW 0x00000004
105 
106 #define TRIGGER_EVENT_BEFORE 0x00000008
107 #define TRIGGER_EVENT_AFTER 0x00000000
108 #define TRIGGER_EVENT_INSTEAD 0x00000010
109 #define TRIGGER_EVENT_TIMINGMASK 0x00000018
110 
111 /* More TriggerEvent flags, used only within trigger.c */
112 
113 #define AFTER_TRIGGER_DEFERRABLE 0x00000020
114 #define AFTER_TRIGGER_INITDEFERRED 0x00000040
115 
116 #define TRIGGER_FIRED_BY_INSERT(event) \
117  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
118 
119 #define TRIGGER_FIRED_BY_DELETE(event) \
120  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
121 
122 #define TRIGGER_FIRED_BY_UPDATE(event) \
123  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
124 
125 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
126  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
127 
128 #define TRIGGER_FIRED_FOR_ROW(event) \
129  ((event) & TRIGGER_EVENT_ROW)
130 
131 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
132  (!TRIGGER_FIRED_FOR_ROW(event))
133 
134 #define TRIGGER_FIRED_BEFORE(event) \
135  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
136 
137 #define TRIGGER_FIRED_AFTER(event) \
138  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
139 
140 #define TRIGGER_FIRED_INSTEAD(event) \
141  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
142 
143 /*
144  * Definitions for replication role based firing.
145  */
146 #define SESSION_REPLICATION_ROLE_ORIGIN 0
147 #define SESSION_REPLICATION_ROLE_REPLICA 1
148 #define SESSION_REPLICATION_ROLE_LOCAL 2
150 
151 /*
152  * States at which a trigger can be fired. These are the
153  * possible values for pg_trigger.tgenabled.
154  */
155 #define TRIGGER_FIRES_ON_ORIGIN 'O'
156 #define TRIGGER_FIRES_ALWAYS 'A'
157 #define TRIGGER_FIRES_ON_REPLICA 'R'
158 #define TRIGGER_DISABLED 'D'
159 
160 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
161  Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
162  Oid funcoid, Oid parentTriggerOid, Node *whenClause,
163  bool isInternal, bool in_partition);
164 
165 extern void RemoveTriggerById(Oid trigOid);
166 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
167 
168 extern ObjectAddress renametrig(RenameStmt *stmt);
169 
170 extern void EnableDisableTrigger(Relation rel, const char *tgname,
171  char fires_when, bool skip_system, LOCKMODE lockmode);
172 
173 extern void RelationBuildTriggers(Relation relation);
174 
175 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
176 
177 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
178 
181 
182 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
183 
184 extern void ExecBSInsertTriggers(EState *estate,
185  ResultRelInfo *relinfo);
186 extern void ExecASInsertTriggers(EState *estate,
187  ResultRelInfo *relinfo,
188  TransitionCaptureState *transition_capture);
189 extern bool ExecBRInsertTriggers(EState *estate,
190  ResultRelInfo *relinfo,
191  TupleTableSlot *slot);
192 extern void ExecARInsertTriggers(EState *estate,
193  ResultRelInfo *relinfo,
194  TupleTableSlot *slot,
195  List *recheckIndexes,
196  TransitionCaptureState *transition_capture);
197 extern bool ExecIRInsertTriggers(EState *estate,
198  ResultRelInfo *relinfo,
199  TupleTableSlot *slot);
200 extern void ExecBSDeleteTriggers(EState *estate,
201  ResultRelInfo *relinfo);
202 extern void ExecASDeleteTriggers(EState *estate,
203  ResultRelInfo *relinfo,
204  TransitionCaptureState *transition_capture);
205 extern bool ExecBRDeleteTriggers(EState *estate,
206  EPQState *epqstate,
207  ResultRelInfo *relinfo,
208  ItemPointer tupleid,
209  HeapTuple fdw_trigtuple,
210  TupleTableSlot **epqslot);
211 extern void ExecARDeleteTriggers(EState *estate,
212  ResultRelInfo *relinfo,
213  ItemPointer tupleid,
214  HeapTuple fdw_trigtuple,
215  TransitionCaptureState *transition_capture);
216 extern bool ExecIRDeleteTriggers(EState *estate,
217  ResultRelInfo *relinfo,
218  HeapTuple trigtuple);
219 extern void ExecBSUpdateTriggers(EState *estate,
220  ResultRelInfo *relinfo);
221 extern void ExecASUpdateTriggers(EState *estate,
222  ResultRelInfo *relinfo,
223  TransitionCaptureState *transition_capture);
224 extern bool ExecBRUpdateTriggers(EState *estate,
225  EPQState *epqstate,
226  ResultRelInfo *relinfo,
227  ItemPointer tupleid,
228  HeapTuple fdw_trigtuple,
229  TupleTableSlot *slot);
230 extern void ExecARUpdateTriggers(EState *estate,
231  ResultRelInfo *relinfo,
232  ItemPointer tupleid,
233  HeapTuple fdw_trigtuple,
234  TupleTableSlot *slot,
235  List *recheckIndexes,
236  TransitionCaptureState *transition_capture);
237 extern bool ExecIRUpdateTriggers(EState *estate,
238  ResultRelInfo *relinfo,
239  HeapTuple trigtuple,
240  TupleTableSlot *slot);
241 extern void ExecBSTruncateTriggers(EState *estate,
242  ResultRelInfo *relinfo);
243 extern void ExecASTruncateTriggers(EState *estate,
244  ResultRelInfo *relinfo);
245 
246 extern void AfterTriggerBeginXact(void);
247 extern void AfterTriggerBeginQuery(void);
248 extern void AfterTriggerEndQuery(EState *estate);
249 extern void AfterTriggerFireDeferred(void);
250 extern void AfterTriggerEndXact(bool isCommit);
251 extern void AfterTriggerBeginSubXact(void);
252 extern void AfterTriggerEndSubXact(bool isCommit);
253 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
254 extern bool AfterTriggerPendingOnRel(Oid relid);
255 
256 
257 /*
258  * in utils/adt/ri_triggers.c
259  */
260 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
261  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
262 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
263  TupleTableSlot *old_slot, TupleTableSlot *new_slot);
264 extern bool RI_Initial_Check(Trigger *trigger,
265  Relation fk_rel, Relation pk_rel);
266 extern void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel,
267  Relation pk_rel);
268 
269 /* result values for RI_FKey_trigger_type: */
270 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
271 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
272 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
273 
274 extern int RI_FKey_trigger_type(Oid tgfoid);
275 
276 #endif /* TRIGGER_H */
void RI_PartitionRemove_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1572
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2684
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:5051
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:1196
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system, LOCKMODE lockmode)
Definition: trigger.c:1807
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:1297
int LOCKMODE
Definition: lockdefs.h:26
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot)
Definition: trigger.c:3006
Definition: nodes.h:525
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot)
Definition: trigger.c:2759
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
#define PGDLLIMPORT
Definition: c.h:1243
HeapTuple tg_trigtuple
Definition: trigger.h:35
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2467
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1577
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5003
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5648
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2741
struct AfterTriggersTableData * tcs_private
Definition: trigger.h:87
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:3134
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1930
TupleConversionMap * tcs_map
Definition: trigger.h:73
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3244
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3297
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
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4783
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:67
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2993
unsigned int uint32
Definition: c.h:358
TupleTableSlot * tg_newslot
Definition: trigger.h:39
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2868
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5099
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2158
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1499
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4661
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4803
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, TupleTableSlot *old_slot, TupleTableSlot *new_slot)
Definition: ri_triggers.c:1164
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:3176
struct TransitionCaptureState TransitionCaptureState
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2535
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:2213
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1673
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2933
uint32 TriggerEvent
Definition: trigger.h:28
TriggerEvent tg_event
Definition: trigger.h:33
const char * name
Definition: encode.c:521
Tuplestorestate * tg_oldtable
Definition: trigger.h:40
struct TriggerData TriggerData
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4947
NodeTag type
Definition: trigger.h:32
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2524
Tuplestorestate * tg_newtable
Definition: trigger.h:41
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2880
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2343
void AfterTriggerBeginXact(void)
Definition: trigger.c:4751
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2602
Definition: pg_list.h:50
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture)
Definition: trigger.c:2850
CmdType
Definition: nodes.h:668
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2617
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:5323
Relation tg_relation
Definition: trigger.h:34
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:82