Re: Combining Aggregates - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: Combining Aggregates
Date
Msg-id 54E797BC.9030702@gmx.net
Whole thread Raw
In response to Re: Combining Aggregates  (Tomas Vondra <tomas.vondra@2ndquadrant.com>)
Responses Re: Combining Aggregates  (Tomas Vondra <tomas.vondra@2ndquadrant.com>)
List pgsql-hackers
On 2/20/15 3:09 PM, Tomas Vondra wrote:
> On 20.2.2015 21:01, Peter Eisentraut wrote:
>> Is there a case where the combining function is different from the
>> transition function, other than for count?
> 
> It's different in all the cases when the aggregate state is not
> identical to a single value - for example the usual avg(), sum() and
> stddev() aggregates keep state which is equal to
> 
>    {count(X), sum(X), sum(X*X)}
> 
> The 'combine' function gets two such 'state' values, while transition
> gets 'state' + next value.

That's just because the count is hidden there in an opaque custom
transition function.  If, say, we had instead an array of transition
functions {inc, plus, plussq} and we knew that plus and plussq are
associative operators, all we'd need to special case is the count case.This would avoid a lot of repetitive code for
stddev,avg, etc.
 

(As a bonus, you could use this knowledge to compute count, sum, avg,
and stddev in parallel using only three counters.)




pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Precedence of standard comparison operators
Next
From: Tomas Vondra
Date:
Subject: Re: Combining Aggregates