Re: rownum

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: chester c young <chestercyoung(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: rownum
Date: 2003-02-14 05:20:34
Message-ID: 4353.1045200034@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Josh Berkus <josh(at)agliodbs(dot)com> writes:
>> sorry about this - braindead and cannot find in doc. what's pg's
>> rownum pseudo-column or function name that returns the record number of
>> a set?

> There isn't one, unless there's something in /contrib that you can build.

Right now the only way I've heard of is to use a sequence, for example

create temp sequence rownum;

select nextval('rownum'), ... from ...;

drop sequence rownum;

This is a hack, and it will fail if the SELECT involves any sort
specification (not only ORDER BY, but DISTINCT) because the nextval()s
will be computed before sorting. You can get around that with

select nextval('rownum'), * from (select ... order by ...) sub;

The overhead of using a sequence for this is pretty annoying. It would
be a simple matter to write a C function that emits sequential values
without any database access (see pg_stat_get_backend_idset() for some
inspiration). But you'd still need the subselect to avoid getting
re-sorted. AFAICS any rownum() function that doesn't behave like that
is a flat violation of the SQL standard...

regards, tom lane

In response to

  • Re: rownum at 2003-02-14 04:56:20 from Josh Berkus

Responses

  • Re: rownum at 2003-02-14 10:10:15 from Richard Huxton

Browse pgsql-sql by date

  From Date Subject
Next Message Joe Conway 2003-02-14 05:51:41 Re: dblink question please
Previous Message Stephan Szabo 2003-02-14 04:57:33 Re: [SQL] Passing arrays