Josh Berkus <josh@agliodbs.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