34#include "utils/fmgroids.h"
52 Oid constraintNamespace,
60 const int16 *constraintKey,
62 int constraintNTotalKeys,
66 const int16 *foreignKey,
71 char foreignUpdateType,
72 char foreignDeleteType,
73 const int16 *fkDeleteSetCols,
74 int numFkDeleteSetCols,
75 char foreignMatchType,
88 bool nulls[Natts_pg_constraint];
104 Assert(isEnforced || constraintType == CONSTRAINT_CHECK);
106 Assert(isEnforced || !isValidated);
116 if (constraintNKeys > 0)
121 for (
i = 0;
i < constraintNKeys;
i++)
128 if (foreignNKeys > 0)
133 for (
i = 0;
i < foreignNKeys;
i++)
136 for (
i = 0;
i < foreignNKeys;
i++)
139 for (
i = 0;
i < foreignNKeys;
i++)
142 for (
i = 0;
i < foreignNKeys;
i++)
146 if (numFkDeleteSetCols > 0)
148 for (
i = 0;
i < numFkDeleteSetCols;
i++)
153 confdelsetcolsArray = NULL;
158 conpfeqopArray = NULL;
159 conppeqopArray = NULL;
160 conffeqopArray = NULL;
161 confdelsetcolsArray = NULL;
169 for (
i = 0;
i < constraintNKeys;
i++)
174 conexclopArray = NULL;
177 for (
i = 0;
i < Natts_pg_constraint;
i++)
184 Anum_pg_constraint_oid);
209 nulls[Anum_pg_constraint_conkey - 1] =
true;
214 nulls[Anum_pg_constraint_confkey - 1] =
true;
219 nulls[Anum_pg_constraint_conpfeqop - 1] =
true;
224 nulls[Anum_pg_constraint_conppeqop - 1] =
true;
229 nulls[Anum_pg_constraint_conffeqop - 1] =
true;
231 if (confdelsetcolsArray)
234 nulls[Anum_pg_constraint_confdelsetcols - 1] =
true;
239 nulls[Anum_pg_constraint_conexclop - 1] =
true;
244 nulls[Anum_pg_constraint_conbin - 1] =
true;
265 if (constraintNTotalKeys > 0)
267 for (
i = 0;
i < constraintNTotalKeys;
i++)
307 if (foreignNKeys > 0)
309 for (
i = 0;
i < foreignNKeys;
i++)
312 foreignRelId, foreignKey[
i]);
323 if (
OidIsValid(indexRelId) && constraintType == CONSTRAINT_FOREIGN)
337 if (foreignNKeys > 0)
347 oprobject.
classId = OperatorRelationId;
350 for (
i = 0;
i < foreignNKeys;
i++)
354 if (ppEqOp[
i] != pfEqOp[
i])
359 if (ffEqOp[
i] != pfEqOp[
i])
421 Anum_pg_constraint_conrelid,
426 Anum_pg_constraint_contypid,
431 Anum_pg_constraint_conname,
436 true, NULL, 3, skey);
465 Anum_pg_constraint_conname,
470 Anum_pg_constraint_connamespace,
514 char *conname = NULL;
535 if (strcmp((
char *)
lfirst(l), conname) == 0)
545 Anum_pg_constraint_conname,
550 Anum_pg_constraint_connamespace,
595 Anum_pg_constraint_conrelid,
599 true, NULL, 1, &
key);
610 if (con->contype != CONSTRAINT_NOTNULL)
612 if (!con->convalidated)
662 Anum_pg_constraint_contypid,
666 true, NULL, 1, &
key);
675 if (con->contype != CONSTRAINT_NOTNULL)
677 if (!con->convalidated)
705 Anum_pg_constraint_conkey);
711 elog(
ERROR,
"conkey is not a 1-D smallint array");
732 bool is_local,
bool is_no_inherit)
741 bool changed =
false;
750 if (is_no_inherit != conform->connoinherit)
752 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
753 errmsg(
"cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"",
759 &conform->coninhcount))
761 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
762 errmsg(
"too many inheritance parents"));
765 else if (!conform->conislocal)
767 conform->conislocal =
true;
803 Anum_pg_constraint_conrelid,
814 if (conForm->contype != CONSTRAINT_NOTNULL)
816 if (conForm->connoinherit && !include_noinh)
828 cooked->
conoid = conForm->oid;
838 notnulls =
lappend(notnulls, cooked);
856 notnulls =
lappend(notnulls, constr);
881 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
902 if (con->contype == CONSTRAINT_CHECK)
912 elog(
ERROR,
"cache lookup failed for relation %u",
916 if (classForm->relchecks == 0)
917 elog(
ERROR,
"relation \"%s\" has relchecks = 0",
919 classForm->relchecks--;
941 elog(
ERROR,
"constraint %u is not of a known type", conId);
972 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
984 errmsg(
"constraint \"%s\" for relation \"%s\" already exists",
992 errmsg(
"constraint \"%s\" for domain %s already exists",
1025 Anum_pg_constraint_conrelid,
1029 Anum_pg_constraint_contypid,
1047 if (conform->connamespace == oldNspId && oldNspId != newNspId)
1052 conform->connamespace = newNspId;
1097 elog(
ERROR,
"cache lookup failed for constraint %u", childConstrId);
1103 Assert(constrForm->coninhcount == 0);
1105 elog(
ERROR,
"constraint %u already has a parent constraint",
1108 constrForm->conislocal =
false;
1110 &constrForm->coninhcount))
1112 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1113 errmsg(
"too many inheritance parents"));
1115 constrForm->conparentid = parentConstrId;
1129 constrForm->coninhcount--;
1130 constrForm->conislocal =
true;
1134 Assert(constrForm->coninhcount == 0);
1139 ConstraintRelationId,
1168 Anum_pg_constraint_conrelid,
1172 Anum_pg_constraint_contypid,
1176 Anum_pg_constraint_conname,
1192 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1193 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1215 bool missing_ok,
Oid *constraintOid)
1229 Anum_pg_constraint_conrelid,
1233 Anum_pg_constraint_contypid,
1237 Anum_pg_constraint_conname,
1253 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1268 elog(
ERROR,
"conkey is not a 1-D smallint array");
1272 for (
i = 0;
i < numcols;
i++)
1283 if (!
OidIsValid(*constraintOid) && !missing_ok)
1285 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1286 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1314 Anum_pg_constraint_conrelid,
1319 true, NULL, 1, &
key);
1327 if (constrForm->contype != CONSTRAINT_PRIMARY &&
1328 constrForm->contype != CONSTRAINT_UNIQUE &&
1329 constrForm->contype != CONSTRAINT_EXCLUSION)
1332 if (constrForm->conindid == indexId)
1334 constraintId = constrForm->oid;
1341 return constraintId;
1361 Anum_pg_constraint_conrelid,
1365 Anum_pg_constraint_contypid,
1369 Anum_pg_constraint_conname,
1385 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1386 errmsg(
"constraint \"%s\" for domain %s does not exist",
1424 Anum_pg_constraint_conrelid,
1442 if (con->contype != CONSTRAINT_PRIMARY)
1450 if (con->condeferrable && !deferrableOk)
1454 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1457 elog(
ERROR,
"null conkey for constraint %u",
1465 elog(
ERROR,
"conkey is not a 1-D smallint array");
1469 for (
i = 0;
i < numkeys;
i++)
1497 Oid *pf_eq_oprs,
Oid *pp_eq_oprs,
Oid *ff_eq_oprs,
1498 int *num_fk_del_set_cols,
AttrNumber *fk_del_set_cols)
1511 Anum_pg_constraint_conkey);
1516 elog(
ERROR,
"conkey is not a 1-D smallint array");
1519 elog(
ERROR,
"foreign key constraint cannot have %d columns", numkeys);
1525 Anum_pg_constraint_confkey);
1531 elog(
ERROR,
"confkey is not a 1-D smallint array");
1539 Anum_pg_constraint_conpfeqop);
1546 elog(
ERROR,
"conpfeqop is not a 1-D Oid array");
1555 Anum_pg_constraint_conppeqop);
1561 elog(
ERROR,
"conppeqop is not a 1-D Oid array");
1570 Anum_pg_constraint_conffeqop);
1576 elog(
ERROR,
"conffeqop is not a 1-D Oid array");
1582 if (fk_del_set_cols)
1585 Anum_pg_constraint_confdelsetcols, &isNull);
1588 *num_fk_del_set_cols = 0;
1592 int num_delete_cols;
1598 elog(
ERROR,
"confdelsetcols is not a 1-D smallint array");
1599 num_delete_cols =
ARR_DIMS(arr)[0];
1604 *num_fk_del_set_cols = num_delete_cols;
1626 Oid *containedbyoperoid,
1627 Oid *aggedcontainedbyoperoid,
1628 Oid *intersectoperoid)
1637 if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
1639 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1640 errmsg(
"invalid type for PERIOD part of foreign key"),
1641 errdetail(
"Only range and multirange are supported."));
1645 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
1667 aggedcontainedbyoperoid,
1673 *intersectoperoid = OID_RANGE_INTERSECT_RANGE_OP;
1675 case ANYMULTIRANGEOID:
1676 *intersectoperoid = OID_MULTIRANGE_INTERSECT_MULTIRANGE_OP;
1679 elog(
ERROR,
"unexpected opcintype: %u", opcintype);
1701 List *grouping_columns,
1702 List **constraintDeps)
1711 if (pkattnos == NULL)
1715 groupbyattnos = NULL;
1716 foreach(gl, grouping_columns)
1721 gvar->
varno == varno &&
1730 *constraintDeps =
lappend_oid(*constraintDeps, constraintOid);
#define DatumGetArrayTypeP(X)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
#define InvalidAttrNumber
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Bitmapset * bms_add_member(Bitmapset *a, int x)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define Assert(condition)
#define OidIsValid(objectId)
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
@ DEPENDENCY_PARTITION_PRI
@ DEPENDENCY_PARTITION_SEC
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
HeapTuple heap_copytuple(HeapTuple tuple)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static void * GETSTRUCT(const HeapTupleData *tuple)
char * makeObjectName(const char *name1, const char *name2, const char *label)
void GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype, Oid *opid, StrategyNumber *strat)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
List * lappend(List *list, void *datum)
List * lappend_oid(List *list, Oid datum)
#define AccessExclusiveLock
char * get_rel_name(Oid relid)
AttrNumber get_attnum(Oid relid, const char *attname)
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
char * pstrdup(const char *in)
void pfree(void *pointer)
void namestrcpy(Name name, const char *str)
#define IsA(nodeptr, _type_)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
#define ObjectAddressSet(addr, class_id, object_id)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
FormData_pg_class * Form_pg_class
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isEnforced, 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, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int16 conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)
HeapTuple findNotNullConstraint(Oid relid, const char *colname)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
void RemoveConstraintById(Oid conId)
void FindFKPeriodOpers(Oid opclass, Oid *containedbyoperoid, Oid *aggedcontainedbyoperoid, Oid *intersectoperoid)
void RenameConstraintById(Oid conId, const char *newname)
Oid get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
void ConstraintSetParentConstraint(Oid childConstrId, Oid parentConstrId, Oid childTableId)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
HeapTuple findDomainNotNullConstraint(Oid typid)
void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks, AttrNumber *conkey, AttrNumber *confkey, Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs, int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols)
HeapTuple findNotNullConstraintAttnum(Oid relid, AttrNumber attnum)
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
List * RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
AttrNumber extractNotNullColumn(HeapTuple constrTup)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, bool is_local, bool is_no_inherit)
FormData_pg_constraint * Form_pg_constraint
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
size_t strlcpy(char *dst, const char *src, size_t siz)
static Datum PointerGetDatum(const void *X)
static Datum Int16GetDatum(int16 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum NameGetDatum(const NameData *X)
static Pointer DatumGetPointer(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum CharGetDatum(char X)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
#define BTEqualStrategyNumber
#define ERRCODE_DUPLICATE_OBJECT
#define FirstLowInvalidHeapAttributeNumber
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
#define SearchSysCacheCopy1(cacheId, key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
String * makeString(char *str)