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 ||
105 constraintType == CONSTRAINT_FOREIGN);
107 Assert(isEnforced || !isValidated);
117 if (constraintNKeys > 0)
122 for (
i = 0;
i < constraintNKeys;
i++)
129 if (foreignNKeys > 0)
132 int nkeys =
Max(foreignNKeys, numFkDeleteSetCols);
135 for (
i = 0;
i < foreignNKeys;
i++)
138 for (
i = 0;
i < foreignNKeys;
i++)
141 for (
i = 0;
i < foreignNKeys;
i++)
144 for (
i = 0;
i < foreignNKeys;
i++)
148 if (numFkDeleteSetCols > 0)
150 for (
i = 0;
i < numFkDeleteSetCols;
i++)
155 confdelsetcolsArray = NULL;
160 conpfeqopArray = NULL;
161 conppeqopArray = NULL;
162 conffeqopArray = NULL;
163 confdelsetcolsArray = NULL;
171 for (
i = 0;
i < constraintNKeys;
i++)
176 conexclopArray = NULL;
179 for (
i = 0;
i < Natts_pg_constraint;
i++)
186 Anum_pg_constraint_oid);
211 nulls[Anum_pg_constraint_conkey - 1] =
true;
216 nulls[Anum_pg_constraint_confkey - 1] =
true;
221 nulls[Anum_pg_constraint_conpfeqop - 1] =
true;
226 nulls[Anum_pg_constraint_conppeqop - 1] =
true;
231 nulls[Anum_pg_constraint_conffeqop - 1] =
true;
233 if (confdelsetcolsArray)
236 nulls[Anum_pg_constraint_confdelsetcols - 1] =
true;
241 nulls[Anum_pg_constraint_conexclop - 1] =
true;
246 nulls[Anum_pg_constraint_conbin - 1] =
true;
267 if (constraintNTotalKeys > 0)
269 for (
i = 0;
i < constraintNTotalKeys;
i++)
309 if (foreignNKeys > 0)
311 for (
i = 0;
i < foreignNKeys;
i++)
314 foreignRelId, foreignKey[
i]);
325 if (
OidIsValid(indexRelId) && constraintType == CONSTRAINT_FOREIGN)
339 if (foreignNKeys > 0)
349 oprobject.
classId = OperatorRelationId;
352 for (
i = 0;
i < foreignNKeys;
i++)
356 if (ppEqOp[
i] != pfEqOp[
i])
361 if (ffEqOp[
i] != pfEqOp[
i])
423 Anum_pg_constraint_conrelid,
428 Anum_pg_constraint_contypid,
433 Anum_pg_constraint_conname,
438 true, NULL, 3, skey);
467 Anum_pg_constraint_conname,
472 Anum_pg_constraint_connamespace,
516 char *conname = NULL;
537 if (strcmp((
char *)
lfirst(l), conname) == 0)
547 Anum_pg_constraint_conname,
552 Anum_pg_constraint_connamespace,
597 Anum_pg_constraint_conrelid,
601 true, NULL, 1, &
key);
612 if (con->contype != CONSTRAINT_NOTNULL)
663 Anum_pg_constraint_contypid,
667 true, NULL, 1, &
key);
676 if (con->contype != CONSTRAINT_NOTNULL)
678 if (!con->convalidated)
706 Anum_pg_constraint_conkey);
712 elog(
ERROR,
"conkey is not a 1-D smallint array");
737 bool is_local,
bool is_no_inherit,
bool is_notvalid)
746 bool changed =
false;
755 if (is_no_inherit != conform->connoinherit)
757 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
758 errmsg(
"cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"",
760 errhint(
"You might need to make the existing constraint inheritable using %s.",
761 "ALTER TABLE ... ALTER CONSTRAINT ... INHERIT"));
767 if (!is_notvalid && !conform->convalidated)
769 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
770 errmsg(
"incompatible NOT VALID constraint \"%s\" on relation \"%s\"",
772 errhint(
"You might need to validate it using %s.",
773 "ALTER TABLE ... VALIDATE CONSTRAINT"));
778 &conform->coninhcount))
780 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
781 errmsg(
"too many inheritance parents"));
784 else if (!conform->conislocal)
786 conform->conislocal =
true;
822 Anum_pg_constraint_conrelid,
833 if (conForm->contype != CONSTRAINT_NOTNULL)
835 if (conForm->connoinherit && !include_noinh)
847 cooked->
conoid = conForm->oid;
857 notnulls =
lappend(notnulls, cooked);
875 notnulls =
lappend(notnulls, constr);
900 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
921 if (con->contype == CONSTRAINT_CHECK)
931 elog(
ERROR,
"cache lookup failed for relation %u",
935 if (classForm->relchecks == 0)
936 elog(
ERROR,
"relation \"%s\" has relchecks = 0",
938 classForm->relchecks--;
960 elog(
ERROR,
"constraint %u is not of a known type", conId);
991 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
1003 errmsg(
"constraint \"%s\" for relation \"%s\" already exists",
1011 errmsg(
"constraint \"%s\" for domain %s already exists",
1044 Anum_pg_constraint_conrelid,
1048 Anum_pg_constraint_contypid,
1066 if (conform->connamespace == oldNspId && oldNspId != newNspId)
1071 conform->connamespace = newNspId;
1116 elog(
ERROR,
"cache lookup failed for constraint %u", childConstrId);
1122 Assert(constrForm->coninhcount == 0);
1124 elog(
ERROR,
"constraint %u already has a parent constraint",
1127 constrForm->conislocal =
false;
1129 &constrForm->coninhcount))
1131 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1132 errmsg(
"too many inheritance parents"));
1134 constrForm->conparentid = parentConstrId;
1148 constrForm->coninhcount--;
1149 constrForm->conislocal =
true;
1153 Assert(constrForm->coninhcount == 0);
1158 ConstraintRelationId,
1187 Anum_pg_constraint_conrelid,
1191 Anum_pg_constraint_contypid,
1195 Anum_pg_constraint_conname,
1211 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1212 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1234 bool missing_ok,
Oid *constraintOid)
1248 Anum_pg_constraint_conrelid,
1252 Anum_pg_constraint_contypid,
1256 Anum_pg_constraint_conname,
1272 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1287 elog(
ERROR,
"conkey is not a 1-D smallint array");
1291 for (
i = 0;
i < numcols;
i++)
1302 if (!
OidIsValid(*constraintOid) && !missing_ok)
1304 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1305 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1333 Anum_pg_constraint_conrelid,
1338 true, NULL, 1, &
key);
1346 if (constrForm->contype != CONSTRAINT_PRIMARY &&
1347 constrForm->contype != CONSTRAINT_UNIQUE &&
1348 constrForm->contype != CONSTRAINT_EXCLUSION)
1351 if (constrForm->conindid == indexId)
1353 constraintId = constrForm->oid;
1360 return constraintId;
1380 Anum_pg_constraint_conrelid,
1384 Anum_pg_constraint_contypid,
1388 Anum_pg_constraint_conname,
1404 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1405 errmsg(
"constraint \"%s\" for domain %s does not exist",
1443 Anum_pg_constraint_conrelid,
1461 if (con->contype != CONSTRAINT_PRIMARY)
1469 if (con->condeferrable && !deferrableOk)
1473 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1476 elog(
ERROR,
"null conkey for constraint %u",
1484 elog(
ERROR,
"conkey is not a 1-D smallint array");
1488 for (
i = 0;
i < numkeys;
i++)
1516 Oid *pf_eq_oprs,
Oid *pp_eq_oprs,
Oid *ff_eq_oprs,
1517 int *num_fk_del_set_cols,
AttrNumber *fk_del_set_cols)
1530 Anum_pg_constraint_conkey);
1535 elog(
ERROR,
"conkey is not a 1-D smallint array");
1538 elog(
ERROR,
"foreign key constraint cannot have %d columns", numkeys);
1544 Anum_pg_constraint_confkey);
1550 elog(
ERROR,
"confkey is not a 1-D smallint array");
1558 Anum_pg_constraint_conpfeqop);
1565 elog(
ERROR,
"conpfeqop is not a 1-D Oid array");
1574 Anum_pg_constraint_conppeqop);
1580 elog(
ERROR,
"conppeqop is not a 1-D Oid array");
1589 Anum_pg_constraint_conffeqop);
1595 elog(
ERROR,
"conffeqop is not a 1-D Oid array");
1601 if (fk_del_set_cols)
1604 Anum_pg_constraint_confdelsetcols, &isNull);
1607 *num_fk_del_set_cols = 0;
1611 int num_delete_cols;
1617 elog(
ERROR,
"confdelsetcols is not a 1-D smallint array");
1618 num_delete_cols =
ARR_DIMS(arr)[0];
1623 *num_fk_del_set_cols = num_delete_cols;
1645 Oid *containedbyoperoid,
1646 Oid *aggedcontainedbyoperoid,
1647 Oid *intersectoperoid)
1656 if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
1658 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1659 errmsg(
"invalid type for PERIOD part of foreign key"),
1660 errdetail(
"Only range and multirange are supported."));
1664 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
1686 aggedcontainedbyoperoid,
1692 *intersectoperoid = OID_RANGE_INTERSECT_RANGE_OP;
1694 case ANYMULTIRANGEOID:
1695 *intersectoperoid = OID_MULTIRANGE_INTERSECT_MULTIRANGE_OP;
1698 elog(
ERROR,
"unexpected opcintype: %u", opcintype);
1720 List *grouping_columns,
1721 List **constraintDeps)
1730 if (pkattnos == NULL)
1734 groupbyattnos = NULL;
1735 foreach(gl, grouping_columns)
1740 gvar->
varno == varno &&
1749 *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 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 errhint(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)
Assert(PointerIsAligned(start, uint64))
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)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, bool is_local, bool is_no_inherit, bool is_notvalid)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
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)