Improve performance of numeric sum(), avg(), stddev(), variance(), etc

Enterprise / PostgreSQL - Tom Lane [sss.pgh.pa.us] - 16 November 2013 17:46 EST

This patch improves performance of most built-in aggregates that formerly used a NUMERIC or NUMERIC array as their transition type; this includes not only aggregates on numeric inputs, but some aggregates on integer inputs where overflow of an int8 value is a possibility. The code now uses a special-purpose data structure to avoid array construction and deconstruction overhead, as well as packing and unpacking overhead for numeric values.

These aggregates' transition type is now declared as INTERNAL, since it doesn't correspond to any SQL data type. To keep the planner from thinking that that means a lot of storage will be used, we make use of the just-added pg_aggregate.aggtransspace feature. The space estimate is set to 128 bytes, which is at least in the right ballpark.

Hadi Moshayedi, reviewed by Pavel Stehule and Tomas Vondra

69c8fba Improve performance of numeric sum(), avg(), stddev(), variance(), etc.
src/backend/utils/adt/numeric.c | 392 +++++++++++++++++++-----------
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_aggregate.h | 56 ++---
src/include/catalog/pg_proc.h | 24 +-
src/include/utils/builtins.h | 1 +
src/test/regress/expected/aggregates.out | 61 +++++
src/test/regress/sql/aggregates.sql | 12 +
7 files changed, 362 insertions(+), 186 deletions(-)

Upstream: git.postgresql.org


  • Share