Skip to content

Commit

Permalink
Do not produce partial aggregation plans for the only chunk
Browse files Browse the repository at this point in the history
It doesn't make much sense.
  • Loading branch information
akuzm committed Dec 14, 2023
1 parent e0a3e30 commit 9a4eb12
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 193 deletions.
17 changes: 17 additions & 0 deletions src/planner/partialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,15 @@ generate_agg_pushdown_path(PlannerInfo *root, Path *cheapest_total_path, RelOptI
if (subpaths == NIL)
return;

if (list_length(subpaths) < 2)
{
/*
* Doesn't make sense to add per-chunk aggregation paths if there's
* only one chunk.
*/
return;
}

/* Generate agg paths on top of the append children */
List *sorted_subpaths = NIL;
List *hashed_subpaths = NIL;
Expand Down Expand Up @@ -580,6 +589,14 @@ generate_partial_agg_pushdown_path(PlannerInfo *root, Path *cheapest_partial_pat
if (subpaths == NIL)
return;

if (list_length(subpaths) < 2)
{
/*
* Doesn't make sense to add per-chunk aggregation paths if there's
* only one chunk.
*/
return;
}
/* Generate agg paths on top of the append children */
ListCell *lc;
List *sorted_subpaths = NIL;
Expand Down
130 changes: 50 additions & 80 deletions test/expected/agg_bookends.out
Original file line number Diff line number Diff line change
Expand Up @@ -77,116 +77,92 @@ SELECT setting, current_setting(setting) AS value from (VALUES ('timescaledb.ena
(5 rows)

:PREFIX SELECT last(temp, time_alt) FROM btest;
QUERY PLAN
------------------------------------------------------------------
Finalize HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Partial HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(5 rows)
QUERY PLAN
------------------------------------------------------------
Aggregate (actual rows=1 loops=1)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(2 rows)

:PREFIX SELECT first(temp, time_alt) FROM btest;
QUERY PLAN
------------------------------------------------------------------
Finalize HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Partial HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(5 rows)
QUERY PLAN
------------------------------------------------------------
Aggregate (actual rows=1 loops=1)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(2 rows)

:PREFIX SELECT gp, last(temp, time) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

:PREFIX SELECT gp, first(temp, time) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

--check whole row
:PREFIX SELECT gp, first(btest, time) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

--check toasted col
:PREFIX SELECT gp, left(last(strid, time), 10) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

:PREFIX SELECT gp, last(temp, strid) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

:PREFIX SELECT gp, last(strid, temp) FROM btest GROUP BY gp ORDER BY gp;
QUERY PLAN
------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------
Sort (actual rows=2 loops=1)
Sort Key: _hyper_1_1_chunk.gp
Sort Method: quicksort
-> Finalize HashAggregate (actual rows=2 loops=1)
-> HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Partial HashAggregate (actual rows=2 loops=1)
Group Key: _hyper_1_1_chunk.gp
Batches: 1
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(10 rows)
-> Seq Scan on _hyper_1_1_chunk (actual rows=6 loops=1)
(7 rows)

BEGIN;
--check null value as last element
Expand Down Expand Up @@ -849,24 +825,18 @@ INSERT INTO btest_numeric VALUES('2018-01-20T09:00:43', NULL);
(5 rows)

:PREFIX SELECT first(time, quantity) FROM btest_numeric;
QUERY PLAN
------------------------------------------------------------------
Finalize HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Partial HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Seq Scan on _hyper_2_8_chunk (actual rows=2 loops=1)
(5 rows)
QUERY PLAN
------------------------------------------------------------
Aggregate (actual rows=1 loops=1)
-> Seq Scan on _hyper_2_8_chunk (actual rows=2 loops=1)
(2 rows)

:PREFIX SELECT last(time, quantity) FROM btest_numeric;
QUERY PLAN
------------------------------------------------------------------
Finalize HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Partial HashAggregate (actual rows=1 loops=1)
Batches: 1
-> Seq Scan on _hyper_2_8_chunk (actual rows=2 loops=1)
(5 rows)
QUERY PLAN
------------------------------------------------------------
Aggregate (actual rows=1 loops=1)
-> Seq Scan on _hyper_2_8_chunk (actual rows=2 loops=1)
(2 rows)

-- NULL values followed by non-NULL values
INSERT INTO btest_numeric VALUES('2019-01-20T09:00:43', 1);
Expand Down
10 changes: 5 additions & 5 deletions test/expected/parallel-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ SET max_parallel_workers_per_gather TO 2;
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Partial Aggregate
-> Partial Aggregate
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Seq Scan on _hyper_1_2_chunk
Expand Down
10 changes: 5 additions & 5 deletions test/expected/parallel-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ SET max_parallel_workers_per_gather TO 2;
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Partial Aggregate
-> Partial Aggregate
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Seq Scan on _hyper_1_2_chunk
Expand Down
10 changes: 5 additions & 5 deletions test/expected/parallel-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,11 @@ SET max_parallel_workers_per_gather TO 2;
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Partial Aggregate
-> Partial Aggregate
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Seq Scan on _hyper_1_2_chunk
Expand Down
10 changes: 5 additions & 5 deletions test/expected/parallel-16.out
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ SET max_parallel_workers_per_gather TO 2;
Finalize Aggregate
-> Gather
Workers Planned: 2
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Partial Aggregate
-> Partial Aggregate
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Custom Scan (ChunkAppend) on test
Chunks excluded during startup: 0
-> Result
One-Time Filter: (length(version()) > 0)
-> Parallel Seq Scan on _hyper_1_2_chunk
Expand Down
17 changes: 7 additions & 10 deletions test/expected/partitionwise-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -889,18 +889,15 @@ FROM hyper_timepart
WHERE device = 1
GROUP BY 1
LIMIT 10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Limit
Output: _hyper_3_8_chunk.device, (avg(_hyper_3_8_chunk.temp))
-> Finalize GroupAggregate
-> GroupAggregate
Output: _hyper_3_8_chunk.device, avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Partial GroupAggregate
Output: _hyper_3_8_chunk.device, PARTIAL avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(11 rows)
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(8 rows)

17 changes: 7 additions & 10 deletions test/expected/partitionwise-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -889,18 +889,15 @@ FROM hyper_timepart
WHERE device = 1
GROUP BY 1
LIMIT 10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Limit
Output: _hyper_3_8_chunk.device, (avg(_hyper_3_8_chunk.temp))
-> Finalize GroupAggregate
-> GroupAggregate
Output: _hyper_3_8_chunk.device, avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Partial GroupAggregate
Output: _hyper_3_8_chunk.device, PARTIAL avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(11 rows)
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(8 rows)

17 changes: 7 additions & 10 deletions test/expected/partitionwise-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -889,18 +889,15 @@ FROM hyper_timepart
WHERE device = 1
GROUP BY 1
LIMIT 10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Limit
Output: _hyper_3_8_chunk.device, (avg(_hyper_3_8_chunk.temp))
-> Finalize GroupAggregate
-> GroupAggregate
Output: _hyper_3_8_chunk.device, avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Partial GroupAggregate
Output: _hyper_3_8_chunk.device, PARTIAL avg(_hyper_3_8_chunk.temp)
Group Key: _hyper_3_8_chunk.device
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(11 rows)
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(8 rows)

16 changes: 7 additions & 9 deletions test/expected/partitionwise-16.out
Original file line number Diff line number Diff line change
Expand Up @@ -889,16 +889,14 @@ FROM hyper_timepart
WHERE device = 1
GROUP BY 1
LIMIT 10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Limit
Output: _hyper_3_8_chunk.device, (avg(_hyper_3_8_chunk.temp))
-> Finalize GroupAggregate
-> GroupAggregate
Output: _hyper_3_8_chunk.device, avg(_hyper_3_8_chunk.temp)
-> Partial GroupAggregate
Output: _hyper_3_8_chunk.device, PARTIAL avg(_hyper_3_8_chunk.temp)
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(9 rows)
-> Index Scan using _hyper_3_8_chunk_hyper_timepart_device_expr_idx on _timescaledb_internal._hyper_3_8_chunk
Output: _hyper_3_8_chunk.device, _hyper_3_8_chunk.temp
Index Cond: (_hyper_3_8_chunk.device = 1)
(7 rows)

Loading

0 comments on commit 9a4eb12

Please sign in to comment.