Re: Surprising (?) Sequence Behavior

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Richard M(dot) Kues" <software(at)riva(dot)at>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Surprising (?) Sequence Behavior
Date: 2008-01-28 21:13:24
Message-ID: 7237.1201554804@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Richard M. Kues" <software(at)riva(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-01-28 21:38:01 Re: handling of COUNT(record) vs IS NULL
Previous Message Greg Sabino Mullane 2008-01-28 20:15:54 Re: [GENERAL] SHA1 on postgres 8.3