Disallow pushing volatile qual expressions down into DISTINCT subqueries.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 Jun 2014 18:08:48 +0000 (11:08 -0700)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 Jun 2014 18:08:48 +0000 (11:08 -0700)
commit1147035203a47a424b2399fc74829d097b7061e4
tree41b707100aa6fe16d7ae5383ddba19b07a0c23ef
parentf71136eeeb5c6a234e19a245db7ae1484fc7bf4f
Disallow pushing volatile qual expressions down into DISTINCT subqueries.

A WHERE clause applied to the output of a subquery with DISTINCT should
theoretically be applied only once per distinct row; but if we push it
into the subquery then it will be evaluated at each row before duplicate
elimination occurs.  If the qual is volatile this can give rise to
observably wrong results, so don't do that.

While at it, refactor a little bit to allow subquery_is_pushdown_safe
to report more than one kind of restrictive condition without indefinitely
expanding its argument list.

Although this is a bug fix, it seems unwise to back-patch it into released
branches, since it might de-optimize plans for queries that aren't giving
any trouble in practice.  So apply to 9.4 but not further back.
src/backend/optimizer/path/allpaths.c
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql