Re: BUG #4902: Subquery in VALUES referencing a CTE

Lists: pgsql-bugs
From: "Marko Tiikkaja" <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4902: Subquery in VALUES referencing a CTE
Date: 2009-07-05 19:27:47
Message-ID: 200907051927.n65JRlWQ018712@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4902
Logged by: Marko Tiikkaja
Email address: marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi
PostgreSQL version: 8.4.0
Operating system: Linux
Description: Subquery in VALUES referencing a CTE
Details:

While playing around with common table expressions, I found this:

=> with cte(foo) as ( values(0) ) values((select foo from cte));
ERROR: XX000: SubPlan found with no parent plan
LOCATION: ExecInitExpr, execQual.c:4343

In src/backend/executor/nodeValuesscan.c the comment near line 116 says that
subqueries inside VALUES should be initplans, but in this case we get a
SubPlan. Passing node instead of NULL to ExecInitExpr() below gives the
expected output.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Marko Tiikkaja" <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4902: Subquery in VALUES referencing a CTE
Date: 2009-07-06 02:28:22
Message-ID: 22085.1246847302@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Marko Tiikkaja" <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi> writes:
> While playing around with common table expressions, I found this:

> => with cte(foo) as ( values(0) ) values((select foo from cte));
> ERROR: XX000: SubPlan found with no parent plan

Oh, interesting, thanks for the bug report!

> In src/backend/executor/nodeValuesscan.c the comment near line 116 says that
> subqueries inside VALUES should be initplans, but in this case we get a
> SubPlan. Passing node instead of NULL to ExecInitExpr() below gives the
> expected output.

I don't think that's a safe fix because of the reason cited in that same
comment: it's going to result in dangling pointers and probable crashes
anytime the ValuesScan node contains multiple rows or is executed more
than once. The right fix is to make sure that the subquery really is an
initplan, which takes a bit of fooling around in the planner:
http://archives.postgresql.org/pgsql-committers/2009-07/msg00041.php

regards, tom lane