Re: Last insert id

From: Rod Taylor <pg(at)rbt(dot)ca>
To: Andrei Bintintan <klodoma(at)ar-sd(dot)net>
Cc: 'mixo' <mixo(at)coza(dot)net(dot)za>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Last insert id
Date: 2004-06-15 10:30:08
Message-ID: 1087295408.27896.50.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, 2004-06-15 at 03:05, Andrei Bintintan wrote:
> "Is it safe to use "select max(table1_id) from table1" after the insert?"
>
> Yes it is safe, but ONLY if you use it inside a transaction.(BEGIN/COMMIT).

No, this is not safe outside of the serializable isolation.

rbt=# begin;
BEGIN
rbt=# select max(id) from l;
max
-----
1
(1 row)

rbt=# insert into l values (2);
INSERT 62597 1
rbt=# select max(id) from l;
max
-----
3
(1 row)

rbt=# commit;
COMMIT

I inserted 3 from another connection after 2 was inserted.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Pankaj Chugh 2004-06-15 12:28:34 Number of rows effected
Previous Message Andrei Bintintan 2004-06-15 07:05:40 Re: Last insert id