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-2018, 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/genbki.h"
22 #include "catalog/pg_constraint_d.h"
23 
24 #include "catalog/dependency.h"
25 #include "nodes/pg_list.h"
26 
27 /* ----------------
28  * pg_constraint definition. cpp turns this into
29  * typedef struct FormData_pg_constraint
30  * ----------------
31  */
32 CATALOG(pg_constraint,2606,ConstraintRelationId)
33 {
34  /*
35  * conname + connamespace is deliberately not unique; we allow, for
36  * example, the same name to be used for constraints of different
37  * relations. This is partly for backwards compatibility with past
38  * Postgres practice, and partly because we don't want to have to obtain a
39  * global lock to generate a globally unique name for a nameless
40  * constraint. We associate a namespace with constraint names only for
41  * SQL-spec compatibility.
42  */
43  NameData conname; /* name of this constraint */
44  Oid connamespace; /* OID of namespace containing constraint */
45  char contype; /* constraint type; see codes below */
46  bool condeferrable; /* deferrable constraint? */
47  bool condeferred; /* deferred by default? */
48  bool convalidated; /* constraint has been validated? */
49 
50  /*
51  * conrelid and conkey are only meaningful if the constraint applies to a
52  * specific relation (this excludes domain constraints and assertions).
53  * Otherwise conrelid is 0 and conkey is NULL.
54  */
55  Oid conrelid; /* relation this constraint constrains */
56 
57  /*
58  * contypid links to the pg_type row for a domain if this is a domain
59  * constraint. Otherwise it's 0.
60  *
61  * For SQL-style global ASSERTIONs, both conrelid and contypid would be
62  * zero. This is not presently supported, however.
63  */
64  Oid contypid; /* domain this constraint constrains */
65 
66  /*
67  * conindid links to the index supporting the constraint, if any;
68  * otherwise it's 0. This is used for unique, primary-key, and exclusion
69  * constraints, and less obviously for foreign-key constraints (where the
70  * index is a unique index on the referenced relation's referenced
71  * columns). Notice that the index is on conrelid in the first case but
72  * confrelid in the second.
73  */
74  Oid conindid; /* index supporting this constraint */
75 
76  /*
77  * If this constraint is on a partition inherited from a partitioned
78  * table, this is the OID of the corresponding constraint in the parent.
79  */
80  Oid conparentid;
81 
82  /*
83  * These fields, plus confkey, are only meaningful for a foreign-key
84  * constraint. Otherwise confrelid is 0 and the char fields are spaces.
85  */
86  Oid confrelid; /* relation referenced by foreign key */
87  char confupdtype; /* foreign key's ON UPDATE action */
88  char confdeltype; /* foreign key's ON DELETE action */
89  char confmatchtype; /* foreign key's match type */
90 
91  /* Has a local definition (hence, do not drop when coninhcount is 0) */
92  bool conislocal;
93 
94  /* Number of times inherited from direct parent relation(s) */
95  int32 coninhcount;
96 
97  /* Has a local definition and cannot be inherited */
98  bool connoinherit;
99 
100 #ifdef CATALOG_VARLEN /* variable-length fields start here */
101 
102  /*
103  * Columns of conrelid that the constraint applies to, if known (this is
104  * NULL for trigger constraints)
105  */
106  int16 conkey[1];
107 
108  /*
109  * Columns of conrelid that the constraint does not apply to, but are
110  * included into the same index as the key columns
111  */
112  int16 conincluding[1];
113 
114  /*
115  * If a foreign key, the referenced columns of confrelid
116  */
117  int16 confkey[1];
118 
119  /*
120  * If a foreign key, the OIDs of the PK = FK equality operators for each
121  * column of the constraint
122  */
123  Oid conpfeqop[1];
124 
125  /*
126  * If a foreign key, the OIDs of the PK = PK equality operators for each
127  * column of the constraint (i.e., equality for the referenced columns)
128  */
129  Oid conppeqop[1];
130 
131  /*
132  * If a foreign key, the OIDs of the FK = FK equality operators for each
133  * column of the constraint (i.e., equality for the referencing columns)
134  */
135  Oid conffeqop[1];
136 
137  /*
138  * If an exclusion constraint, the OIDs of the exclusion operators for
139  * each column of the constraint
140  */
141  Oid conexclop[1];
142 
143  /*
144  * If a check constraint, nodeToString representation of expression
145  */
146  pg_node_tree conbin;
147 
148  /*
149  * If a check constraint, source-text representation of expression
150  */
151  text consrc;
152 #endif
154 
155 /* ----------------
156  * Form_pg_constraint corresponds to a pointer to a tuple with
157  * the format of pg_constraint relation.
158  * ----------------
159  */
161 
162 #ifdef EXPOSE_TO_CLIENT_CODE
163 
164 /* Valid values for contype */
165 #define CONSTRAINT_CHECK 'c'
166 #define CONSTRAINT_FOREIGN 'f'
167 #define CONSTRAINT_PRIMARY 'p'
168 #define CONSTRAINT_UNIQUE 'u'
169 #define CONSTRAINT_TRIGGER 't'
170 #define CONSTRAINT_EXCLUSION 'x'
171 
172 /*
173  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
174  * constants defined in parsenodes.h. Valid values for confmatchtype are
175  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
176  */
177 
178 #endif /* EXPOSE_TO_CLIENT_CODE */
179 
180 /*
181  * Identify constraint type for lookup purposes
182  */
183 typedef enum ConstraintCategory
184 {
187  CONSTRAINT_ASSERTION /* for future expansion */
189 
190 /*
191  * Used when cloning a foreign key constraint to a partition, so that the
192  * caller can optionally set up a verification pass for it.
193  */
194 typedef struct ClonedConstraint
195 {
202 
203 
204 extern Oid CreateConstraintEntry(const char *constraintName,
205  Oid constraintNamespace,
206  char constraintType,
207  bool isDeferrable,
208  bool isDeferred,
209  bool isValidated,
210  Oid parentConstrId,
211  Oid relId,
212  const int16 *constraintKey,
213  int constraintNKeys,
214  int constraintNTotalKeys,
215  Oid domainId,
216  Oid indexRelId,
217  Oid foreignRelId,
218  const int16 *foreignKey,
219  const Oid *pfEqOp,
220  const Oid *ppEqOp,
221  const Oid *ffEqOp,
222  int foreignNKeys,
223  char foreignUpdateType,
224  char foreignDeleteType,
225  char foreignMatchType,
226  const Oid *exclOp,
227  Node *conExpr,
228  const char *conBin,
229  const char *conSrc,
230  bool conIsLocal,
231  int conInhCount,
232  bool conNoInherit,
233  bool is_internal);
234 
235 extern void CloneForeignKeyConstraints(Oid parentId, Oid relationId,
236  List **cloned);
237 
238 extern void RemoveConstraintById(Oid conId);
239 extern void RenameConstraintById(Oid conId, const char *newname);
240 
241 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
242  Oid objNamespace, const char *conname);
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 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
252 extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
253  bool missing_ok, Oid *constraintOid);
254 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
255 extern Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId);
256 
257 extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,
258  Oid *constraintOid);
259 
261  Index varno, Index varlevelsup,
262  List *grouping_columns,
263  List **constraintDeps);
264 
265 #endif /* PG_CONSTRAINT_H */
signed short int16
Definition: c.h:312
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
CATALOG(pg_constraint, 2606, ConstraintRelationId)
Definition: pg_constraint.h:32
FormData_pg_constraint
void RenameConstraintById(Oid conId, const char *newname)
Definition: nodes.h:517
unsigned int Oid
Definition: postgres_ext.h:31
ConstraintCategory
signed int int32
Definition: c.h:313
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Definition: c.h:570
Constraint * constraint
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
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
Definition: pg_basebackup.c:84
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, const char *conSrc, bool conIsLocal, int conInhCount, bool conNoInherit, bool is_internal)
Definition: pg_constraint.c:51
unsigned int Index
Definition: c.h:442
FormData_pg_constraint * Form_pg_constraint
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, Oid objNamespace, const char *conname)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
Definition: c.h:516
void RemoveConstraintById(Oid conId)
Definition: pg_list.h:45
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
void CloneForeignKeyConstraints(Oid parentId, Oid relationId, List **cloned)
struct ClonedConstraint ClonedConstraint