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 1vIN7G-006Jgk-Ke for pgsql-bugs@arkaria.postgresql.org; Mon, 10 Nov 2025 08:19:10 +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 1vIN7F-00EzLE-88 for pgsql-bugs@arkaria.postgresql.org; Mon, 10 Nov 2025 08:19:09 +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 1vIITp-00DVpa-NQ for pgsql-bugs@lists.postgresql.org; Mon, 10 Nov 2025 03:22:10 +0000 Received: from fout-a5-smtp.messagingengine.com ([103.168.172.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vIITm-006uOW-20 for pgsql-bugs@lists.postgresql.org; Mon, 10 Nov 2025 03:22:09 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id D8134EC01A5; Sun, 9 Nov 2025 22:22:03 -0500 (EST) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-06.internal (MEProxy); Sun, 09 Nov 2025 22:22:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monicz.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1762744923; x=1762831323; bh=5JHqeewkLCOjCzaY9wQExHrh62kCEnZa5tTYnseMYYM=; b= FearJ3qoBob0gzhCxZEwJqtp5mM9urdazZ/0LPemhcOjY8nry6YZCF+wtHKYpxF1 d3xjVnD8Yryf+Vylif268c76D7V6KuGnslwlgWGCOPdlLTIs1fzXLAVWrXjSHJGX ppi0Pb4j8NGFfACraoZ+tjxfhHH+5yxoRDdkPQnVp/JBoNxK1nvNnT5sOGrJTXxX 6PdXVYhRZo6ctJzYG7ehPpl3lZvZgRNjE/9s3rIAXLLctvjQ+Buj18zG2tprtr+K Z8V1ouA5SF6WjNGLF6qpcI2j+fnSAKdT7l02CKM5ufqdhBa7lAhVKc4wQNEEu2NU YnKzu0v1h837/Y49QhZciA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1762744923; x= 1762831323; bh=5JHqeewkLCOjCzaY9wQExHrh62kCEnZa5tTYnseMYYM=; b=y XPqcCiW0zgyeveraGGYJ/w5Jt9hej+Nz1avfOqVHuYwvkNVVlruTU26KvEZOIfau L2FvoEW3m3uApzKUu5mlBu3S9Q2skhMsFeZYYSjVoFHz0VaO2fqd7tiBlDEQyiPB YDlYiuuKsVaSyhvcZjFVRUHP176h55aITn+z/4wSUtqkB0uH+kNTmT/awsC7B3vQ jDslx0gBdnFDDb/yK8AmQ6X67BMnVz54s1qhV1qnxn0rbyAdK6Cbk2tETS2EeNwA Yo+8YIknaDLT+4f9sMsRBdqdHqpElKDm4Y8o1zpeD5BB6Z7rxQcFeAaafqBDeB87 8e/rNWCag9r5GYwgwvd0g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdduleejvdduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvvefkjghfufgtgfesthejre dtredttdenucfhrhhomhepfdfmrghmihhlucfoohhnihgtiidfuceokhgrmhhilhesmhho nhhitgiirdguvghvqeenucggtffrrghtthgvrhhnpeelffettedvueehudfhleeukedvud elkefhtdeivdeggfeuudeihffhhedvueegfeenucffohhmrghinhepghhithhhuhgsrdgt ohhmpdgvlhgvmhgvnhhtpghsphgrthhirghlpghsvghrvhhitggvrdhphienucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehkrghmihhlsehmohhn ihgtiidruggvvhdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtg hpthhtohepphhgshhqlhdqsghughhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhr ghdprhgtphhtthhopehtghhlsehsshhsrdhpghhhrdhprgdruhhs X-ME-Proxy: Feedback-ID: i3ffe4831:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id E7069240004F; Sun, 9 Nov 2025 22:22:02 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: Ag1RL93bHJ3c Date: Mon, 10 Nov 2025 04:21:20 +0100 From: "Kamil Monicz" To: "Tom Lane" Cc: pgsql-bugs@lists.postgresql.org Message-Id: In-Reply-To: <1650412.1762703636@sss.pgh.pa.us> References: <19106-9dd3668a0734cd72@postgresql.org> <1650412.1762703636@sss.pgh.pa.us> Subject: Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18 Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Nov 9, 2025, at 16:53, Tom Lane wrote: > PG Bug reporting form writes: > > After upgrading from Postgres 17 to 18, one of my queries started raising an > > error: > > "unexpected outer reference in CTE query" > > I agree that sounds like a bug ... > > > The problematic query is: > > https://github.com/openstreetmap-ng/openstreetmap-ng/blob/eb805d8766fb4b359b96eb6b50acc8c2a835a165/app/services/element_spatial_service.py#L82-L215 > > ... but I am not going to spend time trying to reproduce it given > this amount of detail. There's too much missing context, like what > data you were running the query on. I could spend all day, not > see the failure, and be left no wiser than before as to whether > it's already fixed or I just didn't duplicate your context closely > enough. Please see if you can reduce the problem case to a > self-contained SQL script. > > regards, tom lane > Hello Tom, It's my first time here (and realistically on a proper mailing list), so please excuse me. Here's the small, self-contained reproduction: ``` EXPLAIN SELECT * FROM ( SELECT ARRAY[1, 2] AS arr ) r CROSS JOIN LATERAL ( WITH a AS ( SELECT CASE WHEN id = 1 THEN ST_GeomFromText('LINESTRING(0 0,1 0,1 1)') ELSE ST_GeomFromText('POINT(0 0)') END AS geom FROM unnest(r.arr) AS id ), b AS ( SELECT ST_Polygonize( (SELECT ST_UnaryUnion(ST_Collect(geom)) FROM a) ) AS st_polygonize ) SELECT (SELECT st_polygonize FROM b), (SELECT st_polygonize FROM b) ) s; ``` ``` ERROR: unexpected outer reference in CTE query SQL state: XX000 ``` It depends on PostGIS being installed and loaded. In my case, it's version 3.6.0. I tried to make it work without it, but I couldn't figure it out. -Kamil Monicz