Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff720a5

Browse files
committed
Fix planner to consider matches to boolean columns in extension indexes.
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
1 parent d885a6b commit ff720a5

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

contrib/btree_gin/expected/bool.out

+6-4
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i<=true ORDER BY i;
8787
(6 rows)
8888

8989
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i=true ORDER BY i;
90-
QUERY PLAN
91-
-----------------------------
90+
QUERY PLAN
91+
-------------------------------------------
9292
Sort
9393
Sort Key: i
94-
-> Seq Scan on test_bool
94+
-> Bitmap Heap Scan on test_bool
9595
Filter: i
96-
(4 rows)
96+
-> Bitmap Index Scan on idx_bool
97+
Index Cond: (i = true)
98+
(6 rows)
9799

98100
EXPLAIN (COSTS OFF) SELECT * FROM test_bool WHERE i>=true ORDER BY i;
99101
QUERY PLAN

contrib/btree_gist/expected/bool.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ SELECT * FROM booltmp WHERE a;
7171
QUERY PLAN
7272
------------------------------------------
7373
Index Only Scan using boolidx on booltmp
74-
Filter: a
74+
Index Cond: (a = true)
7575
(2 rows)
7676

7777
SELECT * FROM booltmp WHERE a;
@@ -85,7 +85,7 @@ SELECT * FROM booltmp WHERE NOT a;
8585
QUERY PLAN
8686
------------------------------------------
8787
Index Only Scan using boolidx on booltmp
88-
Filter: (NOT a)
88+
Index Cond: (a = false)
8989
(2 rows)
9090

9191
SELECT * FROM booltmp WHERE NOT a;

src/backend/optimizer/path/indxpath.c

+18
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
153153
RestrictInfo *rinfo,
154154
int indexcol,
155155
IndexOptInfo *index);
156+
static bool IsBooleanOpfamily(Oid opfamily);
156157
static IndexClause *match_boolean_index_clause(PlannerInfo *root,
157158
RestrictInfo *rinfo,
158159
int indexcol, IndexOptInfo *index);
@@ -2342,6 +2343,23 @@ match_clause_to_indexcol(PlannerInfo *root,
23422343
return NULL;
23432344
}
23442345

2346+
/*
2347+
* IsBooleanOpfamily
2348+
* Detect whether an opfamily supports boolean equality as an operator.
2349+
*
2350+
* If the opfamily OID is in the range of built-in objects, we can rely
2351+
* on hard-wired knowledge of which built-in opfamilies support this.
2352+
* For extension opfamilies, there's no choice but to do a catcache lookup.
2353+
*/
2354+
static bool
2355+
IsBooleanOpfamily(Oid opfamily)
2356+
{
2357+
if (opfamily < FirstNormalObjectId)
2358+
return IsBuiltinBooleanOpfamily(opfamily);
2359+
else
2360+
return op_in_opfamily(BooleanEqualOperator, opfamily);
2361+
}
2362+
23452363
/*
23462364
* match_boolean_index_clause
23472365
* Recognize restriction clauses that can be matched to a boolean index.

src/backend/optimizer/path/pathkeys.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,13 @@ partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
11911191
PartitionScheme partscheme = partrel->part_scheme;
11921192
ListCell *lc;
11931193

1194-
/* If the partkey isn't boolean, we can't possibly get a match */
1195-
if (!IsBooleanOpfamily(partscheme->partopfamily[partkeycol]))
1194+
/*
1195+
* If the partkey isn't boolean, we can't possibly get a match.
1196+
*
1197+
* Partitioning currently can only use built-in AMs, so checking for
1198+
* built-in boolean opfamilies is good enough.
1199+
*/
1200+
if (!IsBuiltinBooleanOpfamily(partscheme->partopfamily[partkeycol]))
11961201
return false;
11971202

11981203
/* Check each restriction clause for the partitioned rel */

src/backend/partitioning/partprune.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,11 @@ match_boolean_partition_clause(Oid partopfamily, Expr *clause, Expr *partkey,
35963596

35973597
*outconst = NULL;
35983598

3599-
if (!IsBooleanOpfamily(partopfamily))
3599+
/*
3600+
* Partitioning currently can only use built-in AMs, so checking for
3601+
* built-in boolean opfamilies is good enough.
3602+
*/
3603+
if (!IsBuiltinBooleanOpfamily(partopfamily))
36003604
return PARTCLAUSE_UNSUPPORTED;
36013605

36023606
if (IsA(clause, BooleanTest))

src/include/catalog/pg_opfamily.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ DECLARE_UNIQUE_INDEX_PKEY(pg_opfamily_oid_index, 2755, OpfamilyOidIndexId, on pg
5555

5656
#ifdef EXPOSE_TO_CLIENT_CODE
5757

58-
#define IsBooleanOpfamily(opfamily) \
58+
/* This does not account for non-core opfamilies that might accept boolean */
59+
#define IsBuiltinBooleanOpfamily(opfamily) \
5960
((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
6061

6162
#endif /* EXPOSE_TO_CLIENT_CODE */

0 commit comments

Comments
 (0)