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-2019, 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; /* OID of namespace containing constraint */
50  char contype; /* constraint type; see codes below */
51  bool condeferrable; /* deferrable constraint? */
52  bool condeferred; /* deferred by default? */
53  bool convalidated; /* constraint has been validated? */
54 
55  /*
56  * conrelid and conkey are only meaningful if the constraint applies to a
57  * specific relation (this excludes domain constraints and assertions).
58  * Otherwise conrelid is 0 and conkey is NULL.
59  */
60  Oid conrelid; /* relation this constraint constrains */
61 
62  /*
63  * contypid links to the pg_type row for a domain if this is a domain
64  * constraint. Otherwise it's 0.
65  *
66  * For SQL-style global ASSERTIONs, both conrelid and contypid would be
67  * zero. This is not presently supported, however.
68  */
69  Oid contypid; /* domain this constraint constrains */
70 
71  /*
72  * conindid links to the index supporting the constraint, if any;
73  * otherwise it's 0. This is used for unique, primary-key, and exclusion
74  * constraints, and less obviously for foreign-key constraints (where the
75  * index is a unique index on the referenced relation's referenced
76  * columns). Notice that the index is on conrelid in the first case but
77  * confrelid in the second.
78  */
79  Oid conindid; /* index supporting this constraint */
80 
81  /*
82  * If this constraint is on a partition inherited from a partitioned
83  * table, this is the OID of the corresponding constraint in the parent.
84  */
85  Oid conparentid;
86 
87  /*
88  * These fields, plus confkey, are only meaningful for a foreign-key
89  * constraint. Otherwise confrelid is 0 and the char fields are spaces.
90  */
91  Oid confrelid; /* relation referenced by foreign key */
92  char confupdtype; /* foreign key's ON UPDATE action */
93  char confdeltype; /* foreign key's ON DELETE action */
94  char confmatchtype; /* foreign key's match type */
95 
96  /* Has a local definition (hence, do not drop when coninhcount is 0) */
97  bool conislocal;
98 
99  /* Number of times inherited from direct parent relation(s) */
100  int32 coninhcount;
101 
102  /* Has a local definition and cannot be inherited */
103  bool connoinherit;
104 
105 #ifdef CATALOG_VARLEN /* variable-length fields start here */
106 
107  /*
108  * Columns of conrelid that the constraint applies to, if known (this is
109  * NULL for trigger constraints)
110  */
111  int16 conkey[1];
112 
113  /*
114  * If a foreign key, the referenced columns of confrelid
115  */
116  int16 confkey[1];
117 
118  /*
119  * If a foreign key, the OIDs of the PK = FK equality operators for each
120  * column of the constraint
121  */
122  Oid conpfeqop[1];
123 
124  /*
125  * If a foreign key, the OIDs of the PK = PK equality operators for each
126  * column of the constraint (i.e., equality for the referenced columns)
127  */
128  Oid conppeqop[1];
129 
130  /*
131  * If a foreign key, the OIDs of the FK = FK equality operators for each
132  * column of the constraint (i.e., equality for the referencing columns)
133  */
134  Oid conffeqop[1];
135 
136  /*
137  * If an exclusion constraint, the OIDs of the exclusion operators for
138  * each column of the constraint
139  */
140  Oid conexclop[1];
141 
142  /*
143  * If a check constraint, nodeToString representation of expression
144  */
145  pg_node_tree conbin;
146 #endif
148 
149 /* ----------------
150  * Form_pg_constraint corresponds to a pointer to a tuple with
151  * the format of pg_constraint relation.
152  * ----------------
153  */
155 
156 #ifdef EXPOSE_TO_CLIENT_CODE
157 
158 /* Valid values for contype */
159 #define CONSTRAINT_CHECK 'c'
160 #define CONSTRAINT_FOREIGN 'f'
161 #define CONSTRAINT_PRIMARY 'p'
162 #define CONSTRAINT_UNIQUE 'u'
163 #define CONSTRAINT_TRIGGER 't'
164 #define CONSTRAINT_EXCLUSION 'x'
165 
166 /*
167  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
168  * constants defined in parsenodes.h. Valid values for confmatchtype are
169  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
170  */
171 
172 #endif /* EXPOSE_TO_CLIENT_CODE */
173 
174 /*
175  * Identify constraint type for lookup purposes
176  */
177 typedef enum ConstraintCategory
178 {
181  CONSTRAINT_ASSERTION /* for future expansion */
183 
184 
185 extern Oid CreateConstraintEntry(const char *constraintName,
186  Oid constraintNamespace,
187  char constraintType,
188  bool isDeferrable,
189  bool isDeferred,
190  bool isValidated,
191  Oid parentConstrId,
192  Oid relId,
193  const int16 *constraintKey,
194  int constraintNKeys,
195  int constraintNTotalKeys,
196  Oid domainId,
197  Oid indexRelId,
198  Oid foreignRelId,
199  const int16 *foreignKey,
200  const Oid *pfEqOp,
201  const Oid *ppEqOp,
202  const Oid *ffEqOp,
203  int foreignNKeys,
204  char foreignUpdateType,
205  char foreignDeleteType,
206  char foreignMatchType,
207  const Oid *exclOp,
208  Node *conExpr,
209  const char *conBin,
210  bool conIsLocal,
211  int conInhCount,
212  bool conNoInherit,
213  bool is_internal);
214 
215 extern void RemoveConstraintById(Oid conId);
216 extern void RenameConstraintById(Oid conId, const char *newname);
217 
218 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
219  const char *conname);
220 extern bool ConstraintNameExists(const char *conname, Oid namespaceid);
221 extern char *ChooseConstraintName(const char *name1, const char *name2,
222  const char *label, Oid namespaceid,
223  List *others);
224 
225 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
226  Oid newNspId, bool isType, ObjectAddresses *objsMoved);
227 extern void ConstraintSetParentConstraint(Oid childConstrId,
228  Oid parentConstrId,
229  Oid childTableId);
230 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
231 extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
232  bool missing_ok, Oid *constraintOid);
233 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
234 extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
235 
236 extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
237  Oid *constraintOid);
238 extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
239  AttrNumber *conkey, AttrNumber *confkey,
240  Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs);
241 
242 extern bool check_functional_grouping(Oid relid,
243  Index varno, Index varlevelsup,
244  List *grouping_columns,
245  List **constraintDeps);
246 
247 #endif /* PG_CONSTRAINT_H */
signed short int16
Definition: c.h:346
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:51
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:525
unsigned int Oid
Definition: postgres_ext.h:31
ConstraintCategory
signed int int32
Definition: c.h:347
bool ConstraintNameExists(const char *conname, Oid namespaceid)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Definition: c.h:610
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
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)
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
static char * label
unsigned int Index
Definition: c.h:476
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
FormData_pg_constraint * Form_pg_constraint
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
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)