Re: SEQUENCE primary key

Lists: pgsql-general
From: gustavo halperin <ggh(dot)develop(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: SEQUENCE primary key
Date: 2007-02-13 19:52:05
Message-ID: 45D216E5.1020007@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello

I have a question, if I have a table with a SEQUENCE primary key, that
obviously, I doesn't give in each new row inserted. For example if the
table locks:
CREATE SEQUENCE id_seq;
CREATE TABLE table (
id integer DEFAULT nextval('id_seq') CONSTRAINT table_id
PRIMARY KEY,
arg1 integer,
arg2 integer
)

How can I know which id receive each new row. I mean,
<http://ultralingua.com/onlinedictionary/index.html?action=define&ignoreaccents=on&wholewords=on&searchtype=stemming&text=sudden&service=english2spanish>
suddenly I insert one row (*) with the arg1 and arg2 . So ..., there
are something that I receive back ?? Some pointer, something? There are
any way to know which number receive my row ?

Thank you,
Gustavo

(*) This process can be multi-thread. In my case I use the libraries
with the wxWidget project with the class wxDbTable.


From: John McCawley <nospam(at)hardgeus(dot)com>
To: gustavo halperin <ggh(dot)develop(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SEQUENCE primary key
Date: 2007-02-14 01:09:26
Message-ID: 45D26146.80903@hardgeus.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In PostgreSQL 8 and up:

SELECT lastval();

gustavo halperin wrote:

> Hello
>
> I have a question, if I have a table with a SEQUENCE primary key, that
> obviously, I doesn't give in each new row inserted. For example if the
> table locks:
> CREATE SEQUENCE id_seq;
> CREATE TABLE table ( id integer DEFAULT
> nextval('id_seq') CONSTRAINT table_id PRIMARY KEY,
> arg1 integer,
> arg2 integer
> )
>
> How can I know which id receive each new row. I mean,
> <http://ultralingua.com/onlinedictionary/index.html?action=define&ignoreaccents=on&wholewords=on&searchtype=stemming&text=sudden&service=english2spanish>
> suddenly I insert one row (*) with the arg1 and arg2 . So ..., there
> are something that I receive back ?? Some pointer, something? There
> are any way to know which number receive my row ?
>
> Thank you,
> Gustavo
>
> (*) This process can be multi-thread. In my case I use the libraries
> with the wxWidget project with the class wxDbTable.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match


From: Chris <dmagick(at)gmail(dot)com>
To: John McCawley <nospam(at)hardgeus(dot)com>
Cc: gustavo halperin <ggh(dot)develop(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: SEQUENCE primary key
Date: 2007-02-14 02:06:46
Message-ID: 45D26EB6.9090500@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

John McCawley wrote:
> In PostgreSQL 8 and up:
>
> SELECT lastval();

Actually it's better to use currval.

See
http://people.planetpostgresql.org/xzilla/index.php?/archives/169-Is-lastval-evil.html

--
Postgresql & php tutorials
http://www.designmagick.com/


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris <dmagick(at)gmail(dot)com>
Cc: John McCawley <nospam(at)hardgeus(dot)com>, gustavo halperin <ggh(dot)develop(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: SEQUENCE primary key
Date: 2007-02-14 02:20:48
Message-ID: 5868.1171419648@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Chris <dmagick(at)gmail(dot)com> writes:
> Actually it's better to use currval.

Right. Also, in 8.2 and up there's INSERT RETURNING, which is far
more flexible --- for instance it could pull back an insertion
timestamp.

regards, tom lane