get sequence value of insert command

hi

create sequence mysequence;

create table foo(
id integer default nextval('mysequence'),
bla text,
wombat integer,
foobar date,
primary key(id)
);

insert into foo (wombat) values (88);

now how do i know the id of my newly inserted element? and
how can this be done in a completely concurrency safe way?


cya
erik

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
Erik [ Fr, 19 November 2004 09:41 ] [ ID #492436 ]

Re: get sequence value of insert command

O Erik Thiele έγραψε στις Nov 19, 2004 :

> hi
>
> create sequence mysequence;
>
> create table foo(
> id integer default nextval('mysequence'),
> bla text,
> wombat integer,
> foobar date,
> primary key(id)
> );
>
> insert into foo (wombat) values (88);
>
> now how do i know the id of my newly inserted element? and
> how can this be done in a completely concurrency safe way?

The way to do this is by reading the docs :)

use currval, it is session safe.

>
>
> cya
> erik
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>

--
-Achilleus


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html
achill [ Fr, 19 November 2004 10:37 ] [ ID #492437 ]

Re: get sequence value of insert command

On Nov 19, 2004, at 5:41 PM, Erik Thiele wrote:

> now how do i know the id of my newly inserted element? and
> how can this be done in a completely concurrency safe way?

This is a FAQ (4.15.1, among others). See currval() and nextval() in
the documentation as well.

Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org
grzm [ Fr, 19 November 2004 10:20 ] [ ID #492438 ]

Re: get sequence value of insert command

Achilleus Mantzios wrote:
>>
>>now how do i know the id of my newly inserted element? and
>>how can this be done in a completely concurrency safe way?
>
>
> The way to do this is by reading the docs :)
>
> use currval, it is session safe.

The increase in this question suggests the number of new users has
increased since 8.0 went into beta-test. It's pretty much the first
question anyone asks.

I seem to recall it was mine. I made the mistake of assuming it wasn't
concurrency safe and was gently corrected by one of the community. I
think it might have been a Tim/Tam Lane. Wonder what happened to him? :-)

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org
dev [ Fr, 19 November 2004 10:26 ] [ ID #492439 ]

Re: get sequence value of insert command

> I seem to recall it was mine. I made the mistake of assuming it wasn't
> concurrency safe and was gently corrected by one of the community. I
> think it might have been a Tim/Tam Lane. Wonder what happened to him? :-)

Mmmmm.... tim tams

rgds
Homer

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
Iain [ Fr, 19 November 2004 10:49 ] [ ID #492440 ]

Re: get sequence value of insert command

> -----Original Message-----
> From: Erik Thiele [mailto:erik [at] thiele-hydraulik.de]
> Sent: Friday, November 19, 2004 3:42 AM
> To: pgsql-sql [at] postgresql.org
> Subject: [SQL] get sequence value of insert command
>
>
> hi
>
> create sequence mysequence;
>
> create table foo(
> id integer default nextval('mysequence'),
> bla text,
> wombat integer,
> foobar date,
> primary key(id)
> );
>
> insert into foo (wombat) values (88);
>
> now how do i know the id of my newly inserted element? and
> how can this be done in a completely concurrency safe way?

CREATE RULE get_pkey_on_insert AS ON INSERT TO foo DO SELECT
currval('mysequence') AS id LIMIT 1;

>
>
> cya
> erik
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo [at] postgresql.org)
Vadim.Passynkov [ Fr, 19 November 2004 16:57 ] [ ID #492936 ]

Re: get sequence value of insert command

> > > create sequence mysequence;
> > >
> > > create table foo(
> > > id integer default nextval('mysequence'),
> > > bla text,
> > > wombat integer,
> > > foobar date,
> > > primary key(id)
> > > );
> > >
> > > insert into foo (wombat) values (88);
> > >
> > > now how do i know the id of my newly inserted element? and
> > > how can this be done in a completely concurrency safe way?
> >
> > CREATE RULE get_pkey_on_insert AS ON INSERT TO foo DO SELECT
> > currval('mysequence') AS id LIMIT 1;
>
> now that one is really great! you should definitly add it to the
> faq. plus an additional explanation why the limit 1 is needed here.

INSERT INTO foo ( ... ) ( SELECT * FROM foo1 );

>
> thanks!
> erik
>

--
Vadim Passynkov

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo [at] postgresql.org so that your
message can get through to the mailing list cleanly
Vadim.Passynkov [ Mo, 22 November 2004 15:58 ] [ ID #498942 ]
Datenbanken » comp.databases.postgresql.sql » get sequence value of insert command

Vorheriges Thema: Missing SELECT INTO ... DEFAULT VALUES in plpgsql for composite t
Nächstes Thema: Doubt :- Image_Insert