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