Quick revision
The short answer
A session advisory lock belongs to the PostgreSQL server connection, the backend, that took it, but in transaction pooling mode PgBouncer hands each transaction to whichever backend is free. So the lock may be taken on one backend and the unlock sent on another, where it does nothing, leaving the lock held by an idle backend forever; and another pod's transaction may land on the backend that holds the lock, which PostgreSQL then treats as the holder, so two pods both think they own it. The fix is pg_advisory_xact_lock inside one transaction that covers the work, a direct or session-pooled connection for the lock, or a lease-based lock table such as ShedLock.
What to remember
In this answer, we saw that session advisory locks belong to a PostgreSQL backend, and that PgBouncer in transaction mode moves each transaction to whichever backend is free. An unlock sent to a different backend does nothing, leaving the lock stuck on an idle pooled backend, and another pod's transaction landing on the holder's backend is granted the re-entrant lock, so two pods run the job. pg_advisory_xact_lock inside one transaction, a direct or session-mode connection, or a lease table such as ShedLock fix it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A session advisory lock belongs to the PostgreSQL server connection, the backend, that took it, but in transaction pooling mode PgBouncer hands each transaction to whichever backend is free. So the lock may be taken on one backend and the unlock sent on another, where it does nothing, leaving the lock held by an idle backend forever; and another pod's transaction may land on the backend that holds the lock, which PostgreSQL then treats as the holder, so two pods both think they own it. The fix is pg_advisory_xact_lock inside one transaction that covers the work, a direct or session-pooled connection for the lock, or a lease-based lock table such as ShedLock.
Why this question is asked
PgBouncer is often added to a Spring Boot fleet late, to save database connections, and features that quietly depended on connection identity start misbehaving. Advisory locks are the textbook example, and the failures look contradictory: sometimes no lock at all, sometimes a lock nobody can release. Interviewers ask this because explaining it needs a clear picture of three layers of connections. In this answer, we will recap how PgBouncer's pooling modes work, follow the lock and unlock through transaction pooling, see how two pods end up running the job together, and then look at the fixes.
1A Quick Recap: Three Layers of Connections
There are three kinds of connection here. The Spring Boot service has a HikariCP pool of client connections to PgBouncer. PgBouncer has its own pool of server connections to PostgreSQL, and each server connection is one PostgreSQL backend process, which is what PostgreSQL calls a session. Advisory locks taken with pg_advisory_lock belong to that backend session.
PgBouncer's pool mode decides when a client gets a server connection. In session mode, a client keeps the same backend for as long as it is connected, so session state works. In transaction mode, the client gets a backend only for the duration of one transaction and gives it back at commit; the next transaction from the same client can run on a different backend. Statements outside an explicit transaction are each their own transaction. PgBouncer documents session-level advisory locks, along with SET and LISTEN, as features that do not work reliably in transaction mode.
- Spring Boot podHikariCP client connections to PgBouncer
- PgBouncer, transaction modeassigns a server connection per transaction
- PostgreSQL backendone session; session advisory locks live here
3 layers, from the top down. Step through them one at a time.
The pod thinks it holds the lock; PostgreSQL thinks a backend does.
2Why the Lock Is Never Released
The job runs SELECT pg_advisory_lock(7001) as a standalone statement, so PgBouncer routes it to, say, backend B1. The lock now belongs to B1's session. The statement commits and PgBouncer returns B1 to its pool. The job does its work through other transactions, on whatever backends are free, and at the end it calls pg_advisory_unlock(7001), which PgBouncer sends to backend B3.
B3 does not hold the lock, so the unlock returns false with a warning that the lock was not held, which the code probably ignores. B1 is still alive in PgBouncer's pool, idle and healthy, so its session never ends and the lock is held indefinitely. The next night, every pod's pg_advisory_lock waits forever or every pg_try_advisory_lock returns false, and the job never runs again until someone restarts PgBouncer or terminates B1.
3Why Two Pods Sometimes Run the Job Together
The other failure comes from the same mix-up in the opposite direction. Pod A takes the lock on B1 and starts working. Pod B tries pg_try_advisory_lock(7001), and PgBouncer happens to give it B1, because B1 is idle between pod A's transactions. Advisory locks are re-entrant within a session, so PostgreSQL sees the holder asking again and grants it: true. Pod B now also believes it holds the lock and runs the job.
So the lock's owner, from PostgreSQL's point of view, is a backend that different pods share over time, while each pod thinks it personally owns the lock. Whether we see double runs or stuck locks depends only on which backend each statement happens to land on, which is why the symptoms seem random.
8 messages in order. Play them, or step through them one at a time.
4Fix 1: Transaction-Scoped Locks Around the Work
In transaction mode, the only thing PgBouncer guarantees is that one transaction stays on one backend. So we make the lock and the protected work one transaction: pg_try_advisory_xact_lock at the start, the work, then commit, which releases the lock on the same backend automatically. Nothing can leak to the pool, and no other client can get the backend while our transaction is open.
The limit is that the job's work must fit in one transaction, which for a two-hour job means a two-hour transaction holding back vacuum and taking a pooled backend the whole time.
5Fixes 2 and 3: A Direct Connection or a Lease Table
For long jobs that commit in many batches, the lock can use a separate DataSource that connects directly to PostgreSQL, or to a PgBouncer pool in session mode, with one dedicated connection kept for the job's duration. A session lock then behaves as designed, at the cost of one extra connection per running job. Session mode for the whole service would also work but gives up the connection savings that were the reason for PgBouncer.
The most robust option for scheduled jobs is a lock table with a lease, which is what ShedLock does: the pod writes the job name, its own identity and a lock_until time with a conditional update, commits, and the row stays valid until it expires or the pod releases it. It does not depend on connection identity at all, survives pod crashes through the expiry, and works through any pooler.
6Follow-up questions
How do you clean up the stuck lock right now?
Find the backend holding it in pg_locks (locktype = 'advisory'), check in pg_stat_activity that it is idle and belongs to PgBouncer's pool, and end it with pg_terminate_backend(pid). PgBouncer will open a fresh server connection when needed. Then deploy one of the fixes, or it will happen again.
Does PgBouncer's newer prepared statement support change this?
No. Recent PgBouncer versions can track protocol-level prepared statements in transaction mode, but session state such as advisory locks, SET values and LISTEN still belongs to the backend and is not carried between transactions, so the same rules apply.
Are other session features affected the same way?
Yes. SET without LOCAL, temporary tables, LISTEN, session advisory locks and WITH HOLD cursors all attach to the backend. In transaction mode they can leak to other clients or vanish between transactions. SET LOCAL, transaction-scoped advisory locks and anything inside one transaction are safe.
7Conclusion
In this answer, we saw that session advisory locks belong to a PostgreSQL backend, and that PgBouncer in transaction mode moves each transaction to whichever backend is free. An unlock sent to a different backend does nothing, leaving the lock stuck on an idle pooled backend, and another pod's transaction landing on the holder's backend is granted the re-entrant lock, so two pods run the job. pg_advisory_xact_lock inside one transaction, a direct or session-mode connection, or a lease table such as ShedLock fix it.
Open with
A session advisory lock belongs to the PostgreSQL backend session that took it, but with PgBouncer in transaction pooling each transaction can run on a different backend, and a standalone statement is its own transaction.
Then, if they want more
So pg_advisory_lock might run on backend one, the job's work on others, and pg_advisory_unlock on backend three. That unlock does nothing because backend three doesn't hold the lock, while backend one sits idle in PgBouncer's pool still holding it, possibly forever. That's the never-released case.
The double-run case is the reverse: another pod's pg_try_advisory_lock happens to be routed to the backend that already holds the lock, and because advisory locks are re-entrant within a session, PostgreSQL says true. Now two pods think they own it.
To fix it I'd use pg_try_advisory_xact_lock at the start of a transaction that contains the whole job, since PgBouncer keeps one transaction on one backend and commit releases the lock.
If the job is long and commits in batches, I'd take the lock on a dedicated direct connection or a session-mode pool, or better, use ShedLock's lease table, which doesn't depend on connection identity and recovers from crashes through expiry. Session mode for everything works too, but loses the pooling benefit.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot service uses session-level pg_advisory_lock to make one pod run a nightly...": https://scrivio-interview-library.netlify.app/databases/database-locking/8fee330b89/