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 1rxoqD-00CCrP-LK for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Apr 2024 14:03:49 +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 1rxoqB-002kMu-Vu for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Apr 2024 14:03:47 +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.94.2) (envelope-from ) id 1rxoqB-002kMl-NQ for pgsql-bugs@lists.postgresql.org; Fri, 19 Apr 2024 14:03:47 +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.94.2) (envelope-from ) id 1rxoq9-001p3i-Eg for pgsql-bugs@lists.postgresql.org; Fri, 19 Apr 2024 14:03:47 +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 43JE3hlc2135178; Fri, 19 Apr 2024 10:03:43 -0400 From: Tom Lane To: hu_yajun@qq.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18442: Unnecessary Sort operator in indexScan Plan In-reply-to: <18442-aca4c9134416990e@postgresql.org> References: <18442-aca4c9134416990e@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 19 Apr 2024 03:10:24 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2135176.1713535423.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 19 Apr 2024 10:03:43 -0400 Message-ID: <2135177.1713535423@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > create index on t(a,b); > postgres=3D# explain select * from t where a is null order by b; -- need > sort > QUERY PLAN > ------------------------------------------------------------------------= -------- > Sort (cost=3D9.54..9.80 rows=3D103 width=3D8) > Sort Key: b > -> Index Only Scan using t_a_b_idx on t (cost=3D0.29..6.10 rows=3D1= 03 > width=3D8) > Index Cond: (a IS NULL) > (4 rows) Postgres doesn't detect that it could do this because "a IS NULL" is not an equivalence condition. You're not the first to suggest that it could be treated as one, but I believe there are semantic difficulties that would ensue. One example is that given "a IS NULL" and "a =3D b", the EquivalenceClass machinery would think it can discard "a =3D b" and instead emit "b IS NULL", which would not give the same answers. regards, tom lane