When checking whether a sub-select's output columns can produce NULL, so as to decide whether a NOT IN can be converted to an anti-join, query_outputs_are_not_nullable() falls back on find_nonnullable_vars() for targetlist entries that are plain Vars: if the sub-select's own quals prove the Var non-null, the output is non-nullable. But that test compared only varno and varattno, without checking varlevelsup. An outer reference in the targetlist could thus be matched against a Var of the sub-select's own range table that happens to share the same varno and varattno, wrongly proving the output non-nullable and allowing an invalid conversion to an anti-join, which yields wrong answers when the outer reference is NULL. To fix, restrict the fallback to Vars of the current query level. Author: Rui Zhao <zhaorui126@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Reviewed-by: Richard Guo <guofenglinux@gmail.com> Discussion: https://postgr.es/m/CAHWVJhGuaFFRpmq4j+mcMcm_HC5QOT7LZsC9bf9b7BCBmvbfMA@mail.gmail.com Backpatch-through: 19
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.