34 #include "utils/fmgroids.h"
52 Oid constraintNamespace,
59 const int16 *constraintKey,
61 int constraintNTotalKeys,
65 const int16 *foreignKey,
70 char foreignUpdateType,
71 char foreignDeleteType,
72 const int16 *fkDeleteSetCols,
73 int numFkDeleteSetCols,
74 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);
200 nulls[Anum_pg_constraint_conkey - 1] =
true;
205 nulls[Anum_pg_constraint_confkey - 1] =
true;
210 nulls[Anum_pg_constraint_conpfeqop - 1] =
true;
215 nulls[Anum_pg_constraint_conppeqop - 1] =
true;
220 nulls[Anum_pg_constraint_conffeqop - 1] =
true;
222 if (confdelsetcolsArray)
225 nulls[Anum_pg_constraint_confdelsetcols - 1] =
true;
230 nulls[Anum_pg_constraint_conexclop - 1] =
true;
235 nulls[Anum_pg_constraint_conbin - 1] =
true;
256 if (constraintNTotalKeys > 0)
258 for (
i = 0;
i < constraintNTotalKeys;
i++)
298 if (foreignNKeys > 0)
300 for (
i = 0;
i < foreignNKeys;
i++)
303 foreignRelId, foreignKey[
i]);
314 if (
OidIsValid(indexRelId) && constraintType == CONSTRAINT_FOREIGN)
328 if (foreignNKeys > 0)
338 oprobject.
classId = OperatorRelationId;
341 for (
i = 0;
i < foreignNKeys;
i++)
345 if (ppEqOp[
i] != pfEqOp[
i])
350 if (ffEqOp[
i] != pfEqOp[
i])
412 Anum_pg_constraint_conrelid,
417 Anum_pg_constraint_contypid,
422 Anum_pg_constraint_conname,
427 true, NULL, 3, skey);
456 Anum_pg_constraint_conname,
461 Anum_pg_constraint_connamespace,
505 char *conname = NULL;
526 if (strcmp((
char *)
lfirst(l), conname) == 0)
536 Anum_pg_constraint_conname,
541 Anum_pg_constraint_connamespace,
585 Anum_pg_constraint_conrelid,
589 true, NULL, 1, &
key);
600 if (con->contype != CONSTRAINT_NOTNULL)
602 if (!con->convalidated)
647 Anum_pg_constraint_conkey);
653 elog(
ERROR,
"conkey is not a 1-D smallint array");
692 if (is_no_inherit != conform->connoinherit)
694 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
695 errmsg(
"cannot change NO INHERIT status of inherited NOT NULL constraint \"%s\" on relation \"%s\"",
699 conform->coninhcount += count;
702 if (conform->coninhcount < 0)
703 elog(
ERROR,
"invalid inhcount %d for constraint \"%s\" on relation \"%s\"",
704 conform->coninhcount,
NameStr(conform->conname),
712 if (conform->coninhcount == 0)
713 conform->conislocal =
true;
757 errcode(ERRCODE_DATATYPE_MISMATCH),
758 errmsg(
"column \"%s\" in child table must be marked NOT NULL",
763 conform->coninhcount += count;
764 if (conform->coninhcount < 0)
765 elog(
ERROR,
"invalid inhcount %d for constraint \"%s\" on relation \"%s\"",
766 conform->coninhcount,
NameStr(conform->conname),
774 if (conform->coninhcount == 0)
775 conform->conislocal =
true;
805 Anum_pg_constraint_conrelid,
816 if (conForm->contype != CONSTRAINT_NOTNULL)
818 if (conForm->connoinherit)
838 notnulls =
lappend(notnulls, cooked);
854 notnulls =
lappend(notnulls, constr);
879 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
900 if (con->contype == CONSTRAINT_CHECK)
910 elog(
ERROR,
"cache lookup failed for relation %u",
914 if (classForm->relchecks == 0)
915 elog(
ERROR,
"relation \"%s\" has relchecks = 0",
917 classForm->relchecks--;
939 elog(
ERROR,
"constraint %u is not of a known type", conId);
970 elog(
ERROR,
"cache lookup failed for constraint %u", conId);
982 errmsg(
"constraint \"%s\" for relation \"%s\" already exists",
990 errmsg(
"constraint \"%s\" for domain %s already exists",
1023 Anum_pg_constraint_conrelid,
1027 Anum_pg_constraint_contypid,
1045 if (conform->connamespace == oldNspId && oldNspId != newNspId)
1050 conform->connamespace = newNspId;
1095 elog(
ERROR,
"cache lookup failed for constraint %u", childConstrId);
1101 Assert(constrForm->coninhcount == 0);
1103 elog(
ERROR,
"constraint %u already has a parent constraint",
1106 constrForm->conislocal =
false;
1107 constrForm->coninhcount++;
1108 if (constrForm->coninhcount < 0)
1110 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1111 errmsg(
"too many inheritance parents"));
1112 constrForm->conparentid = parentConstrId;
1126 constrForm->coninhcount--;
1127 constrForm->conislocal =
true;
1131 Assert(constrForm->coninhcount == 0);
1136 ConstraintRelationId,
1165 Anum_pg_constraint_conrelid,
1169 Anum_pg_constraint_contypid,
1173 Anum_pg_constraint_conname,
1189 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1190 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1212 bool missing_ok,
Oid *constraintOid)
1226 Anum_pg_constraint_conrelid,
1230 Anum_pg_constraint_contypid,
1234 Anum_pg_constraint_conname,
1250 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1265 elog(
ERROR,
"conkey is not a 1-D smallint array");
1269 for (
i = 0;
i < numcols;
i++)
1280 if (!
OidIsValid(*constraintOid) && !missing_ok)
1282 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1283 errmsg(
"constraint \"%s\" for table \"%s\" does not exist",
1311 Anum_pg_constraint_conrelid,
1316 true, NULL, 1, &
key);
1324 if (constrForm->contype != CONSTRAINT_PRIMARY &&
1325 constrForm->contype != CONSTRAINT_UNIQUE &&
1326 constrForm->contype != CONSTRAINT_EXCLUSION)
1329 if (constrForm->conindid == indexId)
1331 constraintId = constrForm->oid;
1338 return constraintId;
1358 Anum_pg_constraint_conrelid,
1362 Anum_pg_constraint_contypid,
1366 Anum_pg_constraint_conname,
1382 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1383 errmsg(
"constraint \"%s\" for domain %s does not exist",
1421 Anum_pg_constraint_conrelid,
1439 if (con->contype != CONSTRAINT_PRIMARY)
1447 if (con->condeferrable && !deferrableOk)
1451 adatum =
heap_getattr(tuple, Anum_pg_constraint_conkey,
1454 elog(
ERROR,
"null conkey for constraint %u",
1462 elog(
ERROR,
"conkey is not a 1-D smallint array");
1466 for (
i = 0;
i < numkeys;
i++)
1494 Oid *pf_eq_oprs,
Oid *pp_eq_oprs,
Oid *ff_eq_oprs,
1495 int *num_fk_del_set_cols,
AttrNumber *fk_del_set_cols)
1508 Anum_pg_constraint_conkey);
1513 elog(
ERROR,
"conkey is not a 1-D smallint array");
1516 elog(
ERROR,
"foreign key constraint cannot have %d columns", numkeys);
1522 Anum_pg_constraint_confkey);
1528 elog(
ERROR,
"confkey is not a 1-D smallint array");
1536 Anum_pg_constraint_conpfeqop);
1543 elog(
ERROR,
"conpfeqop is not a 1-D Oid array");
1552 Anum_pg_constraint_conppeqop);
1558 elog(
ERROR,
"conppeqop is not a 1-D Oid array");
1567 Anum_pg_constraint_conffeqop);
1573 elog(
ERROR,
"conffeqop is not a 1-D Oid array");
1579 if (fk_del_set_cols)
1582 Anum_pg_constraint_confdelsetcols, &isNull);
1585 *num_fk_del_set_cols = 0;
1589 int num_delete_cols;
1595 elog(
ERROR,
"confdelsetcols is not a 1-D smallint array");
1596 num_delete_cols =
ARR_DIMS(arr)[0];
1601 *num_fk_del_set_cols = num_delete_cols;
1626 List *grouping_columns,
1627 List **constraintDeps)
1636 if (pkattnos == NULL)
1640 groupbyattnos = NULL;
1641 foreach(gl, grouping_columns)
1646 gvar->
varno == varno &&
1655 *constraintDeps =
lappend_oid(*constraintDeps, constraintOid);
#define DatumGetArrayTypeP(X)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
int bms_next_member(const Bitmapset *a, int prevbit)
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)
elog(ERROR, "%s: %s", p2, msg)
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 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_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
HeapTuple heap_copytuple(HeapTuple tuple)
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 CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_oid(List *list, Oid datum)
#define AccessExclusiveLock
AttrNumber get_attnum(Oid relid, const char *attname)
char * get_rel_name(Oid relid)
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
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
HeapTuple findNotNullConstraint(Oid relid, const char *colname)
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)
List * RelationGetNotNullConstraints(Oid relid, bool cooked)
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 AdjustNotNullInheritance(Oid relid, Bitmapset *columns, int count)
HeapTuple findNotNullConstraintAttnum(Oid relid, AttrNumber attnum)
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
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, int conInhCount, bool conNoInherit, bool is_internal)
bool ConstraintNameExists(const char *conname, Oid namespaceid)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
AttrNumber extractNotNullColumn(HeapTuple constrTup)
bool AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count, bool is_no_inherit)
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)
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)