X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9A311475DDB for ; Fri, 14 Feb 2003 00:20:35 -0500 (EST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 9CC25475DC8 for ; Fri, 14 Feb 2003 00:20:30 -0500 (EST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.6/8.12.6) with ESMTP id h1E5KY5u004354; Fri, 14 Feb 2003 00:20:34 -0500 (EST) To: Josh Berkus Cc: chester c young , pgsql-sql@postgresql.org Subject: Re: rownum In-reply-to: <200302132056.20122.josh@agliodbs.com> References: <20030214043800.99417.qmail@web12707.mail.yahoo.com> <200302132056.20122.josh@agliodbs.com> Comments: In-reply-to Josh Berkus message dated "Thu, 13 Feb 2003 20:56:20 -0800" Date: Fri, 14 Feb 2003 00:20:34 -0500 Message-ID: <4353.1045200034@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/317 X-Sequence-Number: 11715 Josh Berkus 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