diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2019-01-18 14:40:13 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2019-01-18 14:59:44 -0300 |
commit | 0080396dad4fe59a91f6d7e9c1f806affcfc68c3 (patch) | |
tree | ba3f39628888a2abc16f848f10e4f41d35bcf503 /src/backend/utils | |
parent | 9194c4270b28bb19b43a0156e5a296d1a0a3dd48 (diff) | |
download | postgresql-0080396dad4fe59a91f6d7e9c1f806affcfc68c3.tar.gz postgresql-0080396dad4fe59a91f6d7e9c1f806affcfc68c3.zip |
Refactor duplicate code into DeconstructFkConstraintRow
My commit 3de241dba86f 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.
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/adt/ri_triggers.c | 89 | ||||
-rw-r--r-- | src/backend/utils/cache/relcache.c | 61 |
2 files changed, 12 insertions, 138 deletions
diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index e606eb342fd..0c1ee8d1f69 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -2189,10 +2189,6 @@ ri_LoadConstraintInfo(Oid constraintOid) bool found; HeapTuple tup; Form_pg_constraint conForm; - Datum adatum; - bool isNull; - ArrayType *arr; - int numkeys; /* * On the first call initialize the hashtable @@ -2234,84 +2230,13 @@ ri_LoadConstraintInfo(Oid constraintOid) riinfo->confdeltype = conForm->confdeltype; riinfo->confmatchtype = conForm->confmatchtype; - /* - * We expect the arrays to be 1-D arrays of the right types; verify that. - * We don't need to use deconstruct_array() since the array data is just - * going to look like a C array of values. - */ - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_conkey, &isNull); - if (isNull) - elog(ERROR, "null conkey for constraint %u", constraintOid); - arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ - if (ARR_NDIM(arr) != 1 || - ARR_HASNULL(arr) || - ARR_ELEMTYPE(arr) != INT2OID) - elog(ERROR, "conkey is not a 1-D smallint array"); - numkeys = ARR_DIMS(arr)[0]; - if (numkeys <= 0 || numkeys > RI_MAX_NUMKEYS) - elog(ERROR, "foreign key constraint cannot have %d columns", numkeys); - riinfo->nkeys = numkeys; - memcpy(riinfo->fk_attnums, ARR_DATA_PTR(arr), numkeys * sizeof(int16)); - if ((Pointer) arr != DatumGetPointer(adatum)) - pfree(arr); /* free de-toasted copy, if any */ - - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_confkey, &isNull); - if (isNull) - elog(ERROR, "null confkey for constraint %u", constraintOid); - arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ - if (ARR_NDIM(arr) != 1 || - ARR_DIMS(arr)[0] != numkeys || - ARR_HASNULL(arr) || - ARR_ELEMTYPE(arr) != INT2OID) - elog(ERROR, "confkey is not a 1-D smallint array"); - memcpy(riinfo->pk_attnums, ARR_DATA_PTR(arr), numkeys * sizeof(int16)); - if ((Pointer) arr != DatumGetPointer(adatum)) - pfree(arr); /* free de-toasted copy, if any */ - - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_conpfeqop, &isNull); - if (isNull) - elog(ERROR, "null conpfeqop for constraint %u", constraintOid); - arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ - /* see TryReuseForeignKey if you change the test below */ - if (ARR_NDIM(arr) != 1 || - ARR_DIMS(arr)[0] != numkeys || - ARR_HASNULL(arr) || - ARR_ELEMTYPE(arr) != OIDOID) - elog(ERROR, "conpfeqop is not a 1-D Oid array"); - memcpy(riinfo->pf_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid)); - if ((Pointer) arr != DatumGetPointer(adatum)) - pfree(arr); /* free de-toasted copy, if any */ - - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_conppeqop, &isNull); - if (isNull) - elog(ERROR, "null conppeqop for constraint %u", constraintOid); - arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ - if (ARR_NDIM(arr) != 1 || - ARR_DIMS(arr)[0] != numkeys || - ARR_HASNULL(arr) || - ARR_ELEMTYPE(arr) != OIDOID) - elog(ERROR, "conppeqop is not a 1-D Oid array"); - memcpy(riinfo->pp_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid)); - if ((Pointer) arr != DatumGetPointer(adatum)) - pfree(arr); /* free de-toasted copy, if any */ - - adatum = SysCacheGetAttr(CONSTROID, tup, - Anum_pg_constraint_conffeqop, &isNull); - if (isNull) - elog(ERROR, "null conffeqop for constraint %u", constraintOid); - arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */ - if (ARR_NDIM(arr) != 1 || - ARR_DIMS(arr)[0] != numkeys || - ARR_HASNULL(arr) || - ARR_ELEMTYPE(arr) != OIDOID) - elog(ERROR, "conffeqop is not a 1-D Oid array"); - memcpy(riinfo->ff_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid)); - if ((Pointer) arr != DatumGetPointer(adatum)) - pfree(arr); /* free de-toasted copy, if any */ + DeconstructFkConstraintRow(tup, + &riinfo->nkeys, + riinfo->fk_attnums, + riinfo->pk_attnums, + riinfo->pf_eq_oprs, + riinfo->pp_eq_oprs, + riinfo->ff_eq_oprs); ReleaseSysCache(tup); diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index e3b1473e6c9..7ea371a3c71 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -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); |