pg_trigger.h 5.6 KB

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