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

From Andy Chambers
Subject new rows based on existing rows
Date
Msg-id CAAfW55qWE01BL-M0yYJdPe7CLawFGxTDKt7Vm2UpKs5nP1BdLw@mail.gmail.com
Whole thread Raw
Responses Re: new rows based on existing rows
Re: new rows based on existing rows
List pgsql-general
Hi,

I frequently have a need to insert new rows into a table that are
based on existing rows but with small changes.

This is easy using something like

insert into foo (a,b,foo_date)
  select a,b,now() from foo old where ....
  returning oid

However in the application layer, I need to know which new record
corresponds with which original record

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?

Cheers,
Andy

--
Andy Chambers
Software Engineer
(e) achambers@mcna.net
(t) 954-682-0573

CONFIDENTIALITY NOTICE: This electronic mail may contain information
that is privileged, confidential, and/or otherwise protected from
disclosure to anyone other than its intended recipient(s). Any
dissemination or use of this electronic mail or its contents by
persons other than the intended recipient(s) is strictly prohibited.
If you have received this communication in error, please notify the
sender immediately by reply e-mail so that we may correct our internal
records. Please then delete the original message. Thank you.

pgsql-general by date:

Previous
From: Evan Martin
Date:
Subject: Re: SQL functions not being inlined
Next
From: "Albe Laurenz"
Date:
Subject: Re: Significance of Postgres (version 9.1.1) Compilation parameters - Performance