Kolus Maximiliano <Kolus.maximiliano@bcr.com.ar> writes:
> I tried something slightly different:
> * LOCK the table in SHARE ROW EXCLUSIVE mode,
Well, sure, if you don't mind giving up concurrency of writers completely.
How likely is a conflict, anyway? If it's very probable then you're not
losing much, but if it's not then this isn't a good approach.
> * If i use LOCK TABLE users IN SHARE ROW EXCLUSIVE MODE in two psql session
> it works (one session gets the lock, the other waits). But inside the
> function it seems to be ignored (i had the dup key problem).
Within a function the snapshot doesn't get updated, so you cannot see
results of other transactions that commit after the function starts.
This means you pretty much have to issue the LOCK as a separate
interactive command.
regards, tom lane