Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iAGGz-0003nh-Sc for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Sep 2019 16:24:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iAGGy-0003kB-Gn for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Sep 2019 16:24:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iAGGy-0003k2-8a for pgsql-bugs@lists.postgresql.org; Tue, 17 Sep 2019 16:24:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iAGGv-0006IS-5k for pgsql-bugs@lists.postgresql.org; Tue, 17 Sep 2019 16:24:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x8HGO5Ge006684; Tue, 17 Sep 2019 12:24:05 -0400 From: Tom Lane 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 In-reply-to: <16010-251e81f75eeaf50e@postgresql.org> References: <16010-251e81f75eeaf50e@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Tue, 17 Sep 2019 15:47:19 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6682.1568737445.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 17 Sep 2019 12:24:05 -0400 Message-ID: <6683.1568737445@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form 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 invali= d > 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 >=3D 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-EX= PRESS-EVAL