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,
518 char *conname = NULL;
529 if (
label[0] !=
'\0')
542 if (strcmp((
char *)
lfirst(l), conname) == 0)
552 Anum_pg_constraint_conname,
557 Anum_pg_constraint_connamespace,
602 Anum_pg_constraint_conrelid,
606 true, NULL, 1, &
key);
617 if (con->contype != CONSTRAINT_NOTNULL)
668 Anum_pg_constraint_contypid,
672 true, NULL, 1, &
key);
681 if (con->contype != CONSTRAINT_NOTNULL)
683 if (!con->convalidated)
711 Anum_pg_constraint_conkey);
717 elog(
ERROR,
"conkey is not a 1-D smallint array");
742 bool is_local,
bool is_no_inherit,
bool is_notvalid)
751 bool changed =
false;
760 if (is_no_inherit != conform->connoinherit)
762 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
763 errmsg(
"cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"",
765 errhint(
"You might need to make the existing constraint inheritable using %s.",
766 "ALTER TABLE ... ALTER CONSTRAINT ... INHERIT"));
772 if (!is_notvalid && !conform->convalidated)
774 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
775 errmsg(
"incompatible NOT VALID constraint \"%s\" on relation \"%s\"",
777 errhint(
"You might need to validate it using %s.",
778 "ALTER TABLE ... VALIDATE CONSTRAINT"));
783 &conform->coninhcount))
785 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
786 errmsg(
"too many inheritance parents"));
789 else if (!conform->conislocal)
791 conform->conislocal =
true;
827 Anum_pg_constraint_conrelid,
838 if (conForm->contype != CONSTRAINT_NOTNULL)
840 if (conForm->connoinherit && !include_noinh)
852 cooked->
conoid = conForm->oid;
862 notnulls =
lappend(notnulls, cooked);
880 notnulls =
lappend(notnulls, constr);
905 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
926 if (con->contype == CONSTRAINT_CHECK)
936 elog(
ERROR,
"cache lookup failed for relation %u",
940 if (classForm->relchecks == 0)
941 elog(
ERROR,
"relation \"%s\" has relchecks = 0",
943 classForm->relchecks--;
965 elog(
ERROR,
"constraint %u is not of a known type", conId);
996 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
1008 errmsg(
"constraint \"%s\" for relation \"%s\" already exists",
1016 errmsg(
"constraint \"%s\" for domain %s already exists",
1049 Anum_pg_constraint_conrelid,
1053 Anum_pg_constraint_contypid,
1071 if (conform->connamespace == oldNspId && oldNspId != newNspId)
1076 conform->connamespace = newNspId;
1121 elog(
ERROR,
"cache lookup failed for constraint %u", childConstrId);
1127 Assert(constrForm->coninhcount == 0);
1129 elog(
ERROR,
"constraint %u already has a parent constraint",
1132 constrForm->conislocal =
false;
1134 &constrForm->coninhcount))
1136 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1137 errmsg(
"too many inheritance parents"));
1139 constrForm->conparentid = parentConstrId;
1153 constrForm->coninhcount--;
1154 constrForm->conislocal =
true;
1158 Assert(constrForm->coninhcount == 0);
1163 ConstraintRelationId,
1192 Anum_pg_constraint_conrelid,
1196 Anum_pg_constraint_contypid,
1200 Anum_pg_constraint_conname,
1216 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1217 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1239 bool missing_ok,
Oid *constraintOid)
1253 Anum_pg_constraint_conrelid,
1257 Anum_pg_constraint_contypid,
1261 Anum_pg_constraint_conname,
1277 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1292 elog(
ERROR,
"conkey is not a 1-D smallint array");
1296 for (
i = 0;
i < numcols;
i++)
1307 if (!
OidIsValid(*constraintOid) && !missing_ok)
1309 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1310 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1338 Anum_pg_constraint_conrelid,
1343 true, NULL, 1, &
key);
1351 if (constrForm->contype != CONSTRAINT_PRIMARY &&
1352 constrForm->contype != CONSTRAINT_UNIQUE &&
1353 constrForm->contype != CONSTRAINT_EXCLUSION)
1356 if (constrForm->conindid == indexId)
1358 constraintId = constrForm->oid;
1365 return constraintId;
1385 Anum_pg_constraint_conrelid,
1389 Anum_pg_constraint_contypid,
1393 Anum_pg_constraint_conname,
1409 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1410 errmsg(
"constraint \"%s\" for domain %s does not exist",
1448 Anum_pg_constraint_conrelid,
1466 if (con->contype != CONSTRAINT_PRIMARY)
1474 if (con->condeferrable && !deferrableOk)
1478 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1481 elog(
ERROR,
"null conkey for constraint %u",
1489 elog(
ERROR,
"conkey is not a 1-D smallint array");
1493 for (
i = 0;
i < numkeys;
i++)
1521 Oid *pf_eq_oprs,
Oid *pp_eq_oprs,
Oid *ff_eq_oprs,
1522 int *num_fk_del_set_cols,
AttrNumber *fk_del_set_cols)
1535 Anum_pg_constraint_conkey);
1540 elog(
ERROR,
"conkey is not a 1-D smallint array");
1543 elog(
ERROR,
"foreign key constraint cannot have %d columns", numkeys);
1549 Anum_pg_constraint_confkey);
1555 elog(
ERROR,
"confkey is not a 1-D smallint array");
1563 Anum_pg_constraint_conpfeqop);
1570 elog(
ERROR,
"conpfeqop is not a 1-D Oid array");
1579 Anum_pg_constraint_conppeqop);
1585 elog(
ERROR,
"conppeqop is not a 1-D Oid array");
1594 Anum_pg_constraint_conffeqop);
1600 elog(
ERROR,
"conffeqop is not a 1-D Oid array");
1606 if (fk_del_set_cols)
1609 Anum_pg_constraint_confdelsetcols, &isNull);
1612 *num_fk_del_set_cols = 0;
1616 int num_delete_cols;
1622 elog(
ERROR,
"confdelsetcols is not a 1-D smallint array");
1623 num_delete_cols =
ARR_DIMS(arr)[0];
1628 *num_fk_del_set_cols = num_delete_cols;
1650 Oid *containedbyoperoid,
1651 Oid *aggedcontainedbyoperoid,
1652 Oid *intersectoperoid)
1661 if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
1663 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1664 errmsg(
"invalid type for PERIOD part of foreign key"),
1665 errdetail(
"Only range and multirange are supported."));
1669 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
1691 aggedcontainedbyoperoid,
1697 *intersectoperoid = OID_RANGE_INTERSECT_RANGE_OP;
1699 case ANYMULTIRANGEOID:
1700 *intersectoperoid = OID_MULTIRANGE_INTERSECT_MULTIRANGE_OP;
1703 elog(
ERROR,
"unexpected opcintype: %u", opcintype);
1725 List *grouping_columns,
1726 List **constraintDeps)
1735 if (pkattnos == NULL)
1739 groupbyattnos = NULL;
1740 foreach(gl, grouping_columns)
1745 gvar->
varno == varno &&
1754 *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)