Skip to content

Commit e880a1c

Browse files
authored
Test the handling of a PG partitioned table in skip scan planner (#9723)
The skip-scan upper-paths hook fires for every UPPERREL_DISTINCT, even for regular Postgres tables. There's a check for this case already, but it is not triggered by the existing tests according to the line coverage info, so add a small test for this.
1 parent 2c424c2 commit e880a1c

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- This file and its contents are licensed under the Timescale License.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-TIMESCALE for a copy of the license.
4+
-- DISTINCT on a regular PostgreSQL partitioned table (not a TimescaleDB
5+
-- hypertable) must produce a correct plan. The SkipScan upper-paths hook
6+
-- is invoked for every UPPERREL_DISTINCT, so get_distinct_var has to bail
7+
-- out when the input rel is not a hypertable child.
8+
\c :TEST_DBNAME :ROLE_SUPERUSER
9+
CREATE TABLE pg_part (a int, b int) PARTITION BY RANGE (a);
10+
CREATE TABLE pg_part_p1 PARTITION OF pg_part FOR VALUES FROM (0) TO (100);
11+
CREATE TABLE pg_part_p2 PARTITION OF pg_part FOR VALUES FROM (100) TO (200);
12+
INSERT INTO pg_part SELECT i % 200, i FROM generate_series(0, 999) i;
13+
CREATE INDEX ON pg_part_p1 (b);
14+
CREATE INDEX ON pg_part_p2 (b);
15+
ANALYZE pg_part;
16+
SET enable_hashagg = off;
17+
SET enable_seqscan = off;
18+
-- A Var from a non-hypertable parent reaches get_distinct_var through the
19+
-- MergeAppend subpath and is rejected; the resulting plan is plain Unique
20+
-- over MergeAppend over per-partition Index Only Scan, with no SkipScan.
21+
EXPLAIN (costs off) SELECT DISTINCT b FROM pg_part ORDER BY b;
22+
--- QUERY PLAN ---
23+
Unique
24+
-> Merge Append
25+
Sort Key: pg_part.b
26+
-> Index Only Scan using pg_part_p1_b_idx on pg_part_p1 pg_part_1
27+
-> Index Only Scan using pg_part_p2_b_idx on pg_part_p2 pg_part_2
28+
29+
SELECT count(*) FROM (SELECT DISTINCT b FROM pg_part ORDER BY b) s;
30+
count
31+
-------
32+
1000
33+
34+
DROP TABLE pg_part;

tsl/test/sql/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ set(TEST_FILES
7070
direct_compress_insert.sql
7171
move.sql
7272
plan_skip_scan_notnull.sql
73+
plan_skip_scan_pg_partition.sql
7374
policy_generalization.sql
7475
rebuild_columnstore_tests.sql
7576
recompression_integrity_tests.sql
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- This file and its contents are licensed under the Timescale License.
2+
-- Please see the included NOTICE for copyright information and
3+
-- LICENSE-TIMESCALE for a copy of the license.
4+
5+
-- DISTINCT on a regular PostgreSQL partitioned table (not a TimescaleDB
6+
-- hypertable) must produce a correct plan. The SkipScan upper-paths hook
7+
-- is invoked for every UPPERREL_DISTINCT, so get_distinct_var has to bail
8+
-- out when the input rel is not a hypertable child.
9+
10+
\c :TEST_DBNAME :ROLE_SUPERUSER
11+
12+
CREATE TABLE pg_part (a int, b int) PARTITION BY RANGE (a);
13+
CREATE TABLE pg_part_p1 PARTITION OF pg_part FOR VALUES FROM (0) TO (100);
14+
CREATE TABLE pg_part_p2 PARTITION OF pg_part FOR VALUES FROM (100) TO (200);
15+
INSERT INTO pg_part SELECT i % 200, i FROM generate_series(0, 999) i;
16+
CREATE INDEX ON pg_part_p1 (b);
17+
CREATE INDEX ON pg_part_p2 (b);
18+
ANALYZE pg_part;
19+
20+
SET enable_hashagg = off;
21+
SET enable_seqscan = off;
22+
23+
-- A Var from a non-hypertable parent reaches get_distinct_var through the
24+
-- MergeAppend subpath and is rejected; the resulting plan is plain Unique
25+
-- over MergeAppend over per-partition Index Only Scan, with no SkipScan.
26+
EXPLAIN (costs off) SELECT DISTINCT b FROM pg_part ORDER BY b;
27+
SELECT count(*) FROM (SELECT DISTINCT b FROM pg_part ORDER BY b) s;
28+
29+
DROP TABLE pg_part;

0 commit comments

Comments
 (0)