PostgreSQL Source Code  git master
pg_trigger.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_trigger.h
4  * definition of the "trigger" system catalog (pg_trigger)
5  *
6  *
7  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/catalog/pg_trigger.h
11  *
12  * NOTES
13  * The Catalog.pm module reads this file and derives schema
14  * information.
15  *
16  *-------------------------------------------------------------------------
17  */
18 #ifndef PG_TRIGGER_H
19 #define PG_TRIGGER_H
20 
21 #include "catalog/genbki.h"
22 #include "catalog/pg_trigger_d.h"
23 
24 /* ----------------
25  * pg_trigger definition. cpp turns this into
26  * typedef struct FormData_pg_trigger
27  *
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.
32  * ----------------
33  */
34 CATALOG(pg_trigger,2620,TriggerRelationId)
35 {
36  Oid oid; /* oid */
37  Oid tgrelid BKI_LOOKUP(pg_class); /* relation trigger is
38  * attached to */
39  Oid tgparentid BKI_LOOKUP_OPT(pg_trigger); /* OID of parent
40  * trigger, if any */
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
49  * table, if any */
50  Oid tgconstrindid BKI_LOOKUP_OPT(pg_class); /* constraint's
51  * supporting index, if
52  * any */
53  Oid tgconstraint BKI_LOOKUP_OPT(pg_constraint); /* associated
54  * pg_constraint entry,
55  * if any */
56  bool tgdeferrable; /* constraint trigger is deferrable */
57  bool tginitdeferred; /* constraint trigger is deferred initially */
58  int16 tgnargs; /* # of extra arguments in tgargs */
59 
60  /*
61  * Variable-length fields start here, but we allow direct access to
62  * tgattr. Note: tgattr and tgargs must not be null.
63  */
64  int2vector tgattr BKI_FORCE_NOT_NULL; /* column numbers, if trigger is
65  * on columns */
66 
67 #ifdef CATALOG_VARLEN
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 */
72 #endif
74 
75 /* ----------------
76  * Form_pg_trigger corresponds to a pointer to a tuple with
77  * the format of pg_trigger relation.
78  * ----------------
79  */
81 
82 DECLARE_TOAST(pg_trigger, 2336, 2337);
83 
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));
87 
88 DECLARE_ARRAY_FOREIGN_KEY((tgrelid, tgattr), pg_attribute, (attrelid, attnum));
89 
90 #ifdef EXPOSE_TO_CLIENT_CODE
91 
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)
100 
101 #define TRIGGER_TYPE_LEVEL_MASK (TRIGGER_TYPE_ROW)
102 #define TRIGGER_TYPE_STATEMENT 0
103 
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
108 
109 #define TRIGGER_TYPE_EVENT_MASK \
110  (TRIGGER_TYPE_INSERT | TRIGGER_TYPE_DELETE | TRIGGER_TYPE_UPDATE | TRIGGER_TYPE_TRUNCATE)
111 
112 /* Macros for manipulating tgtype */
113 #define TRIGGER_CLEAR_TYPE(type) ((type) = 0)
114 
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)
124 
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)
133 
134 /*
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.
140  */
141 #define TRIGGER_TYPE_MATCHES(type, level, timing, event) \
142  (((type) & (TRIGGER_TYPE_LEVEL_MASK | TRIGGER_TYPE_TIMING_MASK | (event))) == ((level) | (timing) | (event)))
143 
144 /*
145  * Macro to determine whether tgnewtable or tgoldtable has been specified for
146  * a trigger.
147  */
148 #define TRIGGER_USES_TRANSITION_TABLE(namepointer) \
149  ((namepointer) != (char *) NULL)
150 
151 #endif /* EXPOSE_TO_CLIENT_CODE */
152 
153 #endif /* PG_TRIGGER_H */
signed short int16
Definition: c.h:481
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
#define BKI_LOOKUP_OPT(catalog)
Definition: genbki.h:47
#define BKI_FORCE_NOT_NULL
Definition: genbki.h:33
int16 attnum
Definition: pg_attribute.h:74
DECLARE_UNIQUE_INDEX_PKEY(pg_trigger_oid_index, 2702, TriggerOidIndexId, pg_trigger, btree(oid oid_ops))
DECLARE_UNIQUE_INDEX(pg_trigger_tgrelid_tgname_index, 2701, TriggerRelidNameIndexId, pg_trigger, btree(tgrelid oid_ops, tgname name_ops))
DECLARE_TOAST(pg_trigger, 2336, 2337)
FormData_pg_trigger * Form_pg_trigger
Definition: pg_trigger.h:80
DECLARE_ARRAY_FOREIGN_KEY((tgrelid, tgattr), pg_attribute,(attrelid, attnum))
FormData_pg_trigger
Definition: pg_trigger.h:73
DECLARE_INDEX(pg_trigger_tgconstraint_index, 2699, TriggerConstraintIndexId, pg_trigger, btree(tgconstraint oid_ops))
CATALOG(pg_trigger, 2620, TriggerRelationId)
Definition: pg_trigger.h:34
unsigned int Oid
Definition: postgres_ext.h:31
Definition: c.h:694
Definition: c.h:720
Definition: c.h:666