pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ross J. Reedstrom <reedstrm@rice.edu>
To: Bruce Momjian <pgman@candle.pha.pa.us>
To: Roland Roberts <roland@astrofoto.org>
Cc: Yudie <yudie@axiontech.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to select and result row number??
Date: Tue, 17 Sep 2002 09:46:35 -0500
Message-ID: <20020917144635.GB6538@rice.edu> (raw)
In-Reply-To: <m265x4r9ct.fsf@kuiper.rlent.pnet> <200209170312.g8H3C6s16543@candle.pha.pa.us>
References: <001301c25b5a$2ef0ea40$8a01a8c0@yudie>
	<m265x4r9ct.fsf@kuiper.rlent.pnet>
	<001301c25b5a$2ef0ea40$8a01a8c0@yudie>
	<200209170312.g8H3C6s16543@candle.pha.pa.us>

On Mon, Sep 16, 2002 at 11:12:06PM -0400, Bruce Momjian wrote:
> Yudie wrote:
<wants numbered records>

> 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



view thread (9+ messages)  latest in thread

Message-ID: <20020917144635.GB6538@rice.edu>
Permalink:  ../20020917144635.GB6538@rice.edu/
Also on:    postgresql.org/message-id/20020917144635.GB6538@rice.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: reedstrm@rice.edu, pgman@candle.pha.pa.us, roland@astrofoto.org, yudie@axiontech.com
  Subject: Re: How to select and result row number??
  In-Reply-To: <20020917144635.GB6538@rice.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox