Fix planner to consider matches to boolean columns in extension indexes.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 2 Sep 2022 21:01:51 +0000 (17:01 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 2 Sep 2022 21:01:51 +0000 (17:01 -0400)
The planner has to special-case indexes on boolean columns, because
what we need for an indexscan on such a column is a qual of the shape
of "boolvar = pseudoconstant".  For plain bool constants, previous
simplification will have reduced this to "boolvar" or "NOT boolvar",
and we have to reverse that if we want to make an indexqual.  There is
existing code to do so, but it only fires when the index's opfamily
is BOOL_BTREE_FAM_OID or BOOL_HASH_FAM_OID.  Thus extension AMs, or
extension opclasses such as contrib/btree_gin, are out in the cold.

The reason for hard-wiring the set of relevant opfamilies was mostly
to avoid a catalog lookup in a hot code path.  We can improve matters
while not taking much of a performance hit by relying on the
hard-wired set when the opfamily OID is visibly built-in, and only
checking the catalogs when dealing with an extension opfamily.

While here, rename IsBooleanOpfamily to IsBuiltinBooleanOpfamily
to remind future users of that macro of its limitations.  At some
point we might want to make indxpath.c's improved version of the
test globally accessible, but it's not presently needed elsewhere.

Zongliang Quan and Tom Lane

Discussion: https://postgr.es/m/f293b91d-1d46-d386-b6bb-4b06ff5c667b@yeah.net

contrib/btree_gin/expected/bool.out
contrib/btree_gist/expected/bool.out
src/backend/optimizer/path/indxpath.c
src/backend/optimizer/path/pathkeys.c
src/backend/partitioning/partprune.c
src/include/catalog/pg_opfamily.h

index efb3e1e327cc58164c70d8f797e71cced198e3e5..207a3f232810731c61a2ad32254d0b1d820002e1 100644 (file)
@@ -87,13 +87,15 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i<=true ORDER BY i;
 (6 rows)
 
 EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i=true ORDER BY i;
-         QUERY PLAN          
------------------------------
+                QUERY PLAN                 
+-------------------------------------------
  Sort
    Sort Key: i
-   ->  Seq Scan on test_bool
+   ->  Bitmap Heap Scan on test_bool
          Filter: i
-(4 rows)
+         ->  Bitmap Index Scan on idx_bool
+               Index Cond: (i = true)
+(6 rows)
 
 EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i>=true ORDER BY i;
                 QUERY PLAN                 
index c67a9685d52a341feb9e5dd9f20f0d8896ce8c84..29390f079c02889b1b5860ea6da9561555c40173 100644 (file)
@@ -71,7 +71,7 @@ SELECT * FROM booltmp WHERE a;
                 QUERY PLAN                
 ------------------------------------------
  Index Only Scan using boolidx on booltmp
-   Filter: a
+   Index Cond: (a = true)
 (2 rows)
 
 SELECT * FROM booltmp WHERE a;
@@ -85,7 +85,7 @@ SELECT * FROM booltmp WHERE NOT a;
                 QUERY PLAN                
 ------------------------------------------
  Index Only Scan using boolidx on booltmp
-   Filter: (NOT a)
+   Index Cond: (a = false)
 (2 rows)
 
 SELECT * FROM booltmp WHERE NOT a;
index 045ff2e487ec9a830f5878c063fe03d3dbf3ade0..63a8eef45cd3f11cce8eccf3b0176474b4ac5e91 100644 (file)
@@ -153,6 +153,7 @@ static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
                                             RestrictInfo *rinfo,
                                             int indexcol,
                                             IndexOptInfo *index);
+static bool IsBooleanOpfamily(Oid opfamily);
 static IndexClause *match_boolean_index_clause(PlannerInfo *root,
                                               RestrictInfo *rinfo,
                                               int indexcol, IndexOptInfo *index);
@@ -2342,6 +2343,23 @@ match_clause_to_indexcol(PlannerInfo *root,
    return NULL;
 }
 
+/*
+ * IsBooleanOpfamily
+ *   Detect whether an opfamily supports boolean equality as an operator.
+ *
+ * If the opfamily OID is in the range of built-in objects, we can rely
+ * on hard-wired knowledge of which built-in opfamilies support this.
+ * For extension opfamilies, there's no choice but to do a catcache lookup.
+ */
+static bool
+IsBooleanOpfamily(Oid opfamily)
+{
+   if (opfamily < FirstNormalObjectId)
+       return IsBuiltinBooleanOpfamily(opfamily);
+   else
+       return op_in_opfamily(BooleanEqualOperator, opfamily);
+}
+
 /*
  * match_boolean_index_clause
  *   Recognize restriction clauses that can be matched to a boolean index.
index 1fa7fc99b5144cd2dea5fb05a0623e0a65d4e4bb..18f88700981104ee2ce1b7130b89e4da8bd9f9b4 100644 (file)
@@ -1191,8 +1191,13 @@ partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
    PartitionScheme partscheme = partrel->part_scheme;
    ListCell   *lc;
 
-   /* If the partkey isn't boolean, we can't possibly get a match */
-   if (!IsBooleanOpfamily(partscheme->partopfamily[partkeycol]))
+   /*
+    * If the partkey isn't boolean, we can't possibly get a match.
+    *
+    * Partitioning currently can only use built-in AMs, so checking for
+    * built-in boolean opfamilies is good enough.
+    */
+   if (!IsBuiltinBooleanOpfamily(partscheme->partopfamily[partkeycol]))
        return false;
 
    /* Check each restriction clause for the partitioned rel */
index bf9fe5b7aafe2284361e051b00bf34d20df0a47d..5ab46123671773e7408404451f02b50256727fae 100644 (file)
@@ -3596,7 +3596,11 @@ match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey,
 
    *outconst = NULL;
 
-   if (!IsBooleanOpfamily(partopfamily))
+   /*
+    * Partitioning currently can only use built-in AMs, so checking for
+    * built-in boolean opfamilies is good enough.
+    */
+   if (!IsBuiltinBooleanOpfamily(partopfamily))
        return PARTCLAUSE_UNSUPPORTED;
 
    if (IsA(clause, BooleanTest))
index 8dc9ce01bb92d081ac069ce1de58ec889d8f35da..67ad01ce07cc0cdd028a061ccbb0f6199dde994c 100644 (file)
@@ -55,7 +55,8 @@ DECLARE_UNIQUE_INDEX_PKEY(pg_opfamily_oid_index, 2755, OpfamilyOidIndexId, on pg
 
 #ifdef EXPOSE_TO_CLIENT_CODE
 
-#define IsBooleanOpfamily(opfamily) \
+/* This does not account for non-core opfamilies that might accept boolean */
+#define IsBuiltinBooleanOpfamily(opfamily) \
    ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
 
 #endif                         /* EXPOSE_TO_CLIENT_CODE */