Re: BUG #4902: Subquery in VALUES referencing a CTE - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #4902: Subquery in VALUES referencing a CTE
Date
Msg-id 22085.1246847302@sss.pgh.pa.us
Whole thread Raw
In response to BUG #4902: Subquery in VALUES referencing a CTE  ("Marko Tiikkaja" <marko.tiikkaja@cs.helsinki.fi>)
List pgsql-bugs
"Marko Tiikkaja" <marko.tiikkaja@cs.helsinki.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

pgsql-bugs by date:

Previous
From: Steve Purcell
Date:
Subject: Re: BUG #4901: Column name "window" breaks pg_dump/pg_restore
Next
From: Oleg Serov
Date:
Subject: Re: Diffrent column ordering after dump/restore tables with INHERITS