Skip to content

Commit 5d90853

Browse files
authored
Account for CPU tuple cost in DecompressChunk (#7551)
It was always zero due to a typo.
1 parent 317cd14 commit 5d90853

24 files changed

+1071
-588
lines changed

tsl/src/nodes/decompress_chunk/decompress_chunk.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,14 @@ static void
443443
cost_decompress_chunk(PlannerInfo *root, Path *path, Path *compressed_path)
444444
{
445445
/* startup_cost is cost before fetching first tuple */
446-
if (compressed_path->rows > 0)
447-
path->startup_cost = compressed_path->total_cost / compressed_path->rows;
446+
const double compressed_rows = Max(1, compressed_path->rows);
447+
path->startup_cost =
448+
compressed_path->startup_cost +
449+
(compressed_path->total_cost - compressed_path->startup_cost) / compressed_rows;
448450

449451
/* total_cost is cost for fetching all tuples */
450-
path->total_cost = compressed_path->total_cost + path->rows * cpu_tuple_cost;
451452
path->rows = compressed_path->rows * TARGET_COMPRESSED_BATCH_SIZE;
453+
path->total_cost = compressed_path->total_cost + path->rows * cpu_tuple_cost;
452454
}
453455

454456
/* Smoothstep function S1 (the h01 cubic Hermite spline). */

tsl/test/expected/compress_auto_sparse_index.out

+35-35
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ select count(compress_chunk(x)) from show_chunks('sparse') x;
2323
1
2424
(1 row)
2525

26-
explain select * from sparse where value = 1;
26+
explain (costs off) select * from sparse where value = 1;
2727
QUERY PLAN
2828
-------------------------------------------------------------------------------------------------------------------------
29-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.27..20.20 rows=76000 width=12)
29+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
3030
Vectorized Filter: (value = '1'::double precision)
31-
-> Seq Scan on compress_hyper_2_2_chunk (cost=0.00..20.20 rows=76 width=76)
31+
-> Seq Scan on compress_hyper_2_2_chunk
3232
Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))
3333
(4 rows)
3434

@@ -40,12 +40,12 @@ select count(compress_chunk(decompress_chunk(x))) from show_chunks('sparse') x;
4040
1
4141
(1 row)
4242

43-
explain select * from sparse where value = 1;
44-
QUERY PLAN
45-
--------------------------------------------------------------------------------------------
46-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.02..17.80 rows=780000 width=12)
43+
explain (costs off) select * from sparse where value = 1;
44+
QUERY PLAN
45+
------------------------------------------------------
46+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
4747
Vectorized Filter: (value = '1'::double precision)
48-
-> Seq Scan on compress_hyper_2_3_chunk (cost=0.00..17.80 rows=780 width=76)
48+
-> Seq Scan on compress_hyper_2_3_chunk
4949
(3 rows)
5050

5151
reset timescaledb.auto_sparse_indexes;
@@ -55,33 +55,33 @@ select count(compress_chunk(decompress_chunk(x))) from show_chunks('sparse') x;
5555
1
5656
(1 row)
5757

58-
explain select * from sparse where value = 1;
58+
explain (costs off) select * from sparse where value = 1;
5959
QUERY PLAN
6060
-------------------------------------------------------------------------------------------------------------------------
61-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.27..20.20 rows=76000 width=12)
61+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
6262
Vectorized Filter: (value = '1'::double precision)
63-
-> Seq Scan on compress_hyper_2_4_chunk (cost=0.00..20.20 rows=76 width=76)
63+
-> Seq Scan on compress_hyper_2_4_chunk
6464
Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))
6565
(4 rows)
6666

6767
-- Should survive renames.
6868
alter table sparse rename column value to wert;
69-
explain select * from sparse where wert = 1;
69+
explain (costs off) select * from sparse where wert = 1;
7070
QUERY PLAN
7171
-----------------------------------------------------------------------------------------------------------------------
72-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.27..20.20 rows=76000 width=12)
72+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
7373
Vectorized Filter: (wert = '1'::double precision)
74-
-> Seq Scan on compress_hyper_2_4_chunk (cost=0.00..20.20 rows=76 width=76)
74+
-> Seq Scan on compress_hyper_2_4_chunk
7575
Filter: ((_ts_meta_v2_min_wert <= '1'::double precision) AND (_ts_meta_v2_max_wert >= '1'::double precision))
7676
(4 rows)
7777

7878
alter table sparse rename column wert to value;
79-
explain select * from sparse where value = 1;
79+
explain (costs off) select * from sparse where value = 1;
8080
QUERY PLAN
8181
-------------------------------------------------------------------------------------------------------------------------
82-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.27..20.20 rows=76000 width=12)
82+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
8383
Vectorized Filter: (value = '1'::double precision)
84-
-> Seq Scan on compress_hyper_2_4_chunk (cost=0.00..20.20 rows=76 width=76)
84+
-> Seq Scan on compress_hyper_2_4_chunk
8585
Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))
8686
(4 rows)
8787

@@ -94,12 +94,12 @@ select count(compress_chunk(decompress_chunk(x))) from show_chunks('sparse') x;
9494
1
9595
(1 row)
9696

97-
explain select * from sparse where value = 1;
98-
QUERY PLAN
99-
--------------------------------------------------------------------------------------------
100-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.02..17.80 rows=780000 width=12)
97+
explain (costs off) select * from sparse where value = 1;
98+
QUERY PLAN
99+
------------------------------------------------------
100+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
101101
Vectorized Filter: (value = '1'::double precision)
102-
-> Seq Scan on compress_hyper_2_5_chunk (cost=0.00..17.80 rows=780 width=76)
102+
-> Seq Scan on compress_hyper_2_5_chunk
103103
(3 rows)
104104

105105
-- Not for other index types.
@@ -111,12 +111,12 @@ select count(compress_chunk(decompress_chunk(x))) from show_chunks('sparse') x;
111111
1
112112
(1 row)
113113

114-
explain select * from sparse where value = 1;
115-
QUERY PLAN
116-
--------------------------------------------------------------------------------------------
117-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.02..17.80 rows=780000 width=12)
114+
explain (costs off) select * from sparse where value = 1;
115+
QUERY PLAN
116+
------------------------------------------------------
117+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
118118
Vectorized Filter: (value = '1'::double precision)
119-
-> Seq Scan on compress_hyper_2_6_chunk (cost=0.00..17.80 rows=780 width=76)
119+
-> Seq Scan on compress_hyper_2_6_chunk
120120
(3 rows)
121121

122122
-- When the chunk is recompressed without index, no sparse index is created.
@@ -127,12 +127,12 @@ select count(compress_chunk(decompress_chunk(x))) from show_chunks('sparse') x;
127127
1
128128
(1 row)
129129

130-
explain select * from sparse where value = 1;
131-
QUERY PLAN
132-
--------------------------------------------------------------------------------------------
133-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=0.02..17.80 rows=780000 width=12)
130+
explain (costs off) select * from sparse where value = 1;
131+
QUERY PLAN
132+
------------------------------------------------------
133+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
134134
Vectorized Filter: (value = '1'::double precision)
135-
-> Seq Scan on compress_hyper_2_7_chunk (cost=0.00..17.80 rows=780 width=76)
135+
-> Seq Scan on compress_hyper_2_7_chunk
136136
(3 rows)
137137

138138
-- Long column names.
@@ -149,12 +149,12 @@ select count(compress_chunk(x)) from show_chunks('sparse') x;
149149
1
150150
(1 row)
151151

152-
explain select * from sparse where Abcdef012345678_Bbcdef012345678_Cbcdef012345678_Dbcdef0 = 1;
152+
explain (costs off) select * from sparse where Abcdef012345678_Bbcdef012345678_Cbcdef012345678_Dbcdef0 = 1;
153153
QUERY PLAN
154154
---------------------------------------------------------------------------------------------------------------------------------------------------------------
155-
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (cost=3.48..10.45 rows=3000 width=264)
155+
Custom Scan (DecompressChunk) on _hyper_1_1_chunk
156156
Vectorized Filter: (abcdef012345678_bbcdef012345678_cbcdef012345678_dbcdef0 = 1)
157-
-> Seq Scan on compress_hyper_2_8_chunk (cost=0.00..10.45 rows=3 width=2092)
157+
-> Seq Scan on compress_hyper_2_8_chunk
158158
Filter: ((_ts_meta_v2_min_9218_abcdef012345678_bbcdef012345678_cbcdef0 <= 1) AND (_ts_meta_v2_max_9218_abcdef012345678_bbcdef012345678_cbcdef0 >= 1))
159159
(4 rows)
160160

tsl/test/expected/compression.out

+7-7
Original file line numberDiff line numberDiff line change
@@ -1863,17 +1863,17 @@ SELECT sum(cpu) FROM f_sensor_data;
18631863
Output: (PARTIAL sum(_hyper_37_73_chunk.cpu))
18641864
Workers Planned: 4
18651865
-> Parallel Append
1866-
-> Partial Aggregate
1867-
Output: PARTIAL sum(_hyper_37_73_chunk.cpu)
1868-
-> Parallel Seq Scan on _timescaledb_internal._hyper_37_73_chunk
1869-
Output: _hyper_37_73_chunk.cpu
18701866
-> Custom Scan (VectorAgg)
18711867
Output: (PARTIAL sum(_hyper_37_73_chunk.cpu))
18721868
Grouping Policy: all compressed batches
18731869
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_37_73_chunk
18741870
Output: _hyper_37_73_chunk.cpu
18751871
-> Parallel Seq Scan on _timescaledb_internal.compress_hyper_38_74_chunk
18761872
Output: compress_hyper_38_74_chunk._ts_meta_count, compress_hyper_38_74_chunk.sensor_id, compress_hyper_38_74_chunk._ts_meta_min_1, compress_hyper_38_74_chunk._ts_meta_max_1, compress_hyper_38_74_chunk."time", compress_hyper_38_74_chunk.cpu, compress_hyper_38_74_chunk.temperature
1873+
-> Partial Aggregate
1874+
Output: PARTIAL sum(_hyper_37_73_chunk.cpu)
1875+
-> Parallel Seq Scan on _timescaledb_internal._hyper_37_73_chunk
1876+
Output: _hyper_37_73_chunk.cpu
18771877
(17 rows)
18781878

18791879
:explain
@@ -1884,15 +1884,15 @@ SELECT * FROM f_sensor_data WHERE sensor_id > 100;
18841884
Output: _hyper_37_73_chunk."time", _hyper_37_73_chunk.sensor_id, _hyper_37_73_chunk.cpu, _hyper_37_73_chunk.temperature
18851885
Workers Planned: 3
18861886
-> Parallel Append
1887-
-> Parallel Index Scan using _hyper_37_73_chunk_f_sensor_data_time_sensor_id_idx on _timescaledb_internal._hyper_37_73_chunk
1888-
Output: _hyper_37_73_chunk."time", _hyper_37_73_chunk.sensor_id, _hyper_37_73_chunk.cpu, _hyper_37_73_chunk.temperature
1889-
Index Cond: (_hyper_37_73_chunk.sensor_id > 100)
18901887
-> Custom Scan (DecompressChunk) on _timescaledb_internal._hyper_37_73_chunk
18911888
Output: _hyper_37_73_chunk."time", _hyper_37_73_chunk.sensor_id, _hyper_37_73_chunk.cpu, _hyper_37_73_chunk.temperature
18921889
Filter: (_hyper_37_73_chunk.sensor_id > 100)
18931890
-> Parallel Index Scan using compress_hyper_38_74_chunk_sensor_id__ts_meta_min_1__ts_met_idx on _timescaledb_internal.compress_hyper_38_74_chunk
18941891
Output: compress_hyper_38_74_chunk._ts_meta_count, compress_hyper_38_74_chunk.sensor_id, compress_hyper_38_74_chunk._ts_meta_min_1, compress_hyper_38_74_chunk._ts_meta_max_1, compress_hyper_38_74_chunk."time", compress_hyper_38_74_chunk.cpu, compress_hyper_38_74_chunk.temperature
18951892
Index Cond: (compress_hyper_38_74_chunk.sensor_id > 100)
1893+
-> Parallel Index Scan using _hyper_37_73_chunk_f_sensor_data_time_sensor_id_idx on _timescaledb_internal._hyper_37_73_chunk
1894+
Output: _hyper_37_73_chunk."time", _hyper_37_73_chunk.sensor_id, _hyper_37_73_chunk.cpu, _hyper_37_73_chunk.temperature
1895+
Index Cond: (_hyper_37_73_chunk.sensor_id > 100)
18961896
(13 rows)
18971897

18981898
-- Test non-partial paths below append are not executed multiple times

0 commit comments

Comments
 (0)