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.96) (envelope-from ) id 1vo9TJ-00FyW5-20 for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Feb 2026 00:13:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vo9TH-001QB7-0M for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Feb 2026 00:13:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vo9TG-001QAz-2b for pgsql-hackers@lists.postgresql.org; Fri, 06 Feb 2026 00:13:14 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vo9TE-00000001H09-1cOR for pgsql-hackers@postgresql.org; Fri, 06 Feb 2026 00:13:14 +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 6160D90q1668739; Thu, 5 Feb 2026 19:13:09 -0500 From: Tom Lane To: Andres Freund cc: pgsql-hackers@postgresql.org, David Rowley Subject: Re: Unfortunate pushing down of expressions below sort In-reply-to: References: Comments: In-reply-to Andres Freund message dated "Thu, 05 Feb 2026 15:52:30 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1668737.1770336789.1@sss.pgh.pa.us> Date: Thu, 05 Feb 2026 19:13:09 -0500 Message-ID: <1668738.1770336789@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andres Freund writes: > We push down expressions below a sort node, even though they could be > evaluated above. Yeah. That's a hangover from an ancient decision that sort/limit would always be applied at the top of the plan tree. I'm too lazy to check the details right now, but I think we already relaxed that in some cases (or maybe it was about evaluating stuff before/after GROUP BY?). > That can very substantially increase the space needed for the > sort. Could decrease it too, eg if what you are outputting is md5(some-wide-column). Not sure we are smart enough to tell which way is better. regards, tom lane