#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"
 
    Oid         opcintype;
    Oid         opckeytype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
 
    Oid         opfamilyoid;
    Oid         opcintype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
 
    Oid         opcintype;
    Oid         opckeytype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
    Oid         opcintype;
    Oid         opckeytype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
    Oid         opfamilyoid;
    Oid         opcintype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
    Oid         opfamilyoid;
    Oid         opcintype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
    Oid         opcintype;
    Oid         opckeytype;
    char       *opclassname;
-   HeapTuple   familytup;
-   Form_pg_opfamily familyform;
    char       *opfamilyname;
    CatCList   *proclist,
               *oprlist;
    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));
 
    ReleaseCatCacheList(proclist);
    ReleaseCatCacheList(oprlist);
-   ReleaseSysCache(familytup);
    ReleaseSysCache(classtup);
 
    return result;
 
 #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"
            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);
    }
 
    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"));
 }
 
 /*
 
 #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"
    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 ----------                     */
 
 /*
 
 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);