Re: Warn against transaction level advisory locks for emulating "flat file" DBMS locking strategies

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: me(at)jix(dot)one
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: Warn against transaction level advisory locks for emulating "flat file" DBMS locking strategies
Date: 2021-09-08 16:37:40
Message-ID: 3433006.1631119060@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

PG Doc comments form <noreply(at)postgresql(dot)org> writes:
> The docs mention "For example, a common use of advisory locks is to emulate
> pessimistic locking strategies typical of so-called “flat file” data
> management systems" which is exactly what I wanted to use to port some code
> from using SQLite to using PostgreSQL. (The code in question requires
> serializable transactions and cannot not handle retries.)

Hmm. I'm afraid you're out of luck on that combination of requirements:
if you use serializable mode in Postgres, you had better be prepared to
retry serialization failures. It's not optional, because even if the
client transactions theoretically can't cause serialization anomalies,
you can still get failures because our implementation analyzes anomaly
risks only approximately. The approximation is conservative in the sense
that it won't let any actual failures get by; but it may produce false
positives. We haven't felt this is a problem, because if you're using
this stuff in the first place, you likely have *actual* anomaly hazards
and thus need the retry logic anyway.

> Later I discovered that obtaining a transaction level lock as first
> statement _within_ a transaction is not sufficient to emulate global
> pessimistic locking and can occasionally still result in serialization
> failures.

The advice in the manual is thinking about READ COMMITTED mode, where
I think this should work fine. It is a bit problematic in serializable
mode, because when you do "SELECT pg_advisory_lock...", the SELECT will
acquire the transaction snapshot before getting the lock. So yeah,
this method won't keep you out of serialization anomalies --- but as
I explained above, you have a risk of those regardless. (It's hard
to tell on the basis of what you've said whether the failures you saw
were due to this effect or were implementation-dependent false
positives.)

What I'm inclined to think here is that maybe the docs are not
sufficiently vocal about the fact that you can't avoid serialization
failures altogether.

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Jannis Harder 2021-09-08 17:21:16 Re: Warn against transaction level advisory locks for emulating "flat file" DBMS locking strategies
Previous Message Laurenz Albe 2021-09-08 16:26:12 Re: Warn against transaction level advisory locks for emulating "flat file" DBMS locking strategies