Re: Example of RETURNING clause to get auto-generated keys

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Ken Johanson <pg-user(at)kensystem(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Example of RETURNING clause to get auto-generated keys
Date: 2007-01-25 03:49:46
Message-ID: 20070125034946.GH26006@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ken Johanson wrote:
> >
> >Now playing devil's advocate, can anyone see scenarios where this will
> >not work as expected? Examples (descriptions not sql necessarily) of
> >those would be helpful too...
> >
>
> Just to be sure, will the RETURNING clause work with custom sequences
> (say, non numeric or increment by two) or other types of key
> generators?... And how will triggers interfere with it (if at all)?
>
> I honestly have limited experience with server generated keys that are
> not numeric/serial (or uuids), or with cases where triggers,
> constraints, etc might come into play (I'm used to using the DB mostly
> as a storage device and using server-side logic..)

As far as I know, RETURNING will give you exactly the values that are
put into the table. If you had a weird sequence or strange stuff
invoked in functions, they will be computed much earlier than the
RETURNING values be fetched, so the latter will get the correct values
all the time. (It would be quite dumb to do otherwise anyway).

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Neal Clark 2007-01-25 04:14:07 indexing primary and foreign keys w/lookup table
Previous Message John D. Burger 2007-01-25 03:31:51 Re: Example of RETURNING clause to get auto-generated keys