Skip to content

Commit

Permalink
[#22203] YSQL: Add new flag yb_enable_bitmapscan = false
Browse files Browse the repository at this point in the history
Summary:
`enable_bitmapscan` does not disable bitmap scan in all cases - e.g. if sequential scan and bitmap scan
are both disabled, they both have `disable_cost` added to their plan costs. Then bitmap scan might
still be the lowest costed plan.

Add a new flag `yb_enable_bitmapscan` that prevents YB Bitmap Scan plans from being created for YB
relations.

The default of this new flag is false, to prevent YB Bitmap Scans from being chosen until bitmap scans
are more stable.

The default of `enable_bitmapscan` is reset to true, to allow bitmap scans on PG temp tables by default
again. This restores the default behaviour for temp tables.

|                             | enable_bitmapscan = false                                           | enable_bitmapscan = true
| yb_enable_bitmapscan = false | PG bitmap scans have `disable_cost` added, YB bitmap scans disabled | PG bitmap scans enabled, YB bitmap scans disabled
| yb_enable_bitmapscan = true  | YB + PG bitmap scans have `disable_cost` added                      | YB + PG bitmap scans are enabled and costed normally
Jira: DB-11125

Test Plan:
```
./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressYbBitmapScans'
```

Reviewers: amartsinchyk, tnayak, mtakahara

Reviewed By: mtakahara

Subscribers: smishra, yql

Differential Revision: https://phorge.dev.yugabyte.com/D34646
  • Loading branch information
timothy-e committed May 3, 2024
1 parent 187d286 commit 2338431
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/postgres/src/backend/optimizer/path/costsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ int max_parallel_workers_per_gather = 2;
bool enable_seqscan = true;
bool enable_indexscan = true;
bool enable_indexonlyscan = true;
bool enable_bitmapscan = false;
bool enable_bitmapscan = true;
bool yb_enable_bitmapscan = false;
bool enable_tidscan = true;
bool enable_sort = true;
bool enable_hashagg = true;
Expand Down Expand Up @@ -1126,6 +1127,7 @@ cost_yb_bitmap_table_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
Path *bitmapqual, double loop_count)
{
Assert(baserel->is_yb_relation);
Assert(yb_enable_bitmapscan);
return cost_bitmap_heap_scan(path, root, baserel, param_info, bitmapqual,
loop_count);
}
Expand Down Expand Up @@ -6341,7 +6343,7 @@ yb_get_index_tuple_width(IndexOptInfo *index, Oid baserel_oid,
/* Aggregate the width of the columns in the secondary index */
for (int i = 0; i < index->ncolumns; i++)
{
index_tuple_width +=
index_tuple_width +=
index->rel->attr_widths[index->indexkeys[i] - index->rel->min_attr];
}
index_tuple_width += HIDDEN_COLUMNS_SIZE;
Expand Down
49 changes: 32 additions & 17 deletions src/postgres/src/backend/optimizer/path/indxpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,16 @@ create_index_paths(PlannerInfo *root, RelOptInfo *rel)

if (IsYugaByteEnabled() && rel->is_yb_relation)
{
YbBitmapTablePath *bpath;
bpath = create_yb_bitmap_table_path(root, rel, bitmapqual,
rel->lateral_relids, 1.0, 0);
add_path(rel, (Path *) bpath);
if (yb_enable_bitmapscan)
{
YbBitmapTablePath *bpath;
bpath = create_yb_bitmap_table_path(root, rel, bitmapqual,
rel->lateral_relids, 1.0,
0);
add_path(rel, (Path *) bpath);

/* TODO(#20575): support parallel bitmap scans */
/* TODO(#20575): support parallel bitmap scans */
}
}
else
{
Expand Down Expand Up @@ -430,16 +434,23 @@ create_index_paths(PlannerInfo *root, RelOptInfo *rel)
loop_count = get_loop_count(root, rel->relid, required_outer);

if (IsYugaByteEnabled() && rel->is_yb_relation)
bpath = (Path *) create_yb_bitmap_table_path(root, rel,
bitmapqual,
required_outer,
loop_count, 0);

{
if (yb_enable_bitmapscan)
{
bpath = (Path *) create_yb_bitmap_table_path(root, rel,
bitmapqual,
required_outer,
loop_count, 0);
add_path(rel, bpath);
}
}
else
{
bpath = (Path *) create_bitmap_heap_path(root, rel, bitmapqual,
required_outer,
loop_count, 0);
add_path(rel, bpath);
add_path(rel, bpath);
}
}
}
}
Expand Down Expand Up @@ -1703,6 +1714,9 @@ generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
List *all_clauses;
ListCell *lc;

if (IsYugaByteEnabled() && rel->is_yb_relation && !yb_enable_bitmapscan)
return NIL;

/*
* We can use both the current and other clauses as context for
* build_paths_for_OR; no need to remove ORs from the lists.
Expand Down Expand Up @@ -2110,12 +2124,13 @@ yb_bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
*/
bpath.path.parallel_workers = 0;

/* Now we can do cost_yb_bitmap_table_scan */
cost_yb_bitmap_table_scan(&bpath.path, root, rel,
bpath.path.param_info,
ipath,
get_loop_count(root, rel->relid,
PATH_REQ_OUTER(ipath)));
if (yb_enable_bitmapscan)
/* Now we can do cost_yb_bitmap_table_scan */
cost_yb_bitmap_table_scan(&bpath.path, root, rel,
bpath.path.param_info,
ipath,
get_loop_count(root, rel->relid,
PATH_REQ_OUTER(ipath)));

return bpath.path.total_cost;
}
Expand Down
12 changes: 11 additions & 1 deletion src/postgres/src/backend/utils/misc/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ static struct config_bool ConfigureNamesBool[] =
NULL
},
&enable_bitmapscan,
false,
true,
NULL, NULL, NULL
},
{
Expand Down Expand Up @@ -1112,6 +1112,16 @@ static struct config_bool ConfigureNamesBool[] =
false,
NULL, NULL, NULL
},
{
{"yb_enable_bitmapscan", PGC_USERSET, QUERY_TUNING_METHOD,
gettext_noop("Enables the planner's use of YB bitmap-scan plans."),
gettext_noop("To use YB Bitmap Scans, both yb_enable_bitmapscan "
"and enable_bitmapscan must be true.")
},
&yb_enable_bitmapscan,
false,
NULL, NULL, NULL
},
{
{"enable_partition_pruning", PGC_USERSET, QUERY_TUNING_METHOD,
gettext_noop("Enable plan-time and run-time partition pruning."),
Expand Down
1 change: 1 addition & 0 deletions src/postgres/src/include/optimizer/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extern PGDLLIMPORT bool enable_seqscan;
extern PGDLLIMPORT bool enable_indexscan;
extern PGDLLIMPORT bool enable_indexonlyscan;
extern PGDLLIMPORT bool enable_bitmapscan;
extern PGDLLIMPORT bool yb_enable_bitmapscan;
extern PGDLLIMPORT bool enable_tidscan;
extern PGDLLIMPORT bool enable_sort;
extern PGDLLIMPORT bool enable_hashagg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--
SET yb_explain_hide_non_deterministic_fields = true;
SET enable_bitmapscan = true;
SET yb_enable_bitmapscan = true;
SET yb_prefer_bnl = false;
CREATE TABLE joina (k INT, a INT, b INT, PRIMARY KEY (k ASC));
CREATE INDEX ON joina (a ASC);
Expand Down
132 changes: 132 additions & 0 deletions src/postgres/src/test/regress/expected/yb_bitmap_scans.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,137 @@
-- YB Bitmap Scans (bitmap index scans + YB bitmap table scans)
--
SET yb_explain_hide_non_deterministic_fields = true;
--
-- -- test disabling bitmap scans --
-- for each combination of yb_enable_bitmapscan and enable_bitmapscan, try
-- 1. a case where the planner chooses bitmap scans
-- 2. a case where we tell the planner to use bitmap scans
-- 3. a case where the alternative option (seq scan) is disabled
--
CREATE TABLE test_disable(a int, b int);
CREATE INDEX ON test_disable(a ASC);
CREATE INDEX ON test_disable(b ASC);
SET yb_enable_bitmapscan = true;
SET enable_bitmapscan = true;
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
-----------------------------------------------------
YB Bitmap Table Scan on test_disable
-> BitmapOr
-> Bitmap Index Scan on test_disable_a_idx
Index Cond: (a < 5)
-> Bitmap Index Scan on test_disable_b_idx
Index Cond: (b < 5)
(6 rows)

/*+ BitmapScan(test_disable) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
-----------------------------------------------------
YB Bitmap Table Scan on test_disable
-> BitmapOr
-> Bitmap Index Scan on test_disable_a_idx
Index Cond: (a < 5)
-> Bitmap Index Scan on test_disable_b_idx
Index Cond: (b < 5)
(6 rows)

/*+ Set(enable_seqscan false) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
-----------------------------------------------------
YB Bitmap Table Scan on test_disable
-> BitmapOr
-> Bitmap Index Scan on test_disable_a_idx
Index Cond: (a < 5)
-> Bitmap Index Scan on test_disable_b_idx
Index Cond: (b < 5)
(6 rows)

SET yb_enable_bitmapscan = true;
SET enable_bitmapscan = false;
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

/*+ BitmapScan(test_disable) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
-----------------------------------------------------
YB Bitmap Table Scan on test_disable
-> BitmapOr
-> Bitmap Index Scan on test_disable_a_idx
Index Cond: (a < 5)
-> Bitmap Index Scan on test_disable_b_idx
Index Cond: (b < 5)
(6 rows)

/*+ Set(enable_seqscan false) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
-----------------------------------------------------
YB Bitmap Table Scan on test_disable
-> BitmapOr
-> Bitmap Index Scan on test_disable_a_idx
Index Cond: (a < 5)
-> Bitmap Index Scan on test_disable_b_idx
Index Cond: (b < 5)
(6 rows)

SET yb_enable_bitmapscan = false;
SET enable_bitmapscan = true;
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

/*+ BitmapScan(test_disable) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

/*+ Set(enable_seqscan false) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

SET yb_enable_bitmapscan = false;
SET enable_bitmapscan = false;
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

/*+ BitmapScan(test_disable) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

/*+ Set(enable_seqscan false) */
EXPLAIN (COSTS OFF) SELECT * FROM test_disable WHERE a < 5 OR b < 5;
QUERY PLAN
----------------------------------------
Seq Scan on test_disable
Storage Filter: ((a < 5) OR (b < 5))
(2 rows)

SET yb_enable_bitmapscan = true;
SET enable_bitmapscan = true;
-- tenk1 already has 4 ASC indexes: unique1, unique2, hundred, and (thousand, tenthous)
-- each query has an order by to make asserting results easier
Expand Down Expand Up @@ -1323,6 +1454,7 @@ CREATE DATABASE colo WITH colocation = true;
\c colo;
SET yb_explain_hide_non_deterministic_fields = true;
SET enable_bitmapscan = true;
SET yb_enable_bitmapscan = true;
CREATE TABLE pk_colo (k INT PRIMARY KEY, a INT);
CREATE INDEX ON pk_colo(a ASC);
INSERT INTO pk_colo SELECT i, i FROM generate_series(1, 1000) i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- test distinct bitmap scans (distinct pushdown is not supported by bitmap scans)
--
SET yb_explain_hide_non_deterministic_fields = true;
SET yb_enable_bitmapscan = true;
CREATE TABLE test_distinct (r1 INT, r2 INT, r3 INT, v INT, PRIMARY KEY(r1 ASC, r2 ASC, r3 ASC)) SPLIT AT VALUES ((1, 1, 500));
INSERT INTO test_distinct (SELECT 1, i%3, i, i/3 FROM GENERATE_SERIES(1, 1000) AS i);
-- Add one more distinct value to catch bugs that arise only with more than one distinct value.
Expand Down
2 changes: 2 additions & 0 deletions src/postgres/src/test/regress/expected/yb_guc.out
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ CREATE TABLE test_scan (i int, j int);
CREATE INDEX NONCONCURRENTLY ON test_scan (j);
-- Don't add (costs off) to EXPLAIN to be able to see when disable_cost=1.0e10
-- is added.
set yb_enable_bitmapscan = on;
set enable_seqscan = on;
set enable_indexscan = on;
set enable_indexonlyscan = on;
Expand Down Expand Up @@ -352,3 +353,4 @@ ERROR: unrecognized configuration parameter "foo"
RESET plpgsql.extra_foo_warnings;
WARNING: unrecognized configuration parameter "plpgsql.extra_foo_warnings"
DETAIL: "plpgsql" is a reserved prefix.
RESET yb_enable_bitmapscan;
2 changes: 2 additions & 0 deletions src/postgres/src/test/regress/expected/yb_partition_prune.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set yb_enable_bitmapscan = true;
create table ab (a int not null, b int not null) partition by list (a);
create table ab_a2 partition of ab for values in(2) partition by list (b);
create table ab_a2_b1 partition of ab_a2 for values in (1);
Expand Down Expand Up @@ -110,4 +111,5 @@ explain (analyze, costs off, summary off, timing off)
(31 rows)

reset enable_bitmapscan;
reset yb_enable_bitmapscan;
drop table ab;
5 changes: 3 additions & 2 deletions src/postgres/src/test/regress/expected/yb_pg_select.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--
-- SELECT
--
SET yb_enable_bitmapscan TO on;
-- lsm index
-- awk '{if($1<10){print;}else{next;}}' onek.data | sort +0n -1
--
Expand Down Expand Up @@ -292,7 +293,7 @@ SELECT onek2.unique1, onek2.stringu1 FROM onek2
(19 rows)

RESET enable_seqscan;
SET enable_bitmapscan = on;
RESET enable_bitmapscan;
RESET enable_sort;
SELECT two, stringu1, ten, string4
INTO TABLE tmp
Expand Down Expand Up @@ -694,4 +695,4 @@ LIMIT ALL) ybview ORDER BY unique2;
0 | 998
(2 rows)

RESET enable_bitmapscan;
RESET yb_enable_bitmapscan;
1 change: 1 addition & 0 deletions src/postgres/src/test/regress/sql/yb_bitmap_scan_joins.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--
SET yb_explain_hide_non_deterministic_fields = true;
SET enable_bitmapscan = true;
SET yb_enable_bitmapscan = true;
SET yb_prefer_bnl = false;

CREATE TABLE joina (k INT, a INT, b INT, PRIMARY KEY (k ASC));
Expand Down

0 comments on commit 2338431

Please sign in to comment.