How session-based locking works
mainPgLock uses Postgres advisory locks, which are unique to a database session. If the same process (sharing the same database connection session) attempts to acquire the same lock, it will succeed immediately without blocking.
Warning: PgLock does not synchronize code execution within the same process/session. If you need to synchronize code within a single process, use a Ruby Mutex instead.
key = "all_your_base"
PgLock.new(name: key).lock do
puts "First block called"
PgLock.new(name: key).lock do
puts "Second block called because it's sharing the same session"
end
end
# Result:
# First block called
# Second block called because it's sharing the same session