Re: Bug? Function with side effects not evaluated in CTE

From: Rowan Collins <rowan(dot)collins(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Bug? Function with side effects not evaluated in CTE
Date: 2013-10-21 22:10:59
Message-ID: 5265A673.5090002@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 21/10/2013 20:40, Moshe Jacobson wrote:
> I don't think your example above is analogous, because in your
> example, you are asking /how many times/ to execute the function,
> whereas in my example, the question is /whether/ to execute the query
> at all.
> If the outer statement of the CTE doesn't need to use the contents of
> the CTE, and there is no volatile function in there, then I agree that
> it's fine not to execute it.

Surely the point is that "not executing at all" is just a particular
case of "executing N times" where N is calculated as zero?

Your original example was this:

|with tt_created as
(
select fn_new_item(1) as item
)|

That CTE has a maximum of 1 row, so the choices are only 0 or 1. But how
is it fundamentally different from this:

|with tt_created as
(
select fn_new_item(i) as item from generate_series(1, 10) as i
)|

This CTE has up to 10 rows, but the current optimization (as I
understand it) might mean that only 5 of those are calculated. It might
also decide that 0 of them are calculated, but that's not a
qualitatively different decision, it's just a different value for the
"how many rows do you need" parameter.

Personally, I'm definitely in the "this is surprising behaviour" camp,
although "surprising" and "wrong" aren't *necessarily* the same thing,
so I'll leave it to greater minds to decide on the latter...

--
Rowan Collins
[IMSoP]

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message BladeOfLight16 2013-10-21 22:52:42 Re: Bug? Function with side effects not evaluated in CTE
Previous Message David Johnston 2013-10-21 20:56:27 Re: Bug? Function with side effects not evaluated in CTE