mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Refactor duplicate code into DeconstructFkConstraintRow
My commit 3de241dba8
introduced some code (in tablecmds.c) to obtain
data from a pg_constraint row for a foreign key, that already existed in
ri_triggers.c. Split it out into its own routine in pg_constraint.c,
where it naturally belongs.
No functional code changes, only code movement.
Backpatch to pg11, because a future bugfix is simpler after this.
This commit is contained in:
61
src/backend/utils/cache/relcache.c
vendored
61
src/backend/utils/cache/relcache.c
vendored
@ -4125,10 +4125,6 @@ RelationGetFKeyList(Relation relation)
|
||||
{
|
||||
Form_pg_constraint constraint = (Form_pg_constraint) GETSTRUCT(htup);
|
||||
ForeignKeyCacheInfo *info;
|
||||
Datum adatum;
|
||||
bool isnull;
|
||||
ArrayType *arr;
|
||||
int nelem;
|
||||
|
||||
/* consider only foreign keys */
|
||||
if (constraint->contype != CONSTRAINT_FOREIGN)
|
||||
@ -4139,58 +4135,11 @@ RelationGetFKeyList(Relation relation)
|
||||
info->conrelid = constraint->conrelid;
|
||||
info->confrelid = constraint->confrelid;
|
||||
|
||||
/* Extract data from conkey field */
|
||||
adatum = fastgetattr(htup, Anum_pg_constraint_conkey,
|
||||
conrel->rd_att, &isnull);
|
||||
if (isnull)
|
||||
elog(ERROR, "null conkey for rel %s",
|
||||
RelationGetRelationName(relation));
|
||||
|
||||
arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
|
||||
nelem = ARR_DIMS(arr)[0];
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
nelem < 1 ||
|
||||
nelem > INDEX_MAX_KEYS ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != INT2OID)
|
||||
elog(ERROR, "conkey is not a 1-D smallint array");
|
||||
|
||||
info->nkeys = nelem;
|
||||
memcpy(info->conkey, ARR_DATA_PTR(arr), nelem * sizeof(AttrNumber));
|
||||
|
||||
/* Likewise for confkey */
|
||||
adatum = fastgetattr(htup, Anum_pg_constraint_confkey,
|
||||
conrel->rd_att, &isnull);
|
||||
if (isnull)
|
||||
elog(ERROR, "null confkey for rel %s",
|
||||
RelationGetRelationName(relation));
|
||||
|
||||
arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
|
||||
nelem = ARR_DIMS(arr)[0];
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
nelem != info->nkeys ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != INT2OID)
|
||||
elog(ERROR, "confkey is not a 1-D smallint array");
|
||||
|
||||
memcpy(info->confkey, ARR_DATA_PTR(arr), nelem * sizeof(AttrNumber));
|
||||
|
||||
/* Likewise for conpfeqop */
|
||||
adatum = fastgetattr(htup, Anum_pg_constraint_conpfeqop,
|
||||
conrel->rd_att, &isnull);
|
||||
if (isnull)
|
||||
elog(ERROR, "null conpfeqop for rel %s",
|
||||
RelationGetRelationName(relation));
|
||||
|
||||
arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
|
||||
nelem = ARR_DIMS(arr)[0];
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
nelem != info->nkeys ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != OIDOID)
|
||||
elog(ERROR, "conpfeqop is not a 1-D OID array");
|
||||
|
||||
memcpy(info->conpfeqop, ARR_DATA_PTR(arr), nelem * sizeof(Oid));
|
||||
DeconstructFkConstraintRow(htup, &info->nkeys,
|
||||
info->conkey,
|
||||
info->confkey,
|
||||
info->conpfeqop,
|
||||
NULL, NULL);
|
||||
|
||||
/* Add FK's node to the result list */
|
||||
result = lappend(result, info);
|
||||
|
Reference in New Issue
Block a user