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.


In reply to Re: DBI: table locking by dga
in thread DBI: table locking by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.