pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Kaimeh <kkaimeh@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: Error when using array_agg with filter where clause in pg16 and pg17
Date: Tue, 08 Apr 2025 20:52:26 -0400
Message-ID: <101611.1744159946@sss.pgh.pa.us> (raw)
In-Reply-To: <CAApHDvrf8q52wZN08zygw5H2RE5z74u9toXckZnK_rt5KXNVyQ@mail.gmail.com>
References: <CAK-+Jz9J=Q06-M7cDJoPNeYbz5EZDqkjQbJnmRyQyzkbRGsYkA@mail.gmail.com>
	<4040920.1744126348@sss.pgh.pa.us>
	<CAApHDvqb8BxPvh3wwVc1YXNWGVVw9nJ49mFsdOwzHjBji0qDPw@mail.gmail.com>
	<90177.1744158354@sss.pgh.pa.us>
	<CAApHDvrf8q52wZN08zygw5H2RE5z74u9toXckZnK_rt5KXNVyQ@mail.gmail.com>

David Rowley <dgrowleyml@gmail.com> writes:
> On Wed, 9 Apr 2025 at 12:25, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Oooh.  If the FILTER clause is selective, that could easily mean that
>> the "optimization" loses big from having to sort many more tuples.
>> I wonder if we should just not apply it when there's a FILTER,
>> full stop.

> The only way I can see to fix that properly is to cost it in during
> aggregate planning. IIRC, there's no costing for the implicit sorts in
> Aggref. We could add some of those and put a flag in AggPath which
> gets propagated to Agg to specify if aggpresorted should be ignored or
> not for the given Agg node. We'd have to add_path() for both versions
> of the AggPath and let the cheapest Path win.

Yeah, AFAIR we never did any real costing of aggregate-internal
sorting.  However, adding that would pose the same risk you mentioned
that some queries might regress due to picking the worse plan.

> I suspect we should just leave this for v18 and maybe come back and
> improve for v19.

I think not doing anything is unacceptable: even though it took awhile
to notice, presorted_agg flat out breaks some queries that worked
before.  That trumps any worries about "maybe the plan will be worse",
and I don't even think it's a close decision.  So my inclination is
to do the simplest possible thing in v16-v18, and that seems to be
to disable presorted_agg if there's a FILTER.  Then we can look
into better ideas at leisure for v19.

			regards, tom lane





view thread (11+ messages)  latest in thread

Message-ID: <101611.1744159946@sss.pgh.pa.us>
Permalink:  ../101611.1744159946@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/101611.1744159946@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, dgrowleyml@gmail.com, kkaimeh@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: Error when using array_agg with filter where clause in pg16 and pg17
  In-Reply-To: <101611.1744159946@sss.pgh.pa.us>

* 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