PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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.
47  */
48 typedef struct TransitionCaptureState
49 {
50  /*
51  * Is there at least one trigger specifying each transition relation on
52  * the relation explicitly named in the DML statement or COPY command?
53  */
58 
59  /*
60  * For UPDATE and DELETE, AfterTriggerSaveEvent may need to convert the
61  * new and old tuples from a child table's format to the format of the
62  * relation named in a query so that it is compatible with the transition
63  * tuplestores.
64  */
66 
67  /*
68  * For INSERT and COPY, it would be wasteful to convert tuples from child
69  * format to parent format after they have already been converted in the
70  * opposite direction during routing. In that case we bypass conversion
71  * and allow the inserting code (copy.c and nodeModifyTable.c) to provide
72  * the original tuple directly.
73  */
75 
76  /*
77  * The tuplestores backing the transition tables. We use separate
78  * tuplestores for INSERT and UPDATE, because INSERT ... ON CONFLICT ...
79  * DO UPDATE causes INSERT and UPDATE triggers to fire and needs a way to
80  * keep track of the new tuple images resulting from the two cases
81  * separately. We only need a single old image tuplestore, because there
82  * is no statement that can both update and delete at the same time.
83  */
84  Tuplestorestate *tcs_old_tuplestore; /* for DELETE and UPDATE old
85  * images */
86  Tuplestorestate *tcs_insert_tuplestore; /* for INSERT new images */
87  Tuplestorestate *tcs_update_tuplestore; /* for UPDATE new images */
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  bool isInternal);
163 
164 extern void RemoveTriggerById(Oid trigOid);
165 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
166 
167 extern ObjectAddress renametrig(RenameStmt *stmt);
168 
169 extern void EnableDisableTrigger(Relation rel, const char *tgname,
170  char fires_when, bool skip_system);
171 
172 extern void RelationBuildTriggers(Relation relation);
173 
174 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
175 
176 extern const char *FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc);
179 
180 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
181 
182 extern void ExecBSInsertTriggers(EState *estate,
183  ResultRelInfo *relinfo);
184 extern void ExecASInsertTriggers(EState *estate,
185  ResultRelInfo *relinfo,
186  TransitionCaptureState *transition_capture);
188  ResultRelInfo *relinfo,
189  TupleTableSlot *slot);
190 extern void ExecARInsertTriggers(EState *estate,
191  ResultRelInfo *relinfo,
192  HeapTuple trigtuple,
193  List *recheckIndexes,
194  TransitionCaptureState *transition_capture);
196  ResultRelInfo *relinfo,
197  TupleTableSlot *slot);
198 extern void ExecBSDeleteTriggers(EState *estate,
199  ResultRelInfo *relinfo);
200 extern void ExecASDeleteTriggers(EState *estate,
201  ResultRelInfo *relinfo,
202  TransitionCaptureState *transition_capture);
203 extern bool ExecBRDeleteTriggers(EState *estate,
204  EPQState *epqstate,
205  ResultRelInfo *relinfo,
206  ItemPointer tupleid,
207  HeapTuple fdw_trigtuple);
208 extern void ExecARDeleteTriggers(EState *estate,
209  ResultRelInfo *relinfo,
210  ItemPointer tupleid,
211  HeapTuple fdw_trigtuple,
212  TransitionCaptureState *transition_capture);
213 extern bool ExecIRDeleteTriggers(EState *estate,
214  ResultRelInfo *relinfo,
215  HeapTuple trigtuple);
216 extern void ExecBSUpdateTriggers(EState *estate,
217  ResultRelInfo *relinfo);
218 extern void ExecASUpdateTriggers(EState *estate,
219  ResultRelInfo *relinfo,
220  TransitionCaptureState *transition_capture);
222  EPQState *epqstate,
223  ResultRelInfo *relinfo,
224  ItemPointer tupleid,
225  HeapTuple fdw_trigtuple,
226  TupleTableSlot *slot);
227 extern void ExecARUpdateTriggers(EState *estate,
228  ResultRelInfo *relinfo,
229  ItemPointer tupleid,
230  HeapTuple fdw_trigtuple,
231  HeapTuple newtuple,
232  List *recheckIndexes,
233  TransitionCaptureState *transition_capture);
235  ResultRelInfo *relinfo,
236  HeapTuple trigtuple,
237  TupleTableSlot *slot);
238 extern void ExecBSTruncateTriggers(EState *estate,
239  ResultRelInfo *relinfo);
240 extern void ExecASTruncateTriggers(EState *estate,
241  ResultRelInfo *relinfo);
242 
243 extern void AfterTriggerBeginXact(void);
244 extern void AfterTriggerBeginQuery(void);
245 extern void AfterTriggerEndQuery(EState *estate);
246 extern void AfterTriggerFireDeferred(void);
247 extern void AfterTriggerEndXact(bool isCommit);
248 extern void AfterTriggerBeginSubXact(void);
249 extern void AfterTriggerEndSubXact(bool isCommit);
250 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
251 extern bool AfterTriggerPendingOnRel(Oid relid);
252 
253 
254 /*
255  * in utils/adt/ri_triggers.c
256  */
257 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
258  HeapTuple old_row, HeapTuple new_row);
259 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
260  HeapTuple old_row, HeapTuple new_row);
261 extern bool RI_Initial_Check(Trigger *trigger,
262  Relation fk_rel, Relation pk_rel);
263 
264 /* result values for RI_FKey_trigger_type: */
265 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
266 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
267 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
268 
269 extern int RI_FKey_trigger_type(Oid tgfoid);
270 
271 #endif /* TRIGGER_H */
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2494
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:4551
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:2254
TupleTableSlot * ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2927
Tuplestorestate * tcs_update_tuplestore
Definition: trigger.h:87
void DestroyTransitionCaptureState(TransitionCaptureState *tcs)
Definition: trigger.c:2167
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, HeapTuple newtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2890
Buffer tg_newtuplebuf
Definition: trigger.h:39
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
Tuplestorestate * tcs_old_tuplestore
Definition: trigger.h:84
HeapTuple tcs_original_insert_tuple
Definition: trigger.h:74
#define PGDLLIMPORT
Definition: c.h:1064
HeapTuple tg_trigtuple
Definition: trigger.h:35
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2428
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2284
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1327
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4499
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5157
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2546
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1657
TupleConversionMap * tcs_map
Definition: trigger.h:65
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2993
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system)
Definition: trigger.c:1555
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:3045
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2413
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4343
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:65
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2760
Buffer tg_trigtuplebuf
Definition: trigger.h:38
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc)
Definition: trigger.c:2104
unsigned int uint32
Definition: c.h:268
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:3701
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4619
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1885
ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString, Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid, bool isInternal)
Definition: trigger.c:140
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1249
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4363
struct TransitionCaptureState TransitionCaptureState
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1940
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1423
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2705
uint32 TriggerEvent
Definition: trigger.h:28
TriggerEvent tg_event
Definition: trigger.h:33
TupleTableSlot * ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2773
const char * name
Definition: encode.c:521
Tuplestorestate * tg_oldtable
Definition: trigger.h:40
struct TriggerData TriggerData
Tuplestorestate * tcs_insert_tuplestore
Definition: trigger.h:86
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4443
NodeTag type
Definition: trigger.h:32
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple)
Definition: trigger.c:2557
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2336
Tuplestorestate * tg_newtable
Definition: trigger.h:41
bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel, HeapTuple old_row, HeapTuple new_row)
Definition: ri_triggers.c:2141
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2658
const char * FindTriggerIncompatibleWithInheritance(TriggerDesc *trigdesc)
Definition: trigger.c:2070
void AfterTriggerBeginXact(void)
Definition: trigger.c:4307
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture)
Definition: trigger.c:2626
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2347
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, HeapTuple old_row, HeapTuple new_row)
Definition: ri_triggers.c:2084
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:4863
Relation tg_relation
Definition: trigger.h:34