Skip to content

Commit b8a1d03

Browse files
committed
Fix partial aggregation for variance(int4) and related aggregates.
A typo in numeric_poly_combine caused bogus results for queries using it, but of course would only manifest if parallel aggregation is performed. Reported by Rajkumar Raghuwanshi. David Rowley did the diagnosis and the fix; I editorialized rather heavily on his regression test additions. Back-patch to v10 where the breakage was introduced (by 9cca11c). Discussion: https://postgr.es/m/CAKcux6nU4E2x8nkSBpLOT2DPvQ5LviJ3SGyAN6Sz7qDH4G4+Pw@mail.gmail.com
1 parent a4c95b0 commit b8a1d03

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,8 +4054,8 @@ numeric_poly_combine(PG_FUNCTION_ARGS)
40544054
state1->sumX = state2->sumX;
40554055
state1->sumX2 = state2->sumX2;
40564056
#else
4057-
accum_sum_copy(&state2->sumX, &state1->sumX);
4058-
accum_sum_copy(&state2->sumX2, &state1->sumX2);
4057+
accum_sum_copy(&state1->sumX, &state2->sumX);
4058+
accum_sum_copy(&state1->sumX2, &state2->sumX2);
40594059
#endif
40604060

40614061
MemoryContextSwitchTo(old_context);

src/test/regress/expected/aggregates.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,3 +2054,30 @@ SELECT balk(hundred) FROM tenk1;
20542054
(1 row)
20552055

20562056
ROLLBACK;
2057+
-- test coverage for aggregate combine/serial/deserial functions
2058+
BEGIN ISOLATION LEVEL REPEATABLE READ;
2059+
SET parallel_setup_cost = 0;
2060+
SET parallel_tuple_cost = 0;
2061+
SET min_parallel_table_scan_size = 0;
2062+
SET max_parallel_workers_per_gather = 4;
2063+
SET enable_indexonlyscan = off;
2064+
-- variance(int4) covers numeric_poly_combine
2065+
-- sum(int8) covers int8_avg_combine
2066+
EXPLAIN (COSTS OFF)
2067+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
2068+
QUERY PLAN
2069+
----------------------------------------------
2070+
Finalize Aggregate
2071+
-> Gather
2072+
Workers Planned: 4
2073+
-> Partial Aggregate
2074+
-> Parallel Seq Scan on tenk1
2075+
(5 rows)
2076+
2077+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
2078+
variance | sum
2079+
----------------------+----------
2080+
8334166.666666666667 | 49995000
2081+
(1 row)
2082+
2083+
ROLLBACK;

src/test/regress/sql/aggregates.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,3 +900,21 @@ EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1;
900900
SELECT balk(hundred) FROM tenk1;
901901

902902
ROLLBACK;
903+
904+
-- test coverage for aggregate combine/serial/deserial functions
905+
BEGIN ISOLATION LEVEL REPEATABLE READ;
906+
907+
SET parallel_setup_cost = 0;
908+
SET parallel_tuple_cost = 0;
909+
SET min_parallel_table_scan_size = 0;
910+
SET max_parallel_workers_per_gather = 4;
911+
SET enable_indexonlyscan = off;
912+
913+
-- variance(int4) covers numeric_poly_combine
914+
-- sum(int8) covers int8_avg_combine
915+
EXPLAIN (COSTS OFF)
916+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
917+
918+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
919+
920+
ROLLBACK;

0 commit comments

Comments
 (0)