Re: Surprising (?) Sequence Behavior - Mailing list pgsql-general

From Tom Lane
Subject Re: Surprising (?) Sequence Behavior
Date
Msg-id 7237.1201554804@sss.pgh.pa.us
Whole thread Raw
In response to Surprising (?) Sequence Behavior  ("Richard M. Kues" <software@riva.at>)
Responses Re: Surprising (?) Sequence Behavior
List pgsql-general
"Richard M. Kues" <software@riva.at> writes:
> CREATE TEMPORARY SEQUENCE s;

> SELECT
>     nextval('s'), t.name
> FROM
> (
>     SELECT
>        tablename AS name
>     FROM
>        pg_tables
>     ORDER BY
>        tablename
> ) AS t
> WHERE
>     t.name = 'pg_am'
> ;

> The result is:
>     1 pg_am

> instead of:
>     2 pg_am

> At least for me this is surprising!

Why do you find it surprising?  Per spec, the SELECT output list is not
evaluated at rows that fail the WHERE clause.  This must be so; consider
examples like
    SELECT 1/x FROM t WHERE x <> 0;

I think what you need is three levels of nested SELECT, with the
nextval() done in the middle level, and probably an "OFFSET 0" in the
middle one to keep Postgres from collapsing the top and middle together.

            regards, tom lane

pgsql-general by date:

Previous
From: Decibel!
Date:
Subject: Re: [pgsql-advocacy] PostgreSQL professionals group at LinkedIn.com
Next
From: Tom Lane
Date:
Subject: Re: handling of COUNT(record) vs IS NULL