Re:Distributed locking with Postgres advisory locks
by 3pt14159 on Monday January 24, 2022 @08:43AM
(#30057466)
Right. Well, I guess I just think the article is worded confusingly then. Because when I read it I thought "wait, it doesn't error, it waits" and I think that's an important feature.
Re:Distributed locking with Postgres advisory locks
by anentropic on Tuesday January 25, 2022 @06:02AM
(#30070484)
Yes you can choose, either to wait until your configured timeout (and if you set it to 0 it will wait indefinitely) or error immediately
normal Postgres locks have a https://postgresqlco.nf/doc/en/param/lock_timeout/ which is max time a client will wait to acquire lock before returning an error instead
SELECT FOR UPDATE queries also accept NOWAIT keyword to return error immediately if locked
I would guess the former applies to advisory locks too. I think NOWAIT only applies to FOR UPDATE / FOR SHARE queries
But it looks like there is try_advisory_lock method https://www.postgresql.org/docs/current/functions-admin.html... that does the equivalent