1 /*-------------------------------------------------------------------------
4 * definition of the "trigger" system catalog (pg_trigger)
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/catalog/pg_trigger.h
13 * The Catalog.pm module reads this file and derives schema
16 *-------------------------------------------------------------------------
21 #include "catalog/genbki.h"
22 #include "catalog/pg_trigger_d.h" /* IWYU pragma: export */
25 * pg_trigger definition. cpp turns this into
26 * typedef struct FormData_pg_trigger
28 * Note: when tgconstraint is nonzero, tgconstrrelid, tgconstrindid,
29 * tgdeferrable, and tginitdeferred are largely redundant with the referenced
30 * pg_constraint entry. However, it is possible for a non-deferrable trigger
31 * to be associated with a deferrable constraint.
34 CATALOG(pg_trigger
,2620,TriggerRelationId
)
37 Oid tgrelid
BKI_LOOKUP(pg_class
); /* relation trigger is
39 Oid tgparentid
BKI_LOOKUP_OPT(pg_trigger
); /* OID of parent
41 NameData tgname
; /* trigger's name */
42 Oid tgfoid
BKI_LOOKUP(pg_proc
); /* OID of function to be called */
43 int16 tgtype
; /* BEFORE/AFTER/INSTEAD, UPDATE/DELETE/INSERT,
44 * ROW/STATEMENT; see below */
45 char tgenabled
; /* trigger's firing configuration WRT
46 * session_replication_role */
47 bool tgisinternal
; /* trigger is system-generated */
48 Oid tgconstrrelid
BKI_LOOKUP_OPT(pg_class
); /* constraint's FROM
50 Oid tgconstrindid
BKI_LOOKUP_OPT(pg_class
); /* constraint's
51 * supporting index, if
53 Oid tgconstraint
BKI_LOOKUP_OPT(pg_constraint
); /* associated
54 * pg_constraint entry,
56 bool tgdeferrable
; /* constraint trigger is deferrable */
57 bool tginitdeferred
; /* constraint trigger is deferred initially */
58 int16 tgnargs
; /* # of extra arguments in tgargs */
61 * Variable-length fields start here, but we allow direct access to
62 * tgattr. Note: tgattr and tgargs must not be null.
64 int2vector tgattr BKI_FORCE_NOT_NULL
; /* column numbers, if trigger is
68 bytea tgargs BKI_FORCE_NOT_NULL
; /* first\000second\000tgnargs\000 */
69 pg_node_tree tgqual
; /* WHEN expression, or NULL if none */
70 NameData tgoldtable
; /* old transition table, or NULL if none */
71 NameData tgnewtable
; /* new transition table, or NULL if none */
73 } FormData_pg_trigger
;
76 * Form_pg_trigger corresponds to a pointer to a tuple with
77 * the format of pg_trigger relation.
80 typedef FormData_pg_trigger
*Form_pg_trigger
;
82 DECLARE_TOAST(pg_trigger
, 2336, 2337);
84 DECLARE_INDEX(pg_trigger_tgconstraint_index
, 2699, TriggerConstraintIndexId
, pg_trigger
, btree(tgconstraint oid_ops
));
85 DECLARE_UNIQUE_INDEX(pg_trigger_tgrelid_tgname_index
, 2701, TriggerRelidNameIndexId
, pg_trigger
, btree(tgrelid oid_ops
, tgname name_ops
));
86 DECLARE_UNIQUE_INDEX_PKEY(pg_trigger_oid_index
, 2702, TriggerOidIndexId
, pg_trigger
, btree(oid oid_ops
));
88 DECLARE_ARRAY_FOREIGN_KEY((tgrelid
, tgattr
), pg_attribute
, (attrelid
, attnum
));
90 #ifdef EXPOSE_TO_CLIENT_CODE
92 /* Bits within tgtype */
93 #define TRIGGER_TYPE_ROW (1 << 0)
94 #define TRIGGER_TYPE_BEFORE (1 << 1)
95 #define TRIGGER_TYPE_INSERT (1 << 2)
96 #define TRIGGER_TYPE_DELETE (1 << 3)
97 #define TRIGGER_TYPE_UPDATE (1 << 4)
98 #define TRIGGER_TYPE_TRUNCATE (1 << 5)
99 #define TRIGGER_TYPE_INSTEAD (1 << 6)
101 #define TRIGGER_TYPE_LEVEL_MASK (TRIGGER_TYPE_ROW)
102 #define TRIGGER_TYPE_STATEMENT 0
104 /* Note bits within TRIGGER_TYPE_TIMING_MASK aren't adjacent */
105 #define TRIGGER_TYPE_TIMING_MASK \
106 (TRIGGER_TYPE_BEFORE | TRIGGER_TYPE_INSTEAD)
107 #define TRIGGER_TYPE_AFTER 0
109 #define TRIGGER_TYPE_EVENT_MASK \
110 (TRIGGER_TYPE_INSERT | TRIGGER_TYPE_DELETE | TRIGGER_TYPE_UPDATE | TRIGGER_TYPE_TRUNCATE)
112 /* Macros for manipulating tgtype */
113 #define TRIGGER_CLEAR_TYPE(type) ((type) = 0)
115 #define TRIGGER_SETT_ROW(type) ((type) |= TRIGGER_TYPE_ROW)
116 #define TRIGGER_SETT_STATEMENT(type) ((type) |= TRIGGER_TYPE_STATEMENT)
117 #define TRIGGER_SETT_BEFORE(type) ((type) |= TRIGGER_TYPE_BEFORE)
118 #define TRIGGER_SETT_AFTER(type) ((type) |= TRIGGER_TYPE_AFTER)
119 #define TRIGGER_SETT_INSTEAD(type) ((type) |= TRIGGER_TYPE_INSTEAD)
120 #define TRIGGER_SETT_INSERT(type) ((type) |= TRIGGER_TYPE_INSERT)
121 #define TRIGGER_SETT_DELETE(type) ((type) |= TRIGGER_TYPE_DELETE)
122 #define TRIGGER_SETT_UPDATE(type) ((type) |= TRIGGER_TYPE_UPDATE)
123 #define TRIGGER_SETT_TRUNCATE(type) ((type) |= TRIGGER_TYPE_TRUNCATE)
125 #define TRIGGER_FOR_ROW(type) ((type) & TRIGGER_TYPE_ROW)
126 #define TRIGGER_FOR_BEFORE(type) (((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_BEFORE)
127 #define TRIGGER_FOR_AFTER(type) (((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_AFTER)
128 #define TRIGGER_FOR_INSTEAD(type) (((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_INSTEAD)
129 #define TRIGGER_FOR_INSERT(type) ((type) & TRIGGER_TYPE_INSERT)
130 #define TRIGGER_FOR_DELETE(type) ((type) & TRIGGER_TYPE_DELETE)
131 #define TRIGGER_FOR_UPDATE(type) ((type) & TRIGGER_TYPE_UPDATE)
132 #define TRIGGER_FOR_TRUNCATE(type) ((type) & TRIGGER_TYPE_TRUNCATE)
135 * Efficient macro for checking if tgtype matches a particular level
136 * (TRIGGER_TYPE_ROW or TRIGGER_TYPE_STATEMENT), timing (TRIGGER_TYPE_BEFORE,
137 * TRIGGER_TYPE_AFTER or TRIGGER_TYPE_INSTEAD), and event (TRIGGER_TYPE_INSERT,
138 * TRIGGER_TYPE_DELETE, TRIGGER_TYPE_UPDATE, or TRIGGER_TYPE_TRUNCATE). Note
139 * that a tgtype can match more than one event, but only one level or timing.
141 #define TRIGGER_TYPE_MATCHES(type, level, timing, event) \
142 (((type) & (TRIGGER_TYPE_LEVEL_MASK | TRIGGER_TYPE_TIMING_MASK | (event))) == ((level) | (timing) | (event)))
145 * Macro to determine whether tgnewtable or tgoldtable has been specified for
148 #define TRIGGER_USES_TRANSITION_TABLE(namepointer) \
149 ((namepointer) != (char *) NULL)
151 #endif /* EXPOSE_TO_CLIENT_CODE */
153 #endif /* PG_TRIGGER_H */