Re: Subselects in select expressions

From: Royce Ausburn <royce(dot)ml(at)inomial(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Subselects in select expressions
Date: 2011-11-02 04:27:30
Message-ID: D9934B49-E2BD-4AEB-A675-9B002CD82938@inomial.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 02/11/2011, at 2:16 PM, Tom Lane wrote:

> Royce Ausburn <royce(dot)ml(at)inomial(dot)com> writes:
>> [ random() is evaluated only once in ]
>> test=# select (select random()) from generate_series(1,10); -- rows are the same
>
>> I understand that it's likely an optimisation thing -- postgres knows
>> that the subselect doesn't depend on the FROM rows so it evaluates it
>> only once, but is this really correct behaviour?
>
> I can't find anything in the SQL standard that explicitly addresses this
> point, but a bit of googling turns up documentation for both Oracle and
> DB/2 that say they evaluate uncorrelated subqueries just once:
> http://www.smart-soft.co.uk/Oracle/advanced-sql-tutorial.htm
> http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/sqlp/rbafycorrs.htm
> whereas correlated subqueries are evaluated once per row.
>
> So we're in good company here. Even if we were not, I'd be loath to
> change it, because people have historically relied on this behavior to
> force single evaluation of what otherwise would be considered volatile
> functions.

That's good enough for me, Tom. Thanks =)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Roger Niederland 2011-11-02 04:49:35 Re: variable not found in subplan target list
Previous Message Ondrej Ivanič 2011-11-02 04:21:15 Re: Regarding the shared disk fail over cluster configuration