Re: new rows based on existing rows - Mailing list pgsql-general

From Chris Angelico
Subject Re: new rows based on existing rows
Date
Msg-id CAPTjJmoSr=vGeQ41pfPQpJ-ye6vfQnmq9XNpLy778+2eHPykTg@mail.gmail.com
Whole thread Raw
In response to new rows based on existing rows  (Andy Chambers <achambers@mcna.net>)
List pgsql-general
On Wed, May 2, 2012 at 10:52 PM, Andy Chambers <achambers@mcna.net> wrote:
> So ideally, I'd like to be able to do
>
> insert into foo (a,b,foo_date)
>  select a,b,now() from foo old where ....
>  returning oid, old.oid
>
> ...but this doesn't work.  It seems you only have access to the table
> being modified in a returning clause.  Is there a way I can return a
> simple mapping between old oids and new oids as part of the statement
> that inserts the new ones?

I'd recommend not using OIDs but having your own ID field (eg a
[BIG]SERIAL PRIMARY KEY). Is the mapping of old ID to new ID something
that would be worth saving into the table? Even if you don't need it
later, that might be the easiest way to do the job. Alternatively, you
could play around with joins (an INSERT RETURNING can quite happily be
used in a WITH clause) to see if you can get what you want that way.

ChrisA

pgsql-general by date:

Previous
From: Radosław Smogura
Date:
Subject: Re: Which Java persistence library would you use with PostgreSQL?
Next
From: leaf_yxj
Date:
Subject: Re: How to insert random character data into tables for testing purpose. THanks.