Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: Need help with DBD::Pg

by eclark (Scribe)
on Feb 23, 2005 at 22:40 UTC ( #433886=note: print w/replies, xml ) Need Help??

in reply to Re^2: Need help with DBD::Pg
in thread Need help with DBD::Pg

That code is incorrectly designed. Another process could call nextval() between the INSERT and SELECT. Sequences are not atomic in transactions.

Sequences are to be used by calling nextval() first and then inserting that value in a second statement. The database guarantees that no clients will retrieve the same nextval.

Replies are listed 'Best First'.
Re^4: Need help with DBD::Pg
by Miguel (Friar) on Feb 23, 2005 at 23:11 UTC
    ++ for you eclark for pointing me to the docs one more time :-) I always tought it was correct, since I read this:

    Advance the sequence object to its next value and return that value. This is done atomically: even if multiple sessions execute nextval concurrently, each will safely receive a distinct sequence value.


    Return the value most recently obtained by nextval for this sequence in the current session. (An error is reported if nextval has never been called for this sequence in this session.) Notice that because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed nextval since the current session did.

    About my problem:
    I'm rewriting everything using:

    use DBI qw(:sql_types); ..... $sth->bind_param(1,undef,SQL_....); $sth->execute(<values>);

    and spliting the multiple statements I have.


      Actually you were correct, I'm wrong. My memory must be fading.

      That said, I doubt you'd be able to convince anyone in the DBI community that running multiple statements in prepare is a good thing. I'm still curious why you were locking a table, I've never felt the need to do that in Postgres, just let MVCC take care of it.

Re^4: Need help with DBD::Pg
by Anonymous Monk on Feb 25, 2005 at 14:15 UTC
    Umm... What? The call to 'currval' will most certainly do exactly what is intended by the code. Because any call to currval returns the database _session's_ notion of the most recent squence value, Postgres will return whatever the last call to nextval (including column's DEFAULT constraint) returned *only for that session*. And because the INSERT and SELECT statements are in a single string there is no chance of another call to nextval between the INSERT and SELECT.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://433886]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-05-20 11:43 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.