pgsql: Prevent pushing down WHERE clauses into unsafe UNION/INTERSECT n - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Prevent pushing down WHERE clauses into unsafe UNION/INTERSECT n
Date
Msg-id E1UkR8o-0004tz-68@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Prevent pushing down WHERE clauses into unsafe UNION/INTERSECT nests.

The planner is aware that it mustn't push down upper-level quals into
subqueries if the quals reference subquery output columns that contain
set-returning functions or volatile functions, or are non-DISTINCT outputs
of a DISTINCT ON subquery.  However, it missed making this check when
there were one or more levels of UNION or INTERSECT above the dangerous
expression.  This could lead to "set-valued function called in context that
cannot accept a set" errors, as seen in bug #8213 from Eric Soroos, or to
silently wrong answers in the other cases.

To fix, refactor the checks so that we make the column-is-unsafe checks
during subquery_is_pushdown_safe(), which already has to recursively
inspect all arms of a set-operation tree.  This makes
qual_is_pushdown_safe() considerably simpler, at the cost that we will
spend some cycles checking output columns that possibly aren't referenced
in any upper qual.  But the cases where this code gets executed at all
are already nontrivial queries, so it's unlikely anybody will notice any
slowdown of planning.

This has been broken since commit 05f916e6add9726bf4ee046e4060c1b03c9961f2,
which makes the bug over ten years old.  A bit surprising nobody noticed it
before now.

Branch
------
REL8_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/5e9027bd50c584951f4f85ba6c209742c7b23d95

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |  216 +++++++++++++++++++--------------
src/test/regress/expected/union.out   |   34 ++++++
src/test/regress/sql/union.sql        |   20 +++
3 files changed, 179 insertions(+), 91 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Prevent pushing down WHERE clauses into unsafe UNION/INTERSECT n
Next
From: Bruce Momjian
Date:
Subject: pgsql: pg_upgrade: document that --link should be used with --check