1 /*-------------------------------------------------------------------------
4 * definition of the system "constraint" relation (pg_constraint)
5 * along with the relation's initial contents.
8 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/include/catalog/pg_constraint.h
14 * the genbki.pl script reads this file and generates .bki
15 * information from the DATA() statements.
17 *-------------------------------------------------------------------------
19 #ifndef PG_CONSTRAINT_H
20 #define PG_CONSTRAINT_H
22 #include "catalog/genbki.h"
23 #include "catalog/dependency.h"
24 #include "nodes/pg_list.h"
27 * pg_constraint definition. cpp turns this into
28 * typedef struct FormData_pg_constraint
31 #define ConstraintRelationId 2606
33 CATALOG(pg_constraint,2606)
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.
44 NameData conname; /* name of this constraint */
45 Oid connamespace; /* OID of namespace containing constraint */
46 char contype; /* constraint type; see codes below */
47 bool condeferrable; /* deferrable constraint? */
48 bool condeferred; /* deferred by default? */
49 bool convalidated; /* constraint has been validated? */
52 * conrelid and conkey are only meaningful if the constraint applies to a
53 * specific relation (this excludes domain constraints and assertions).
54 * Otherwise conrelid is 0 and conkey is NULL.
56 Oid conrelid; /* relation this constraint constrains */
59 * contypid links to the pg_type row for a domain if this is a domain
60 * constraint. Otherwise it's 0.
62 * For SQL-style global ASSERTIONs, both conrelid and contypid would be
63 * zero. This is not presently supported, however.
65 Oid contypid; /* domain this constraint constrains */
68 * conindid links to the index supporting the constraint, if any;
69 * otherwise it's 0. This is used for unique, primary-key, and exclusion
70 * constraints, and less obviously for foreign-key constraints (where the
71 * index is a unique index on the referenced relation's referenced
72 * columns). Notice that the index is on conrelid in the first case but
73 * confrelid in the second.
75 Oid conindid; /* index supporting this constraint */
78 * These fields, plus confkey, are only meaningful for a foreign-key
79 * constraint. Otherwise confrelid is 0 and the char fields are spaces.
81 Oid confrelid; /* relation referenced by foreign key */
82 char confupdtype; /* foreign key's ON UPDATE action */
83 char confdeltype; /* foreign key's ON DELETE action */
84 char confmatchtype; /* foreign key's match type */
86 /* Has a local definition (hence, do not drop when coninhcount is 0) */
89 /* Number of times inherited from direct parent relation(s) */
92 /* Has a local definition and cannot be inherited */
95 #ifdef CATALOG_VARLEN /* variable-length fields start here */
98 * Columns of conrelid that the constraint applies to, if known (this is
99 * NULL for trigger constraints)
104 * If a foreign key, the referenced columns of confrelid
109 * If a foreign key, the OIDs of the PK = FK equality operators for each
110 * column of the constraint
115 * If a foreign key, the OIDs of the PK = PK equality operators for each
116 * column of the constraint (i.e., equality for the referenced columns)
121 * If a foreign key, the OIDs of the FK = FK equality operators for each
122 * column of the constraint (i.e., equality for the referencing columns)
127 * If an exclusion constraint, the OIDs of the exclusion operators for
128 * each column of the constraint
133 * If a check constraint, nodeToString representation of expression
138 * If a check constraint, source-text representation of expression
142 } FormData_pg_constraint;
145 * Form_pg_constraint corresponds to a pointer to a tuple with
146 * the format of pg_constraint relation.
149 typedef FormData_pg_constraint *Form_pg_constraint;
152 * compiler constants for pg_constraint
155 #define Natts_pg_constraint 24
156 #define Anum_pg_constraint_conname 1
157 #define Anum_pg_constraint_connamespace 2
158 #define Anum_pg_constraint_contype 3
159 #define Anum_pg_constraint_condeferrable 4
160 #define Anum_pg_constraint_condeferred 5
161 #define Anum_pg_constraint_convalidated 6
162 #define Anum_pg_constraint_conrelid 7
163 #define Anum_pg_constraint_contypid 8
164 #define Anum_pg_constraint_conindid 9
165 #define Anum_pg_constraint_confrelid 10
166 #define Anum_pg_constraint_confupdtype 11
167 #define Anum_pg_constraint_confdeltype 12
168 #define Anum_pg_constraint_confmatchtype 13
169 #define Anum_pg_constraint_conislocal 14
170 #define Anum_pg_constraint_coninhcount 15
171 #define Anum_pg_constraint_connoinherit 16
172 #define Anum_pg_constraint_conkey 17
173 #define Anum_pg_constraint_confkey 18
174 #define Anum_pg_constraint_conpfeqop 19
175 #define Anum_pg_constraint_conppeqop 20
176 #define Anum_pg_constraint_conffeqop 21
177 #define Anum_pg_constraint_conexclop 22
178 #define Anum_pg_constraint_conbin 23
179 #define Anum_pg_constraint_consrc 24
182 /* Valid values for contype */
183 #define CONSTRAINT_CHECK 'c'
184 #define CONSTRAINT_FOREIGN 'f'
185 #define CONSTRAINT_PRIMARY 'p'
186 #define CONSTRAINT_UNIQUE 'u'
187 #define CONSTRAINT_TRIGGER 't'
188 #define CONSTRAINT_EXCLUSION 'x'
191 * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
192 * constants defined in parsenodes.h. Valid values for confmatchtype are
193 * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
197 * Identify constraint type for lookup purposes
199 typedef enum ConstraintCategory
203 CONSTRAINT_ASSERTION /* for future expansion */
204 } ConstraintCategory;
207 * prototypes for functions in pg_constraint.c
209 extern Oid CreateConstraintEntry(const char *constraintName,
210 Oid constraintNamespace,
216 const int16 *constraintKey,
221 const int16 *foreignKey,
226 char foreignUpdateType,
227 char foreignDeleteType,
228 char foreignMatchType,
238 extern void RemoveConstraintById(Oid conId);
239 extern void RenameConstraintById(Oid conId, const char *newname);
240 extern void SetValidatedConstraintById(Oid conId);
242 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
243 Oid objNamespace, const char *conname);
244 extern char *ChooseConstraintName(const char *name1, const char *name2,
245 const char *label, Oid namespaceid,
248 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
249 Oid newNspId, bool isType, ObjectAddresses *objsMoved);
250 extern void get_constraint_relation_oids(Oid constraint_oid, Oid *conrelid, Oid *confrelid);
251 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
252 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
254 extern bool check_functional_grouping(Oid relid,
255 Index varno, Index varlevelsup,
256 List *grouping_columns,
257 List **constraintDeps);
259 #endif /* PG_CONSTRAINT_H */