1 /*-------------------------------------------------------------------------
4 * Declarations for trigger handling.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
16 #include "nodes/execnodes.h"
17 #include "nodes/parsenodes.h"
20 * TriggerData is the node type that is passed as fmgr "context" info
21 * when a function is called by the trigger manager.
24 #define CALLED_AS_TRIGGER(fcinfo) \
25 ((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
27 typedef uint32 TriggerEvent
;
29 typedef struct TriggerData
32 TriggerEvent tg_event
;
34 HeapTuple tg_trigtuple
;
35 HeapTuple tg_newtuple
;
37 Buffer tg_trigtuplebuf
;
38 Buffer tg_newtuplebuf
;
42 * TriggerEvent bit flags
44 * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
45 * can't be OR'd together in a single TriggerEvent. This is unlike the
46 * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
49 #define TRIGGER_EVENT_INSERT 0x00000000
50 #define TRIGGER_EVENT_DELETE 0x00000001
51 #define TRIGGER_EVENT_UPDATE 0x00000002
52 #define TRIGGER_EVENT_TRUNCATE 0x00000003
53 #define TRIGGER_EVENT_OPMASK 0x00000003
54 #define TRIGGER_EVENT_ROW 0x00000004
55 #define TRIGGER_EVENT_BEFORE 0x00000008
57 /* More TriggerEvent flags, used only within trigger.c */
59 #define AFTER_TRIGGER_DEFERRABLE 0x00000010
60 #define AFTER_TRIGGER_INITDEFERRED 0x00000020
62 #define TRIGGER_FIRED_BY_INSERT(event) \
63 (((TriggerEvent) (event) & TRIGGER_EVENT_OPMASK) == \
66 #define TRIGGER_FIRED_BY_DELETE(event) \
67 (((TriggerEvent) (event) & TRIGGER_EVENT_OPMASK) == \
70 #define TRIGGER_FIRED_BY_UPDATE(event) \
71 (((TriggerEvent) (event) & TRIGGER_EVENT_OPMASK) == \
74 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
75 (((TriggerEvent) (event) & TRIGGER_EVENT_OPMASK) == \
76 TRIGGER_EVENT_TRUNCATE)
78 #define TRIGGER_FIRED_FOR_ROW(event) \
79 ((TriggerEvent) (event) & TRIGGER_EVENT_ROW)
81 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
82 (!TRIGGER_FIRED_FOR_ROW (event))
84 #define TRIGGER_FIRED_BEFORE(event) \
85 ((TriggerEvent) (event) & TRIGGER_EVENT_BEFORE)
87 #define TRIGGER_FIRED_AFTER(event) \
88 (!TRIGGER_FIRED_BEFORE (event))
91 * Definitions for the replication role based firing.
93 #define SESSION_REPLICATION_ROLE_ORIGIN 0
94 #define SESSION_REPLICATION_ROLE_REPLICA 1
95 #define SESSION_REPLICATION_ROLE_LOCAL 2
96 extern PGDLLIMPORT
int SessionReplicationRole
;
99 * States at which a trigger can be fired. These are the
100 * possible values for pg_trigger.tgenabled.
102 #define TRIGGER_FIRES_ON_ORIGIN 'O'
103 #define TRIGGER_FIRES_ALWAYS 'A'
104 #define TRIGGER_FIRES_ON_REPLICA 'R'
105 #define TRIGGER_DISABLED 'D'
107 extern Oid
CreateTrigger(CreateTrigStmt
*stmt
, Oid constraintOid
,
108 bool checkPermissions
);
110 extern void DropTrigger(Oid relid
, const char *trigname
,
111 DropBehavior behavior
, bool missing_ok
);
112 extern void RemoveTriggerById(Oid trigOid
);
114 extern void renametrig(Oid relid
, const char *oldname
, const char *newname
);
116 extern void EnableDisableTrigger(Relation rel
, const char *tgname
,
117 char fires_when
, bool skip_system
);
119 extern void RelationBuildTriggers(Relation relation
);
121 extern TriggerDesc
*CopyTriggerDesc(TriggerDesc
*trigdesc
);
123 extern void FreeTriggerDesc(TriggerDesc
*trigdesc
);
125 extern void ExecBSInsertTriggers(EState
*estate
,
126 ResultRelInfo
*relinfo
);
127 extern void ExecASInsertTriggers(EState
*estate
,
128 ResultRelInfo
*relinfo
);
129 extern HeapTuple
ExecBRInsertTriggers(EState
*estate
,
130 ResultRelInfo
*relinfo
,
131 HeapTuple trigtuple
);
132 extern void ExecARInsertTriggers(EState
*estate
,
133 ResultRelInfo
*relinfo
,
134 HeapTuple trigtuple
);
135 extern void ExecBSDeleteTriggers(EState
*estate
,
136 ResultRelInfo
*relinfo
);
137 extern void ExecASDeleteTriggers(EState
*estate
,
138 ResultRelInfo
*relinfo
);
139 extern bool ExecBRDeleteTriggers(EState
*estate
,
140 ResultRelInfo
*relinfo
,
141 ItemPointer tupleid
);
142 extern void ExecARDeleteTriggers(EState
*estate
,
143 ResultRelInfo
*relinfo
,
144 ItemPointer tupleid
);
145 extern void ExecBSUpdateTriggers(EState
*estate
,
146 ResultRelInfo
*relinfo
);
147 extern void ExecASUpdateTriggers(EState
*estate
,
148 ResultRelInfo
*relinfo
);
149 extern HeapTuple
ExecBRUpdateTriggers(EState
*estate
,
150 ResultRelInfo
*relinfo
,
153 extern void ExecARUpdateTriggers(EState
*estate
,
154 ResultRelInfo
*relinfo
,
157 extern void ExecBSTruncateTriggers(EState
*estate
,
158 ResultRelInfo
*relinfo
);
159 extern void ExecASTruncateTriggers(EState
*estate
,
160 ResultRelInfo
*relinfo
);
162 extern void AfterTriggerBeginXact(void);
163 extern void AfterTriggerBeginQuery(void);
164 extern void AfterTriggerEndQuery(EState
*estate
);
165 extern void AfterTriggerFireDeferred(void);
166 extern void AfterTriggerEndXact(bool isCommit
);
167 extern void AfterTriggerBeginSubXact(void);
168 extern void AfterTriggerEndSubXact(bool isCommit
);
169 extern void AfterTriggerSetState(ConstraintsSetStmt
*stmt
);
170 extern bool AfterTriggerPendingOnRel(Oid relid
);
174 * in utils/adt/ri_triggers.c
176 extern bool RI_FKey_keyequal_upd_pk(Trigger
*trigger
, Relation pk_rel
,
177 HeapTuple old_row
, HeapTuple new_row
);
178 extern bool RI_FKey_keyequal_upd_fk(Trigger
*trigger
, Relation fk_rel
,
179 HeapTuple old_row
, HeapTuple new_row
);
180 extern bool RI_Initial_Check(Trigger
*trigger
,
181 Relation fk_rel
, Relation pk_rel
);
183 /* result values for RI_FKey_trigger_type: */
184 #define RI_TRIGGER_PK 1 /* is a trigger on the PK relation */
185 #define RI_TRIGGER_FK 2 /* is a trigger on the FK relation */
186 #define RI_TRIGGER_NONE 0 /* is not an RI trigger function */
188 extern int RI_FKey_trigger_type(Oid tgfoid
);
190 #endif /* TRIGGER_H */