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-2021, 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  int32 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 an exclusion constraint, the OIDs of the exclusion operators for
143  * each column of the constraint
144  */
145  Oid conexclop[1] BKI_LOOKUP(pg_operator);
146 
147  /*
148  * If a check constraint, nodeToString representation of expression
149  */
150  pg_node_tree conbin;
151 #endif
153 
154 /* ----------------
155  * Form_pg_constraint corresponds to a pointer to a tuple with
156  * the format of pg_constraint relation.
157  * ----------------
158  */
160 
161 DECLARE_TOAST(pg_constraint, 2832, 2833);
162 
163 DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, on pg_constraint using btree(conname name_ops, connamespace oid_ops));
164 #define ConstraintNameNspIndexId 2664
165 DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, on pg_constraint using btree(conrelid oid_ops, contypid oid_ops, conname name_ops));
166 #define ConstraintRelidTypidNameIndexId 2665
167 DECLARE_INDEX(pg_constraint_contypid_index, 2666, on pg_constraint using btree(contypid oid_ops));
168 #define ConstraintTypidIndexId 2666
169 DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, on pg_constraint using btree(oid oid_ops));
170 #define ConstraintOidIndexId 2667
171 DECLARE_INDEX(pg_constraint_conparentid_index, 2579, on pg_constraint using btree(conparentid oid_ops));
172 #define ConstraintParentIndexId 2579
173 
174 /* conkey can contain zero (InvalidAttrNumber) if a whole-row Var is used */
175 DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute, (attrelid, attnum));
176 DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute, (attrelid, attnum));
177 
178 #ifdef EXPOSE_TO_CLIENT_CODE
179 
180 /* Valid values for contype */
181 #define CONSTRAINT_CHECK 'c'
182 #define CONSTRAINT_FOREIGN 'f'
183 #define CONSTRAINT_PRIMARY 'p'
184 #define CONSTRAINT_UNIQUE 'u'
185 #define CONSTRAINT_TRIGGER 't'
186 #define CONSTRAINT_EXCLUSION 'x'
187 
188 /*
189  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
190  * constants defined in parsenodes.h. Valid values for confmatchtype are
191  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
192  */
193 
194 #endif /* EXPOSE_TO_CLIENT_CODE */
195 
196 /*
197  * Identify constraint type for lookup purposes
198  */
199 typedef enum ConstraintCategory
200 {
203  CONSTRAINT_ASSERTION /* for future expansion */
205 
206 
207 extern Oid CreateConstraintEntry(const char *constraintName,
208  Oid constraintNamespace,
209  char constraintType,
210  bool isDeferrable,
211  bool isDeferred,
212  bool isValidated,
213  Oid parentConstrId,
214  Oid relId,
215  const int16 *constraintKey,
216  int constraintNKeys,
217  int constraintNTotalKeys,
218  Oid domainId,
219  Oid indexRelId,
220  Oid foreignRelId,
221  const int16 *foreignKey,
222  const Oid *pfEqOp,
223  const Oid *ppEqOp,
224  const Oid *ffEqOp,
225  int foreignNKeys,
226  char foreignUpdateType,
227  char foreignDeleteType,
228  char foreignMatchType,
229  const Oid *exclOp,
230  Node *conExpr,
231  const char *conBin,
232  bool conIsLocal,
233  int conInhCount,
234  bool conNoInherit,
235  bool is_internal);
236 
237 extern void RemoveConstraintById(Oid conId);
238 extern void RenameConstraintById(Oid conId, const char *newname);
239 
240 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
241  const char *conname);
242 extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
243 extern char *ChooseConstraintName(const char *name1, const char *name2,
244  const char *label, Oid namespaceid,
245  List *others);
246 
247 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
248  Oid newNspId, bool isType, ObjectAddresses *objsMoved);
249 extern void ConstraintSetParentConstraint(Oid childConstrId,
250  Oid parentConstrId,
251  Oid childTableId);
252 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
253 extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
254  bool missing_ok, Oid *constraintOid);
255 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
256 extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
257 
258 extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
259  Oid *constraintOid);
260 extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
261  AttrNumber *conkey, AttrNumber *confkey,
262  Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs);
263 
264 extern bool check_functional_grouping(Oid relid,
265  Index varno, Index varlevelsup,
266  List *grouping_columns,
267  List **constraintDeps);
268 
269 #endif /* PG_CONSTRAINT_H */
signed short int16
Definition: c.h:428
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
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, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int conInhCount, bool conNoInherit, bool is_internal)
Definition: pg_constraint.c:50
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
CATALOG(pg_constraint, 2606, ConstraintRelationId)
Definition: pg_constraint.h:31
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
FormData_pg_constraint
void RenameConstraintById(Oid conId, const char *newname)
Definition: nodes.h:533
unsigned int Oid
Definition: postgres_ext.h:31
ConstraintCategory
DECLARE_ARRAY_FOREIGN_KEY((confrelid, confkey), pg_attribute,(attrelid, attnum))
signed int int32
Definition: c.h:429
DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, on pg_constraint using btree(oid oid_ops))
bool ConstraintNameExists(const char *conname, Oid namespaceid)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Definition: c.h:675
DECLARE_INDEX(pg_constraint_conname_nsp_index, 2664, on pg_constraint using btree(conname name_ops, connamespace oid_ops))
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
#define BKI_LOOKUP_OPT(catalog)
Definition: genbki.h:47
void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks, AttrNumber *conkey, AttrNumber *confkey, Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs)
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, on pg_constraint using 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)
static char * label
unsigned int Index
Definition: c.h:549
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
int16 attnum
Definition: pg_attribute.h:83
FormData_pg_constraint * Form_pg_constraint
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
DECLARE_ARRAY_FOREIGN_KEY_OPT((conrelid, conkey), pg_attribute,(attrelid, attnum))
void RemoveConstraintById(Oid conId)
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
DECLARE_TOAST(pg_constraint, 2832, 2833)