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 1vVzD7-00AJh2-1k for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Dec 2025 21:37:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVzD6-00GoNo-1I for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Dec 2025 21:37:29 +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 1vVzD6-00GoNf-01 for pgsql-bugs@lists.postgresql.org; Wed, 17 Dec 2025 21:37:28 +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.96) (envelope-from ) id 1vVzD4-001Ifu-1S for pgsql-bugs@lists.postgresql.org; Wed, 17 Dec 2025 21:37:28 +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 5BHLbObJ2727003; Wed, 17 Dec 2025 16:37:25 -0500 From: Tom Lane To: 798604270@qq.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19358: Short circuit optimization exists in generic plan but missed in custom plan In-reply-to: <19358-013f22cf91350df1@postgresql.org> References: <19358-013f22cf91350df1@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 17 Dec 2025 12:03:44 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2727001.1766007444.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 17 Dec 2025 16:37:24 -0500 Message-ID: <2727002.1766007444@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: > ... So why the short > circuit optimization applied in the prepared update (with a generic plan= ) > but not in the normal update (with a custom plan). Const-folding doesn't stop just because the current part of the expression tree might not be reached at runtime. EXPLAIN shows that your generic plan is regression=3D# explain verbose EXECUTE prepare_query('', '[-993693027,1525= 305818]'::int4range, '[-168306621,-163656947)'::int4range); QUERY PLAN = = --------------------------------------------------------------------------= --------------------------- Update on public.t3 (cost=3D0.00..47.40 rows=3D0 width=3D0) -> Seq Scan on public.t3 (cost=3D0.00..47.40 rows=3D157 width=3D38) Output: $1, ctid Filter: ((t3.c0 =3D t3.c0) OR ((t3.c0 >=3D (t3.c0 || (($2 - $3)):= :text)) AND (t3.c0 <=3D t3.c0))) (4 rows) So the "t3.c0 =3D t3.c0" condition will be found to be true and we don't reach the right-hand side of the OR. However, if $2 and $3 are replaced by constants, the planner will attempt to reduce the range difference to a constant at plan time. There have been previous discussions about how this sometimes leads to unintuitive failures, but I don't see how we could skip const-folding without huge performance penalties in some cases. In any case, we clearly document that you can't rely on specific execution order of boolean expressions. You will not find anything in our docs promising left-to-right evaluation of ORs. regards, tom lane