Add get_opfamily_name() function
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 24 Jan 2025 21:58:13 +0000 (22:58 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Sat, 1 Feb 2025 09:42:58 +0000 (10:42 +0100)
This refactors and simplifies various existing code to make use of the
new function.

Reviewed-by: Mark Dilger <mark.dilger@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/flat/E72EAA49-354D-4C2E-8EB9-255197F55330@enterprisedb.com

contrib/bloom/blvalidate.c
src/backend/access/brin/brin_validate.c
src/backend/access/gin/ginvalidate.c
src/backend/access/gist/gistvalidate.c
src/backend/access/hash/hashvalidate.c
src/backend/access/nbtree/nbtvalidate.c
src/backend/access/spgist/spgvalidate.c
src/backend/commands/indexcmds.c
src/backend/utils/cache/lsyscache.c
src/include/utils/lsyscache.h

index d9a3b329aaa9e19b3ef4f299c73946c1daef2056..001c188aeb712caaf502bbf078bee3d193e9aacb 100644 (file)
@@ -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;
index a49a90096265b33a311d4b2f5d10e1bc89aa85d5..915b8628b460ed362a271f7ec9adc0d70d691724 100644 (file)
@@ -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;
index 6bd94e448546e7b401d9d3a5aa20fdae7b44394f..5b0bfe8cc1dbc6f0944f002b69f0b6a3758a21e4 100644 (file)
@@ -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;
index bb86b5594868e2c75905629cedc7ed7d103eafe9..ffefa12d97ae17328f9a261a1bce568763e7b995 100644 (file)
@@ -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;
index b54ecde9224e49754a6c639011cca36a99878e3a..06ac832ba10f813a47da9eaec037fa24daa99854 100644 (file)
@@ -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;
index b87c959a2fd6b5c19e2fabccecdeb6fe2c21993b..dd6f5a15c653e657b815d23ea4f6835fa801d665 100644 (file)
@@ -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;
index 7d05e43720b69914db91fdfdfe114c709b223824..e9964fab4f41a3c058142ad02aa2d2f6264fe96b 100644 (file)
@@ -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;
index 0aa15a42efec024ad77e0d9a0a0ae7e9d740edf2..f788b8f29b85b0f46b03fc3a7007beeb170a2baa 100644 (file)
@@ -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"));
 }
 
 /*
index 7a9af03c960c39c51bb736bb01d4bef29cecf1e6..bcfa5cb4add01e22070bb26a82e5e5daf4f656f9 100644 (file)
@@ -30,6 +30,7 @@
 #include "catalog/pg_language.h"
 #include "catalog/pg_namespace.h"
 #include "catalog/pg_opclass.h"
+#include "catalog/pg_opfamily.h"
 #include "catalog/pg_operator.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_publication.h"
@@ -1273,6 +1274,32 @@ get_opclass_method(Oid opclass)
    return result;
 }
 
+/*             ---------- OPFAMILY CACHE ----------                     */
+
+char *
+get_opfamily_name(Oid opfid, bool missing_ok)
+{
+   HeapTuple   tup;
+   char       *opfname;
+   Form_pg_opfamily opfform;
+
+   tup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
+
+   if (!HeapTupleIsValid(tup))
+   {
+       if (!missing_ok)
+           elog(ERROR, "cache lookup failed for operator family %u", opfid);
+       return NULL;
+   }
+
+   opfform = (Form_pg_opfamily) GETSTRUCT(tup);
+   opfname = pstrdup(NameStr(opfform->opfname));
+
+   ReleaseSysCache(tup);
+
+   return opfname;
+}
+
 /*             ---------- OPERATOR CACHE ----------                     */
 
 /*
index b23deb9662f164d19d2825d9183d92ed4a512377..6fab7aa60095f537f39f285c38bc6a03de25d9ea 100644 (file)
@@ -108,6 +108,7 @@ extern Oid  get_opclass_input_type(Oid opclass);
 extern bool get_opclass_opfamily_and_input_type(Oid opclass,
                                                Oid *opfamily, Oid *opcintype);
 extern Oid get_opclass_method(Oid opclass);
+extern char *get_opfamily_name(Oid opfid, bool missing_ok);
 extern RegProcedure get_opcode(Oid opno);
 extern char *get_opname(Oid opno);
 extern Oid get_op_rettype(Oid opno);