]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Simplify GetOperatorFromCompareType() code
authorDavid Rowley <drowley@postgresql.org>
Tue, 6 Jan 2026 02:25:13 +0000 (15:25 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 6 Jan 2026 02:25:13 +0000 (15:25 +1300)
The old code would set *opid = InvalidOid to determine if the
get_opclass_opfamily_and_input_type() worked or not.  This means more
moving parts that what's really needed here.  Let's just fail
immediately if the get_opclass_opfamily_and_input_type() lookup fails.

Author: Paul A Jungwirth <pj@illuminatedcomputing.com>
Reviewed-by: Neil Chen <carpenter.nail.cz@gmail.com>
Discussion: https://postgr.es/m/CA+renyXOrjLacP_nhqEQUf2W+ZCoY2q5kpQCfG05vQVYzr8b9w@mail.gmail.com

src/backend/commands/indexcmds.c

index 0a7b6f18fa28373bc4d7f032acfe00c6a9d4c47e..08c86cc163c6a2c083d16dee6e4904593ec38514 100644 (file)
@@ -2473,34 +2473,36 @@ GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype,
 
        Assert(cmptype == COMPARE_EQ || cmptype == COMPARE_OVERLAP || cmptype == COMPARE_CONTAINED_BY);
 
-       amid = get_opclass_method(opclass);
+       /*
+        * Use the opclass to get the opfamily, opcintype, and access method. If
+        * any of this fails, quit early.
+        */
+       if (!get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
+               elog(ERROR, "cache lookup failed for opclass %u", opclass);
 
-       *opid = InvalidOid;
+       amid = get_opclass_method(opclass);
 
-       if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
-       {
-               /*
-                * Ask the index AM to translate to its internal stratnum
-                */
-               *strat = IndexAmTranslateCompareType(cmptype, amid, opfamily, true);
-               if (*strat == InvalidStrategy)
-                       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("Could not translate compare type %d for operator family \"%s\" of access method \"%s\".",
-                                                         cmptype, get_opfamily_name(opfamily, false), get_am_name(amid)));
+       /*
+        * Ask the index AM to translate to its internal stratnum
+        */
+       *strat = IndexAmTranslateCompareType(cmptype, amid, opfamily, true);
+       if (*strat == InvalidStrategy)
+               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("Could not translate compare type %d for operator family \"%s\" of access method \"%s\".",
+                                                 cmptype, get_opfamily_name(opfamily, false), get_am_name(amid)));
 
-               /*
-                * We parameterize rhstype so foreign keys can ask for a <@ operator
-                * whose rhs matches the aggregate function. For example range_agg
-                * returns anymultirange.
-                */
-               if (!OidIsValid(rhstype))
-                       rhstype = opcintype;
-               *opid = get_opfamily_member(opfamily, opcintype, rhstype, *strat);
-       }
+       /*
+        * We parameterize rhstype so foreign keys can ask for a <@ operator whose
+        * rhs matches the aggregate function. For example range_agg returns
+        * anymultirange.
+        */
+       if (!OidIsValid(rhstype))
+               rhstype = opcintype;
+       *opid = get_opfamily_member(opfamily, opcintype, rhstype, *strat);
 
        if (!OidIsValid(*opid))
                ereport(ERROR,