Re: subselect bug (was Re: [GENERAL] DBLink: interesting issue)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Oleg Lebedev <oleg(dot)lebedev(at)waterford(dot)org>
Subject: Re: subselect bug (was Re: [GENERAL] DBLink: interesting issue)
Date: 2002-09-24 18:48:32
Message-ID: 10408.1032893312@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Joe Conway <mail(at)joeconway(dot)com> writes:
> replica=# create table foo(f1 int);
> CREATE TABLE
> replica=# SELECT * FROM foo t WHERE NOT EXISTS (SELECT remoteid FROM (SELECT
> f1 as remoteid FROM foo WHERE f1 = t.f1) AS t1);
> server closed the connection unexpectedly

Got it --- this bug has been there awhile :-(, ever since we had the
pull-up-subquery logic, which was in 7.1 IIRC. The pullup code
neglected to adjust references to uplevel Vars. Surprising that no one
reported this sooner.

The attached patch is against CVS tip. It will not apply cleanly to
7.2 because pull_up_subqueries() has been modified since then, but if
anyone's desperate for a fix in 7.2 it could probably be adapted.

regards, tom lane

*** src/backend/optimizer/plan/planner.c.orig Wed Sep 4 17:30:30 2002
--- src/backend/optimizer/plan/planner.c Tue Sep 24 14:02:54 2002
***************
*** 337,352 ****

/*
* Now make a modifiable copy of the subquery that we can run
! * OffsetVarNodes on.
*/
subquery = copyObject(subquery);

/*
! * Adjust varnos in subquery so that we can append its
* rangetable to upper query's.
*/
rtoffset = length(parse->rtable);
OffsetVarNodes((Node *) subquery, rtoffset, 0);

/*
* Replace all of the top query's references to the subquery's
--- 337,358 ----

/*
* Now make a modifiable copy of the subquery that we can run
! * OffsetVarNodes and IncrementVarSublevelsUp on.
*/
subquery = copyObject(subquery);

/*
! * Adjust level-0 varnos in subquery so that we can append its
* rangetable to upper query's.
*/
rtoffset = length(parse->rtable);
OffsetVarNodes((Node *) subquery, rtoffset, 0);
+
+ /*
+ * Upper-level vars in subquery are now one level closer to their
+ * parent than before.
+ */
+ IncrementVarSublevelsUp((Node *) subquery, -1, 1);

/*
* Replace all of the top query's references to the subquery's

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Beutin 2002-09-24 19:01:09 Re: rotatelog / logrotate with PostgreSQL
Previous Message Tom Lane 2002-09-24 18:09:01 Re: rotatelog / logrotate with PostgreSQL

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 2002-09-24 18:49:41 Re: PGXLOG variable worthwhile?
Previous Message Peter Eisentraut 2002-09-24 18:07:27 Re: Default privileges for 7.3