pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrei Lepikhov <lepihov@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: Chao Li <li.evan.chao@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: David Rowley <dgrowleyml@gmail.com>
Subject: Re: Make the transition state of avg(int2)/avg(int4)/sum(int2)/sum(int4) internal
Date: Mon, 7 Sep 2026 16:28:16 +0200
Message-ID: <2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com> (raw)
In-Reply-To: <1571559.1788537022@sss.pgh.pa.us>
References: <762e811a-6584-4b03-a6ca-52600adcd6e9@gmail.com>
	<q2ksopavjswuwybvw643w652s374eqv2rnamtelaueyt4nfbqm@5vjzkkuujv33>
	<1571559.1788537022@sss.pgh.pa.us>

On 04/09/2026 17:50, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> writes:
>> Seems like if we do this - and I think we should - we should go broader than
>> just doing this for int8[]. So yea, let's do it for float8 too.
>> I think it might make sense to have an opr_sanity.sql check that verifies that
>> we don't add new builtin aggregates that have an array transition state. Or
>> perhaps even more strictly, test that aggregates either have an internal
>> transition state, or the argument type's (for stuff like min/max).
> 
> On reflection, I'm not sure that such a policy is a win.  If you go
> with an internal-type transition state, then (if you want parallel
> aggregation support) you need serialize/deserialize functions, and
> I think also some other stuff that comes for free if the transition
> state is a real SQL type.  So this is not so much a clear win as
> a tradeoff of which code you want to write.
> 

As I see it, with an internal state, some aggregates gain, while others don't.

Plain sum(int2/int4/float8) already has a trivial 8-byte transition state:
minimal memory, passed by value, no detoast. The moving-aggregate mode of
sum(int2/int4), on the other hand, reuses avg()'s int2/int4_avg_accum_inv
functions, which pay for array overhead and a detoast check on every row.
Internal state might reduce memory consumption there.

float8 has no moving-aggregate mode. Also note that the regression tests
currently call float8_regr_combine() directly, with literal array values.

Not sure how much it matters, but introducing an internal state also means
giving up initcond. Hence, sum(float8) with an internal state would lose its
STRICT marking, pushing NULL-checking into the function body.

Quick tests show sum() losing 1-3% in serial mode and a more significant ~7% in
a parallel GROUP BY, due to serialize/deserialize across workers. This was run
on my laptop, not a dedicated benchmarking box, but the overhead looks clearly
detectable.

Overall, in my opinion, only AVG() and other statistical aggregates, such as
STDDEV(), benefit from moving to internal state. Basic aggregates like sum()
should probably stay as they are, if we want to avoid a performance regression.

-- 
regards, Andrei Lepikhov,
pgEdge






view thread (4+ messages)

Message-ID: <2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com>
Permalink:  ../2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com/
Also on:    postgresql.org/message-id/2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: lepihov@gmail.com, tgl@sss.pgh.pa.us, andres@anarazel.de, li.evan.chao@gmail.com, michael@paquier.xyz, dgrowleyml@gmail.com
  Subject: Re: Make the transition state of avg(int2)/avg(int4)/sum(int2)/sum(int4) internal
  In-Reply-To: <2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com>
  References: <762e811a-6584-4b03-a6ca-52600adcd6e9@gmail.com> <q2ksopavjswuwybvw643w652s374eqv2rnamtelaueyt4nfbqm@5vjzkkuujv33> <1571559.1788537022@sss.pgh.pa.us> <2c61c2c2-6bf3-4155-b569-713ddbb5d22c@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox