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
34{
35 Oid oid; /* oid */
36
37 /*
38 * conname + connamespace is deliberately not unique; we allow, for
39 * example, the same name to be used for constraints of different
40 * relations. This is partly for backwards compatibility with past
41 * Postgres practice, and partly because we don't want to have to obtain a
42 * global lock to generate a globally unique name for a nameless
43 * constraint. We associate a namespace with constraint names only for
44 * SQL-spec compatibility.
45 *
46 * However, we do require conname to be unique among the constraints of a
47 * single relation or domain. This is enforced by a unique index on
48 * conrelid + contypid + conname.
49 */
50 NameData conname; /* name of this constraint */
51 Oid connamespace BKI_LOOKUP(pg_namespace); /* OID of namespace
52 * containing constraint */
53 char contype; /* constraint type; see codes below */
54 bool condeferrable; /* deferrable constraint? */
55 bool condeferred; /* deferred by default? */
56 bool conenforced; /* enforced constraint? */
57 bool convalidated; /* constraint has been validated? */
58
59 /*
60 * conrelid and conkey are only meaningful if the constraint applies to a
61 * specific relation (this excludes domain constraints and assertions).
62 * Otherwise conrelid is 0 and conkey is NULL.
63 */
64 Oid conrelid BKI_LOOKUP_OPT(pg_class); /* relation this
65 * constraint constrains */
66
67 /*
68 * contypid links to the pg_type row for a domain if this is a domain
69 * constraint. Otherwise it's 0.
70 *
71 * For SQL-style global ASSERTIONs, both conrelid and contypid would be
72 * zero. This is not presently supported, however.
73 */
74 Oid contypid BKI_LOOKUP_OPT(pg_type); /* domain this constraint
75 * constrains */
76
77 /*
78 * conindid links to the index supporting the constraint, if any;
79 * otherwise it's 0. This is used for unique, primary-key, and exclusion
80 * constraints, and less obviously for foreign-key constraints (where the
81 * index is a unique index on the referenced relation's referenced
82 * columns). Notice that the index is on conrelid in the first case but
83 * confrelid in the second.
84 */
85 Oid conindid BKI_LOOKUP_OPT(pg_class); /* index supporting this
86 * constraint */
87
88 /*
89 * If this constraint is on a partition inherited from a partitioned
90 * table, this is the OID of the corresponding constraint in the parent.
91 */
93
94 /*
95 * These fields, plus confkey, are only meaningful for a foreign-key
96 * constraint. Otherwise confrelid is 0 and the char fields are spaces.
97 */
98 Oid confrelid BKI_LOOKUP_OPT(pg_class); /* relation referenced by
99 * foreign key */
100 char confupdtype; /* foreign key's ON UPDATE action */
101 char confdeltype; /* foreign key's ON DELETE action */
102 char confmatchtype; /* foreign key's match type */
103
104 /* Has a local definition (hence, do not drop when coninhcount is 0) */
105 bool conislocal;
106
107 /* Number of times inherited from direct parent relation(s) */
109
110 /* Has a local definition and cannot be inherited */
111 bool connoinherit;
112
113 /*
114 * For primary keys, unique constraints, and foreign keys, signifies the
115 * last column uses overlaps instead of equals.
116 */
117 bool conperiod;
118
119#ifdef CATALOG_VARLEN /* variable-length fields start here */
120
121 /*
122 * Columns of conrelid that the constraint applies to, if known (this is
123 * NULL for trigger constraints)
124 */
125 int16 conkey[1];
126
127 /*
128 * If a foreign key, the referenced columns of confrelid
129 */
130 int16 confkey[1];
131
132 /*
133 * If a foreign key, the OIDs of the PK = FK equality/overlap operators
134 * for each column of the constraint
135 */
137
138 /*
139 * If a foreign key, the OIDs of the PK = PK equality/overlap operators
140 * for each column of the constraint (i.e., equality for the referenced
141 * columns)
142 */
144
145 /*
146 * If a foreign key, the OIDs of the FK = FK equality/overlap operators
147 * for each column of the constraint (i.e., equality for the referencing
148 * columns)
149 */
151
152 /*
153 * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset
154 * of conkey to updated. If null, all columns are updated.
155 */
156 int16 confdelsetcols[1];
157
158 /*
159 * If an exclusion constraint, the OIDs of the exclusion operators for
160 * each column of the constraint. Also set for unique constraints/primary
161 * keys using WITHOUT OVERLAPS.
162 */
164
165 /*
166 * If a check constraint, nodeToString representation of expression
167 */
169#endif
171
173
174/* ----------------
175 * Form_pg_constraint corresponds to a pointer to a tuple with
176 * the format of pg_constraint relation.
177 * ----------------
178 */
180
182
188
190
191/* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
194
195#ifdef EXPOSE_TO_CLIENT_CODE
196
197/* Valid values for contype */
198#define CONSTRAINT_CHECK 'c'
199#define CONSTRAINT_FOREIGN 'f'
200#define CONSTRAINT_NOTNULL 'n'
201#define CONSTRAINT_PRIMARY 'p'
202#define CONSTRAINT_UNIQUE 'u'
203#define CONSTRAINT_TRIGGER 't'
204#define CONSTRAINT_EXCLUSION 'x'
205
206/*
207 * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
208 * constants defined in parsenodes.h. Valid values for confmatchtype are
209 * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
210 */
211
212#endif /* EXPOSE_TO_CLIENT_CODE */
213
214/*
215 * Identify constraint type for lookup purposes
216 */
223
224
225extern Oid CreateConstraintEntry(const char *constraintName,
227 char constraintType,
228 bool isDeferrable,
229 bool isDeferred,
230 bool isEnforced,
231 bool isValidated,
233 Oid relId,
234 const int16 *constraintKey,
235 int constraintNKeys,
240 const int16 *foreignKey,
241 const Oid *pfEqOp,
242 const Oid *ppEqOp,
243 const Oid *ffEqOp,
244 int foreignNKeys,
247 const int16 *fkDeleteSetCols,
249 char foreignMatchType,
250 const Oid *exclOp,
251 Node *conExpr,
252 const char *conBin,
253 bool conIsLocal,
255 bool conNoInherit,
256 bool conPeriod,
257 bool is_internal);
258
260 const char *conname);
261extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
262extern char *ChooseConstraintName(const char *name1, const char *name2,
263 const char *label, Oid namespaceid,
264 List *others);
265
267extern HeapTuple findNotNullConstraint(Oid relid, const char *colname);
270extern bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, const char *new_conname,
271 bool is_local, bool is_no_inherit, bool is_notvalid);
272extern List *RelationGetNotNullConstraints(Oid relid, bool cooked,
273 bool include_noinh);
274
275extern void RemoveConstraintById(Oid conId);
276extern void RenameConstraintById(Oid conId, const char *newname);
277
278extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
283extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
284extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
285 bool missing_ok, Oid *constraintOid);
286extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
288
291extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
293 Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
294 int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
295extern void FindFKPeriodOpers(Oid opclass,
299
300extern bool check_functional_grouping(Oid relid,
301 Index varno, Index varlevelsup,
304
305#endif /* PG_CONSTRAINT_H */
int16 AttrNumber
Definition attnum.h:21
int16_t int16
Definition c.h:553
unsigned int Index
Definition c.h:640
#define BEGIN_CATALOG_STRUCT
Definition genbki.h:37
#define DECLARE_UNIQUE_INDEX_PKEY(name, oid, oidmacro, tblname, decl)
Definition genbki.h:105
#define BKI_LOOKUP(catalog)
Definition genbki.h:65
#define DECLARE_ARRAY_FOREIGN_KEY(cols, reftbl, refcols)
Definition genbki.h:139
#define END_CATALOG_STRUCT
Definition genbki.h:38
#define BKI_LOOKUP_OPT(catalog)
Definition genbki.h:66
#define DECLARE_UNIQUE_INDEX(name, oid, oidmacro, tblname, decl)
Definition genbki.h:104
#define DECLARE_TOAST(name, toastoid, indexoid)
Definition genbki.h:82
#define CATALOG(name, oid, oidmacro)
Definition genbki.h:42
#define DECLARE_ARRAY_FOREIGN_KEY_OPT(cols, reftbl, refcols)
Definition genbki.h:140
#define DECLARE_INDEX(name, oid, oidmacro, tblname, decl)
Definition genbki.h:103
#define MAKE_SYSCACHE(name, idxname, nbuckets)
Definition genbki.h:146
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
void RemoveConstraintById(Oid conId)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, const char *new_conname, bool is_local, bool is_no_inherit, bool is_notvalid)
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)
END_CATALOG_STRUCT typedef FormData_pg_constraint * Form_pg_constraint
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)
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:772