pg_constraint.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_constraint.h
  4. * definition of the "constraint" system catalog (pg_constraint)
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/catalog/pg_constraint.h
  11. *
  12. * NOTES
  13. * The Catalog.pm module reads this file and derives schema
  14. * information.
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef PG_CONSTRAINT_H
  19. #define PG_CONSTRAINT_H
  20. #include "catalog/dependency.h"
  21. #include "catalog/genbki.h"
  22. #include "catalog/pg_constraint_d.h"
  23. #include "nodes/pg_list.h"
  24. /* ----------------
  25. * pg_constraint definition. cpp turns this into
  26. * typedef struct FormData_pg_constraint
  27. * ----------------
  28. */
  29. CATALOG(pg_constraint,2606,ConstraintRelationId)
  30. {
  31. Oid oid; /* oid */
  32. /*
  33. * conname + connamespace is deliberately not unique; we allow, for
  34. * example, the same name to be used for constraints of different
  35. * relations. This is partly for backwards compatibility with past
  36. * Postgres practice, and partly because we don't want to have to obtain a
  37. * global lock to generate a globally unique name for a nameless
  38. * constraint. We associate a namespace with constraint names only for
  39. * SQL-spec compatibility.
  40. *
  41. * However, we do require conname to be unique among the constraints of a
  42. * single relation or domain. This is enforced by a unique index on
  43. * conrelid + contypid + conname.
  44. */
  45. NameData conname; /* name of this constraint */
  46. Oid connamespace BKI_LOOKUP(pg_namespace); /* OID of namespace
  47. * containing constraint */
  48. char contype; /* constraint type; see codes below */
  49. bool condeferrable; /* deferrable constraint? */
  50. bool condeferred; /* deferred by default? */
  51. bool convalidated; /* constraint has been validated? */
  52. /*
  53. * conrelid and conkey are only meaningful if the constraint applies to a
  54. * specific relation (this excludes domain constraints and assertions).
  55. * Otherwise conrelid is 0 and conkey is NULL.
  56. */
  57. Oid conrelid BKI_LOOKUP_OPT(pg_class); /* relation this
  58. * constraint constrains */
  59. /*
  60. * contypid links to the pg_type row for a domain if this is a domain
  61. * constraint. Otherwise it's 0.
  62. *
  63. * For SQL-style global ASSERTIONs, both conrelid and contypid would be
  64. * zero. This is not presently supported, however.
  65. */
  66. Oid contypid BKI_LOOKUP_OPT(pg_type); /* domain this constraint
  67. * constrains */
  68. /*
  69. * conindid links to the index supporting the constraint, if any;
  70. * otherwise it's 0. This is used for unique, primary-key, and exclusion
  71. * constraints, and less obviously for foreign-key constraints (where the
  72. * index is a unique index on the referenced relation's referenced
  73. * columns). Notice that the index is on conrelid in the first case but
  74. * confrelid in the second.
  75. */
  76. Oid conindid BKI_LOOKUP_OPT(pg_class); /* index supporting this
  77. * constraint */
  78. /*
  79. * If this constraint is on a partition inherited from a partitioned
  80. * table, this is the OID of the corresponding constraint in the parent.
  81. */
  82. Oid conparentid BKI_LOOKUP_OPT(pg_constraint);
  83. /*
  84. * These fields, plus confkey, are only meaningful for a foreign-key
  85. * constraint. Otherwise confrelid is 0 and the char fields are spaces.
  86. */
  87. Oid confrelid BKI_LOOKUP_OPT(pg_class); /* relation referenced by
  88. * foreign key */
  89. char confupdtype; /* foreign key's ON UPDATE action */
  90. char confdeltype; /* foreign key's ON DELETE action */
  91. char confmatchtype; /* foreign key's match type */
  92. /* Has a local definition (hence, do not drop when coninhcount is 0) */
  93. bool conislocal;
  94. /* Number of times inherited from direct parent relation(s) */
  95. int32 coninhcount;
  96. /* Has a local definition and cannot be inherited */
  97. bool connoinherit;
  98. #ifdef CATALOG_VARLEN /* variable-length fields start here */
  99. /*
  100. * Columns of conrelid that the constraint applies to, if known (this is
  101. * NULL for trigger constraints)
  102. */
  103. int16 conkey[1];
  104. /*
  105. * If a foreign key, the referenced columns of confrelid
  106. */
  107. int16 confkey[1];
  108. /*
  109. * If a foreign key, the OIDs of the PK = FK equality operators for each
  110. * column of the constraint
  111. */
  112. Oid conpfeqop[1] BKI_LOOKUP(pg_operator);
  113. /*
  114. * If a foreign key, the OIDs of the PK = PK equality operators for each
  115. * column of the constraint (i.e., equality for the referenced columns)
  116. */
  117. Oid conppeqop[1] BKI_LOOKUP(pg_operator);
  118. /*
  119. * If a foreign key, the OIDs of the FK = FK equality operators for each
  120. * column of the constraint (i.e., equality for the referencing columns)
  121. */
  122. Oid conffeqop[1] BKI_LOOKUP(pg_operator);
  123. /*
  124. * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset
  125. * of conkey to updated. If null, all columns are updated.
  126. */
  127. int16 confdelsetcols[1];
  128. /*
  129. * If an exclusion constraint, the OIDs of the exclusion operators for
  130. * each column of the constraint
  131. */
  132. Oid conexclop[1] BKI_LOOKUP(pg_operator);
  133. /*
  134. * If a check constraint, nodeToString representation of expression
  135. */
  136. pg_node_tree conbin;
  137. #endif
  138. } FormData_pg_constraint;
  139. /* ----------------
  140. * Form_pg_constraint corresponds to a pointer to a tuple with
  141. * the format of pg_constraint relation.
  142. * ----------------
  143. */
  144. typedef FormData_pg_constraint *Form_pg_constraint;
  145. DECLARE_TOAST(pg_constraint, 2832, 2833);
  146. DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, on pg_constraint using btree(conname name_ops, connamespace oid_ops));
  147. DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, on pg_constraint using btree(conrelid oid_ops, contypid oid_ops, conname name_ops));
  148. DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, on pg_constraint using btree(contypid oid_ops));
  149. DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, on pg_constraint using btree(oid oid_ops));
  150. DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, on pg_constraint using btree(conparentid oid_ops));
  151. /* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
  152. DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));
  153. DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));
  154. #ifdef EXPOSE_TO_CLIENT_CODE
  155. /* Valid values for contype */
  156. #define CONSTRAINT_CHECK 'c'
  157. #define CONSTRAINT_FOREIGN 'f'
  158. #define CONSTRAINT_PRIMARY 'p'
  159. #define CONSTRAINT_UNIQUE 'u'
  160. #define CONSTRAINT_TRIGGER 't'
  161. #define CONSTRAINT_EXCLUSION 'x'
  162. /*
  163. * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
  164. * constants defined in parsenodes.h. Valid values for confmatchtype are
  165. * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
  166. */
  167. #endif /* EXPOSE_TO_CLIENT_CODE */
  168. /*
  169. * Identify constraint type for lookup purposes
  170. */
  171. typedef enum ConstraintCategory
  172. {
  173. CONSTRAINT_RELATION,
  174. CONSTRAINT_DOMAIN,
  175. CONSTRAINT_ASSERTION /* for future expansion */
  176. } ConstraintCategory;
  177. extern Oid CreateConstraintEntry(const char *constraintName,
  178. Oid constraintNamespace,
  179. char constraintType,
  180. bool isDeferrable,
  181. bool isDeferred,
  182. bool isValidated,
  183. Oid parentConstrId,
  184. Oid relId,
  185. const int16 *constraintKey,
  186. int constraintNKeys,
  187. int constraintNTotalKeys,
  188. Oid domainId,
  189. Oid indexRelId,
  190. Oid foreignRelId,
  191. const int16 *foreignKey,
  192. const Oid *pfEqOp,
  193. const Oid *ppEqOp,
  194. const Oid *ffEqOp,
  195. int foreignNKeys,
  196. char foreignUpdateType,
  197. char foreignDeleteType,
  198. const int16 *fkDeleteSetCols,
  199. int numFkDeleteSetCols,
  200. char foreignMatchType,
  201. const Oid *exclOp,
  202. Node *conExpr,
  203. const char *conBin,
  204. bool conIsLocal,
  205. int conInhCount,
  206. bool conNoInherit,
  207. bool is_internal);
  208. extern void RemoveConstraintById(Oid conId);
  209. extern void RenameConstraintById(Oid conId, const char *newname);
  210. extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
  211. const char *conname);
  212. extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
  213. extern char *ChooseConstraintName(const char *name1, const char *name2,
  214. const char *label, Oid namespaceid,
  215. List *others);
  216. extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
  217. Oid newNspId, bool isType, ObjectAddresses *objsMoved);
  218. extern void ConstraintSetParentConstraint(Oid childConstrId,
  219. Oid parentConstrId,
  220. Oid childTableId);
  221. extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
  222. extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
  223. bool missing_ok, Oid *constraintOid);
  224. extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
  225. extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
  226. extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
  227. Oid *constraintOid);
  228. extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
  229. AttrNumber *conkey, AttrNumber *confkey,
  230. Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
  231. int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
  232. extern bool check_functional_grouping(Oid relid,
  233. Index varno, Index varlevelsup,
  234. List *grouping_columns,
  235. List **constraintDeps);
  236. #endif /* PG_CONSTRAINT_H */