Improve possible performance regression
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 7 Mar 2025 10:20:26 +0000 (11:20 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Fri, 7 Mar 2025 10:46:33 +0000 (11:46 +0100)
Commit ce62f2f2a0a introduced calls to GetIndexAmRoutineByAmId() in
lsyscache.c functions.  This call is a bit more expensive than a
simple syscache lookup.  So rearrange the nesting so that we call that
one last and do the cheaper checks first.

Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/E1tngY6-0000UL-2n%40gemulon.postgresql.org

src/backend/utils/cache/lsyscache.c

index 97ad36a031b05b35070c7fc52490a4518caae23f..a712a432938b6f639883b4c41e9ecb2c87aa802f 100644 (file)
@@ -717,11 +717,16 @@ equality_ops_are_compatible(Oid opno1, Oid opno2)
    {
        HeapTuple   op_tuple = &catlist->members[i]->tuple;
        Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
-       IndexAmRoutine *amroutine = GetIndexAmRoutineByAmId(op_form->amopmethod, false);
 
-       if (amroutine->amconsistentequality)
+       /*
+        * op_in_opfamily() is cheaper than GetIndexAmRoutineByAmId(), so
+        * check it first
+        */
+       if (op_in_opfamily(opno2, op_form->amopfamily))
        {
-           if (op_in_opfamily(opno2, op_form->amopfamily))
+           IndexAmRoutine *amroutine = GetIndexAmRoutineByAmId(op_form->amopmethod, false);
+
+           if (amroutine->amconsistentequality)
            {
                result = true;
                break;
@@ -768,11 +773,16 @@ comparison_ops_are_compatible(Oid opno1, Oid opno2)
    {
        HeapTuple   op_tuple = &catlist->members[i]->tuple;
        Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
-       IndexAmRoutine *amroutine = GetIndexAmRoutineByAmId(op_form->amopmethod, false);
 
-       if (amroutine->amcanorder && amroutine->amconsistentordering)
+       /*
+        * op_in_opfamily() is cheaper than GetIndexAmRoutineByAmId(), so
+        * check it first
+        */
+       if (op_in_opfamily(opno2, op_form->amopfamily))
        {
-           if (op_in_opfamily(opno2, op_form->amopfamily))
+           IndexAmRoutine *amroutine = GetIndexAmRoutineByAmId(op_form->amopmethod, false);
+
+           if (amroutine->amcanorder && amroutine->amconsistentordering)
            {
                result = true;
                break;