Commit ec4719cd authored by Tom Lane's avatar Tom Lane

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 9cca11c9).

Discussion: https://postgr.es/m/CAKcux6nU4E2x8nkSBpLOT2DPvQ5LviJ3SGyAN6Sz7qDH4G4+Pw@mail.gmail.com
parent e474c2b7
...@@ -4218,8 +4218,8 @@ numeric_poly_combine(PG_FUNCTION_ARGS) ...@@ -4218,8 +4218,8 @@ numeric_poly_combine(PG_FUNCTION_ARGS)
state1->sumX = state2->sumX; state1->sumX = state2->sumX;
state1->sumX2 = state2->sumX2; state1->sumX2 = state2->sumX2;
#else #else
accum_sum_copy(&state2->sumX, &state1->sumX); accum_sum_copy(&state1->sumX, &state2->sumX);
accum_sum_copy(&state2->sumX2, &state1->sumX2); accum_sum_copy(&state1->sumX2, &state2->sumX2);
#endif #endif
MemoryContextSwitchTo(old_context); MemoryContextSwitchTo(old_context);
......
...@@ -2065,3 +2065,30 @@ SELECT balk(hundred) FROM tenk1; ...@@ -2065,3 +2065,30 @@ SELECT balk(hundred) FROM tenk1;
(1 row) (1 row)
ROLLBACK; ROLLBACK;
-- test coverage for aggregate combine/serial/deserial functions
BEGIN ISOLATION LEVEL REPEATABLE READ;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET max_parallel_workers_per_gather = 4;
SET enable_indexonlyscan = off;
-- variance(int4) covers numeric_poly_combine
-- sum(int8) covers int8_avg_combine
EXPLAIN (COSTS OFF)
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
QUERY PLAN
----------------------------------------------
Finalize Aggregate
-> Gather
Workers Planned: 4
-> Partial Aggregate
-> Parallel Seq Scan on tenk1
(5 rows)
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
variance | sum
----------------------+----------
8334166.666666666667 | 49995000
(1 row)
ROLLBACK;
...@@ -907,3 +907,21 @@ EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1; ...@@ -907,3 +907,21 @@ EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1;
SELECT balk(hundred) FROM tenk1; SELECT balk(hundred) FROM tenk1;
ROLLBACK; ROLLBACK;
-- test coverage for aggregate combine/serial/deserial functions
BEGIN ISOLATION LEVEL REPEATABLE READ;
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET max_parallel_workers_per_gather = 4;
SET enable_indexonlyscan = off;
-- variance(int4) covers numeric_poly_combine
-- sum(int8) covers int8_avg_combine
EXPLAIN (COSTS OFF)
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
ROLLBACK;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment