postgresql/src/backend
Tom Lane 925581d89c Fix improper uses of canonicalize_qual().
One of the things canonicalize_qual() does is to remove constant-NULL
subexpressions of top-level AND/OR clauses.  It does that on the assumption
that what it's given is a top-level WHERE clause, so that NULL can be
treated like FALSE.  Although this is documented down inside a subroutine
of canonicalize_qual(), it wasn't mentioned in the documentation of that
function itself, and some callers hadn't gotten that memo.

Notably, commit d007a9505 caused get_relation_constraints() to apply
canonicalize_qual() to CHECK constraints.  That allowed constraint
exclusion to misoptimize situations in which a CHECK constraint had a
provably-NULL subclause, as seen in the regression test case added here,
in which a child table that should be scanned is not.  (Although this
thinko is ancient, the test case doesn't fail before 9.2, for reasons
I've not bothered to track down in detail.  There may be related cases
that do fail before that.)

More recently, commit f0e44751d added an independent bug by applying
canonicalize_qual() to index expressions, which is even sillier since
those might not even be boolean.  If they are, though, I think this
could lead to making incorrect index entries for affected index
expressions in v10.  I haven't attempted to prove that though.

To fix, add an "is_check" parameter to canonicalize_qual() to specify
whether it should assume WHERE or CHECK semantics, and make it perform
NULL-elimination accordingly.  Adjust the callers to apply the right
semantics, or remove the call entirely in cases where it's not known
that the expression has one or the other semantics.  I also removed
the call in some cases involving partition expressions, where it should
be a no-op because such expressions should be canonical already ...
and was a no-op, independently of whether it could in principle have
done something, because it was being handed the qual in implicit-AND
format which isn't what it expects.  In HEAD, add an Assert to catch
that type of mistake in future.

This represents an API break for external callers of canonicalize_qual().
While that's intentional in HEAD to make such callers think about which
case applies to them, it seems like something we probably wouldn't be
thanked for in released branches.  Hence, in released branches, the
extra parameter is added to a new function canonicalize_qual_ext(),
and canonicalize_qual() is a wrapper that retains its old behavior.

Patch by me with suggestions from Dean Rasheed.  Back-patch to all
supported branches.

Discussion: https://postgr.es/m/24475.1520635069@sss.pgh.pa.us
2018-03-11 18:10:43 -04:00
..
access Make gistvacuumcleanup() count the actual number of index tuples. 2018-03-02 11:22:42 -05:00
bootstrap Protect against multixact members wraparound 2015-04-28 11:32:53 -03:00
catalog Use ereport not elog for some corrupt-HOT-chain reports. 2018-03-01 16:23:51 -05:00
commands Prevent dangling-pointer access when update trigger returns old tuple. 2018-02-27 13:27:38 -05:00
executor Fix misbehavior of CTE-used-in-a-subplan during EPQ rechecks. 2018-02-19 16:00:18 -05:00
foreign Arrange to cache FdwRoutine structs in foreign tables' relcache entries. 2013-03-06 23:48:09 -05:00
lib Misc comment typo fixes. 2014-12-16 16:39:33 +02:00
libpq Fix some null pointer dereferences in LDAP auth code 2017-11-10 14:30:48 -05:00
main Install Windows crash dump handler before all else. 2017-11-12 14:31:05 -08:00
nodes Fix improper repetition of previous results from a hashed aggregate. 2016-08-24 14:37:51 -04:00
optimizer Fix improper uses of canonicalize_qual(). 2018-03-11 18:10:43 -04:00
parser Dept of second thoughts: keep aliasp_item in sync with tlistitem. 2017-10-27 18:16:25 -04:00
po Translation updates 2018-02-05 12:48:55 -05:00
port Avoid depending on non-POSIX behavior of fcntl(2). 2017-04-21 15:55:56 -04:00
postmaster Empty search_path in Autovacuum and non-psql/pgbench clients. 2018-02-26 07:39:48 -08:00
regex Fix regexport.c to behave sanely with lookaround constraints. 2017-04-13 17:18:35 -04:00
replication Fix coding rules violations in walreceiver.c 2017-10-03 14:58:25 +02:00
rewrite Fix multiple assignments to a column of a domain type. 2017-07-11 16:48:59 -04:00
snowball Fix ancient encoding error in hungarian.stop. 2014-06-10 22:48:39 -04:00
storage Clean up assorted messiness around AllocateDir() usage. 2017-12-04 17:02:52 -05:00
tcop Unify SIGHUP handling between normal and walsender backends. 2017-06-05 19:18:16 -07:00
tsearch Reduce memory usage of tsvector type analyze function. 2017-07-12 22:04:08 +03:00
utils Fix improper uses of canonicalize_qual(). 2018-03-11 18:10:43 -04:00
.gitignore Add gitignore for mingw/cygwin build outputs 2011-06-09 18:11:47 +02:00
common.mk Call check_keywords.pl in maintainer-check 2012-02-27 13:53:12 +02:00
Makefile AIX: Link the postgres executable with -Wl,-brtllib. 2015-07-15 21:00:30 -04:00
nls.mk xlogreader.c: Fix report_invalid_record translatability flag 2015-01-09 12:34:24 -03:00