Skip site navigation (1) Skip section navigation (2)

Re: BUG #1083: Insert query reordering interacts badly with

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Martin Langhoff <martin(at)catalyst(dot)net(dot)nz>
Cc: PostgreSQL Bugs List <pgsql-bugs(at)postgresql(dot)org>,pgsql-sql(at)postgresql(dot)org
Subject: Re: BUG #1083: Insert query reordering interacts badly with
Date: 2004-02-27 13:46:53
Message-ID: 20040227134653.GA13683@wolff.to (view raw or flat)
Thread:
Lists: pgsql-bugspgsql-sql
I am going to try to move this over the sql list, since it doesn't belong
on bugs.

On Tue, Feb 24, 2004 at 23:47:48 +1300,
  Martin Langhoff <martin(at)catalyst(dot)net(dot)nz> wrote:
> Tom Lane wrote:
> 
> >How about
> >
> >SELECT nextval('seq');       -- ignore result
> >
> >INSERT INTO ... VALUES (currval('seq'), currval('seq'));
> > 
> >
> 
> Well, it works for my sample case, I have to agree. Maybe I should 
> mention that I tried to boil down the bugreport to the simplest repro 
> case I could.
> 
> My actual SQL looks roughly like
> 
>    INSERT INTO destination (record_id, page, row)
>    SELECT
>        (SELECT record_id FROM record ORDERED BY name),
>        (NEXTVAL('seq') / 200),
>        (CURRVAL('seq') % 200)
> 
> While I have a workaround, I am definitely curious as to whether there 
> is actually a way to do it. Thanks for your patience.

I think the following will do what you want:

    INSERT INTO destination (record_id, page, row)
    SELECT record_id, seq/200, seq%200 FROM
        (SELECT record_id, nextval('seq') as seq FROM record ORDERED BY name);

In response to

pgsql-bugs by date

Next:From: Tom LaneDate: 2004-02-27 16:59:34
Subject: Re: BUG #1086: lower and upper functions
Previous:From: Damon HartDate: 2004-02-27 13:38:53
Subject: Re: intermittent error: 'variable not found in subplan target

pgsql-sql by date

Next:From: Brian KnoxDate: 2004-02-27 16:54:04
Subject: Convert INT to INTERVAL?
Previous:From: Stephan SzaboDate: 2004-02-27 05:16:03
Subject: Re: Return more than a record

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group