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 1syeSa-001K8A-Rl for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Oct 2024 21:43:09 +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 1syeSa-0069WZ-1l for pgsql-bugs@arkaria.postgresql.org; Wed, 09 Oct 2024 21:43:08 +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 1syeSZ-0069WR-Pq for pgsql-bugs@lists.postgresql.org; Wed, 09 Oct 2024 21:43:08 +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 1syeSW-000DtO-Kw for pgsql-bugs@lists.postgresql.org; Wed, 09 Oct 2024 21:43:07 +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 499Lh3lU2527691; Wed, 9 Oct 2024 17:43:03 -0400 From: Tom Lane To: exclusion@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18652: Planner can not find pathkey item to sort for query with expression and expression index In-reply-to: <18652-deaa782ebcca85d1@postgresql.org> References: <18652-deaa782ebcca85d1@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 09 Oct 2024 21:00:01 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2527689.1728510183.1@sss.pgh.pa.us> Date: Wed, 09 Oct 2024 17:43:03 -0400 Message-ID: <2527690.1728510183@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: > The following query: > CREATE TABLE t(i int, j int); > CREATE INDEX idx on t((i + 0)); > SELECT * FROM t, > (SELECT i + 0 AS i FROM > (SELECT i FROM t UNION ALL SELECT i + 1 FROM t) AS t1 > ) AS t2 > WHERE t2.i = t.j; > fails with: > ERROR: XX000: could not find pathkey item to sort > LOCATION: prepare_sort_from_pathkeys, createplan.c:6350 > The error occurs only when the expression in SELECT i + 0 AS i FROM > matches the expression in an index. > Reproduced on REL_10_STABLE .. master. Hm, this seems quite an old bug: I can reproduce it on 9.1 but not 9.0. Unfortunately, that's too far back to build easily on modern platforms, so I'm not seeing a way to "git bisect" for more insight. It looks like we are generating a Path tree in which one of the inputs to a MergeAppend is a plain unsorted seqscan, which'd be all right except it doesn't expose the required sort value in its targetlist. More later. regards, tom lane