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-2025, 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" /* IWYU pragma: export */
24#include "nodes/pg_list.h"
25
26/* ----------------
27 * pg_constraint definition. cpp turns this into
28 * typedef struct FormData_pg_constraint
29 * ----------------
30 */
31CATALOG(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 conenforced; /* enforced constraint? */
55 bool convalidated; /* constraint has been validated? */
56
57 /*
58 * conrelid and conkey are only meaningful if the constraint applies to a
59 * specific relation (this excludes domain constraints and assertions).
60 * Otherwise conrelid is 0 and conkey is NULL.
61 */
62 Oid conrelid BKI_LOOKUP_OPT(pg_class); /* relation this
63 * constraint constrains */
64
65 /*
66 * contypid links to the pg_type row for a domain if this is a domain
67 * constraint. Otherwise it's 0.
68 *
69 * For SQL-style global ASSERTIONs, both conrelid and contypid would be
70 * zero. This is not presently supported, however.
71 */
72 Oid contypid BKI_LOOKUP_OPT(pg_type); /* domain this constraint
73 * constrains */
74
75 /*
76 * conindid links to the index supporting the constraint, if any;
77 * otherwise it's 0. This is used for unique, primary-key, and exclusion
78 * constraints, and less obviously for foreign-key constraints (where the
79 * index is a unique index on the referenced relation's referenced
80 * columns). Notice that the index is on conrelid in the first case but
81 * confrelid in the second.
82 */
83 Oid conindid BKI_LOOKUP_OPT(pg_class); /* index supporting this
84 * constraint */
85
86 /*
87 * If this constraint is on a partition inherited from a partitioned
88 * table, this is the OID of the corresponding constraint in the parent.
89 */
90 Oid conparentid BKI_LOOKUP_OPT(pg_constraint);
91
92 /*
93 * These fields, plus confkey, are only meaningful for a foreign-key
94 * constraint. Otherwise confrelid is 0 and the char fields are spaces.
95 */
96 Oid confrelid BKI_LOOKUP_OPT(pg_class); /* relation referenced by
97 * foreign key */
98 char confupdtype; /* foreign key's ON UPDATE action */
99 char confdeltype; /* foreign key's ON DELETE action */
100 char confmatchtype; /* foreign key's match type */
101
102 /* Has a local definition (hence, do not drop when coninhcount is 0) */
103 bool conislocal;
104
105 /* Number of times inherited from direct parent relation(s) */
106 int16 coninhcount;
107
108 /* Has a local definition and cannot be inherited */
109 bool connoinherit;
110
111 /*
112 * For primary keys, unique constraints, and foreign keys, signifies the
113 * last column uses overlaps instead of equals.
114 */
115 bool conperiod;
116
117#ifdef CATALOG_VARLEN /* variable-length fields start here */
118
119 /*
120 * Columns of conrelid that the constraint applies to, if known (this is
121 * NULL for trigger constraints)
122 */
123 int16 conkey[1];
124
125 /*
126 * If a foreign key, the referenced columns of confrelid
127 */
128 int16 confkey[1];
129
130 /*
131 * If a foreign key, the OIDs of the PK = FK equality/overlap operators
132 * for each column of the constraint
133 */
134 Oid conpfeqop[1] BKI_LOOKUP(pg_operator);
135
136 /*
137 * If a foreign key, the OIDs of the PK = PK equality/overlap operators
138 * for each column of the constraint (i.e., equality for the referenced
139 * columns)
140 */
141 Oid conppeqop[1] BKI_LOOKUP(pg_operator);
142
143 /*
144 * If a foreign key, the OIDs of the FK = FK equality/overlap operators
145 * for each column of the constraint (i.e., equality for the referencing
146 * columns)
147 */
148 Oid conffeqop[1] BKI_LOOKUP(pg_operator);
149
150 /*
151 * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset
152 * of conkey to updated. If null, all columns are updated.
153 */
154 int16 confdelsetcols[1];
155
156 /*
157 * If an exclusion constraint, the OIDs of the exclusion operators for
158 * each column of the constraint. Also set for unique constraints/primary
159 * keys using WITHOUT OVERLAPS.
160 */
161 Oid conexclop[1] BKI_LOOKUP(pg_operator);
162
163 /*
164 * If a check constraint, nodeToString representation of expression
165 */
166 pg_node_tree conbin;
167#endif
169
170/* ----------------
171 * Form_pg_constraint corresponds to a pointer to a tuple with
172 * the format of pg_constraint relation.
173 * ----------------
174 */
176
177DECLARE_TOAST(pg_constraint, 2832, 2833);
178
179DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, pg_constraint, btree(conname name_ops, connamespace oid_ops));
180DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, pg_constraint, btree(conrelid oid_ops, contypid oid_ops, conname name_ops));
181DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, pg_constraint, btree(contypid oid_ops));
182DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, pg_constraint, btree(oid oid_ops));
183DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, pg_constraint, btree(conparentid oid_ops));
184
185MAKE_SYSCACHE(CONSTROID, pg_constraint_oid_index, 16);
186
187/* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
188DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));
189DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));
190
191#ifdef EXPOSE_TO_CLIENT_CODE
192
193/* Valid values for contype */
194#define CONSTRAINT_CHECK 'c'
195#define CONSTRAINT_FOREIGN 'f'
196#define CONSTRAINT_NOTNULL 'n'
197#define CONSTRAINT_PRIMARY 'p'
198#define CONSTRAINT_UNIQUE 'u'
199#define CONSTRAINT_TRIGGER 't'
200#define CONSTRAINT_EXCLUSION 'x'
201
202/*
203 * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
204 * constants defined in parsenodes.h. Valid values for confmatchtype are
205 * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
206 */
207
208#endif /* EXPOSE_TO_CLIENT_CODE */
209
210/*
211 * Identify constraint type for lookup purposes
212 */
214{
217 CONSTRAINT_ASSERTION, /* for future expansion */
219
220
221extern Oid CreateConstraintEntry(const char *constraintName,
222 Oid constraintNamespace,
223 char constraintType,
224 bool isDeferrable,
225 bool isDeferred,
226 bool isEnforced,
227 bool isValidated,
228 Oid parentConstrId,
229 Oid relId,
230 const int16 *constraintKey,
231 int constraintNKeys,
232 int constraintNTotalKeys,
233 Oid domainId,
234 Oid indexRelId,
235 Oid foreignRelId,
236 const int16 *foreignKey,
237 const Oid *pfEqOp,
238 const Oid *ppEqOp,
239 const Oid *ffEqOp,
240 int foreignNKeys,
241 char foreignUpdateType,
242 char foreignDeleteType,
243 const int16 *fkDeleteSetCols,
244 int numFkDeleteSetCols,
245 char foreignMatchType,
246 const Oid *exclOp,
247 Node *conExpr,
248 const char *conBin,
249 bool conIsLocal,
250 int16 conInhCount,
251 bool conNoInherit,
252 bool conPeriod,
253 bool is_internal);
254
255extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
256 const char *conname);
257extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
258extern char *ChooseConstraintName(const char *name1, const char *name2,
259 const char *label, Oid namespaceid,
260 List *others);
261
263extern HeapTuple findNotNullConstraint(Oid relid, const char *colname);
267 bool is_local, bool is_no_inherit);
268extern List *RelationGetNotNullConstraints(Oid relid, bool cooked,
269 bool include_noinh);
270
271extern void RemoveConstraintById(Oid conId);
272extern void RenameConstraintById(Oid conId, const char *newname);
273
274extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
275 Oid newNspId, bool isType, ObjectAddresses *objsMoved);
276extern void ConstraintSetParentConstraint(Oid childConstrId,
277 Oid parentConstrId,
278 Oid childTableId);
279extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
280extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
281 bool missing_ok, Oid *constraintOid);
282extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
283extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
284
285extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
286 Oid *constraintOid);
287extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
288 AttrNumber *conkey, AttrNumber *confkey,
289 Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
290 int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
291extern void FindFKPeriodOpers(Oid opclass,
292 Oid *containedbyoperoid,
293 Oid *aggedcontainedbyoperoid,
294 Oid *intersectoperoid);
295
296extern bool check_functional_grouping(Oid relid,
297 Index varno, Index varlevelsup,
298 List *grouping_columns,
299 List **constraintDeps);
300
301#endif /* PG_CONSTRAINT_H */
int16 AttrNumber
Definition: attnum.h:21
int16_t int16
Definition: c.h:483
unsigned int Index
Definition: c.h:571
#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
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isEnforced, 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, int16 conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)
Definition: pg_constraint.c:51
DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, pg_constraint, btree(conrelid oid_ops, contypid oid_ops, conname name_ops))
HeapTuple findNotNullConstraint(Oid relid, const char *colname)
DECLARE_TOAST(pg_constraint, 2832, 2833)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
FormData_pg_constraint
FormData_pg_constraint * Form_pg_constraint
void RemoveConstraintById(Oid conId)
void FindFKPeriodOpers(Oid opclass, Oid *containedbyoperoid, Oid *aggedcontainedbyoperoid, Oid *intersectoperoid)
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))
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
ConstraintCategory
@ CONSTRAINT_DOMAIN
@ CONSTRAINT_RELATION
@ CONSTRAINT_ASSERTION
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)
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)
List * RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
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))
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, bool is_local, bool is_no_inherit)
unsigned int Oid
Definition: postgres_ext.h:32
Definition: pg_list.h:54
Definition: nodes.h:129
Definition: c.h:698