pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #5754: CTE optimization fails to account for side effects
3+ messages / 2 participants
[nested] [flat]
* BUG #5754: CTE optimization fails to account for side effects
@ 2010-11-15 22:11 David Fetter <david@fetter.org>
2010-11-15 23:55 ` Re: BUG #5754: CTE optimization fails to account for side effects Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 3+ messages in thread
From: David Fetter @ 2010-11-15 22:11 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged online:
Bug reference: 5754
Logged by: David Fetter
Email address: david@fetter.org
PostgreSQL version: 8.4+
Operating system: All
Description: CTE optimization fails to account for side effects
Details:
Here's how to reproduce:
BEGIN;
CREATE SEQUENCE my_seq;
WITH t AS (SELECT nextval('my_seq')) VALUES(1);
SELECT currval('my_seq');
ERROR: currval of sequence "my_seq" is not yet defined in this session
What's happened is that the optimization didn't account for the idea that a
SELECT might have a side effect, and if we're going with the "CTEs execute
exactly once and (equivalent to) fully," this is a bug.
CTEs should not optimize away (parts of) SELECTs that have side effects.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #5754: CTE optimization fails to account for side effects
2010-11-15 22:11 BUG #5754: CTE optimization fails to account for side effects David Fetter <david@fetter.org>
@ 2010-11-15 23:55 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-11-16 14:35 ` Re: BUG #5754: CTE optimization fails to account for side effects David Fetter <david@fetter.org>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2010-11-15 23:55 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: pgsql-bugs
"David Fetter" <david@fetter.org> writes:
> CREATE SEQUENCE my_seq;
> WITH t AS (SELECT nextval('my_seq')) VALUES(1);
> SELECT currval('my_seq');
> ERROR: currval of sequence "my_seq" is not yet defined in this session
> What's happened is that the optimization didn't account for the idea that a
> SELECT might have a side effect, and if we're going with the "CTEs execute
> exactly once and (equivalent to) fully," this is a bug.
The reason it's not a bug is that we have not adopted that position.
There is a proposal to make it so for wCTEs, but that doesn't mean
we should change the existing, documented and useful behavior of regular
CTEs.
(If you're wondering where it's documented, I cite section 7.8's
statement that only as much of a CTE query is evaluated as is read by
the parent query. The limiting case of that is no reference -> no rows
read.)
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #5754: CTE optimization fails to account for side effects
2010-11-15 22:11 BUG #5754: CTE optimization fails to account for side effects David Fetter <david@fetter.org>
2010-11-15 23:55 ` Re: BUG #5754: CTE optimization fails to account for side effects Tom Lane <tgl@sss.pgh.pa.us>
@ 2010-11-16 14:35 ` David Fetter <david@fetter.org>
0 siblings, 0 replies; 3+ messages in thread
From: David Fetter @ 2010-11-16 14:35 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On Mon, Nov 15, 2010 at 06:55:47PM -0500, Tom Lane wrote:
> "David Fetter" <david@fetter.org> writes:
> > CREATE SEQUENCE my_seq;
> > WITH t AS (SELECT nextval('my_seq')) VALUES(1);
> > SELECT currval('my_seq');
>
> > ERROR: currval of sequence "my_seq" is not yet defined in this
> > session
>
> > What's happened is that the optimization didn't account for the
> > idea that a SELECT might have a side effect, and if we're going
> > with the "CTEs execute exactly once and (equivalent to) fully,"
> > this is a bug.
>
> The reason it's not a bug is that we have not adopted that position.
> There is a proposal to make it so for wCTEs, but that doesn't mean
> we should change the existing, documented and useful behavior of
> regular CTEs.
The documented and useful behavior is of read-only CTEs, and since
we've decided that CTEs that cause writes are to behave this way,
simply rewording them as function calls from SELECT shouldn't change
this.
> (If you're wondering where it's documented, I cite section 7.8's
> statement that only as much of a CTE query is evaluated as is read
> by the parent query. The limiting case of that is no reference ->
> no rows read.)
We can fix this inconsistency in the case of data-changing SELECTs and
not damage any code. I seriously doubt that people are using the
current behavior as a write fence.
Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2010-11-16 14:35 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-11-15 22:11 BUG #5754: CTE optimization fails to account for side effects David Fetter <david@fetter.org>
2010-11-15 23:55 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-11-16 14:35 ` David Fetter <david@fetter.org>
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