pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
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
Date: Wed, 17 Dec 2025 16:37:24 -0500
Message-ID: <2727002.1766007444@sss.pgh.pa.us> (raw)
In-Reply-To: <19358-013f22cf91350df1@postgresql.org>
References: <19358-013f22cf91350df1@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> 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=# explain verbose EXECUTE prepare_query('', '[-993693027,1525305818]'::int4range,
'[-168306621,-163656947)'::int4range);
                                             QUERY PLAN                                              
-----------------------------------------------------------------------------------------------------
 Update on public.t3  (cost=0.00..47.40 rows=0 width=0)
   ->  Seq Scan on public.t3  (cost=0.00..47.40 rows=157 width=38)
         Output: $1, ctid
         Filter: ((t3.c0 = t3.c0) OR ((t3.c0 >= (t3.c0 || (($2 - $3))::text)) AND (t3.c0 <= t3.c0)))
(4 rows)

So the "t3.c0 = 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





view thread (2+ messages)

Message-ID: <2727002.1766007444@sss.pgh.pa.us>
Permalink:  ../2727002.1766007444@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2727002.1766007444@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, 798604270@qq.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #19358: Short circuit optimization exists in generic plan but missed in custom plan
  In-Reply-To: <2727002.1766007444@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox