PostgreSQL Source Code  git master
pg_constraint.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_constraint.h
4  * definition of the "constraint" system catalog (pg_constraint)
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_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 
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 into
28  * typedef struct FormData_pg_constraint
29  * ----------------
30  */
31 CATALOG(pg_constraint,2606,ConstraintRelationId)
32 {
33  Oid oid; /* oid */
34 
35  /*
36  * conname + connamespace is deliberately not unique; we allow, for
37  * example, the same name to be used for constraints of different
38  * relations. This is partly for backwards compatibility with past
39  * Postgres practice, and partly because we don't want to have to obtain a
40  * global lock to generate a globally unique name for a nameless
41  * constraint. We associate a namespace with constraint names only for
42  * SQL-spec compatibility.
43  *
44  * However, we do require conname to be unique among the constraints of a
45  * single relation or domain. This is enforced by a unique index on
46  * conrelid + contypid + conname.
47  */
48  NameData conname; /* name of this constraint */
49  Oid connamespace BKI_LOOKUP(pg_namespace); /* OID of namespace
50  * 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 a
58  * 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 this
62  * constraint constrains */
63 
64  /*
65  * contypid links to the pg_type row for a domain if this is a domain
66  * constraint. Otherwise it's 0.
67  *
68  * For SQL-style global ASSERTIONs, both conrelid and contypid would be
69  * zero. This is not presently supported, however.
70  */
71  Oid contypid BKI_LOOKUP_OPT(pg_type); /* domain this constraint
72  * 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 exclusion
77  * constraints, and less obviously for foreign-key constraints (where the
78  * index is a unique index on the referenced relation's referenced
79  * columns). Notice that the index is on conrelid in the first case but
80  * confrelid in the second.
81  */
82  Oid conindid BKI_LOOKUP_OPT(pg_class); /* index supporting this
83  * constraint */
84 
85  /*
86  * If this constraint is on a partition inherited from a partitioned
87  * 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-key
93  * constraint. Otherwise confrelid is 0 and the char fields are spaces.
94  */
95  Oid confrelid BKI_LOOKUP_OPT(pg_class); /* relation referenced by
96  * 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  /*
111  * For primary keys, unique constraints, and foreign keys, signifies the
112  * last column uses overlaps instead of equals.
113  */
114  bool conperiod;
115 
116 #ifdef CATALOG_VARLEN /* variable-length fields start here */
117 
118  /*
119  * Columns of conrelid that the constraint applies to, if known (this is
120  * NULL for trigger constraints)
121  */
122  int16 conkey[1];
123 
124  /*
125  * If a foreign key, the referenced columns of confrelid
126  */
127  int16 confkey[1];
128 
129  /*
130  * If a foreign key, the OIDs of the PK = FK equality/overlap operators
131  * for each column of the constraint
132  */
133  Oid conpfeqop[1] BKI_LOOKUP(pg_operator);
134 
135  /*
136  * If a foreign key, the OIDs of the PK = PK equality/overlap operators
137  * for each column of the constraint (i.e., equality for the referenced
138  * columns)
139  */
140  Oid conppeqop[1] BKI_LOOKUP(pg_operator);
141 
142  /*
143  * If a foreign key, the OIDs of the FK = FK equality/overlap operators
144  * for each column of the constraint (i.e., equality for the referencing
145  * columns)
146  */
147  Oid conffeqop[1] BKI_LOOKUP(pg_operator);
148 
149  /*
150  * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset
151  * of conkey to updated. If null, all columns are updated.
152  */
153  int16 confdelsetcols[1];
154 
155  /*
156  * If an exclusion constraint, the OIDs of the exclusion operators for
157  * each column of the constraint. Also set for unique constraints/primary
158  * keys using WITHOUT OVERLAPS.
159  */
160  Oid conexclop[1] BKI_LOOKUP(pg_operator);
161 
162  /*
163  * If a check constraint, nodeToString representation of expression
164  */
165  pg_node_tree conbin;
166 #endif
168 
169 /* ----------------
170  * Form_pg_constraint corresponds to a pointer to a tuple with
171  * the format of pg_constraint relation.
172  * ----------------
173  */
175 
176 DECLARE_TOAST(pg_constraint, 2832, 2833);
177 
178 DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, pg_constraint, btree(conname name_ops, connamespace oid_ops));
179 DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, pg_constraint, btree(conrelid oid_ops, contypid oid_ops, conname name_ops));
180 DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, pg_constraint, btree(contypid oid_ops));
181 DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, pg_constraint, btree(oid oid_ops));
182 DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, pg_constraint, btree(conparentid oid_ops));
183 
184 MAKE_SYSCACHE(CONSTROID, pg_constraint_oid_index, 16);
185 
186 /* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
187 DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));
188 DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));
189 
190 #ifdef EXPOSE_TO_CLIENT_CODE
191 
192 /* Valid values for contype */
193 #define CONSTRAINT_CHECK 'c'
194 #define CONSTRAINT_FOREIGN 'f'
195 #define CONSTRAINT_NOTNULL 'n'
196 #define CONSTRAINT_PRIMARY 'p'
197 #define CONSTRAINT_UNIQUE 'u'
198 #define CONSTRAINT_TRIGGER 't'
199 #define CONSTRAINT_EXCLUSION 'x'
200 
201 /*
202  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
203  * constants defined in parsenodes.h. Valid values for confmatchtype are
204  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
205  */
206 
207 #endif /* EXPOSE_TO_CLIENT_CODE */
208 
209 /*
210  * Identify constraint type for lookup purposes
211  */
212 typedef enum ConstraintCategory
213 {
216  CONSTRAINT_ASSERTION, /* for future expansion */
218 
219 
220 extern Oid CreateConstraintEntry(const char *constraintName,
221  Oid constraintNamespace,
222  char constraintType,
223  bool isDeferrable,
224  bool isDeferred,
225  bool isValidated,
226  Oid parentConstrId,
227  Oid relId,
228  const int16 *constraintKey,
229  int constraintNKeys,
230  int constraintNTotalKeys,
231  Oid domainId,
232  Oid indexRelId,
233  Oid foreignRelId,
234  const int16 *foreignKey,
235  const Oid *pfEqOp,
236  const Oid *ppEqOp,
237  const Oid *ffEqOp,
238  int foreignNKeys,
239  char foreignUpdateType,
240  char foreignDeleteType,
241  const int16 *fkDeleteSetCols,
242  int numFkDeleteSetCols,
243  char foreignMatchType,
244  const Oid *exclOp,
245  Node *conExpr,
246  const char *conBin,
247  bool conIsLocal,
248  int conInhCount,
249  bool conNoInherit,
250  bool conPeriod,
251  bool is_internal);
252 
253 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
254  const char *conname);
255 extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
256 extern char *ChooseConstraintName(const char *name1, const char *name2,
257  const char *label, Oid namespaceid,
258  List *others);
259 
261 extern HeapTuple findNotNullConstraint(Oid relid, const char *colname);
263 extern AttrNumber extractNotNullColumn(HeapTuple constrTup);
264 extern bool AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count,
265  bool is_no_inherit);
266 extern void AdjustNotNullInheritance(Oid relid, Bitmapset *columns, int count);
267 extern List *RelationGetNotNullConstraints(Oid relid, bool cooked);
268 
269 extern void RemoveConstraintById(Oid conId);
270 extern void RenameConstraintById(Oid conId, const char *newname);
271 
272 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
273  Oid newNspId, bool isType, ObjectAddresses *objsMoved);
274 extern void ConstraintSetParentConstraint(Oid childConstrId,
275  Oid parentConstrId,
276  Oid childTableId);
277 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
278 extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
279  bool missing_ok, Oid *constraintOid);
280 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
281 extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
282 
283 extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
284  Oid *constraintOid);
285 extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
286  AttrNumber *conkey, AttrNumber *confkey,
287  Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
288  int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
289 extern void FindFKPeriodOpers(Oid opclass,
290  Oid *containedbyoperoid,
291  Oid *aggedcontainedbyoperoid);
292 
293 extern bool check_functional_grouping(Oid relid,
294  Index varno, Index varlevelsup,
295  List *grouping_columns,
296  List **constraintDeps);
297 
298 #endif /* PG_CONSTRAINT_H */
int16 AttrNumber
Definition: attnum.h:21
signed short int16
Definition: c.h:493
unsigned int Index
Definition: c.h:614
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
#define BKI_LOOKUP_OPT(catalog)
Definition: genbki.h:47
int16 attnum
Definition: pg_attribute.h:74
static char * label
DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, pg_constraint, btree(conrelid oid_ops, contypid oid_ops, conname name_ops))
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
HeapTuple findNotNullConstraint(Oid relid, const char *colname)
DECLARE_TOAST(pg_constraint, 2832, 2833)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isValidated, Oid parentConstrId, Oid relId, const int16 *constraintKey, int constraintNKeys, int constraintNTotalKeys, Oid domainId, Oid indexRelId, Oid foreignRelId, const int16 *foreignKey, const Oid *pfEqOp, const Oid *ppEqOp, const Oid *ffEqOp, int foreignNKeys, char foreignUpdateType, char foreignDeleteType, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)
Definition: pg_constraint.c:50
FormData_pg_constraint
FormData_pg_constraint * Form_pg_constraint
void RemoveConstraintById(Oid conId)
void RenameConstraintById(Oid conId, const char *newname)
DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, pg_constraint, btree(conname name_ops, connamespace oid_ops))
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, pg_constraint, btree(oid oid_ops))
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute,(attrelid, attnum))
ConstraintCategory
@ CONSTRAINT_DOMAIN
@ CONSTRAINT_RELATION
@ CONSTRAINT_ASSERTION
List * RelationGetNotNullConstraints(Oid relid, bool cooked)
HeapTuple findDomainNotNullConstraint(Oid typid)
CATALOG(pg_constraint, 2606, ConstraintRelationId)
Definition: pg_constraint.h:31
void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks, AttrNumber *conkey, AttrNumber *confkey, Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs, int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols)
void AdjustNotNullInheritance(Oid relid, Bitmapset *columns, int count)
HeapTuple findNotNullConstraintAttnum(Oid relid, AttrNumber attnum)
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
MAKE_SYSCACHE(CONSTROID, pg_constraint_oid_index, 16)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
void FindFKPeriodOpers(Oid opclass, Oid *containedbyoperoid, Oid *aggedcontainedbyoperoid)
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
AttrNumber extractNotNullColumn(HeapTuple constrTup)
DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute,(attrelid, attnum))
bool AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count, bool is_no_inherit)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:54
Definition: nodes.h:129
Definition: c.h:741