From e5bb5355f0f877576a5ea12d3644c0ec5ca0809e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 24 Jan 2025 22:59:23 +0100 Subject: [PATCH v19.2 3/6] Add some uses of get_opfamily_name() This refactors and simplifies various existing code to make use of the new function. --- contrib/bloom/blvalidate.c | 12 ++-------- src/backend/access/brin/brin_validate.c | 12 ++-------- src/backend/access/gin/ginvalidate.c | 12 ++-------- src/backend/access/gist/gistvalidate.c | 11 +-------- src/backend/access/hash/hashvalidate.c | 11 +-------- src/backend/access/nbtree/nbtvalidate.c | 11 +-------- src/backend/access/spgist/spgvalidate.c | 11 +-------- src/backend/commands/indexcmds.c | 30 ++----------------------- 8 files changed, 12 insertions(+), 98 deletions(-) diff --git a/contrib/bloom/blvalidate.c b/contrib/bloom/blvalidate.c index acaf9d2be48..7fc9db0e56f 100644 --- a/contrib/bloom/blvalidate.c +++ b/contrib/bloom/blvalidate.c @@ -18,8 +18,8 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" +#include "utils/lsyscache.h" #include "utils/regproc.h" #include "utils/syscache.h" @@ -36,8 +36,6 @@ blvalidate(Oid opclassoid) Oid opcintype; Oid opckeytype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -60,12 +58,7 @@ blvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -216,7 +209,6 @@ blvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/brin/brin_validate.c b/src/backend/access/brin/brin_validate.c index a49a9009626..915b8628b46 100644 --- a/src/backend/access/brin/brin_validate.c +++ b/src/backend/access/brin/brin_validate.c @@ -19,9 +19,9 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" #include "utils/builtins.h" +#include "utils/lsyscache.h" #include "utils/regproc.h" #include "utils/syscache.h" @@ -42,8 +42,6 @@ brinvalidate(Oid opclassoid) Oid opfamilyoid; Oid opcintype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -65,12 +63,7 @@ brinvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -274,7 +267,6 @@ brinvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/gin/ginvalidate.c b/src/backend/access/gin/ginvalidate.c index 6bd94e44854..5b0bfe8cc1d 100644 --- a/src/backend/access/gin/ginvalidate.c +++ b/src/backend/access/gin/ginvalidate.c @@ -19,8 +19,8 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" +#include "utils/lsyscache.h" #include "utils/regproc.h" #include "utils/syscache.h" @@ -37,8 +37,6 @@ ginvalidate(Oid opclassoid) Oid opcintype; Oid opckeytype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -61,12 +59,7 @@ ginvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -264,7 +257,6 @@ ginvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/gist/gistvalidate.c b/src/backend/access/gist/gistvalidate.c index bb86b559486..ffefa12d97a 100644 --- a/src/backend/access/gist/gistvalidate.c +++ b/src/backend/access/gist/gistvalidate.c @@ -19,7 +19,6 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" #include "utils/lsyscache.h" #include "utils/regproc.h" @@ -39,8 +38,6 @@ gistvalidate(Oid opclassoid) Oid opcintype; Oid opckeytype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -63,12 +60,7 @@ gistvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -282,7 +274,6 @@ gistvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/hash/hashvalidate.c b/src/backend/access/hash/hashvalidate.c index b54ecde9224..06ac832ba10 100644 --- a/src/backend/access/hash/hashvalidate.c +++ b/src/backend/access/hash/hashvalidate.c @@ -21,7 +21,6 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" #include "utils/builtins.h" #include "utils/lsyscache.h" @@ -46,8 +45,6 @@ hashvalidate(Oid opclassoid) Oid opfamilyoid; Oid opcintype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -68,12 +65,7 @@ hashvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -258,7 +250,6 @@ hashvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/nbtree/nbtvalidate.c b/src/backend/access/nbtree/nbtvalidate.c index b87c959a2fd..dd6f5a15c65 100644 --- a/src/backend/access/nbtree/nbtvalidate.c +++ b/src/backend/access/nbtree/nbtvalidate.c @@ -21,7 +21,6 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" #include "utils/builtins.h" #include "utils/lsyscache.h" @@ -46,8 +45,6 @@ btvalidate(Oid opclassoid) Oid opfamilyoid; Oid opcintype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -69,12 +66,7 @@ btvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -280,7 +272,6 @@ btvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/access/spgist/spgvalidate.c b/src/backend/access/spgist/spgvalidate.c index 7d05e43720b..e9964fab4f4 100644 --- a/src/backend/access/spgist/spgvalidate.c +++ b/src/backend/access/spgist/spgvalidate.c @@ -19,7 +19,6 @@ #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" #include "utils/builtins.h" #include "utils/lsyscache.h" @@ -45,8 +44,6 @@ spgvalidate(Oid opclassoid) Oid opcintype; Oid opckeytype; char *opclassname; - HeapTuple familytup; - Form_pg_opfamily familyform; char *opfamilyname; CatCList *proclist, *oprlist; @@ -72,12 +69,7 @@ spgvalidate(Oid opclassoid) opclassname = NameStr(classform->opcname); /* Fetch opfamily information */ - familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid)); - if (!HeapTupleIsValid(familytup)) - elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid); - familyform = (Form_pg_opfamily) GETSTRUCT(familytup); - - opfamilyname = NameStr(familyform->opfname); + opfamilyname = get_opfamily_name(opfamilyoid, false); /* Fetch all operators and support functions of the opfamily */ oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid)); @@ -319,7 +311,6 @@ spgvalidate(Oid opclassoid) ReleaseCatCacheList(proclist); ReleaseCatCacheList(oprlist); - ReleaseSysCache(familytup); ReleaseSysCache(classtup); return result; diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 0aa15a42efe..f788b8f29b8 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -35,7 +35,6 @@ #include "catalog/pg_inherits.h" #include "catalog/pg_namespace.h" #include "catalog/pg_opclass.h" -#include "catalog/pg_opfamily.h" #include "catalog/pg_tablespace.h" #include "catalog/pg_type.h" #include "commands/comment.h" @@ -2147,29 +2146,12 @@ ComputeIndexAttrs(IndexInfo *indexInfo, opfamily = get_opclass_family(opclassOids[attn]); strat = get_op_opfamily_strategy(opid, opfamily); if (strat == 0) - { - HeapTuple opftuple; - Form_pg_opfamily opfform; - - /* - * attribute->opclass might not explicitly name the opfamily, - * so fetch the name of the selected opfamily for use in the - * error message. - */ - opftuple = SearchSysCache1(OPFAMILYOID, - ObjectIdGetDatum(opfamily)); - if (!HeapTupleIsValid(opftuple)) - elog(ERROR, "cache lookup failed for opfamily %u", - opfamily); - opfform = (Form_pg_opfamily) GETSTRUCT(opftuple); - ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("operator %s is not a member of operator family \"%s\"", format_operator(opid), - NameStr(opfform->opfname)), + get_opfamily_name(opfamily, false)), errdetail("The exclusion operator must be related to the index operator class for the constraint."))); - } indexInfo->ii_ExclusionOps[attn] = opid; indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid); @@ -2484,21 +2466,13 @@ GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype, } if (!OidIsValid(*opid)) - { - HeapTuple tuple; - - tuple = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamily)); - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for operator family %u", opfamily); - ereport(ERROR, errcode(ERRCODE_UNDEFINED_OBJECT), cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) : cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) : cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0, errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".", - NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist")); - } + get_opfamily_name(opfamily, false), "gist")); } /* -- 2.48.1