Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2Jg9-00GDwK-2R for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Apr 2025 00:52:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u2Jg7-00GzJa-0x for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Apr 2025 00:52:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2Jg6-00GzJQ-Ok for pgsql-bugs@lists.postgresql.org; Wed, 09 Apr 2025 00:52:31 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u2Jg4-003mHs-1A for pgsql-bugs@lists.postgresql.org; Wed, 09 Apr 2025 00:52:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 5390qQub101612; Tue, 8 Apr 2025 20:52:26 -0400 From: Tom Lane To: David Rowley cc: Kaimeh , pgsql-bugs@lists.postgresql.org Subject: Re: Error when using array_agg with filter where clause in pg16 and pg17 In-reply-to: References: <4040920.1744126348@sss.pgh.pa.us> <90177.1744158354@sss.pgh.pa.us> Comments: In-reply-to David Rowley message dated "Wed, 09 Apr 2025 12:43:53 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <101610.1744159946.1@sss.pgh.pa.us> Date: Tue, 08 Apr 2025 20:52:26 -0400 Message-ID: <101611.1744159946@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Wed, 9 Apr 2025 at 12:25, Tom Lane 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