Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id EF1314767D8 for ; Tue, 17 Sep 2002 10:46:33 -0400 (EDT) Received: from ece.rice.edu (ece.rice.edu [128.42.4.34]) by postgresql.org (Postfix) with ESMTP id 3ABCF4767D4 for ; Tue, 17 Sep 2002 10:46:33 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by ece.rice.edu (Postfix) with ESMTP id 0B02D68A6B; Tue, 17 Sep 2002 09:46:36 -0500 (CDT) Received: from wallace.ece.rice.edu (wallace.ece.rice.edu [128.42.12.154]) by ece.rice.edu (Postfix) with ESMTP id 7804B68A5B; Tue, 17 Sep 2002 09:46:35 -0500 (CDT) Received: from reedstrm by wallace.ece.rice.edu with local (Exim 3.34 #1 (Debian)) id 17rJcZ-0001lj-00; Tue, 17 Sep 2002 09:46:35 -0500 Date: Tue, 17 Sep 2002 09:46:35 -0500 From: "Ross J. Reedstrom" To: Bruce Momjian , Roland Roberts Cc: Yudie , pgsql-sql@postgresql.org Subject: Re: How to select and result row number?? Message-ID: <20020917144635.GB6538@rice.edu> Mail-Followup-To: "Ross J. Reedstrom" , Bruce Momjian , Roland Roberts , Yudie , pgsql-sql@postgresql.org References: <001301c25b5a$2ef0ea40$8a01a8c0@yudie> <001301c25b5a$2ef0ea40$8a01a8c0@yudie> <200209170312.g8H3C6s16543@candle.pha.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200209170312.g8H3C6s16543@candle.pha.pa.us> User-Agent: Mutt/1.3.27i X-Virus-Scanned: by AMaViS snapshot-20020300 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/193 X-Sequence-Number: 9470 On Mon, Sep 16, 2002 at 11:12:06PM -0400, Bruce Momjian wrote: > Yudie wrote: > Good question. The only easy answer I have is the creation of a temp > table with a SERIAL column: > > CREATE TEMP TABLE out (cnt SERIAL, other_cols...); > INSERT INTO out SELECT ... ORDER BY col; Hmm, this needs to be: INSERT INTO out (ther_cols...) SELECT ... ORDER BY col; So that the cnt column gets filled from the default. > SELECT * FROM out; On Tue, Sep 17, 2002 at 10:14:58AM -0400, Roland Roberts wrote: > create sequence temp_counter; > select nextval('temp_counter'), * from whatever; > > If you have an 'order by', the above will not work. You could then > try either building a temporary table or using a subselect > > select nextval('temp_counter'), * from (select .... order by ...); Approximately the same solution, but without saving the result in a temp table. Hmm, with the new dependency code, is the auto-sequence from a SERIAL in a temp table also a temp sequence? It get's put in the temp schema, right? Seems we have a workaround for those wanting numbered result sets. BTW, Bruce's solution could be useful for those times you want to batch a potentially large return set, but can't use cursors - the temp table will live as long as the connection does, so as long as your connection pool doesn't play round robin games on you, you can use the result count from the INSERT to calculate batch sizes, and use OFFSET and LIMIT on SELECTs from the temp table. Ross