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  * TriggerEvent bit flags
46  *
47  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
48  * can't be OR'd together in a single TriggerEvent. This is unlike the
49  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
50  * representation!
51  */
52 #define TRIGGER_EVENT_INSERT 0x00000000
53 #define TRIGGER_EVENT_DELETE 0x00000001
54 #define TRIGGER_EVENT_UPDATE 0x00000002
55 #define TRIGGER_EVENT_TRUNCATE 0x00000003
56 #define TRIGGER_EVENT_OPMASK 0x00000003
57 
58 #define TRIGGER_EVENT_ROW 0x00000004
59 
60 #define TRIGGER_EVENT_BEFORE 0x00000008
61 #define TRIGGER_EVENT_AFTER 0x00000000
62 #define TRIGGER_EVENT_INSTEAD 0x00000010
63 #define TRIGGER_EVENT_TIMINGMASK 0x00000018
64 
65 /* More TriggerEvent flags, used only within trigger.c */
66 
67 #define AFTER_TRIGGER_DEFERRABLE 0x00000020
68 #define AFTER_TRIGGER_INITDEFERRED 0x00000040
69 
70 #define TRIGGER_FIRED_BY_INSERT(event) \
71  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
72 
73 #define TRIGGER_FIRED_BY_DELETE(event) \
74  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
75 
76 #define TRIGGER_FIRED_BY_UPDATE(event) \
77  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
78 
79 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
80  (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
81 
82 #define TRIGGER_FIRED_FOR_ROW(event) \
83  ((event) & TRIGGER_EVENT_ROW)
84 
85 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
86  (!TRIGGER_FIRED_FOR_ROW(event))
87 
88 #define TRIGGER_FIRED_BEFORE(event) \
89  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
90 
91 #define TRIGGER_FIRED_AFTER(event) \
92  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
93 
94 #define TRIGGER_FIRED_INSTEAD(event) \
95  (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
96 
97 /*
98  * Definitions for replication role based firing.
99  */
100 #define SESSION_REPLICATION_ROLE_ORIGIN 0
101 #define SESSION_REPLICATION_ROLE_REPLICA 1
102 #define SESSION_REPLICATION_ROLE_LOCAL 2
104 
105 /*
106  * States at which a trigger can be fired. These are the
107  * possible values for pg_trigger.tgenabled.
108  */
109 #define TRIGGER_FIRES_ON_ORIGIN 'O'
110 #define TRIGGER_FIRES_ALWAYS 'A'
111 #define TRIGGER_FIRES_ON_REPLICA 'R'
112 #define TRIGGER_DISABLED 'D'
113 
114 extern ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
115  Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid,
116  bool isInternal);
117 
118 extern void RemoveTriggerById(Oid trigOid);
119 extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
120 
121 extern ObjectAddress renametrig(RenameStmt *stmt);
122 
123 extern void EnableDisableTrigger(Relation rel, const char *tgname,
124  char fires_when, bool skip_system);
125 
126 extern void RelationBuildTriggers(Relation relation);
127 
128 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
129 
130 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
131 
132 extern void ExecBSInsertTriggers(EState *estate,
133  ResultRelInfo *relinfo);
134 extern void ExecASInsertTriggers(EState *estate,
135  ResultRelInfo *relinfo);
137  ResultRelInfo *relinfo,
138  TupleTableSlot *slot);
139 extern void ExecARInsertTriggers(EState *estate,
140  ResultRelInfo *relinfo,
141  HeapTuple trigtuple,
142  List *recheckIndexes);
144  ResultRelInfo *relinfo,
145  TupleTableSlot *slot);
146 extern void ExecBSDeleteTriggers(EState *estate,
147  ResultRelInfo *relinfo);
148 extern void ExecASDeleteTriggers(EState *estate,
149  ResultRelInfo *relinfo);
150 extern bool ExecBRDeleteTriggers(EState *estate,
151  EPQState *epqstate,
152  ResultRelInfo *relinfo,
153  ItemPointer tupleid,
154  HeapTuple fdw_trigtuple);
155 extern void ExecARDeleteTriggers(EState *estate,
156  ResultRelInfo *relinfo,
157  ItemPointer tupleid,
158  HeapTuple fdw_trigtuple);
159 extern bool ExecIRDeleteTriggers(EState *estate,
160  ResultRelInfo *relinfo,
161  HeapTuple trigtuple);
162 extern void ExecBSUpdateTriggers(EState *estate,
163  ResultRelInfo *relinfo);
164 extern void ExecASUpdateTriggers(EState *estate,
165  ResultRelInfo *relinfo);
167  EPQState *epqstate,
168  ResultRelInfo *relinfo,
169  ItemPointer tupleid,
170  HeapTuple fdw_trigtuple,
171  TupleTableSlot *slot);
172 extern void ExecARUpdateTriggers(EState *estate,
173  ResultRelInfo *relinfo,
174  ItemPointer tupleid,
175  HeapTuple fdw_trigtuple,
176  HeapTuple newtuple,
177  List *recheckIndexes);
179  ResultRelInfo *relinfo,
180  HeapTuple trigtuple,
181  TupleTableSlot *slot);
182 extern void ExecBSTruncateTriggers(EState *estate,
183  ResultRelInfo *relinfo);
184 extern void ExecASTruncateTriggers(EState *estate,
185  ResultRelInfo *relinfo);
186 
187 extern void AfterTriggerBeginXact(void);
188 extern void AfterTriggerBeginQuery(void);
189 extern void AfterTriggerEndQuery(EState *estate);
190 extern void AfterTriggerFireDeferred(void);
191 extern void AfterTriggerEndXact(bool isCommit);
192 extern void AfterTriggerBeginSubXact(void);
193 extern void AfterTriggerEndSubXact(bool isCommit);
194 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
195 extern bool AfterTriggerPendingOnRel(Oid relid);
196 
197 
198 /*
199  * in utils/adt/ri_triggers.c
200  */
201 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
202  HeapTuple old_row, HeapTuple new_row);
203 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
204  HeapTuple old_row, HeapTuple new_row);
205 extern bool RI_Initial_Check(Trigger *trigger,
206  Relation fk_rel, Relation pk_rel);
207 
208 /* result values for RI_FKey_trigger_type: */
209 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
210 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
211 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
212 
213 extern int RI_FKey_trigger_type(Oid tgfoid);
214 
215 #endif /* TRIGGER_H */
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes)
Definition: trigger.c:2239
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2317
void AfterTriggerBeginSubXact(void)
Definition: trigger.c:4374
bool RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
Definition: ri_triggers.c:2257
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2580
TupleTableSlot * ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2741
Buffer tg_newtuplebuf
Definition: trigger.h:39
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2369
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2163
unsigned int Oid
Definition: postgres_ext.h:31
NodeTag
Definition: nodes.h:26
#define PGDLLIMPORT
Definition: c.h:1063
HeapTuple tg_trigtuple
Definition: trigger.h:35
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2251
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2111
Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok)
Definition: trigger.c:1268
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4320
bool AfterTriggerPendingOnRel(Oid relid)
Definition: trigger.c:5008
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple)
Definition: trigger.c:2448
void RelationBuildTriggers(Relation relation)
Definition: trigger.c:1598
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2807
void EnableDisableTrigger(Relation rel, const char *tgname, char fires_when, bool skip_system)
Definition: trigger.c:1496
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2859
void AfterTriggerBeginQuery(void)
Definition: trigger.c:4150
PGDLLIMPORT int SessionReplicationRole
Definition: trigger.c:64
Buffer tg_trigtuplebuf
Definition: trigger.h:38
unsigned int uint32
Definition: c.h:268
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:3704
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4442
TriggerDesc * CopyTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1826
ObjectAddress CreateTrigger(CreateTrigStmt *stmt, const char *queryString, Oid relOid, Oid refRelOid, Oid constraintOid, Oid indexOid, bool isInternal)
Definition: trigger.c:138
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1190
void AfterTriggerEndQuery(EState *estate)
Definition: trigger.c:4170
void FreeTriggerDesc(TriggerDesc *trigdesc)
Definition: trigger.c:1881
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1364
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2525
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:2591
const char * name
Definition: encode.c:521
Tuplestorestate * tg_oldtable
Definition: trigger.h:40
struct TriggerData TriggerData
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4264
NodeTag type
Definition: trigger.h:32
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple)
Definition: trigger.c:2379
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, HeapTuple newtuple, List *recheckIndexes)
Definition: trigger.c:2708
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:2144
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2478
void AfterTriggerBeginXact(void)
Definition: trigger.c:4112
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2173
bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel, HeapTuple old_row, HeapTuple new_row)
Definition: ri_triggers.c:2087
void AfterTriggerSetState(ConstraintsSetStmt *stmt)
Definition: trigger.c:4714
Relation tg_relation
Definition: trigger.h:34