Re: functions, transactions, key violations

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Glaesemann <grzm(at)seespotcode(dot)net>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: functions, transactions, key violations
Date: 2008-06-04 22:45:12
Message-ID: 6577.1212619512@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Glaesemann <grzm(at)seespotcode(dot)net> writes:
> However, my point regarding the example in the docs still holds. Why
> is the exception block necessary? Doesn't wrapping the statements in a
> function ensure the unique_violation couldn't occur?

Well, the point of that example is to deal correctly with the case where
two sessions try to insert the same key concurrently. The second to
reach the index insertion point will see the conflicting index entry
already there, and it will block until it sees the other transaction
commit or abort, and if commit it will then throw a unique exception.

Are you certain that concurrent executions of purchase() for the same
item_id can never happen in your system? Because that certainly seems
the most likely explanation from here.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ralph Smith 2008-06-04 23:04:33 Re: Script errors on run
Previous Message Stephan Szabo 2008-06-04 22:30:43 Re: Script errors on run