Commit 9cca11c9 authored by Heikki Linnakangas's avatar Heikki Linnakangas

Speed up SUM calculation in numeric aggregates.

This introduces a numeric sum accumulator, which performs better than
repeatedly calling add_var(). The performance comes from using wider digits
and delaying carry propagation, tallying positive and negative values
separately, and avoiding a round of palloc/pfree on every value. This
speeds up SUM(), as well as other standard aggregates like AVG() and
STDDEV() that also calculate a sum internally.

Reviewed-by: Andrey Borodin
Discussion: <c0545351-a467-5b76-6d46-4840d1ea8aa4@iki.fi>
parent 9f85784c
This diff is collapsed.
......@@ -1909,3 +1909,19 @@ select scale(-13.000000000000000);
15
(1 row)
--
-- Tests for SUM()
--
-- cases that need carry propagation
SELECT SUM(9999::numeric) FROM generate_series(1, 100000);
sum
-----------
999900000
(1 row)
SELECT SUM((-9999)::numeric) FROM generate_series(1, 100000);
sum
------------
-999900000
(1 row)
......@@ -997,3 +997,11 @@ select scale(1.12345);
select scale(110123.12475871856128);
select scale(-1123.12471856128);
select scale(-13.000000000000000);
--
-- Tests for SUM()
--
-- cases that need carry propagation
SELECT SUM(9999::numeric) FROM generate_series(1, 100000);
SELECT SUM((-9999)::numeric) FROM generate_series(1, 100000);
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