Skip to content

Commit d2ad729

Browse files
akuzmkpan2034
authored andcommitted
Remove outdated workaround from columnar sort planning (timescale#9809)
We used to have a workaround there that added cost to account for projection needed by Sort node. It proved to be more harmful than useful, so it was removed, but some code was left over. Remove it too. The main part removed here: timescale#8416 This leads to some 6x speedups on some queries to partially compressed tables.
1 parent 5da1553 commit d2ad729

15 files changed

Lines changed: 691 additions & 980 deletions

tsl/src/nodes/columnar_scan/columnar_scan.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,15 +1733,6 @@ build_on_single_compressed_path(PlannerInfo *root, const Chunk *chunk, RelOptInf
17331733
continue;
17341734
}
17351735

1736-
if (decompression_path == chunk_path_no_sort)
1737-
{
1738-
/*
1739-
* We can't use the unsorted decompression path directly because it
1740-
* doesn't have the sort projection cost workaround.
1741-
*/
1742-
continue;
1743-
}
1744-
17451736
if (!bms_is_empty(chunk_rel->lateral_relids) || !bms_is_empty(req_outer))
17461737
{
17471738
/*
@@ -1755,10 +1746,7 @@ build_on_single_compressed_path(PlannerInfo *root, const Chunk *chunk, RelOptInf
17551746
/*
17561747
* We have to remove the explicit Sort, otherwise it will lead to
17571748
* planning time regression because of double call of
1758-
* prepare_sort_from_pathkeys() in MergeAppend plan creation. Still,
1759-
* we have to use the copy of ColumnarScan path that we created
1760-
* for explicit sorting, because it has the sort projection cost
1761-
* workaround.
1749+
* prepare_sort_from_pathkeys() in MergeAppend plan creation.
17621750
*/
17631751
decompression_path = castNode(SortPath, decompression_path)->subpath;
17641752
}

tsl/test/expected/columnar_index_scan-16.out

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,11 +2010,13 @@ ORDER BY device;
20102010
:PREFIX SELECT count(DISTINCT device) FROM metrics;
20112011
--- QUERY PLAN ---
20122012
Aggregate
2013-
-> Sort
2013+
-> Merge Append
20142014
Sort Key: metrics.device
2015-
-> Append
2015+
-> Custom Scan (SkipScan) on _hyper_1_1_chunk
20162016
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk
2017-
-> Seq Scan on compress_hyper_2_2_chunk
2017+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_v2_last_tim_idx on compress_hyper_2_2_chunk
2018+
-> Sort
2019+
Sort Key: _hyper_1_1_chunk.device
20182020
-> Seq Scan on _hyper_1_1_chunk
20192021

20202022
:PREFIX SELECT first(DISTINCT value3, time) FROM metrics;
@@ -2462,19 +2464,15 @@ SET enable_partitionwise_aggregate = off;
24622464
-> Merge Append
24632465
Sort Key: m1.device
24642466
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk m1_1
2465-
-> Sort
2466-
Sort Key: compress_hyper_2_2_chunk.device
2467-
-> Seq Scan on compress_hyper_2_2_chunk
2467+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_v2_last_tim_idx on compress_hyper_2_2_chunk
24682468
-> Sort
24692469
Sort Key: m1_1.device
24702470
-> Seq Scan on _hyper_1_1_chunk m1_1
24712471
-> Materialize
24722472
-> Merge Append
24732473
Sort Key: m2.device
24742474
-> Custom Scan (ColumnarScan) on _hyper_1_1_chunk m2_1
2475-
-> Sort
2476-
Sort Key: compress_hyper_2_2_chunk_1.device
2477-
-> Seq Scan on compress_hyper_2_2_chunk compress_hyper_2_2_chunk_1
2475+
-> Index Scan using compress_hyper_2_2_chunk_device_sensor__ts_meta_v2_last_tim_idx on compress_hyper_2_2_chunk compress_hyper_2_2_chunk_1
24782476
-> Sort
24792477
Sort Key: m2_1.device
24802478
-> Seq Scan on _hyper_1_1_chunk m2_1

tsl/test/expected/columnar_index_scan-17.out

Lines changed: 224 additions & 302 deletions
Large diffs are not rendered by default.

tsl/test/expected/columnar_index_scan-18.out

Lines changed: 224 additions & 302 deletions
Large diffs are not rendered by default.

tsl/test/expected/compression_ddl.out

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,16 +1315,15 @@ ORDER BY device_id;
13151315
--- QUERY PLAN ---
13161316
Finalize GroupAggregate
13171317
Group Key: compression_insert.device_id
1318-
-> Sort
1318+
-> Merge Append
13191319
Sort Key: compression_insert.device_id
1320-
-> Append
1321-
-> Partial GroupAggregate
1322-
Group Key: _hyper_31_110_chunk.device_id
1323-
-> Custom Scan (ColumnarIndexScan) on _hyper_31_110_chunk
1324-
-> Index Scan using compress_hyper_32_111_chunk_device_id__ts_meta_v2_last_time_idx on compress_hyper_32_111_chunk
1325-
-> Partial GroupAggregate
1326-
Group Key: _hyper_31_110_chunk.device_id
1327-
-> Index Only Scan using _hyper_31_110_chunk_compression_insert_device_id_time_idx on _hyper_31_110_chunk
1320+
-> Partial GroupAggregate
1321+
Group Key: _hyper_31_110_chunk.device_id
1322+
-> Custom Scan (ColumnarIndexScan) on _hyper_31_110_chunk
1323+
-> Index Scan using compress_hyper_32_111_chunk_device_id__ts_meta_v2_last_time_idx on compress_hyper_32_111_chunk
1324+
-> Partial GroupAggregate
1325+
Group Key: _hyper_31_110_chunk.device_id
1326+
-> Index Only Scan using _hyper_31_110_chunk_compression_insert_device_id_time_idx on _hyper_31_110_chunk
13281327

13291328
SELECT device_id, count(*)
13301329
FROM compression_insert

tsl/test/expected/compression_update_delete-16.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,23 +2625,23 @@ EXPLAIN (buffers off, costs off) SELECT * FROM test_partials ORDER BY time;
26252625
Order: test_partials."time"
26262626
-> Merge Append
26272627
Sort Key: test_partials."time"
2628-
-> Custom Scan (ColumnarScan) on _hyper_35_68_chunk
2629-
-> Sort
2630-
Sort Key: compress_hyper_36_71_chunk._ts_meta_v2_last_time, compress_hyper_36_71_chunk._ts_meta_v2_first_time
2628+
-> Sort
2629+
Sort Key: _hyper_35_68_chunk."time"
2630+
-> Custom Scan (ColumnarScan) on _hyper_35_68_chunk
26312631
-> Seq Scan on compress_hyper_36_71_chunk
26322632
-> Index Scan Backward using _hyper_35_68_chunk_test_partials_time_idx on _hyper_35_68_chunk
26332633
-> Merge Append
26342634
Sort Key: test_partials."time"
2635-
-> Custom Scan (ColumnarScan) on _hyper_35_69_chunk
2636-
-> Sort
2637-
Sort Key: compress_hyper_36_72_chunk._ts_meta_v2_last_time, compress_hyper_36_72_chunk._ts_meta_v2_first_time
2635+
-> Sort
2636+
Sort Key: _hyper_35_69_chunk."time"
2637+
-> Custom Scan (ColumnarScan) on _hyper_35_69_chunk
26382638
-> Seq Scan on compress_hyper_36_72_chunk
26392639
-> Index Scan Backward using _hyper_35_69_chunk_test_partials_time_idx on _hyper_35_69_chunk
26402640
-> Merge Append
26412641
Sort Key: test_partials."time"
2642-
-> Custom Scan (ColumnarScan) on _hyper_35_70_chunk
2643-
-> Sort
2644-
Sort Key: compress_hyper_36_73_chunk._ts_meta_v2_last_time, compress_hyper_36_73_chunk._ts_meta_v2_first_time
2642+
-> Sort
2643+
Sort Key: _hyper_35_70_chunk."time"
2644+
-> Custom Scan (ColumnarScan) on _hyper_35_70_chunk
26452645
-> Seq Scan on compress_hyper_36_73_chunk
26462646
-> Index Scan Backward using _hyper_35_70_chunk_test_partials_time_idx on _hyper_35_70_chunk
26472647

tsl/test/expected/recompress_chunk_segmentwise.out

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,10 @@ INFO: using tuplesort to scan rows from "_hyper_9_10_chunk" for converting to c
313313

314314
INSERT INTO mytab_prep VALUES ('2023-01-01'::timestamptz, 2, 3, 2);
315315
VACUUM ANALYZE mytab_prep;
316-
-- plan should be invalidated to return results from the uncompressed chunk also
317-
set enable_sort to off; /* penalize MergeAppend for predictable plans on PG < 17. */
316+
-- Plan should be invalidated to return results from the uncompressed chunk as well.
317+
-- Penalize MergeAppend for predictable plans on PG < 17.
318+
set enable_sort to off;
319+
set enable_indexscan to off;
318320
EXPLAIN (BUFFERS OFF, COSTS OFF) EXECUTE p1;
319321
--- QUERY PLAN ---
320322
Sort
@@ -332,6 +334,7 @@ EXECUTE p1;
332334
Sun Jan 01 00:00:00 2023 PST | 2 | 3 | 2
333335

334336
reset enable_sort;
337+
reset enable_indexscan;
335338
-- check plan again after recompression
336339
SELECT compress_chunk(:'chunk_to_compress_prep');
337340
INFO: Using index "compress_hyper_10_11_chunk_a_c__ts_meta_v2_last_time__ts_me_idx" for recompression

tsl/test/shared/expected/bulk_decompression_limit.out

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,9 @@ OFFSET 5;
6565
Chunk Status: PARTIAL
6666
Reverse: true
6767
Bulk Decompression: true
68-
-> Sort (actual rows=2.00 loops=1)
68+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
6969
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
70-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
71-
Sort Method: quicksort
72-
-> Seq Scan on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
73-
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
74-
Filter: (compress_hyper_X_X_chunk.device_id = 1)
75-
Rows Removed by Filter: 16
70+
Index Cond: (compress_hyper_X_X_chunk.device_id = 1)
7671
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _timescaledb_internal._hyper_X_X_chunk (actual rows=1598.00 loops=1)
7772
Output: _hyper_X_X_chunk."time", _hyper_X_X_chunk.device_id, _hyper_X_X_chunk.v0
7873
Filter: (_hyper_X_X_chunk.device_id = 1)
@@ -150,14 +145,9 @@ OFFSET 0;
150145
Chunk Status: PARTIAL
151146
Reverse: true
152147
Bulk Decompression: true
153-
-> Sort (actual rows=2.00 loops=1)
148+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
154149
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
155-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
156-
Sort Method: quicksort
157-
-> Seq Scan on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
158-
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
159-
Filter: (compress_hyper_X_X_chunk.device_id = 1)
160-
Rows Removed by Filter: 16
150+
Index Cond: (compress_hyper_X_X_chunk.device_id = 1)
161151
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _timescaledb_internal._hyper_X_X_chunk (actual rows=1598.00 loops=1)
162152
Output: _hyper_X_X_chunk."time", _hyper_X_X_chunk.device_id, _hyper_X_X_chunk.v0
163153
Filter: (_hyper_X_X_chunk.device_id = 1)
@@ -196,14 +186,9 @@ LIMIT 10000;
196186
Chunk Status: PARTIAL
197187
Reverse: true
198188
Bulk Decompression: true
199-
-> Sort (actual rows=2.00 loops=1)
189+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
200190
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
201-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
202-
Sort Method: quicksort
203-
-> Seq Scan on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
204-
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
205-
Filter: (compress_hyper_X_X_chunk.device_id = 1)
206-
Rows Removed by Filter: 16
191+
Index Cond: (compress_hyper_X_X_chunk.device_id = 1)
207192
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _timescaledb_internal._hyper_X_X_chunk (actual rows=1598.00 loops=1)
208193
Output: _hyper_X_X_chunk."time", _hyper_X_X_chunk.device_id, _hyper_X_X_chunk.v0
209194
Filter: (_hyper_X_X_chunk.device_id = 1)
@@ -242,14 +227,9 @@ OFFSET 10000;
242227
Chunk Status: PARTIAL
243228
Reverse: true
244229
Bulk Decompression: true
245-
-> Sort (actual rows=2.00 loops=1)
230+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
246231
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
247-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
248-
Sort Method: quicksort
249-
-> Seq Scan on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
250-
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
251-
Filter: (compress_hyper_X_X_chunk.device_id = 1)
252-
Rows Removed by Filter: 16
232+
Index Cond: (compress_hyper_X_X_chunk.device_id = 1)
253233
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _timescaledb_internal._hyper_X_X_chunk (actual rows=1598.00 loops=1)
254234
Output: _hyper_X_X_chunk."time", _hyper_X_X_chunk.device_id, _hyper_X_X_chunk.v0
255235
Filter: (_hyper_X_X_chunk.device_id = 1)
@@ -288,14 +268,9 @@ LIMIT 100 OFFSET 100;
288268
Chunk Status: PARTIAL
289269
Reverse: true
290270
Bulk Decompression: true
291-
-> Sort (actual rows=1.00 loops=1)
271+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=1.00 loops=1)
292272
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
293-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
294-
Sort Method: quicksort
295-
-> Seq Scan on _timescaledb_internal.compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
296-
Output: compress_hyper_X_X_chunk."time", compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time, compress_hyper_X_X_chunk.device_id, compress_hyper_X_X_chunk.v0, compress_hyper_X_X_chunk._ts_meta_count
297-
Filter: (compress_hyper_X_X_chunk.device_id = 1)
298-
Rows Removed by Filter: 16
273+
Index Cond: (compress_hyper_X_X_chunk.device_id = 1)
299274
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _timescaledb_internal._hyper_X_X_chunk (actual rows=200.00 loops=1)
300275
Output: _hyper_X_X_chunk."time", _hyper_X_X_chunk.device_id, _hyper_X_X_chunk.v0
301276
Filter: (_hyper_X_X_chunk.device_id = 1)

tsl/test/shared/expected/ordered_append_join-16.out

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,12 +1928,8 @@ ORDER BY o1.time;
19281928
-> Merge Append (actual rows=3598.00 loops=1)
19291929
Sort Key: o2."time"
19301930
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o2_1 (actual rows=3598.00 loops=1)
1931-
-> Sort (actual rows=4.00 loops=1)
1932-
Sort Key: compress_hyper_X_X_chunk_1._ts_meta_v2_last_time, compress_hyper_X_X_chunk_1._ts_meta_v2_first_time
1933-
Sort Method: quicksort
1934-
-> Seq Scan on compress_hyper_X_X_chunk compress_hyper_X_X_chunk_1 (actual rows=4.00 loops=1)
1935-
Filter: (device_id = 2)
1936-
Rows Removed by Filter: 14
1931+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on compress_hyper_X_X_chunk compress_hyper_X_X_chunk_1 (actual rows=4.00 loops=1)
1932+
Index Cond: (device_id = 2)
19371933
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_device_id_time_idx on _hyper_X_X_chunk o2_1 (actual rows=0.00 loops=1)
19381934
Index Cond: (device_id = 2)
19391935
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o2_2 (actual rows=5038.00 loops=1)
@@ -1948,12 +1944,8 @@ ORDER BY o1.time;
19481944
-> Merge Append (actual rows=3598.00 loops=1)
19491945
Sort Key: o1."time"
19501946
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o1_1 (actual rows=2000.00 loops=1)
1951-
-> Sort (actual rows=2.00 loops=1)
1952-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
1953-
Sort Method: quicksort
1954-
-> Seq Scan on compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
1955-
Filter: (device_id = 1)
1956-
Rows Removed by Filter: 16
1947+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
1948+
Index Cond: (device_id = 1)
19571949
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _hyper_X_X_chunk o1_1 (actual rows=1598.00 loops=1)
19581950
Filter: (device_id = 1)
19591951
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o1_2 (actual rows=5038.00 loops=1)
@@ -2038,12 +2030,8 @@ ORDER BY time;
20382030
-> Merge Append (actual rows=3598.00 loops=1)
20392031
Sort Key: o1."time"
20402032
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o1_1 (actual rows=2000.00 loops=1)
2041-
-> Sort (actual rows=2.00 loops=1)
2042-
Sort Key: compress_hyper_X_X_chunk._ts_meta_v2_last_time, compress_hyper_X_X_chunk._ts_meta_v2_first_time
2043-
Sort Method: quicksort
2044-
-> Seq Scan on compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
2045-
Filter: (device_id = 1)
2046-
Rows Removed by Filter: 16
2033+
-> Index Scan Backward using compress_hyper_X_X_chunk_device_id__ts_meta_v2_last_time___idx on compress_hyper_X_X_chunk (actual rows=2.00 loops=1)
2034+
Index Cond: (device_id = 1)
20472035
-> Index Scan using _hyper_X_X_chunk_metrics_compressed_time_idx on _hyper_X_X_chunk o1_1 (actual rows=1598.00 loops=1)
20482036
Filter: (device_id = 1)
20492037
-> Custom Scan (ColumnarScan) on _hyper_X_X_chunk o1_2 (actual rows=5038.00 loops=1)

0 commit comments

Comments
 (0)