Hi.

> PostgreSQL:
> $dbh->do(CREATE SEQUENCE table_uid_seq)
> $dbh->do(CREATE TABLE table ( uid INT4 DEFAULT nextval('table_uid_seq')
> .... ))
> $dbh->do(INSERT INTO table (table_uid_seq.NEXTVAL, field...) VALUES (...))
> $dbh->...(SELECT last_value as unique_id FROM table_uid_seq)

This way to get the last allocated value of a sequence shown above can lead
to very tricky problems:

'last_value' gives you the last allocated value _of any backend_.

'currval' gives you the last allocated value _in your transaction_.

The example above should therefore read as follows:

$dbh->do(CREATE SEQUENCE table_uid_seq)
$dbh->do(CREATE TABLE table ( uid INT4 DEFAULT nextval('table_uid_seq')
..... ))
$dbh->do(INSERT INTO table (table_uid_seq.NEXTVAL, field...) VALUES (...))
$dbh->...(SELECT CURRVAL(table_uid_seq))

Cheers,
Florian

Reply via email to