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 1viaLX-00AQqa-02 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 15:42:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viaLW-007sUY-00 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 15:42:14 +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.96) (envelope-from ) id 1viaLV-007sUQ-2L for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 15:42:14 +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 1viaLT-001cAn-1e for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 15:42:13 +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 60LFg7u62202822; Wed, 21 Jan 2026 10:42:07 -0500 From: Tom Lane To: Andrei Lepikhov cc: 798604270@qq.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19386: Unnecessary Sort in query plan for SELECT literal with ORDER BY In-reply-to: <82e59270-d4a0-4dbb-9c2d-5cd2005c933f@gmail.com> References: <19386-be594598921461b9@postgresql.org> <82e59270-d4a0-4dbb-9c2d-5cd2005c933f@gmail.com> Comments: In-reply-to Andrei Lepikhov message dated "Wed, 21 Jan 2026 12:11:05 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2202820.1769010127.1@sss.pgh.pa.us> Date: Wed, 21 Jan 2026 10:42:07 -0500 Message-ID: <2202821.1769010127@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andrei Lepikhov writes: > Another question - should we do anything to optimise this quite narrow > (at least it seems so for me) case and stop simplification of the clause? I don't think so. Shorn of irrelevancies, what we have here is regression=# create table foo (b bool); CREATE TABLE regression=# explain select * from foo where b = true order by b; QUERY PLAN ------------------------------------------------------------- Sort (cost=111.56..115.07 rows=1405 width=1) Sort Key: b -> Seq Scan on foo (cost=0.00..38.10 rows=1405 width=1) Filter: b The planner can omit the sort step if it sees that the sort key has been equated to a constant: regression=# create table bar (i int); CREATE TABLE regression=# explain select * from bar where i = 42 order by i; QUERY PLAN ----------------------------------------------------- Seq Scan on bar (cost=0.00..41.88 rows=13 width=4) Filter: (i = 42) However, in the case of a bool column we go out of our way to convert "b = true" and similar cases to "b" or "NOT b" so that we won't be fooled by variant phrasings of equivalent conditions. That doesn't happen in the OP's generic-plan case because the original is "b = parameter". The planner knows the parameter expression will be effectively constant in any one run, so it elides the Sort even though it doesn't know the parameter's value. But it doesn't see plain "b" as a phrasing of "b = true". We could, I imagine, allow the EquivalenceClass machinery to treat "WHERE b" as a shorthand for "b = true", but that would lead to cluttering the mechanism with a ton of almost-always-useless EquivalenceClasses, because it's hard to see how such a decision doesn't result in every random WHERE qual producing an EquivalenceClass. I think that would slow down planning of most queries by more than such an optimization is worth. regards, tom lane