1 /*-------------------------------------------------------------------------
2  *
3  * pg_trigger.h
4  *	  definition of the system "trigger" relation (pg_trigger)
5  *	  along with the relation's initial contents.
6  *
7  *
8  * Portions Copyright (c) 2003-2017, PgPool Global Development Group
9  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * src/include/catalog/pg_trigger.h
13  *
14  * NOTES
15  *	  the genbki.pl script reads this file and generates .bki
16  *	  information from the DATA() statements.
17  *
18  *-------------------------------------------------------------------------
19  */
20 #ifndef PG_TRIGGER_H
21 #define PG_TRIGGER_H
22 
23 #if 0
24 #include "catalog/genbki.h"
25 
26 /* ----------------
27  *		pg_trigger definition.  cpp turns this into
28  *		typedef struct FormData_pg_trigger
29  *
30  * Note: when tgconstraint is nonzero, tgconstrrelid, tgconstrindid,
31  * tgdeferrable, and tginitdeferred are largely redundant with the referenced
32  * pg_constraint entry.  However, it is possible for a non-deferrable trigger
33  * to be associated with a deferrable constraint.
34  * ----------------
35  */
36 #define TriggerRelationId  2620
37 
38 CATALOG(pg_trigger,2620)
39 {
40 	Oid			tgrelid;		/* relation trigger is attached to */
41 	NameData	tgname;			/* trigger's name */
42 	Oid			tgfoid;			/* 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;	/* constraint's FROM table, if any */
49 	Oid			tgconstrindid;	/* constraint's supporting index, if any */
50 	Oid			tgconstraint;	/* associated pg_constraint entry, if any */
51 	bool		tgdeferrable;	/* constraint trigger is deferrable */
52 	bool		tginitdeferred; /* constraint trigger is deferred initially */
53 	int16		tgnargs;		/* # of extra arguments in tgargs */
54 
55 	/*
56 	 * Variable-length fields start here, but we allow direct access to
57 	 * tgattr. Note: tgattr and tgargs must not be null.
58 	 */
59 	int2vector	tgattr;			/* column numbers, if trigger is on columns */
60 
61 #ifdef CATALOG_VARLEN
62 	bytea		tgargs BKI_FORCE_NOT_NULL;	/* first\000second\000tgnargs\000 */
63 	pg_node_tree tgqual;		/* WHEN expression, or NULL if none */
64 	NameData	tgoldtable;		/* old transition table, or NULL if none */
65 	NameData	tgnewtable;		/* new transition table, or NULL if none */
66 #endif
67 } FormData_pg_trigger;
68 
69 /* ----------------
70  *		Form_pg_trigger corresponds to a pointer to a tuple with
71  *		the format of pg_trigger relation.
72  * ----------------
73  */
74 typedef FormData_pg_trigger *Form_pg_trigger;
75 #endif
76 
77 /* ----------------
78  *		compiler constants for pg_trigger
79  * ----------------
80  */
81 #define Natts_pg_trigger				17
82 #define Anum_pg_trigger_tgrelid			1
83 #define Anum_pg_trigger_tgname			2
84 #define Anum_pg_trigger_tgfoid			3
85 #define Anum_pg_trigger_tgtype			4
86 #define Anum_pg_trigger_tgenabled		5
87 #define Anum_pg_trigger_tgisinternal	6
88 #define Anum_pg_trigger_tgconstrrelid	7
89 #define Anum_pg_trigger_tgconstrindid	8
90 #define Anum_pg_trigger_tgconstraint	9
91 #define Anum_pg_trigger_tgdeferrable	10
92 #define Anum_pg_trigger_tginitdeferred	11
93 #define Anum_pg_trigger_tgnargs			12
94 #define Anum_pg_trigger_tgattr			13
95 #define Anum_pg_trigger_tgargs			14
96 #define Anum_pg_trigger_tgqual			15
97 #define Anum_pg_trigger_tgoldtable		16
98 #define Anum_pg_trigger_tgnewtable		17
99 
100 /* Bits within tgtype */
101 #define TRIGGER_TYPE_ROW				(1 << 0)
102 #define TRIGGER_TYPE_BEFORE				(1 << 1)
103 #define TRIGGER_TYPE_INSERT				(1 << 2)
104 #define TRIGGER_TYPE_DELETE				(1 << 3)
105 #define TRIGGER_TYPE_UPDATE				(1 << 4)
106 #define TRIGGER_TYPE_TRUNCATE			(1 << 5)
107 #define TRIGGER_TYPE_INSTEAD			(1 << 6)
108 
109 #define TRIGGER_TYPE_LEVEL_MASK			(TRIGGER_TYPE_ROW)
110 #define TRIGGER_TYPE_STATEMENT			0
111 
112 /* Note bits within TRIGGER_TYPE_TIMING_MASK aren't adjacent */
113 #define TRIGGER_TYPE_TIMING_MASK \
114 	(TRIGGER_TYPE_BEFORE | TRIGGER_TYPE_INSTEAD)
115 #define TRIGGER_TYPE_AFTER				0
116 
117 #define TRIGGER_TYPE_EVENT_MASK \
118 	(TRIGGER_TYPE_INSERT | TRIGGER_TYPE_DELETE | TRIGGER_TYPE_UPDATE | TRIGGER_TYPE_TRUNCATE)
119 
120 /* Macros for manipulating tgtype */
121 #define TRIGGER_CLEAR_TYPE(type)		((type) = 0)
122 
123 #define TRIGGER_SETT_ROW(type)			((type) |= TRIGGER_TYPE_ROW)
124 #define TRIGGER_SETT_STATEMENT(type)	((type) |= TRIGGER_TYPE_STATEMENT)
125 #define TRIGGER_SETT_BEFORE(type)		((type) |= TRIGGER_TYPE_BEFORE)
126 #define TRIGGER_SETT_AFTER(type)		((type) |= TRIGGER_TYPE_AFTER)
127 #define TRIGGER_SETT_INSTEAD(type)		((type) |= TRIGGER_TYPE_INSTEAD)
128 #define TRIGGER_SETT_INSERT(type)		((type) |= TRIGGER_TYPE_INSERT)
129 #define TRIGGER_SETT_DELETE(type)		((type) |= TRIGGER_TYPE_DELETE)
130 #define TRIGGER_SETT_UPDATE(type)		((type) |= TRIGGER_TYPE_UPDATE)
131 #define TRIGGER_SETT_TRUNCATE(type)		((type) |= TRIGGER_TYPE_TRUNCATE)
132 
133 #define TRIGGER_FOR_ROW(type)			((type) & TRIGGER_TYPE_ROW)
134 #define TRIGGER_FOR_BEFORE(type)		(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_BEFORE)
135 #define TRIGGER_FOR_AFTER(type)			(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_AFTER)
136 #define TRIGGER_FOR_INSTEAD(type)		(((type) & TRIGGER_TYPE_TIMING_MASK) == TRIGGER_TYPE_INSTEAD)
137 #define TRIGGER_FOR_INSERT(type)		((type) & TRIGGER_TYPE_INSERT)
138 #define TRIGGER_FOR_DELETE(type)		((type) & TRIGGER_TYPE_DELETE)
139 #define TRIGGER_FOR_UPDATE(type)		((type) & TRIGGER_TYPE_UPDATE)
140 #define TRIGGER_FOR_TRUNCATE(type)		((type) & TRIGGER_TYPE_TRUNCATE)
141 
142 /*
143  * Efficient macro for checking if tgtype matches a particular level
144  * (TRIGGER_TYPE_ROW or TRIGGER_TYPE_STATEMENT), timing (TRIGGER_TYPE_BEFORE,
145  * TRIGGER_TYPE_AFTER or TRIGGER_TYPE_INSTEAD), and event (TRIGGER_TYPE_INSERT,
146  * TRIGGER_TYPE_DELETE, TRIGGER_TYPE_UPDATE, or TRIGGER_TYPE_TRUNCATE).  Note
147  * that a tgtype can match more than one event, but only one level or timing.
148  */
149 #define TRIGGER_TYPE_MATCHES(type, level, timing, event) \
150 	(((type) & (TRIGGER_TYPE_LEVEL_MASK | TRIGGER_TYPE_TIMING_MASK | (event))) == ((level) | (timing) | (event)))
151 
152 /*
153  * Macro to determine whether tgnewtable or tgoldtable has been specified for
154  * a trigger.
155  */
156 #define TRIGGER_USES_TRANSITION_TABLE(namepointer) \
157 	((namepointer) != (char *) NULL)
158 
159 #endif							/* PG_TRIGGER_H */
160