]> granicus.if.org Git - postgresql/blob - src/include/catalog/pg_constraint.h
Run pgindent on 9.2 source tree in preparation for first 9.3
[postgresql] / src / include / catalog / pg_constraint.h
1 /*-------------------------------------------------------------------------
2  *
3  * pg_constraint.h
4  *        definition of the system "constraint" relation (pg_constraint)
5  *        along with the relation's initial contents.
6  *
7  *
8  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * src/include/catalog/pg_constraint.h
12  *
13  * NOTES
14  *        the genbki.pl script reads this file and generates .bki
15  *        information from the DATA() statements.
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef PG_CONSTRAINT_H
20 #define PG_CONSTRAINT_H
21
22 #include "catalog/genbki.h"
23 #include "nodes/pg_list.h"
24
25 /* ----------------
26  *              pg_constraint definition.  cpp turns this into
27  *              typedef struct FormData_pg_constraint
28  * ----------------
29  */
30 #define ConstraintRelationId  2606
31
32 CATALOG(pg_constraint,2606)
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          * These fields, plus confkey, are only meaningful for a foreign-key
78          * constraint.  Otherwise confrelid is 0 and the char fields are spaces.
79          */
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 */
84
85         /* Has a local definition (hence, do not drop when coninhcount is 0) */
86         bool            conislocal;
87
88         /* Number of times inherited from direct parent relation(s) */
89         int4            coninhcount;
90
91         /* Has a local definition and cannot be inherited */
92         bool            connoinherit;
93
94 #ifdef CATALOG_VARLEN                   /* variable-length fields start here */
95
96         /*
97          * Columns of conrelid that the constraint applies to, if known (this is
98          * NULL for trigger constraints)
99          */
100         int2            conkey[1];
101
102         /*
103          * If a foreign key, the referenced columns of confrelid
104          */
105         int2            confkey[1];
106
107         /*
108          * If a foreign key, the OIDs of the PK = FK equality operators for each
109          * column of the constraint
110          */
111         Oid                     conpfeqop[1];
112
113         /*
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)
116          */
117         Oid                     conppeqop[1];
118
119         /*
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)
122          */
123         Oid                     conffeqop[1];
124
125         /*
126          * If an exclusion constraint, the OIDs of the exclusion operators for
127          * each column of the constraint
128          */
129         Oid                     conexclop[1];
130
131         /*
132          * If a check constraint, nodeToString representation of expression
133          */
134         pg_node_tree conbin;
135
136         /*
137          * If a check constraint, source-text representation of expression
138          */
139         text            consrc;
140 #endif
141 } FormData_pg_constraint;
142
143 /* ----------------
144  *              Form_pg_constraint corresponds to a pointer to a tuple with
145  *              the format of pg_constraint relation.
146  * ----------------
147  */
148 typedef FormData_pg_constraint *Form_pg_constraint;
149
150 /* ----------------
151  *              compiler constants for pg_constraint
152  * ----------------
153  */
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
179
180
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'
188
189 /*
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.
193  */
194
195 /*
196  * Identify constraint type for lookup purposes
197  */
198 typedef enum ConstraintCategory
199 {
200         CONSTRAINT_RELATION,
201         CONSTRAINT_DOMAIN,
202         CONSTRAINT_ASSERTION            /* for future expansion */
203 } ConstraintCategory;
204
205 /*
206  * prototypes for functions in pg_constraint.c
207  */
208 extern Oid CreateConstraintEntry(const char *constraintName,
209                                           Oid constraintNamespace,
210                                           char constraintType,
211                                           bool isDeferrable,
212                                           bool isDeferred,
213                                           bool isValidated,
214                                           Oid relId,
215                                           const int16 *constraintKey,
216                                           int constraintNKeys,
217                                           Oid domainId,
218                                           Oid indexRelId,
219                                           Oid foreignRelId,
220                                           const int16 *foreignKey,
221                                           const Oid *pfEqOp,
222                                           const Oid *ppEqOp,
223                                           const Oid *ffEqOp,
224                                           int foreignNKeys,
225                                           char foreignUpdateType,
226                                           char foreignDeleteType,
227                                           char foreignMatchType,
228                                           const Oid *exclOp,
229                                           Node *conExpr,
230                                           const char *conBin,
231                                           const char *conSrc,
232                                           bool conIsLocal,
233                                           int conInhCount,
234                                           bool conIsOnly);
235
236 extern void RemoveConstraintById(Oid conId);
237 extern void RenameConstraintById(Oid conId, const char *newname);
238 extern void SetValidatedConstraintById(Oid conId);
239
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,
244                                          List *others);
245
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);
250
251 extern bool check_functional_grouping(Oid relid,
252                                                   Index varno, Index varlevelsup,
253                                                   List *grouping_columns,
254                                                   List **constraintDeps);
255
256 #endif   /* PG_CONSTRAINT_H */