RE: INSERT ... RETURNING as Oracle - Mailing list pgsql-general

From Michael Ansley
Subject RE: INSERT ... RETURNING as Oracle
Date
Msg-id 7F124BC48D56D411812500D0B747251480F474@FILESERVER002
Whole thread Raw
In response to INSERT ... RETURNING as Oracle  ("Sipos Andras" <s-andras-nospam4@freemail.hu>)
List pgsql-general

And using MAX is also horrifically slow once you start having any significant volumes of data.

Why not write a function to add the info, which selects from a sequence, inserts the new row using the sequence number, and then returns the sequence number?  I've done it a number of times, and it's been quite successful so far.

Cheers...

MikeA


-----Original Message-----
From: Peter Eisentraut
To: Sipos Andras
Cc: pgsql-general@postgresql.org
Sent: 3-4-01 8:04 PM
Subject: Re: [GENERAL] INSERT ... RETURNING as Oracle

Sipos Andras writes:

> create table basket (
>   id   serial  NOT NULL PRIMARY KEY,
>   timestamp  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
> );
>
> How can I make a one step insert into the table and get values of 'ID'
?
> I am trying to find a similar solution as in the oracle's INSERT ...
> RETURNING.

We have this sort of thing on the wish list, but it doesn't exist yet.
Some people feel it's better to calculate the default in the client
program and insert it explicitly.  This may be a reasonable alternative
for some applications.

> If I use at first the INSERT, and after SELECT MAX(ID), the result
will be
> uncertain.

If you are worried about other transactions getting in the way then you
should do SET TRANSACTION ISOLATION LEVEL SERIABLIZABLE.

--
Peter Eisentraut      peter_e@gmx.net       http://yi.org/peter-e/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
Nick West - Global Infrastructure Manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

pgsql-general by date:

Previous
From: "Paulo Parola"
Date:
Subject: How to automatically start postmastrer with TCP/IP support (-i option) ???
Next
From: Bill Barnes
Date:
Subject: pgaccess on Win9x