Re: Strange behavior of some volatile function like random(), nextval()

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alex Ignatov <a(dot)ignatov(at)postgrespro(dot)ru>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Strange behavior of some volatile function like random(), nextval()
Date: 2016-06-29 14:48:09
Message-ID: 7445.1467211689@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

[ Please do not quote the entire thread in each followup. That's
disrespectful of your readers' time, and will soon cause people to
stop reading the thread, meaning you don't get answers. ]

Alex Ignatov <a(dot)ignatov(at)postgrespro(dot)ru> writes:
> In this subquery(below) we have reference to outer variables but it is
> not working as it should(or i dont understand something):

> postgres=# postgres=# select id, ( select string_agg('a','') from
> generate_series(1,trunc(10*random()+1)::int) where id=id) from
> generate_series(1,10) as id;

The inner generate_series() call does not contain any outer references, so
it doesn't get recomputed. There's a comment in ExecReScanFunctionScan
about that:

* Here we have a choice whether to drop the tuplestores (and recompute
* the function outputs) or just rescan them. We must recompute if an
* expression contains changed parameters, else we rescan.
*
* XXX maybe we should recompute if the function is volatile? But in
* general the executor doesn't conditionalize its actions on that.

So you get some random number of generate_series output rows on the first
call, but then each subsequent run of the subquery just rescans those same
rows. I do not think this is wrong or bad, really; if it was done
differently, examples such as this same generate_series call on the inside
of a nestloop join would behave very strangely.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Shawn 2016-06-29 14:56:35 Re: An unkillable connection caused replication delay on my replica
Previous Message Alex Ignatov 2016-06-29 14:05:59 Re: Strange behavior of some volatile function like random(), nextval()