in reply to DBI: table locking
I see a bit of PostgreSQL discussion here
You can get what you want with a sequence in PgSQL.
#earlier in SQL CREATE TABLE tbl ( id serial, col2 text, col3 text ); NOTICE: CREATE TABLE will create implicit sequence 'tbl_id_seq' for S +ERIAL column 'tbl.id'
my $myid; my $dbh=DBI->connect("DBI:Pg(AutoCommit=>0,RaiseError=>1):dbname=foo") +; eval { $dbh->do("INSERT INTO tbl ( col2, col3 ) VALUES ( 'val', 'val3' )" +; my $sth=$dbh->prepare("SELECT currval('tbl_id_seq')"); $sth->execute; $sth->bind_columns( \($myid )); $sth->fetch; $dbh->commit; }; if($@) { $dbh->rollback; #enlightening error message goes here } # $myid has your id in it here.
You would put a sequence type as a column in the table with autoincrement running etc then it would have a name and you select currval('your_sequence_name_here') which PostgreSQL guarantees will be the one from the most recently inserted row in the current transaction.
Also I like to avoid wildcards in my SQL statements from inside scrips so that its obvious the intent of the script without having to look in the database to find out what columns etc are being affected.
Actually even without transactions, I am pretty sure currval() will return the last id that your process used on the given sequence.
|
|---|