]> granicus.if.org Git - postgresql/commitdiff
Fix partial aggregation for variance(int4) and related aggregates.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 21 Jun 2018 20:18:33 +0000 (16:18 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 21 Jun 2018 20:18:39 +0000 (16:18 -0400)
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 9cca11c91).

Discussion: https://postgr.es/m/CAKcux6nU4E2x8nkSBpLOT2DPvQ5LviJ3SGyAN6Sz7qDH4G4+Pw@mail.gmail.com

src/backend/utils/adt/numeric.c
src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql

index 074294cbcc10b0f4bf2ff4225c508f3c7299e454..82a14295ee1f4bd5fc78a6d64553fb718774d3b2 100644 (file)
@@ -4218,8 +4218,8 @@ numeric_poly_combine(PG_FUNCTION_ARGS)
                state1->sumX = state2->sumX;
                state1->sumX2 = state2->sumX2;
 #else
-               accum_sum_copy(&state2->sumX, &state1->sumX);
-               accum_sum_copy(&state2->sumX2, &state1->sumX2);
+               accum_sum_copy(&state1->sumX, &state2->sumX);
+               accum_sum_copy(&state1->sumX2, &state2->sumX2);
 #endif
 
                MemoryContextSwitchTo(old_context);
index f85e9138504a83cc8eb124147525575e0085ea80..10d6bb68240c8c704e19d564029bc6d624e09b70 100644 (file)
@@ -2065,3 +2065,30 @@ SELECT balk(hundred) FROM tenk1;
 (1 row)
 
 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;
index 506d0442d7941b386bea0c070a49ba98177b63d9..1b6db5095605a2f921aa5b630a6d4e316d2f65a1 100644 (file)
@@ -907,3 +907,21 @@ EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1;
 SELECT balk(hundred) FROM tenk1;
 
 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;