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 1vfaD2-0034dz-1u for pgsql-bugs@arkaria.postgresql.org; Tue, 13 Jan 2026 08:57:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vfaCz-003bq1-2G for pgsql-bugs@arkaria.postgresql.org; Tue, 13 Jan 2026 08:57:01 +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 1vfXhI-0039CS-3A for pgsql-bugs@lists.postgresql.org; Tue, 13 Jan 2026 06:16:09 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vfXhG-000ABS-1x for pgsql-bugs@lists.postgresql.org; Tue, 13 Jan 2026 06:16:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=fPDCeVs23VqADVUQ/Hb13jHP1NzR7PGyprMZ5FFEICw=; b=gzoUX7Q5tLJvpJJLQZLW+wvrzq aPWgO+Q3NKgbpVnzhNKIRCfl8E/jjO/rzErEmnBI5QO5bGrBHIlLWbvJs0Bl0aE3wlq+xVMc0r35K WmBfN//rXoZkJOapk3+JtJw+Ai1sff7MKd8R9IahDCj2PVYip1DN6uUhyQ1R8/VVV09q6WCa7Qwpw CoqQt0bWZbsscO1O2KKdW8oMY2pOrKMun/QhdATx72i/okCI7m8bvNneM8mB0T6vcarSYSM2R6avA R3W/d6YRWVLqGEL8cVMusH9uSlVr+yb25FiLNYUOkrWhQCNcriTEZCUaISGh73ACyQVpgPtNkFHWs 5YAbuKqA==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vfXhE-000Zqg-0s for pgsql-bugs@lists.postgresql.org; Tue, 13 Jan 2026 06:16:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vfXhC-000hWn-2c for pgsql-bugs@lists.postgresql.org; Tue, 13 Jan 2026 06:16:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19377: Query planner interesting behaviour To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: postgresql@juneidy.wibowo.au Reply-To: postgresql@juneidy.wibowo.au, pgsql-bugs@lists.postgresql.org Date: Tue, 13 Jan 2026 06:15:10 +0000 Message-ID: <19377-6797bfc0a06bf23c@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19377 Logged by: Juneidy Wibowo Email address: postgresql@juneidy.wibowo.au PostgreSQL version: 18.0 Operating system: linux postgres:18-trixie Description: =20 Having two queries like below: explain analyze SELECT id from table_a where ST_Intersects(geometry, ST_MakeValid((SELECT st_transform(geom,4326) FROM table_b ))); explain analyze SELECT id from table_b where ST_Intersects(geometry, (SELECT ST_MakeValid((SELECT st_transform(geom,4326) FROM table_b )))); Result in two different query plan/execution: =3D> explain analyze SELECT id from table_a where ST_Intersects(geometry, ST_MakeValid((SELECT st_transform(geom,4326) FROM table_b ))); QUERY PLAN ---------------------------------------------------------------------------= --------------------------------------------------------------------- Index Scan using features_geometry_idx on features (cost=3D17036.38..18524.35 rows=3D55 width=3D8) (actual time=3D1.730..9592.= 568 rows=3D20921 loops=3D1) Index Cond: (geometry && st_makevalid($0)) Filter: st_intersects(geometry, st_makevalid($0)) Rows Removed by Filter: 10731 InitPlan 1 (returns $0) -> Seq Scan on region (cost=3D0.00..17023.60 rows=3D1360 width=3D32) (actual time=3D0.324..0.328 rows=3D1 loops=3D1) Planning Time: 0.090 ms Execution Time: 9619.139 ms (8 rows) =3D> explain analyze SELECT id from table_b where ST_Intersects(geometry, (SELECT ST_MakeValid((SELECT st_transform(geom,4326) FROM table_b )))); QUERY PLAN ---------------------------------------------------------------------------= -------------------------------------------------------------------- Index Scan using features_geometry_idx on features (cost=3D17036.39..17836.86 rows=3D55 width=3D8) (actual time=3D1.467..111.5= 07 rows=3D20921 loops=3D1) Index Cond: (geometry && $1) Filter: st_intersects(geometry, $1) Rows Removed by Filter: 10731 InitPlan 2 (returns $1) -> Result (cost=3D17023.60..17036.11 rows=3D1 width=3D32) (actual time=3D1.232..1.237 rows=3D1 loops=3D1) InitPlan 1 (returns $0) -> Seq Scan on region (cost=3D0.00..17023.60 rows=3D1360 width=3D32) (actual time=3D0.360..0.364 rows=3D1 loops=3D1) Planning Time: 0.085 ms Execution Time: 136.482 ms (10 rows) It's weird that in the first query postgres doesn't seem to treat st_makevalid($0) as a constant and I think re-evaluate st_makevalue($0) for every row. Is that an expected behaviour?