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 and unique constraints, signifies the last column uses
112  * 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 operators for each
131  * 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 operators for each
137  * column of the constraint (i.e., equality for the referenced columns)
138  */
139  Oid conppeqop[1] BKI_LOOKUP(pg_operator);
140 
141  /*
142  * If a foreign key, the OIDs of the FK = FK equality operators for each
143  * column of the constraint (i.e., equality for the referencing columns)
144  */
145  Oid conffeqop[1] BKI_LOOKUP(pg_operator);
146 
147  /*
148  * If a foreign key with an ON DELETE SET NULL/DEFAULT action, the subset
149  * of conkey to updated. If null, all columns are updated.
150  */
151  int16 confdelsetcols[1];
152 
153  /*
154  * If an exclusion constraint, the OIDs of the exclusion operators for
155  * each column of the constraint. Also set for unique constraints/primary
156  * keys using WITHOUT OVERLAPS.
157  */
158  Oid conexclop[1] BKI_LOOKUP(pg_operator);
159 
160  /*
161  * If a check constraint, nodeToString representation of expression
162  */
163  pg_node_tree conbin;
164 #endif
166 
167 /* ----------------
168  * Form_pg_constraint corresponds to a pointer to a tuple with
169  * the format of pg_constraint relation.
170  * ----------------
171  */
173 
174 DECLARE_TOAST(pg_constraint, 2832, 2833);
175 
176 DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, ConstraintNameNspIndexId, pg_constraint, btree(conname name_ops, connamespace oid_ops));
177 DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, ConstraintRelidTypidNameIndexId, pg_constraint, btree(conrelid oid_ops, contypid oid_ops, conname name_ops));
178 DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, pg_constraint, btree(contypid oid_ops));
179 DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, pg_constraint, btree(oid oid_ops));
180 DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, pg_constraint, btree(conparentid oid_ops));
181 
182 MAKE_SYSCACHE(CONSTROID, pg_constraint_oid_index, 16);
183 
184 /* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
185 DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));
186 DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));
187 
188 #ifdef EXPOSE_TO_CLIENT_CODE
189 
190 /* Valid values for contype */
191 #define CONSTRAINT_CHECK 'c'
192 #define CONSTRAINT_FOREIGN 'f'
193 #define CONSTRAINT_NOTNULL 'n'
194 #define CONSTRAINT_PRIMARY 'p'
195 #define CONSTRAINT_UNIQUE 'u'
196 #define CONSTRAINT_TRIGGER 't'
197 #define CONSTRAINT_EXCLUSION 'x'
198 
199 /*
200  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
201  * constants defined in parsenodes.h. Valid values for confmatchtype are
202  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
203  */
204 
205 #endif /* EXPOSE_TO_CLIENT_CODE */
206 
207 /*
208  * Identify constraint type for lookup purposes
209  */
210 typedef enum ConstraintCategory
211 {
214  CONSTRAINT_ASSERTION, /* for future expansion */
216 
217 
218 extern Oid CreateConstraintEntry(const char *constraintName,
219  Oid constraintNamespace,
220  char constraintType,
221  bool isDeferrable,
222  bool isDeferred,
223  bool isValidated,
224  Oid parentConstrId,
225  Oid relId,
226  const int16 *constraintKey,
227  int constraintNKeys,
228  int constraintNTotalKeys,
229  Oid domainId,
230  Oid indexRelId,
231  Oid foreignRelId,
232  const int16 *foreignKey,
233  const Oid *pfEqOp,
234  const Oid *ppEqOp,
235  const Oid *ffEqOp,
236  int foreignNKeys,
237  char foreignUpdateType,
238  char foreignDeleteType,
239  const int16 *fkDeleteSetCols,
240  int numFkDeleteSetCols,
241  char foreignMatchType,
242  const Oid *exclOp,
243  Node *conExpr,
244  const char *conBin,
245  bool conIsLocal,
246  int conInhCount,
247  bool conNoInherit,
248  bool conPeriod,
249  bool is_internal);
250 
251 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
252  const char *conname);
253 extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
254 extern char *ChooseConstraintName(const char *name1, const char *name2,
255  const char *label, Oid namespaceid,
256  List *others);
257 
259 extern HeapTuple findNotNullConstraint(Oid relid, const char *colname);
260 extern AttrNumber extractNotNullColumn(HeapTuple constrTup);
261 extern bool AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count,
262  bool is_no_inherit);
263 extern void AdjustNotNullInheritance(Oid relid, Bitmapset *columns, int count);
264 extern List *RelationGetNotNullConstraints(Oid relid, bool cooked);
265 
266 extern void RemoveConstraintById(Oid conId);
267 extern void RenameConstraintById(Oid conId, const char *newname);
268 
269 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
270  Oid newNspId, bool isType, ObjectAddresses *objsMoved);
271 extern void ConstraintSetParentConstraint(Oid childConstrId,
272  Oid parentConstrId,
273  Oid childTableId);
274 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
275 extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
276  bool missing_ok, Oid *constraintOid);
277 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
278 extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
279 
280 extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
281  Oid *constraintOid);
282 extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
283  AttrNumber *conkey, AttrNumber *confkey,
284  Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
285  int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
286 
287 extern bool check_functional_grouping(Oid relid,
288  Index varno, Index varlevelsup,
289  List *grouping_columns,
290  List **constraintDeps);
291 
292 #endif /* PG_CONSTRAINT_H */
int16 AttrNumber
Definition: attnum.h:21
signed short int16
Definition: c.h:480
unsigned int Index
Definition: c.h:601
#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:49
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)
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)
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:728