Re: ARRAY(subquery) volatility

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: ARRAY(subquery) volatility
Date: 2005-08-16 13:56:07
Message-ID: 292.1124200567@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> Why does the first query below return the same value for each row
> while the second query returns random values? Planner optimization?

> test=> SELECT ARRAY(SELECT random()) FROM generate_series(1, 5);
> test=> SELECT ARRAY(SELECT random() + x * 0) FROM generate_series(1, 5) AS g(x);

The sub-SELECT in the first one is considered an uncorrelated subquery,
so you get a plan that evaluates the subquery just once:

Function Scan on generate_series (cost=0.01..12.51 rows=1000 width=0)
InitPlan
-> Result (cost=0.00..0.01 rows=1 width=0)

In the second case, x is an outer parameter to the subquery, so it has
to be re-evaluated for each row of the outer query:

Function Scan on generate_series g (cost=0.00..32.50 rows=1000 width=4)
SubPlan
-> Result (cost=0.00..0.02 rows=1 width=0)

Note the "InitPlan" vs "SubPlan" labels --- they look similar, but the
evaluation rules are totally different.

The fact that there's a volatile function in the subquery isn't
considered while making this decision. I'm not sure if it should be.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2005-08-16 14:05:43 Re: Testing of MVCC
Previous Message Tino Wildenhain 2005-08-16 13:45:36 Re: Testing of MVCC