pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: spkendall@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16010: Unexpected reordering of WHERE clause operations and SELECT list function calls
Date: Tue, 17 Sep 2019 12:24:05 -0400
Message-ID: <6683.1568737445@sss.pgh.pa.us> (raw)
In-Reply-To: <16010-251e81f75eeaf50e@postgresql.org>
References: <16010-251e81f75eeaf50e@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> writes:
> Expected output is a list of tables where the names contain simple date
> strings that fall within a range. Actual result is an error because one or
> more table names that do not contain validly formatted dates are passed to
> TO_TIMESTAMP(). This should not happen because the table with the invalid
> date in its name is in a schema that should be filtered out by a WHERE
> clause before the evaluation of the TO_TIMESTAMP().

There is not any particular guarantee about the order of evaluation of
WHERE clauses [1], and we're not going to make one because it would
be catastrophic for performance in many real-world cases.  In this case
you might be able to force it by inserting an optimization fence in the
subquery (so that the "where table_date >= time_frame" clause can't be
pushed down into it); but you'd probably be better off to make the
table-name-to-timestamp conversion expression more robust.  You could
do something like

case when tablename ~ '^[^0-9]*[0-9]{8}$'
then to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd')
else null
end

Another approach, but one that's also going to cost you performance,
is to wrap expressions that might throw errors into plpgsql functions
that you mark as volatile.  That will discourage the planner from
moving them around ... but it will probably also cripple optimizations
that you still want.

			regards, tom lane

[1] https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL





view thread (2+ messages)

Message-ID: <6683.1568737445@sss.pgh.pa.us>
Permalink:  ../6683.1568737445@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/6683.1568737445@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, spkendall@gmail.com
  Subject: Re: BUG #16010: Unexpected reordering of WHERE clause operations and SELECT list function calls
  In-Reply-To: <6683.1568737445@sss.pgh.pa.us>
  References: <16010-251e81f75eeaf50e@postgresql.org> <6683.1568737445@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