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 1u2JGS-00G7L8-L1 for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Apr 2025 00:26:00 +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 1u2JGQ-00Gv7U-Ru for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Apr 2025 00:25:59 +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 1u2JGQ-00Gv7M-HF for pgsql-bugs@lists.postgresql.org; Wed, 09 Apr 2025 00:25:58 +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 1u2JGO-003m4X-27 for pgsql-bugs@lists.postgresql.org; Wed, 09 Apr 2025 00:25:57 +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 5390Pswl090178; Tue, 8 Apr 2025 20:25:54 -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> Comments: In-reply-to David Rowley message dated "Wed, 09 Apr 2025 11:55:55 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <90176.1744158354.1@sss.pgh.pa.us> Date: Tue, 08 Apr 2025 20:25:54 -0400 Message-ID: <90177.1744158354@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 03:32, Tom Lane wrote: >> The simplest fix I can think of is to disable the presorted-agg >> optimization if (1) there's a FILTER clause and (2) the proposed >> sort key is anything more complex than a Var. > Unfortunately, the situation is a little worse than what you > highlighted, as I think I didn't consider FILTER at all, and this > means I didn't consider the costing differences between filtering then > sorting vs sorting then filtering. 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. regards, tom lane