pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mark Murawski <markm-lists@intellasoft.net>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Subquery with toplevel reference used to work in pg 8.4
Date: Sat, 24 Mar 2012 12:35:11 -0400
Message-ID: <19235.1332606911@sss.pgh.pa.us> (raw)
In-Reply-To: <4F6D47E8.2040209@intellasoft.net>
References: <4F6D47E8.2040209@intellasoft.net>

Mark Murawski <markm-lists@intellasoft.net> writes:
> I agree the query is a little odd, but I like backwards compatibility!

AFAICT, 8.4 is broken too --- did you try any cases where the
WHERE-condition should filter rows?

I created this similar test case using the regression database:

select * from
  int8_tbl t1 left join
  (select q1 as x, 42 as y from int8_tbl t2) ss
  on t1.q2 = ss.x
where
  1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1);

The raw output without any WHERE clause is

        q1        |        q2         |        x         | y  
------------------+-------------------+------------------+----
              123 |               456 |                  |   
              123 |  4567890123456789 | 4567890123456789 | 42
              123 |  4567890123456789 | 4567890123456789 | 42
              123 |  4567890123456789 | 4567890123456789 | 42
 4567890123456789 |               123 |              123 | 42
 4567890123456789 |               123 |              123 | 42
 4567890123456789 |  4567890123456789 | 4567890123456789 | 42
 4567890123456789 |  4567890123456789 | 4567890123456789 | 42
 4567890123456789 |  4567890123456789 | 4567890123456789 | 42
 4567890123456789 | -4567890123456789 |                  |   
(10 rows)

The WHERE clause ought to be effectively just the same as "where ss.y is
not null", ie it should eliminate the two null-extended rows.  And
in 8.3 and before, that's what you get:

        q1        |        q2        |        x         | y  
------------------+------------------+------------------+----
              123 | 4567890123456789 | 4567890123456789 | 42
              123 | 4567890123456789 | 4567890123456789 | 42
              123 | 4567890123456789 | 4567890123456789 | 42
 4567890123456789 |              123 |              123 | 42
 4567890123456789 |              123 |              123 | 42
 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
 4567890123456789 | 4567890123456789 | 4567890123456789 | 42
(8 rows)

but 8.4 and 9.0 produce all 10 rows, ie no filtering happens.
And 9.1 and HEAD produce
ERROR:  Upper-level PlaceHolderVar found where not expected

After investigating, I believe the problem is that
SS_replace_correlation_vars needs to replace outer PlaceHolderVars just
as if they were outer Vars.  What is getting pushed into the subquery
is a PlaceHolderVar wrapping the constant 42, and if that's left alone
then the subquery WHERE clause ends up as just "42 is not null", which
is not what we need.  That has to be converted into a Param referencing
a value from the outer query.  9.1 and HEAD are correctly bleating about
the fact that this outer-level PlaceHolderVar shouldn't be there by the
time the complaining code runs.

Kinda surprising that this bug escaped detection this long ...

			regards, tom lane



view thread (3+ messages)

Message-ID: <19235.1332606911@sss.pgh.pa.us>
Permalink:  ../19235.1332606911@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/19235.1332606911@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, markm-lists@intellasoft.net
  Subject: Re: Subquery with toplevel reference used to work in pg 8.4
  In-Reply-To: <19235.1332606911@sss.pgh.pa.us>
  References: <4F6D47E8.2040209@intellasoft.net> <19235.1332606911@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox