Re: BUG #19059: PostgreSQL fails to evaluate the cheaper expression first, leading to 45X performance degradation

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Christophe Pettus <xof@thebuild.com>
Cc: jinhui.lai@qq.com, pgsql-bugs@lists.postgresql.org
Date: 2025-09-20T23:19:03Z
Lists: pgsql-bugs
Christophe Pettus <xof@thebuild.com> writes:
> And tangentially, doesn't the SQL standard make it implementation-defined if OR short-circuits or not?

It looks like it says it's implementation-dependent, meaning
that not only is it not defined by the standard, but we're not
required to document it either:

    Subclause 6.3.3.3, “Rule evaluation order”:

	a) It is implementation-dependent whether expressions are
	actually evaluated from left to right when the precedence is
	not otherwise determined by the Formats or by parentheses.

	b) If evaluation of the inessential parts of an expression or
	search condition would cause an exception condition to be
	raised, then it is implementation-dependent whether or not
	that condition is raised.

	c) The declared type of a site that contains an intermediate
	result is implementation-dependent.

This is a good thing, because while we do mention boolean expression
re-ordering and short-circuiting, there's an awful lot of such details
that we've left undocumented.

			regards, tom lane