33 #include "utils/fmgroids.h"
51 Oid constraintNamespace,
58 const int16 *constraintKey,
60 int constraintNTotalKeys,
64 const int16 *foreignKey,
69 char foreignUpdateType,
70 char foreignDeleteType,
71 const int16 *fkDeleteSetCols,
72 int numFkDeleteSetCols,
73 char foreignMatchType,
86 bool nulls[Natts_pg_constraint];
109 if (constraintNKeys > 0)
114 for (
i = 0;
i < constraintNKeys;
i++)
121 if (foreignNKeys > 0)
126 for (
i = 0;
i < foreignNKeys;
i++)
129 for (
i = 0;
i < foreignNKeys;
i++)
132 for (
i = 0;
i < foreignNKeys;
i++)
135 for (
i = 0;
i < foreignNKeys;
i++)
139 if (numFkDeleteSetCols > 0)
141 for (
i = 0;
i < numFkDeleteSetCols;
i++)
146 confdelsetcolsArray = NULL;
151 conpfeqopArray = NULL;
152 conppeqopArray = NULL;
153 conffeqopArray = NULL;
154 confdelsetcolsArray = NULL;
162 for (
i = 0;
i < constraintNKeys;
i++)
167 conexclopArray = NULL;
170 for (
i = 0;
i < Natts_pg_constraint;
i++)
177 Anum_pg_constraint_oid);
201 nulls[Anum_pg_constraint_conkey - 1] =
true;
206 nulls[Anum_pg_constraint_confkey - 1] =
true;
211 nulls[Anum_pg_constraint_conpfeqop - 1] =
true;
216 nulls[Anum_pg_constraint_conppeqop - 1] =
true;
221 nulls[Anum_pg_constraint_conffeqop - 1] =
true;
223 if (confdelsetcolsArray)
226 nulls[Anum_pg_constraint_confdelsetcols - 1] =
true;
231 nulls[Anum_pg_constraint_conexclop - 1] =
true;
236 nulls[Anum_pg_constraint_conbin - 1] =
true;
257 if (constraintNTotalKeys > 0)
259 for (
i = 0;
i < constraintNTotalKeys;
i++)
299 if (foreignNKeys > 0)
301 for (
i = 0;
i < foreignNKeys;
i++)
304 foreignRelId, foreignKey[
i]);
315 if (
OidIsValid(indexRelId) && constraintType == CONSTRAINT_FOREIGN)
329 if (foreignNKeys > 0)
339 oprobject.
classId = OperatorRelationId;
342 for (
i = 0;
i < foreignNKeys;
i++)
346 if (ppEqOp[
i] != pfEqOp[
i])
351 if (ffEqOp[
i] != pfEqOp[
i])
413 Anum_pg_constraint_conrelid,
418 Anum_pg_constraint_contypid,
423 Anum_pg_constraint_conname,
428 true, NULL, 3, skey);
457 Anum_pg_constraint_conname,
462 Anum_pg_constraint_connamespace,
506 char *conname = NULL;
527 if (strcmp((
char *)
lfirst(l), conname) == 0)
537 Anum_pg_constraint_conname,
542 Anum_pg_constraint_connamespace,
582 Anum_pg_constraint_contypid,
586 true, NULL, 1, &
key);
595 if (con->contype != CONSTRAINT_NOTNULL)
597 if (!con->convalidated)
625 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
646 if (con->contype == CONSTRAINT_CHECK)
656 elog(
ERROR,
"cache lookup failed for relation %u",
660 if (classForm->relchecks == 0)
661 elog(
ERROR,
"relation \"%s\" has relchecks = 0",
663 classForm->relchecks--;
685 elog(
ERROR,
"constraint %u is not of a known type", conId);
716 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
728 errmsg(
"constraint \"%s\" for relation \"%s\" already exists",
736 errmsg(
"constraint \"%s\" for domain %s already exists",
769 Anum_pg_constraint_conrelid,
773 Anum_pg_constraint_contypid,
791 if (conform->connamespace == oldNspId && oldNspId != newNspId)
796 conform->connamespace = newNspId;
841 elog(
ERROR,
"cache lookup failed for constraint %u", childConstrId);
847 Assert(constrForm->coninhcount == 0);
849 elog(
ERROR,
"constraint %u already has a parent constraint",
852 constrForm->conislocal =
false;
854 &constrForm->coninhcount))
856 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
857 errmsg(
"too many inheritance parents"));
859 constrForm->conparentid = parentConstrId;
873 constrForm->coninhcount--;
874 constrForm->conislocal =
true;
878 Assert(constrForm->coninhcount == 0);
883 ConstraintRelationId,
912 Anum_pg_constraint_conrelid,
916 Anum_pg_constraint_contypid,
920 Anum_pg_constraint_conname,
936 (
errcode(ERRCODE_UNDEFINED_OBJECT),
937 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
959 bool missing_ok,
Oid *constraintOid)
973 Anum_pg_constraint_conrelid,
977 Anum_pg_constraint_contypid,
981 Anum_pg_constraint_conname,
1012 elog(
ERROR,
"conkey is not a 1-D smallint array");
1016 for (
i = 0;
i < numcols;
i++)
1027 if (!
OidIsValid(*constraintOid) && !missing_ok)
1029 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1030 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1058 Anum_pg_constraint_conrelid,
1063 true, NULL, 1, &
key);
1071 if (constrForm->contype != CONSTRAINT_PRIMARY &&
1072 constrForm->contype != CONSTRAINT_UNIQUE &&
1073 constrForm->contype != CONSTRAINT_EXCLUSION)
1076 if (constrForm->conindid == indexId)
1078 constraintId = constrForm->oid;
1085 return constraintId;
1105 Anum_pg_constraint_conrelid,
1109 Anum_pg_constraint_contypid,
1113 Anum_pg_constraint_conname,
1129 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1130 errmsg(
"constraint \"%s\" for domain %s does not exist",
1168 Anum_pg_constraint_conrelid,
1186 if (con->contype != CONSTRAINT_PRIMARY)
1194 if (con->condeferrable && !deferrableOk)
1198 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1201 elog(
ERROR,
"null conkey for constraint %u",
1209 elog(
ERROR,
"conkey is not a 1-D smallint array");
1213 for (
i = 0;
i < numkeys;
i++)
1241 Oid *pf_eq_oprs,
Oid *pp_eq_oprs,
Oid *ff_eq_oprs,
1242 int *num_fk_del_set_cols,
AttrNumber *fk_del_set_cols)
1255 Anum_pg_constraint_conkey);
1260 elog(
ERROR,
"conkey is not a 1-D smallint array");
1263 elog(
ERROR,
"foreign key constraint cannot have %d columns", numkeys);
1269 Anum_pg_constraint_confkey);
1275 elog(
ERROR,
"confkey is not a 1-D smallint array");
1283 Anum_pg_constraint_conpfeqop);
1290 elog(
ERROR,
"conpfeqop is not a 1-D Oid array");
1299 Anum_pg_constraint_conppeqop);
1305 elog(
ERROR,
"conppeqop is not a 1-D Oid array");
1314 Anum_pg_constraint_conffeqop);
1320 elog(
ERROR,
"conffeqop is not a 1-D Oid array");
1326 if (fk_del_set_cols)
1329 Anum_pg_constraint_confdelsetcols, &isNull);
1332 *num_fk_del_set_cols = 0;
1336 int num_delete_cols;
1342 elog(
ERROR,
"confdelsetcols is not a 1-D smallint array");
1343 num_delete_cols =
ARR_DIMS(arr)[0];
1348 *num_fk_del_set_cols = num_delete_cols;
1368 Oid *containedbyoperoid,
1369 Oid *aggedcontainedbyoperoid)
1378 if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
1380 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1381 errmsg(
"invalid type for PERIOD part of foreign key"),
1382 errdetail(
"Only range and multirange are supported."));
1386 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
1408 aggedcontainedbyoperoid,
1430 List *grouping_columns,
1431 List **constraintDeps)
1440 if (pkattnos == NULL)
1444 groupbyattnos = NULL;
1445 foreach(gl, grouping_columns)
1450 gvar->
varno == varno &&
1459 *constraintDeps =
lappend_oid(*constraintDeps, constraintOid);
#define DatumGetArrayTypeP(X)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
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)
ObjectAddresses * new_object_addresses(void)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
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)
char * makeObjectName(const char *name1, const char *name2, const char *label)
void GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype, 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_oid(List *list, Oid datum)
#define AccessExclusiveLock
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
char * get_rel_name(Oid relid)
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
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
void RemoveConstraintById(Oid conId)
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)
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)
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
void FindFKPeriodOpers(Oid opclass, Oid *containedbyoperoid, Oid *aggedcontainedbyoperoid)
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
Bitmapset * get_relation_constraint_attnos(Oid relid, const char *conname, bool missing_ok, Oid *constraintOid)
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, 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)
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 RTContainedByStrategyNumber
#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)