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

From: Moshe Jacobson <moshe(at)neadwerx(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: David Johnston <polobo(at)yahoo(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Bug? Function with side effects not evaluated in CTE
Date: 2013-10-21 19:40:22
Message-ID: CAJ4CxLnkHhqutONJ9CP9M+qf3ORjZ0rCko6ARkp7izBpz4kyEg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Oct 21, 2013 at 2:53 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Consider
>
> SELECT volatile_function(i) FROM generate_series(1, 10) i LIMIT 1;
>
> How many times should the volatile function get executed? If your answer
> is not "10", how is this different from the CTE case? This LIMIT clause
> is restricting the number of times the function executes in pretty much
> the same way that our definition of CTE evaluation does, AFAICS.
>

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.
But if there is a volatile function, then the CTE query should always be
evaluated just like CREATE TEMP TABLE.
There is no question as to how many times to evaluate it here. It is just
once.

Moshe Jacobson
Manager of Systems Engineering, Nead Werx Inc. <http://www.neadwerx.com>
2323 Cumberland Parkway · Suite 201 · Atlanta, GA 30339

"Quality is not an act, it is a habit." -- Aristotle

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2013-10-21 19:49:16 Re: Bug? Function with side effects not evaluated in CTE
Previous Message John R Pierce 2013-10-21 19:29:52 Re: Upgrade from 9.1 to 9.10