Skip to content

Commit 5bba054

Browse files
committed
Skip not SOAP-supported indexes while transforming an OR clause into SAOP
There is no point in transforming OR-clauses into SAOP's if the target index doesn't support SAOP scans anyway. This commit adds corresponding checks to match_orclause_to_indexcol() and group_similar_or_args(). The first check fixes the actual bug, while the second just saves some cycles. Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/8174de69-9e1a-0827-0e81-ef97f56a5939%40gmail.com Author: Alena Rybakina Reviewed-by: Ranier Vilela, Alexander Korotkov, Andrei Lepikhov
1 parent b6612ae commit 5bba054

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/backend/optimizer/path/indxpath.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,11 @@ group_similar_or_args(PlannerInfo *root, RelOptInfo *rel, RestrictInfo *rinfo)
13541354
{
13551355
IndexOptInfo *index = (IndexOptInfo *) lfirst(lc2);
13561356

1357-
/* Ignore index if it doesn't support bitmap scans */
1358-
if (!index->amhasgetbitmap)
1357+
/*
1358+
* Ignore index if it doesn't support bitmap scans or SAOP
1359+
* clauses.
1360+
*/
1361+
if (!index->amhasgetbitmap || !index->amsearcharray)
13591362
continue;
13601363

13611364
for (colnum = 0; colnum < index->nkeycolumns; colnum++)
@@ -3248,6 +3251,10 @@ match_orclause_to_indexcol(PlannerInfo *root,
32483251
Assert(IsA(orclause, BoolExpr));
32493252
Assert(orclause->boolop == OR_EXPR);
32503253

3254+
/* Ignore index if it doesn't support SAOP clauses */
3255+
if (!index->amsearcharray)
3256+
return NULL;
3257+
32513258
/*
32523259
* Try to convert a list of OR-clauses to a single SAOP expression. Each
32533260
* OR entry must be in the form: (indexkey operator constant) or (constant

src/test/regress/expected/create_index.out

+18
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,24 @@ SELECT count(*) FROM tenk1 WHERE stringu1 = 'TVAAAA';
12331233
14
12341234
(1 row)
12351235

1236+
-- OR-clauses shouldn't be transformed into SAOP because hash indexes don't
1237+
-- support SAOP scans.
1238+
SET enable_seqscan = off;
1239+
EXPLAIN (COSTS OFF)
1240+
SELECT COUNT(*) FROM tenk1 WHERE stringu1 = 'TVAAAA' OR stringu1 = 'TVAAAB';
1241+
QUERY PLAN
1242+
------------------------------------------------------------------------------------
1243+
Aggregate
1244+
-> Bitmap Heap Scan on tenk1
1245+
Recheck Cond: ((stringu1 = 'TVAAAA'::name) OR (stringu1 = 'TVAAAB'::name))
1246+
-> BitmapOr
1247+
-> Bitmap Index Scan on hash_tuplesort_idx
1248+
Index Cond: (stringu1 = 'TVAAAA'::name)
1249+
-> Bitmap Index Scan on hash_tuplesort_idx
1250+
Index Cond: (stringu1 = 'TVAAAB'::name)
1251+
(8 rows)
1252+
1253+
RESET enable_seqscan;
12361254
DROP INDEX hash_tuplesort_idx;
12371255
RESET maintenance_work_mem;
12381256
--

src/test/regress/sql/create_index.sql

+6
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ CREATE INDEX hash_tuplesort_idx ON tenk1 USING hash (stringu1 name_ops) WITH (fi
372372
EXPLAIN (COSTS OFF)
373373
SELECT count(*) FROM tenk1 WHERE stringu1 = 'TVAAAA';
374374
SELECT count(*) FROM tenk1 WHERE stringu1 = 'TVAAAA';
375+
-- OR-clauses shouldn't be transformed into SAOP because hash indexes don't
376+
-- support SAOP scans.
377+
SET enable_seqscan = off;
378+
EXPLAIN (COSTS OFF)
379+
SELECT COUNT(*) FROM tenk1 WHERE stringu1 = 'TVAAAA' OR stringu1 = 'TVAAAB';
380+
RESET enable_seqscan;
375381
DROP INDEX hash_tuplesort_idx;
376382
RESET maintenance_work_mem;
377383

0 commit comments

Comments
 (0)