Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_constraint.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pg_constraint.h
00004  *    definition of the system "constraint" relation (pg_constraint)
00005  *    along with the relation's initial contents.
00006  *
00007  *
00008  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00009  * Portions Copyright (c) 1994, Regents of the University of California
00010  *
00011  * src/include/catalog/pg_constraint.h
00012  *
00013  * NOTES
00014  *    the genbki.pl script reads this file and generates .bki
00015  *    information from the DATA() statements.
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 #ifndef PG_CONSTRAINT_H
00020 #define PG_CONSTRAINT_H
00021 
00022 #include "catalog/genbki.h"
00023 #include "catalog/dependency.h"
00024 #include "nodes/pg_list.h"
00025 
00026 /* ----------------
00027  *      pg_constraint definition.  cpp turns this into
00028  *      typedef struct FormData_pg_constraint
00029  * ----------------
00030  */
00031 #define ConstraintRelationId  2606
00032 
00033 CATALOG(pg_constraint,2606)
00034 {
00035     /*
00036      * conname + connamespace is deliberately not unique; we allow, for
00037      * example, the same name to be used for constraints of different
00038      * relations.  This is partly for backwards compatibility with past
00039      * Postgres practice, and partly because we don't want to have to obtain a
00040      * global lock to generate a globally unique name for a nameless
00041      * constraint.  We associate a namespace with constraint names only for
00042      * SQL-spec compatibility.
00043      */
00044     NameData    conname;        /* name of this constraint */
00045     Oid         connamespace;   /* OID of namespace containing constraint */
00046     char        contype;        /* constraint type; see codes below */
00047     bool        condeferrable;  /* deferrable constraint? */
00048     bool        condeferred;    /* deferred by default? */
00049     bool        convalidated;   /* constraint has been validated? */
00050 
00051     /*
00052      * conrelid and conkey are only meaningful if the constraint applies to a
00053      * specific relation (this excludes domain constraints and assertions).
00054      * Otherwise conrelid is 0 and conkey is NULL.
00055      */
00056     Oid         conrelid;       /* relation this constraint constrains */
00057 
00058     /*
00059      * contypid links to the pg_type row for a domain if this is a domain
00060      * constraint.  Otherwise it's 0.
00061      *
00062      * For SQL-style global ASSERTIONs, both conrelid and contypid would be
00063      * zero. This is not presently supported, however.
00064      */
00065     Oid         contypid;       /* domain this constraint constrains */
00066 
00067     /*
00068      * conindid links to the index supporting the constraint, if any;
00069      * otherwise it's 0.  This is used for unique, primary-key, and exclusion
00070      * constraints, and less obviously for foreign-key constraints (where the
00071      * index is a unique index on the referenced relation's referenced
00072      * columns).  Notice that the index is on conrelid in the first case but
00073      * confrelid in the second.
00074      */
00075     Oid         conindid;       /* index supporting this constraint */
00076 
00077     /*
00078      * These fields, plus confkey, are only meaningful for a foreign-key
00079      * constraint.  Otherwise confrelid is 0 and the char fields are spaces.
00080      */
00081     Oid         confrelid;      /* relation referenced by foreign key */
00082     char        confupdtype;    /* foreign key's ON UPDATE action */
00083     char        confdeltype;    /* foreign key's ON DELETE action */
00084     char        confmatchtype;  /* foreign key's match type */
00085 
00086     /* Has a local definition (hence, do not drop when coninhcount is 0) */
00087     bool        conislocal;
00088 
00089     /* Number of times inherited from direct parent relation(s) */
00090     int32       coninhcount;
00091 
00092     /* Has a local definition and cannot be inherited */
00093     bool        connoinherit;
00094 
00095 #ifdef CATALOG_VARLEN           /* variable-length fields start here */
00096 
00097     /*
00098      * Columns of conrelid that the constraint applies to, if known (this is
00099      * NULL for trigger constraints)
00100      */
00101     int16       conkey[1];
00102 
00103     /*
00104      * If a foreign key, the referenced columns of confrelid
00105      */
00106     int16       confkey[1];
00107 
00108     /*
00109      * If a foreign key, the OIDs of the PK = FK equality operators for each
00110      * column of the constraint
00111      */
00112     Oid         conpfeqop[1];
00113 
00114     /*
00115      * If a foreign key, the OIDs of the PK = PK equality operators for each
00116      * column of the constraint (i.e., equality for the referenced columns)
00117      */
00118     Oid         conppeqop[1];
00119 
00120     /*
00121      * If a foreign key, the OIDs of the FK = FK equality operators for each
00122      * column of the constraint (i.e., equality for the referencing columns)
00123      */
00124     Oid         conffeqop[1];
00125 
00126     /*
00127      * If an exclusion constraint, the OIDs of the exclusion operators for
00128      * each column of the constraint
00129      */
00130     Oid         conexclop[1];
00131 
00132     /*
00133      * If a check constraint, nodeToString representation of expression
00134      */
00135     pg_node_tree conbin;
00136 
00137     /*
00138      * If a check constraint, source-text representation of expression
00139      */
00140     text        consrc;
00141 #endif
00142 } FormData_pg_constraint;
00143 
00144 /* ----------------
00145  *      Form_pg_constraint corresponds to a pointer to a tuple with
00146  *      the format of pg_constraint relation.
00147  * ----------------
00148  */
00149 typedef FormData_pg_constraint *Form_pg_constraint;
00150 
00151 /* ----------------
00152  *      compiler constants for pg_constraint
00153  * ----------------
00154  */
00155 #define Natts_pg_constraint                 24
00156 #define Anum_pg_constraint_conname          1
00157 #define Anum_pg_constraint_connamespace     2
00158 #define Anum_pg_constraint_contype          3
00159 #define Anum_pg_constraint_condeferrable    4
00160 #define Anum_pg_constraint_condeferred      5
00161 #define Anum_pg_constraint_convalidated     6
00162 #define Anum_pg_constraint_conrelid         7
00163 #define Anum_pg_constraint_contypid         8
00164 #define Anum_pg_constraint_conindid         9
00165 #define Anum_pg_constraint_confrelid        10
00166 #define Anum_pg_constraint_confupdtype      11
00167 #define Anum_pg_constraint_confdeltype      12
00168 #define Anum_pg_constraint_confmatchtype    13
00169 #define Anum_pg_constraint_conislocal       14
00170 #define Anum_pg_constraint_coninhcount      15
00171 #define Anum_pg_constraint_connoinherit     16
00172 #define Anum_pg_constraint_conkey           17
00173 #define Anum_pg_constraint_confkey          18
00174 #define Anum_pg_constraint_conpfeqop        19
00175 #define Anum_pg_constraint_conppeqop        20
00176 #define Anum_pg_constraint_conffeqop        21
00177 #define Anum_pg_constraint_conexclop        22
00178 #define Anum_pg_constraint_conbin           23
00179 #define Anum_pg_constraint_consrc           24
00180 
00181 
00182 /* Valid values for contype */
00183 #define CONSTRAINT_CHECK            'c'
00184 #define CONSTRAINT_FOREIGN          'f'
00185 #define CONSTRAINT_PRIMARY          'p'
00186 #define CONSTRAINT_UNIQUE           'u'
00187 #define CONSTRAINT_TRIGGER          't'
00188 #define CONSTRAINT_EXCLUSION        'x'
00189 
00190 /*
00191  * Valid values for confupdtype and confdeltype are the FKCONSTR_ACTION_xxx
00192  * constants defined in parsenodes.h.  Valid values for confmatchtype are
00193  * the FKCONSTR_MATCH_xxx constants defined in parsenodes.h.
00194  */
00195 
00196 /*
00197  * Identify constraint type for lookup purposes
00198  */
00199 typedef enum ConstraintCategory
00200 {
00201     CONSTRAINT_RELATION,
00202     CONSTRAINT_DOMAIN,
00203     CONSTRAINT_ASSERTION        /* for future expansion */
00204 } ConstraintCategory;
00205 
00206 /*
00207  * prototypes for functions in pg_constraint.c
00208  */
00209 extern Oid CreateConstraintEntry(const char *constraintName,
00210                       Oid constraintNamespace,
00211                       char constraintType,
00212                       bool isDeferrable,
00213                       bool isDeferred,
00214                       bool isValidated,
00215                       Oid relId,
00216                       const int16 *constraintKey,
00217                       int constraintNKeys,
00218                       Oid domainId,
00219                       Oid indexRelId,
00220                       Oid foreignRelId,
00221                       const int16 *foreignKey,
00222                       const Oid *pfEqOp,
00223                       const Oid *ppEqOp,
00224                       const Oid *ffEqOp,
00225                       int foreignNKeys,
00226                       char foreignUpdateType,
00227                       char foreignDeleteType,
00228                       char foreignMatchType,
00229                       const Oid *exclOp,
00230                       Node *conExpr,
00231                       const char *conBin,
00232                       const char *conSrc,
00233                       bool conIsLocal,
00234                       int conInhCount,
00235                       bool conNoInherit,
00236                       bool is_internal);
00237 
00238 extern void RemoveConstraintById(Oid conId);
00239 extern void RenameConstraintById(Oid conId, const char *newname);
00240 extern void SetValidatedConstraintById(Oid conId);
00241 
00242 extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
00243                      Oid objNamespace, const char *conname);
00244 extern char *ChooseConstraintName(const char *name1, const char *name2,
00245                      const char *label, Oid namespaceid,
00246                      List *others);
00247 
00248 extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
00249                           Oid newNspId, bool isType, ObjectAddresses *objsMoved);
00250 extern Oid  get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
00251 extern Oid  get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
00252 
00253 extern bool check_functional_grouping(Oid relid,
00254                           Index varno, Index varlevelsup,
00255                           List *grouping_columns,
00256                           List **constraintDeps);
00257 
00258 #endif   /* PG_CONSTRAINT_H */