Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable

From: Daniel Grace <dgrace(at)wingsnw(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable
Date: 2009-04-25 00:58:11
Message-ID: 8a80df380904241758x49f1d9c6rc0239cded1a8c9d3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Apr 24, 2009 at 5:38 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Daniel Grace <dgrace(at)wingsnw(dot)com> writes:
> > The following nonsensical query causes PostgreSQL to fail with ERROR:
> plan
> > should not reference subplan's variable. (This was stripped down from an
> > 'useful' query that triggered the same bug). First encountered on 8.3.4,
> > reproduced on 8.3.7
>
> Hmmm ... I guess something is getting confused about the level of query
> nesting. FWIW, you can avoid the bug in these two examples by omitting
> the inner "SELECT" keyword, which is useless anyway. Perhaps it is
> needed in your real query though ...
>
> regards, tom lane
>
It's required in my case to force the aggregate function to evaluate its
inputs in a set order. I'm trying to replace MySQL's GROUP_CONCAT function,
including the ORDER BY option.

I had another variation (that did not use sub-SELECTs, but instead joining
something along the lines of joining (SELECT * FROM foo ORDER BY fname) AS
foo that partially worked -- however, it had the side effect (due to the
nature of the query) of having some duplicate data and not the type that
could be fixed simply by adding DISTINCT.

I'm not going to spam the list with all of the table definitions for the
real query, but I will paste it by itself to give a better idea of what I
was originally attempting:

SELECT
s.fid, c.flags, c.id, c.title, cs.ut_start, cs.ut_end, cr.full_name,
cal.title AS cancel_reason,
(SELECT GROUP_CONCAT((SELECT s2.fname FROM student AS s2 WHERE s2.id=
s.id ORDER BY fname), '; ')) AS students,
(SELECT GROUP_CONCAT((SELECT p.gname FROM course_teacher AS ct INNER
JOIN person AS p ON ct.tid=p.id WHERE ct.cid=c.id ORDER BY p.gname), '; '))
AS teacher
FROM
student AS s
INNER JOIN student_course_session AS scs ON scs.sid=s.id
INNER JOIN course_session AS cs ON cs.id=scs.csid
INNER JOIN course AS c ON c.id=cs.cid
LEFT JOIN course_room AS cr ON cr.id=c.room_id
LEFT JOIN calendar AS cal ON cal.id=cs.cancelled_by
GROUP BY s.fid, cs.id, c.flags, c.id, c.title, cs.ut_start, cs.ut_end,
cr.full_name, cal.title, cs.tsstart
ORDER BY s.fid, cs.tsstart, c.title;

-- Daniel Grace

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2009-04-25 01:58:03 Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable
Previous Message Tom Lane 2009-04-25 00:38:13 Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable