Re: [HACKERS] Why does the sequence skip a number with generate_series? - Mailing list pgsql-sql

From Tom Lane
Subject Re: [HACKERS] Why does the sequence skip a number with generate_series?
Date
Msg-id 21286.1191511096@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] Why does the sequence skip a number with generate_series?  (Alvaro Herrera <alvherre@commandprompt.com>)
List pgsql-sql
Alvaro Herrera <alvherre@commandprompt.com> writes:
> Shane Ambler wrote:
>>> CREATE TABLE jefftest ( id serial, num int );
>>> INSERT INTO jefftest (num) values (generate_series(1,10));
>>> INSERT INTO jefftest (num) values (generate_series(11,20));
>>> INSERT INTO jefftest (num) values (generate_series(21,30));

> Don't use set-returning functions in "scalar context".

I think what is actually happening is that the expanded targetlist is
nextval('seq'), generate_series(1,10)

On the eleventh iteration, generate_series() returns ExprEndResult to
show that it's done ... but the 11th nextval() call already happened.
If you switched the columns around, you wouldn't get the extra call.

If you think that's bad, the behavior with multiple set-returning
functions in the same targetlist is even stranger.  The whole thing
is a mess and certainly not something we would've invented if we
hadn't inherited it from Berkeley.
        regards, tom lane


pgsql-sql by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] Why does the sequence skip a number with generate_series?
Next
From: "Tore Lukashaugen"
Date:
Subject: What SQL is running against my DB?