Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ec4719c

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 e474c2b commit ec4719c

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
@@ -4218,8 +4218,8 @@ numeric_poly_combine(PG_FUNCTION_ARGS)
42184218
state1->sumX = state2->sumX;
42194219
state1->sumX2 = state2->sumX2;
42204220
#else
4221-
accum_sum_copy(&state2->sumX, &state1->sumX);
4222-
accum_sum_copy(&state2->sumX2, &state1->sumX2);
4221+
accum_sum_copy(&state1->sumX, &state2->sumX);
4222+
accum_sum_copy(&state1->sumX2, &state2->sumX2);
42234223
#endif
42244224

42254225
MemoryContextSwitchTo(old_context);

src/test/regress/expected/aggregates.out

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

20672067
ROLLBACK;
2068+
-- test coverage for aggregate combine/serial/deserial functions
2069+
BEGIN ISOLATION LEVEL REPEATABLE READ;
2070+
SET parallel_setup_cost = 0;
2071+
SET parallel_tuple_cost = 0;
2072+
SET min_parallel_table_scan_size = 0;
2073+
SET max_parallel_workers_per_gather = 4;
2074+
SET enable_indexonlyscan = off;
2075+
-- variance(int4) covers numeric_poly_combine
2076+
-- sum(int8) covers int8_avg_combine
2077+
EXPLAIN (COSTS OFF)
2078+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
2079+
QUERY PLAN
2080+
----------------------------------------------
2081+
Finalize Aggregate
2082+
-> Gather
2083+
Workers Planned: 4
2084+
-> Partial Aggregate
2085+
-> Parallel Seq Scan on tenk1
2086+
(5 rows)
2087+
2088+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
2089+
variance | sum
2090+
----------------------+----------
2091+
8334166.666666666667 | 49995000
2092+
(1 row)
2093+
2094+
ROLLBACK;

src/test/regress/sql/aggregates.sql

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

909909
ROLLBACK;
910+
911+
-- test coverage for aggregate combine/serial/deserial functions
912+
BEGIN ISOLATION LEVEL REPEATABLE READ;
913+
914+
SET parallel_setup_cost = 0;
915+
SET parallel_tuple_cost = 0;
916+
SET min_parallel_table_scan_size = 0;
917+
SET max_parallel_workers_per_gather = 4;
918+
SET enable_indexonlyscan = off;
919+
920+
-- variance(int4) covers numeric_poly_combine
921+
-- sum(int8) covers int8_avg_combine
922+
EXPLAIN (COSTS OFF)
923+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
924+
925+
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
926+
927+
ROLLBACK;

0 commit comments

Comments
 (0)