Re: Count of records in a row

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Rémi Cura <remi(dot)cura(at)gmail(dot)com>
Cc: Hubert Lubaczewski <depesz(at)depesz(dot)com>, Robert James <srobertjames(at)gmail(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Count of records in a row
Date: 2013-10-23 03:01:20
Message-ID: CAHyXU0xMsGe_+1Nb0+N1xOAzdJN2-gnEqzfAPoY+sx-GM-4sEQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> 2013/10/22 Merlin Moncure <mmoncure(at)gmail(dot)com>
>> > With a standard loop, I loop n times, and each times I only need the
>> > current
>> > row plus the previous row which I put in memory, thus O(n).
>>
>> For posterity, the above is incorrect. Since the aggregate is ordered
>> through the window function, it gets executed exactly once per output
>> row. It behaves exactly like a loop. You know this because there is
>> no array in the aggregate state.
>>
> just out of pure curiosity,
> is it always the case or is it due to this particular aggregate?

It is always the case. Generally speaking, aggregates, especially
user defined aggregates, are run once per input row. In this case
the main utility of window functions is to order the aggregate
execution calls and (especially) allow intermediate output per input
row, instead of per aggregate grouping.

On Tue, Oct 22, 2013 at 6:01 PM, Robert James <srobertjames(at)gmail(dot)com> wrote:
> Wow, this is an excellent discussion - and I must admit, a bit beyond
> my abilities. Is there a consensus as to the best approach to adopt?
> Is Elliot's the best?

For this *specific* problem, I would give Elliot's (extremely clever)
query the nod on the basis that it does not require any supporting
infrastructure, which is always nice. That being said, once you start
getting the mojo of user defined aggregates + window functions it
starts to become clear that it's a cleaner way of doing many types of
things that are normally handled by loops.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2013-10-23 03:40:57 Re: ISO8859_1 vs UTF-8 Performance?
Previous Message Andy Colson 2013-10-23 02:45:24 Re: Monitoring number of backends