codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * pg_constraint.h4 * definition of the "constraint" system catalog (pg_constraint)5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/catalog/pg_constraint.h11 *12 * NOTES13 * The Catalog.pm module reads this file and derives schema14 * information.15 *16 *-------------------------------------------------------------------------17 */18#ifndef PG_CONSTRAINT_H19#define PG_CONSTRAINT_H20 21#include "catalog/dependency.h"22#include "catalog/genbki.h"23#include "catalog/pg_constraint_d.h"24#include "nodes/pg_list.h"25 26/* ----------------27 * pg_constraint definition. cpp turns this into28 * typedef struct FormData_pg_constraint29 * ----------------30 */31CATALOG(pg_constraint,2606,ConstraintRelationId)32{33 Oid oid; /* oid */34 35 /*36 * conname + connamespace is deliberately not unique; we allow, for37 * example, the same name to be used for constraints of different38 * relations. This is partly for backwards compatibility with past39 * Postgres practice, and partly because we don't want to have to obtain a40 * global lock to generate a globally unique name for a nameless41 * constraint. We associate a namespace with constraint names only for42 * SQL-spec compatibility.43 *44 * However, we do require conname to be unique among the constraints of a45 * single relation or domain. This is enforced by a unique index on46 * conrelid + contypid + conname.47 */48 NameData conname; /* name of this constraint */49 Oid connamespace BKI_LOOKUP(pg_namespace); /* OID of namespace50 * containing constraint */51 char contype; /* constraint type; see codes below */52 bool condeferrable; /* deferrable constraint? */53 bool condeferred; /* deferred by default? */54 bool convalidated; /* constraint has been validated? */55 56 /*57 * conrelid and conkey are only meaningful if the constraint applies to a58 * specific relation (this excludes domain constraints and assertions).59 * Otherwise conrelid is 0 and conkey is NULL.60 */61 Oid conrelid BKI_LOOKUP_OPT(pg_class); /* relation this62 * constraint constrains */63 64 /*65 * contypid links to the pg_type row for a domain if this is a domain66 * constraint. Otherwise it's 0.67 *68 * For SQL-style global ASSERTIONs, both conrelid and contypid would be69 * zero. This is not presently supported, however.70 */71 Oid contypid BKI_LOOKUP_OPT(pg_type); /* domain this constraint72 * constrains */73 74 /*75 * conindid links to the index supporting the constraint, if any;76 * otherwise it's 0. This is used for unique, primary-key, and exclusion77 * constraints, and less obviously for foreign-key constraints (where the78 * index is a unique index on the referenced relation's referenced79 * columns). Notice that the index is on conrelid in the first case but80 * confrelid in the second.81 */82 Oid conindid BKI_LOOKUP_OPT(pg_class); /* index supporting this83 * constraint */84 85 /*86 * If this constraint is on a partition inherited from a partitioned87 * table, this is the OID of the corresponding constraint in the parent.88 */89 Oid conparentid BKI_LOOKUP_OPT(pg_constraint);90 91 /*92 * These fields, plus confkey, are only meaningful for a foreign-key93 * constraint. Otherwise confrelid is 0 and the char fields are spaces.94 */95 Oid confrelid BKI_LOOKUP_OPT(pg_class); /* relation referenced by96 * foreign key */97 char confupdtype; /* foreign key's ON UPDATE action */98 char confdeltype; /* foreign key's ON DELETE action */99 char confmatchtype; /* foreign key's match type */100 101 /* Has a local definition (hence, do not drop when coninhcount is 0) */102 bool conislocal;103 104 /* Number of times inherited from direct parent relation(s) */105 int16 coninhcount;106 107 /* Has a local definition and cannot be inherited */108 bool connoinherit;109 110#ifdef CATALOG_VARLEN /* variable-length fields start here */111 112 /*113 * Columns of conrelid that the constraint applies to, if known (this is114 * NULL for trigger constraints)115 */116 int16 conkey[1];117 118 /*119 * If a foreign key, the referenced columns of confrelid120 */121 int16 confkey[1];122 123 /*124 * If a foreign key, the OIDs of the PK = FK equality operators for each125 * column of the constraint126 */127 Oid conpfeqop[1] BKI_LOOKUP(pg_operator);128 129 /*130 * If a foreign key, the OIDs of the PK = PK equality operators for each131 * column of the constraint (i.e., equality for the referenced columns)132 */133 Oid conppeqop[1] BKI_LOOKUP(pg_operator);134 135 /*136 * If a foreign key, the OIDs of the FK = FK equality operators for each137 * column of the constraint (i.e., equality for the referencing columns)138 */139 Oid conffeqop[1] BKI_LOOKUP(pg_operator);140 141 /*142 * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset143 * of conkey to updated. If null, all columns are updated.144 */145 int16 confdelsetcols[1];146 147 /*148 * If an exclusion constraint, the OIDs of the exclusion operators for149 * each column of the constraint150 */151 Oid conexclop[1] BKI_LOOKUP(pg_operator);152 153 /*154 * If a check constraint, nodeToString representation of expression155 */156 pg_node_tree conbin;157#endif158} FormData_pg_constraint;159 160/* ----------------161 * Form_pg_constraint corresponds to a pointer to a tuple with162 * the format of pg_constraint relation.163 * ----------------164 */165typedef FormData_pg_constraint *Form_pg_constraint;166 167DECLARE_TOAST(pg_constraint, 2832, 2833);168 169DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, on pg_constraint using btree(conname name_ops, connamespace oid_ops));170DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, on pg_constraint using btree(conrelid oid_ops, contypid oid_ops, conname name_ops));171DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, on pg_constraint using btree(contypid oid_ops));172DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, on pg_constraint using btree(oid oid_ops));173DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, on pg_constraint using btree(conparentid oid_ops));174 175/* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */176DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));177DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));178 179#ifdef EXPOSE_TO_CLIENT_CODE180 181/* Valid values for contype */182#define CONSTRAINT_CHECK 'c'183#define CONSTRAINT_FOREIGN 'f'184#define CONSTRAINT_PRIMARY 'p'185#define CONSTRAINT_UNIQUE 'u'186#define CONSTRAINT_TRIGGER 't'187#define CONSTRAINT_EXCLUSION 'x'188 189/*190 * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx191 * constants defined in parsenodes.h. Valid values for confmatchtype are192 * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.193 */194 195#endif /* EXPOSE_TO_CLIENT_CODE */196 197/*198 * Identify constraint type for lookup purposes199 */200typedef enum ConstraintCategory201{202 CONSTRAINT_RELATION,203 CONSTRAINT_DOMAIN,204 CONSTRAINT_ASSERTION /* for future expansion */205} ConstraintCategory;206 207 208extern Oid CreateConstraintEntry(const char *constraintName,209 Oid constraintNamespace,210 char constraintType,211 bool isDeferrable,212 bool isDeferred,213 bool isValidated,214 Oid parentConstrId,215 Oid relId,216 const int16 *constraintKey,217 int constraintNKeys,218 int constraintNTotalKeys,219 Oid domainId,220 Oid indexRelId,221 Oid foreignRelId,222 const int16 *foreignKey,223 const Oid *pfEqOp,224 const Oid *ppEqOp,225 const Oid *ffEqOp,226 int foreignNKeys,227 char foreignUpdateType,228 char foreignDeleteType,229 const int16 *fkDeleteSetCols,230 int numFkDeleteSetCols,231 char foreignMatchType,232 const Oid *exclOp,233 Node *conExpr,234 const char *conBin,235 bool conIsLocal,236 int conInhCount,237 bool conNoInherit,238 bool is_internal);239 240extern void RemoveConstraintById(Oid conId);241extern void RenameConstraintById(Oid conId, const char *newname);242 243extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,244 const char *conname);245extern bool ConstraintNameExists(const char *conname, Oid namespaceid);246extern char *ChooseConstraintName(const char *name1, const char *name2,247 const char *label, Oid namespaceid,248 List *others);249 250extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,251 Oid newNspId, bool isType, ObjectAddresses *objsMoved);252extern void ConstraintSetParentConstraint(Oid childConstrId,253 Oid parentConstrId,254 Oid childTableId);255extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);256extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,257 bool missing_ok, Oid *constraintOid);258extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);259extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);260 261extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,262 Oid *constraintOid);263extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,264 AttrNumber *conkey, AttrNumber *confkey,265 Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,266 int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);267 268extern bool check_functional_grouping(Oid relid,269 Index varno, Index varlevelsup,270 List *grouping_columns,271 List **constraintDeps);272 273#endif /* PG_CONSTRAINT_H */274 