pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: spkendall@gmail.com
Subject: BUG #16010: Unexpected reordering of WHERE clause operations and SELECT list function calls
Date: Tue, 17 Sep 2019 15:47:19 +0000
Message-ID: <16010-251e81f75eeaf50e@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16010
Logged by:          Stephen Kendall
Email address:      spkendall@gmail.com
PostgreSQL version: 12beta3
Operating system:   MacOS
Description:        

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().

All but the first query shown below actually work in our production
environment (Amazon RDS 10.6). They don't error there and do return the
expected list of tables. However, that environment is too complex and too
full of proprietary data; I can not provide reproduction steps for it.

In any event, I believe that either the first query after the CREATE TABLE
statements should work because I think TO_TIMESTAMP() should *not* be run
against rows that fail the first WHERE clause predicate. That query errors
in both environments.

spkmbp:~ spk$ psql12 service=app-12-postgres-postgres 
psql12 (12beta3)
Type "help" for help.

localhost: spk@postgres=# \set VERBOSITY verbose 
localhost: spk@postgres=# set client_min_messages to debug ;
SET
localhost: spk@postgres=# CREATE SCHEMA proc ;
CREATE SCHEMA
localhost: spk@postgres=# CREATE SCHEMA bkup_proc ;
CREATE SCHEMA
localhost: spk@postgres=# CREATE TABLE proc.decide_proc_tst040318 () ;
CREATE TABLE
localhost: spk@postgres=# CREATE TABLE bkup_proc.solution_use_proc_20190730
() ;
CREATE TABLE
localhost: spk@postgres=# SELECT * FROM (
localhost: spk@postgres(# SELECT schemaname, tablename,
current_date-interval'2'month time_frame,
localhost: spk@postgres(#          
to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd') table_date
localhost: spk@postgres(# from pg_tables
localhost: spk@postgres(# where schemaname = 'bkup_proc'
localhost: spk@postgres(# and (tablename ~* 'decide_proc_'
localhost: spk@postgres(#        or tablename ~* 'solution_use_proc_'
localhost: spk@postgres(#        )
localhost: spk@postgres(# ) subq
localhost: spk@postgres-# where table_date >= time_frame
localhost: spk@postgres-# ;
ERROR:  22008: date/time field value out of range: "040318"
LOCATION:  DateTimeParseError, datetime.c:3741
localhost: spk@postgres=# WITH
localhost: spk@postgres-#  tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM pg_tables
localhost: spk@postgres(#  WHERE schemaname = 'bkup_proc'
localhost: spk@postgres(#    AND (tablename ~* 'decide_proc_'
localhost: spk@postgres(#        OR tablename ~* 'solution_use_proc_'
localhost: spk@postgres(#        )
localhost: spk@postgres(# ) 
localhost: spk@postgres-# SELECT * FROM (
localhost: spk@postgres(# SELECT schemaname, tablename,
current_date-interval'2'month time_frame,
localhost: spk@postgres(#          
to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd') table_date
localhost: spk@postgres(#   FROM tabs
localhost: spk@postgres(# ) subq
localhost: spk@postgres-# where table_date >= time_frame
localhost: spk@postgres-# ;
ERROR:  22008: date/time field value out of range: "040318"
LOCATION:  DateTimeParseError, datetime.c:3741
localhost: spk@postgres=# WITH
localhost: spk@postgres-#  tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM pg_tables
localhost: spk@postgres(#  WHERE schemaname = 'bkup_proc'
localhost: spk@postgres(# ) 
localhost: spk@postgres-# ,proc_tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM tabs
localhost: spk@postgres(#  WHERE (tablename ~* 'decide_proc_'
localhost: spk@postgres(#        OR tablename ~* 'solution_use_proc_'
localhost: spk@postgres(#        )
localhost: spk@postgres(# )
localhost: spk@postgres-# SELECT * FROM (
localhost: spk@postgres(# SELECT schemaname, tablename,
current_date-interval'2'month time_frame,
localhost: spk@postgres(#          
to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd') table_date
localhost: spk@postgres(#   FROM proc_tabs
localhost: spk@postgres(# ) subq
localhost: spk@postgres-# where table_date >= time_frame
localhost: spk@postgres-# ;
ERROR:  22008: date/time field value out of range: "040318"
LOCATION:  DateTimeParseError, datetime.c:3741
localhost: spk@postgres=# WITH
localhost: spk@postgres-#  tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM pg_tables
localhost: spk@postgres(#  WHERE schemaname = 'bkup_proc'
localhost: spk@postgres(# ) 
localhost: spk@postgres-# ,proc_tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM tabs
localhost: spk@postgres(#  WHERE (tablename ~* 'decide_proc_'
localhost: spk@postgres(#        OR tablename ~* 'solution_use_proc_'
localhost: spk@postgres(#        )
localhost: spk@postgres(# )
localhost: spk@postgres-# ,proc_tabs_calc AS (
localhost: spk@postgres(# SELECT schemaname, tablename,
current_date-interval'2'month time_frame,
localhost: spk@postgres(#          
to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd') table_date
localhost: spk@postgres(#   FROM proc_tabs
localhost: spk@postgres(# )
localhost: spk@postgres-# SELECT * 
localhost: spk@postgres-#   FROM proc_tabs_calc
localhost: spk@postgres-#  WHERE table_date >= time_frame
localhost: spk@postgres-# ;
ERROR:  22008: date/time field value out of range: "040318"
LOCATION:  DateTimeParseError, datetime.c:3741
localhost: spk@postgres=# WITH
localhost: spk@postgres-#  tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename
localhost: spk@postgres(#   FROM pg_tables
localhost: spk@postgres(#  WHERE schemaname = 'bkup_proc'
localhost: spk@postgres(# )
localhost: spk@postgres-# ,proc_tabs AS (
localhost: spk@postgres(# SELECT schemaname, tablename,
current_date-interval'2'month time_frame,
localhost: spk@postgres(#          
to_timestamp(regexp_replace(tablename,'[^0-9]*',''),'yyyymmdd') table_date
localhost: spk@postgres(#   FROM tabs
localhost: spk@postgres(#  WHERE tablename ~* 'decide_proc_'
localhost: spk@postgres(#        or tablename ~* 'solution_use_proc_'
localhost: spk@postgres(# )
localhost: spk@postgres-# SELECT *
localhost: spk@postgres-#   FROM proc_tabs
localhost: spk@postgres-#  WHERE table_date >= time_frame      
localhost: spk@postgres-#   ;
ERROR:  22008: date/time field value out of range: "040318"
LOCATION:  DateTimeParseError, datetime.c:3741
localhost: spk@postgres=#



view thread (2+ messages)  latest in thread

Message-ID: <16010-251e81f75eeaf50e@postgresql.org>
Permalink:  ../16010-251e81f75eeaf50e@postgresql.org/
Also on:    postgresql.org/message-id/16010-251e81f75eeaf50e@postgresql.org

 · 

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: noreply@postgresql.org, spkendall@gmail.com
  Subject: Re: BUG #16010: Unexpected reordering of WHERE clause operations and SELECT list function calls
  In-Reply-To: <16010-251e81f75eeaf50e@postgresql.org>
  References: <16010-251e81f75eeaf50e@postgresql.org>

* 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