PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 */
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 */
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) */
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 */
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 */
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 */
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 */
162
163 /*
164 * If a check constraint, nodeToString representation of expression
165 */
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
178
184
186
187/* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
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 */
219
220
221extern Oid CreateConstraintEntry(const char *constraintName,
223 char constraintType,
224 bool isDeferrable,
225 bool isDeferred,
226 bool isEnforced,
227 bool isValidated,
229 Oid relId,
230 const int16 *constraintKey,
231 int constraintNKeys,
236 const int16 *foreignKey,
237 const Oid *pfEqOp,
238 const Oid *ppEqOp,
239 const Oid *ffEqOp,
240 int foreignNKeys,
243 const int16 *fkDeleteSetCols,
245 char foreignMatchType,
246 const Oid *exclOp,
247 Node *conExpr,
248 const char *conBin,
249 bool conIsLocal,
251 bool conNoInherit,
252 bool conPeriod,
253 bool is_internal);
254
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, bool is_notvalid);
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,
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);
284
287extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
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,
295
296extern bool check_functional_grouping(Oid relid,
297 Index varno, Index varlevelsup,
300
301#endif /* PG_CONSTRAINT_H */
int16 AttrNumber
Definition attnum.h:21
int16_t int16
Definition c.h:541
unsigned int Index
Definition c.h:628
#define DECLARE_UNIQUE_INDEX_PKEY(name, oid, oidmacro, tblname, decl)
Definition genbki.h:86
#define BKI_LOOKUP(catalog)
Definition genbki.h:46
#define DECLARE_ARRAY_FOREIGN_KEY(cols, reftbl, refcols)
Definition genbki.h:120
#define BKI_LOOKUP_OPT(catalog)
Definition genbki.h:47
#define DECLARE_UNIQUE_INDEX(name, oid, oidmacro, tblname, decl)
Definition genbki.h:85
#define DECLARE_TOAST(name, toastoid, indexoid)
Definition genbki.h:63
#define CATALOG(name, oid, oidmacro)
Definition genbki.h:23
#define DECLARE_ARRAY_FOREIGN_KEY_OPT(cols, reftbl, refcols)
Definition genbki.h:121
#define DECLARE_INDEX(name, oid, oidmacro, tblname, decl)
Definition genbki.h:84
#define MAKE_SYSCACHE(name, idxname, nbuckets)
Definition genbki.h:127
int16 attnum
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)
HeapTuple findNotNullConstraint(Oid relid, const char *colname)
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)
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
ConstraintCategory
@ CONSTRAINT_DOMAIN
@ CONSTRAINT_RELATION
@ CONSTRAINT_ASSERTION
HeapTuple findDomainNotNullConstraint(Oid typid)
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)
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)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, bool is_local, bool is_no_inherit, bool is_notvalid)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
unsigned int Oid
static int fb(int x)
Definition pg_list.h:54
Definition nodes.h:135
Definition c.h:760