I ran across the following code and was trying to debug it, but I don't understand why the lock couldn't be unlocked in this scenario:
const db = pgp(opts)
await db.one('SELECT pg_try_advisory_lock(1) AS lock') // returns true, and I can see the lock in the DB
// no await
db.tx(async t => {
const fingerprintIds = fingerprints.map(item => item.id)
sql = `UPDATE fingerprint SET blah=1 WHERE id IN ($(fingerprintIds:list)) RETURNING id`
const updatedFingerprintIds = await db.query(sql, { fingerprintIds }) // yes, it is not using the transaction object
// some other database calls
})
result = await db.one('SELECT pg_advisory_unlock(1) AS lock')
// result.lock === false, database logs the error: 'WARNING: you don't own a lock of type ExclusiveLock'
// lock still stays in the database (until the session ends)
Of course, when I add the await in front of the db.tx call, and use t in the transaction callback, then it works as expected.
Does this happen because the pg-promise library is in a transactional state when the unlock is called? Because when I call this succession of queries, the lock will unlock as expected:
SELECT pg_try_advisory_lock(1) AS lock;
BEGIN;
SELECT * FROM table;
SELECT pg_advisory_unlock(1) AS lock;