Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff720a5

Browse files
committed
Fix planner to consider matches to boolean columns in extension indexes.
The planner has to special-case indexes on boolean columns, becausewhat we need for an indexscan on such a column is a qual of the shapeof "boolvar = pseudoconstant". For plain bool constants, previoussimplification will have reduced this to "boolvar" or "NOT boolvar",and we have to reverse that if we want to make an indexqual. There isexisting code to do so, but it only fires when the index's opfamilyis BOOL_BTREE_FAM_OID or BOOL_HASH_FAM_OID. Thus extension AMs, orextension opclasses such as contrib/btree_gin, are out in the cold.The reason for hard-wiring the set of relevant opfamilies was mostlyto avoid a catalog lookup in a hot code path. We can improve matterswhile not taking much of a performance hit by relying on thehard-wired set when the opfamily OID is visibly built-in, and onlychecking the catalogs when dealing with an extension opfamily.While here, rename IsBooleanOpfamily to IsBuiltinBooleanOpfamilyto remind future users of that macro of its limitations. At somepoint we might want to make indxpath.c's improved version of thetest globally accessible, but it's not presently needed elsewhere.Zongliang Quan and Tom LaneDiscussion:https://postgr.es/m/f293b91d-1d46-d386-b6bb-4b06ff5c667b@yeah.net
1 parentd885a6b commitff720a5

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

‎contrib/btree_gin/expected/bool.out

Lines changed: 6 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
153153
RestrictInfo*rinfo,
154154
intindexcol,
155155
IndexOptInfo*index);
156+
staticboolIsBooleanOpfamily(Oidopfamily);
156157
staticIndexClause*match_boolean_index_clause(PlannerInfo*root,
157158
RestrictInfo*rinfo,
158159
intindexcol,IndexOptInfo*index);
@@ -2342,6 +2343,23 @@ match_clause_to_indexcol(PlannerInfo *root,
23422343
returnNULL;
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+
staticbool
2355+
IsBooleanOpfamily(Oidopfamily)
2356+
{
2357+
if (opfamily<FirstNormalObjectId)
2358+
returnIsBuiltinBooleanOpfamily(opfamily);
2359+
else
2360+
returnop_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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,13 @@ partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
11911191
PartitionSchemepartscheme=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

Lines changed: 5 additions & 1 deletion
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
returnPARTCLAUSE_UNSUPPORTED;
36013605

36023606
if (IsA(clause,BooleanTest))

‎src/include/catalog/pg_opfamily.h

Lines changed: 2 additions & 1 deletion
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
#ifdefEXPOSE_TO_CLIENT_CODE
5757

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

6162
#endif/* EXPOSE_TO_CLIENT_CODE */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp