1 /*-------------------------------------------------------------------------
4 * definition of the system "constraint" relation (pg_constraint)
5 * along with the relation's initial contents.
8 * Portions Copyright (c) 1996-2012, 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 "nodes/pg_list.h"
26 * pg_constraint definition. cpp turns this into
27 * typedef struct FormData_pg_constraint
30 #define ConstraintRelationId 2606
32 CATALOG(pg_constraint,2606)
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.
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? */
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.
55 Oid conrelid; /* relation this constraint constrains */
58 * contypid links to the pg_type row for a domain if this is a domain
59 * constraint. Otherwise it's 0.
61 * For SQL-style global ASSERTIONs, both conrelid and contypid would be
62 * zero. This is not presently supported, however.
64 Oid contypid; /* domain this constraint constrains */
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.
74 Oid conindid; /* index supporting this constraint */
77 * These fields, plus confkey, are only meaningful for a foreign-key
78 * constraint. Otherwise confrelid is 0 and the char fields are spaces.
80 Oid confrelid; /* relation referenced by foreign key */
81 char confupdtype; /* foreign key's ON UPDATE action */
82 char confdeltype; /* foreign key's ON DELETE action */
83 char confmatchtype; /* foreign key's match type */
85 /* Has a local definition (hence, do not drop when coninhcount is 0) */
88 /* Number of times inherited from direct parent relation(s) */
91 /* Has a local definition and cannot be inherited */
94 #ifdef CATALOG_VARLEN /* variable-length fields start here */
97 * Columns of conrelid that the constraint applies to, if known (this is
98 * NULL for trigger constraints)
103 * If a foreign key, the referenced columns of confrelid
108 * If a foreign key, the OIDs of the PK = FK equality operators for each
109 * column of the constraint
114 * If a foreign key, the OIDs of the PK = PK equality operators for each
115 * column of the constraint (i.e., equality for the referenced columns)
120 * If a foreign key, the OIDs of the FK = FK equality operators for each
121 * column of the constraint (i.e., equality for the referencing columns)
126 * If an exclusion constraint, the OIDs of the exclusion operators for
127 * each column of the constraint
132 * If a check constraint, nodeToString representation of expression
137 * If a check constraint, source-text representation of expression
141 } FormData_pg_constraint;
144 * Form_pg_constraint corresponds to a pointer to a tuple with
145 * the format of pg_constraint relation.
148 typedef FormData_pg_constraint *Form_pg_constraint;
151 * compiler constants for pg_constraint
154 #define Natts_pg_constraint 24
155 #define Anum_pg_constraint_conname 1
156 #define Anum_pg_constraint_connamespace 2
157 #define Anum_pg_constraint_contype 3
158 #define Anum_pg_constraint_condeferrable 4
159 #define Anum_pg_constraint_condeferred 5
160 #define Anum_pg_constraint_convalidated 6
161 #define Anum_pg_constraint_conrelid 7
162 #define Anum_pg_constraint_contypid 8
163 #define Anum_pg_constraint_conindid 9
164 #define Anum_pg_constraint_confrelid 10
165 #define Anum_pg_constraint_confupdtype 11
166 #define Anum_pg_constraint_confdeltype 12
167 #define Anum_pg_constraint_confmatchtype 13
168 #define Anum_pg_constraint_conislocal 14
169 #define Anum_pg_constraint_coninhcount 15
170 #define Anum_pg_constraint_connoinherit 16
171 #define Anum_pg_constraint_conkey 17
172 #define Anum_pg_constraint_confkey 18
173 #define Anum_pg_constraint_conpfeqop 19
174 #define Anum_pg_constraint_conppeqop 20
175 #define Anum_pg_constraint_conffeqop 21
176 #define Anum_pg_constraint_conexclop 22
177 #define Anum_pg_constraint_conbin 23
178 #define Anum_pg_constraint_consrc 24
181 /* Valid values for contype */
182 #define CONSTRAINT_CHECK 'c'
183 #define CONSTRAINT_FOREIGN 'f'
184 #define CONSTRAINT_PRIMARY 'p'
185 #define CONSTRAINT_UNIQUE 'u'
186 #define CONSTRAINT_TRIGGER 't'
187 #define CONSTRAINT_EXCLUSION 'x'
190 * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
191 * constants defined in parsenodes.h. Valid values for confmatchtype are
192 * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
196 * Identify constraint type for lookup purposes
198 typedef enum ConstraintCategory
202 CONSTRAINT_ASSERTION /* for future expansion */
203 } ConstraintCategory;
206 * prototypes for functions in pg_constraint.c
208 extern Oid CreateConstraintEntry(const char *constraintName,
209 Oid constraintNamespace,
215 const int16 *constraintKey,
220 const int16 *foreignKey,
225 char foreignUpdateType,
226 char foreignDeleteType,
227 char foreignMatchType,
236 extern void RemoveConstraintById(Oid conId);
237 extern void RenameConstraintById(Oid conId, const char *newname);
238 extern void SetValidatedConstraintById(Oid conId);
240 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
241 Oid objNamespace, const char *conname);
242 extern char *ChooseConstraintName(const char *name1, const char *name2,
243 const char *label, Oid namespaceid,
246 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
247 Oid newNspId, bool isType);
248 extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
249 extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
251 extern bool check_functional_grouping(Oid relid,
252 Index varno, Index varlevelsup,
253 List *grouping_columns,
254 List **constraintDeps);
256 #endif /* PG_CONSTRAINT_H */