skip to content

With PgBouncer 1.26 in transaction mode in front of PostgreSQL 18, which session state survives between transactions and which leaks or silently disappears?

level: seniorimportance: should knowfreq 57%

answer

  1. next transaction, maybe another backend
  2. only reported parameters are tracked
  3. search_path joined the list
  4. SET LOCAL keeps it inside

basics

~20 s

Only parameters PostgreSQL reports to the client are tracked and re-applied; with PgBouncer 1.26 on PostgreSQL 18 that includes search_path. Other SET values, SQL PREPARE, LISTEN, session advisory locks and temporary tables stay behind on one backend.

solid answer

~40 s

In transaction mode the server connection goes back to the pool at `COMMIT`, and no reset query runs, so whatever a client leaves in the backend's session stays there for the next client. PgBouncer keeps a per-client cache of the parameters PostgreSQL reports in `ParameterStatus` messages and re-applies them on whichever server the client gets: `application_name`, `client_encoding`, `DateStyle`, `IntervalStyle`, `TimeZone`, `standard_conforming_strings`, `session_authorization`, `default_transaction_read_only`, `scram_iterations`, and since 1.26 `search_path`, which PostgreSQL 18 reports. Other session state leaks or vanishes: `SET statement_timeout`, SQL `PREPARE`, `LISTEN`, `pg_advisory_lock`, temporary tables, `WITH HOLD` cursors. The fix is to keep state inside the transaction (`SET LOCAL`, transaction-level advisory locks) or move such clients to a session pool.

code

sql · 12 lines
sql
-- Through PgBouncer, pool_mode = transaction
-- Unsafe: stays on whichever backend served this statement
SET statement_timeout = '2s';

-- Safe: scoped to this transaction only
BEGIN;
SET LOCAL statement_timeout = '2s';
SELECT pg_advisory_xact_lock(4711);
UPDATE seat_hold
   SET expires_at = now() + interval '5 minutes'
 WHERE seat_id = 4711;
COMMIT;

go deeper

for a junior

Recall that in transaction mode each transaction may use a different backend, so session state set in one transaction cannot be relied on in the next.

for a middle

Explain ParameterStatus tracking: which parameters PgBouncer re-applies, why only reported ones, and why session-level SET of anything else leaks.

for a senior

Audit an application for leaks: untracked SET, SQL PREPARE, LISTEN, session advisory locks, temp tables, and know that search_path is safe only on PgBouncer 1.26 with PostgreSQL 18.

for a principal

Decide which workloads may share a transaction pool and enforce it with separate PgBouncer database names, role-level settings and driver configuration rather than code review alone.

## Why state goes wrong in transaction mode In PgBouncer's `transaction` pooling mode a client holds a PostgreSQL **server connection**, one backend process with its own session, only for the duration of a transaction. At `COMMIT` or `ROLLBACK` the server returns to the pool, and the client's next transaction may be served by a different backend. PgBouncer does **not** run `server_reset_query` in this mode. Two things follow: - state a client creates in a backend's session **stays** in that backend and is inherited by the next client that gets it (**leak**); - the client that created it may not find it on its next transaction (**silent disappearance**). ## What PgBouncer tracks for you PostgreSQL sends a `ParameterStatus` message whenever certain parameters change. PgBouncer watches these, stores each client's values, and re-applies them when it links the client to a server. The default tracked list in PgBouncer 1.26 is: | Parameter | Reported by PostgreSQL since | |---|---| | `application_name`, `client_encoding`, `DateStyle`, `IntervalStyle`, `TimeZone`, `standard_conforming_strings`, `session_authorization` | long-standing | | `default_transaction_read_only` | 14 | | `scram_iterations` | 16 | | `search_path` | **18** | **Version matters twice here.** PostgreSQL 18 is the first release that reports `search_path`, and PgBouncer 1.26 is the first release that tracks it, and `default_transaction_read_only`, by default. Before 1.26, a function that ran `SET search_path = ticketing, public` changed the schema lookup for whichever client next used that backend. On PostgreSQL 17 or older, even PgBouncer 1.26 learns `search_path` only from the startup packet, so a later `SET search_path` still leaks. `default_transaction_read_only` shows why tracking matters. Before 1.26, a reporting function that ran `SET default_transaction_read_only = true` on a transaction-pooled connection made the next client's writes on that backend fail with `cannot execute UPDATE in a read-only transaction` (SQLSTATE 25006), a bug that appeared only under load and only for whichever client inherited the backend. On PostgreSQL 14 and later, 1.26 now follows that parameter per client. `track_extra_parameters` adds names to the list, but only parameters the server **reports** can be followed through `SET`. Adding `statement_timeout` does not help, because PostgreSQL never reports it. ## What still leaks or disappears - **Untracked `SET`**: `SET statement_timeout = '2s'`, `SET work_mem = '64MB'` or `SET lock_timeout` stay on that backend, apply to other clients' transactions, and are missing from the setter's next transaction. - **SQL-level `PREPARE` / `EXECUTE`**: forwarded as plain SQL, so `EXECUTE` on another backend fails with `prepared statement ... does not exist` (SQLSTATE 26000). Protocol-level prepared statements are handled by `max_prepared_statements`, a separate mechanism. - **`LISTEN`**: the registration belongs to one backend session; once the server goes back to the pool, notifications arrive on that backend rather than at the client that subscribed, so delivery becomes unreliable. - **Session advisory locks** (`pg_advisory_lock`): the lock stays held by the backend after `COMMIT`, can be inherited by another client, and an unlock attempted from a different backend fails. - **Temporary tables** and **`WITH HOLD` cursors** kept across transactions. - **Startup parameters** PgBouncer cannot track: a client that sends one, for example in `options`, is disconnected with `unsupported startup parameter` unless the name is listed in `ignore_startup_parameters`, in which case it is silently dropped. ## Making an application safe 1. Scope settings to the transaction with `SET LOCAL`, or set them per role with `ALTER ROLE ... SET`, which applies at backend login for every client of that role. 2. Use transaction-level advisory locks (`pg_advisory_xact_lock`) instead of session-level ones. 3. Use protocol-level prepared statements from the driver, not SQL `PREPARE`. 4. Route `LISTEN` consumers, migrations and anything with temporary tables through a session-pooled PgBouncer database name or a direct connection. 5. Since 1.26 PgBouncer sends `pgbouncer.pool_mode` and `pgbouncer.version` as `ParameterStatus` at login, so a driver or a startup check can detect that it is behind a transaction pooler. `server_reset_query_always = 1` runs `DISCARD ALL` after every transaction even in transaction mode. That turns random leaks into deterministic loss of state, which is useful for flushing out broken code but makes no session feature work. ## Why interviewers ask it The failures are **silent and load-dependent**: the code works in a test with one client and one backend, then misbehaves under concurrency when transactions start landing on different backends. A good answer names the mechanism (no reset, no stickiness, only reported parameters tracked), the release facts (1.26 plus PostgreSQL 18 for `search_path`), and the transaction-scoped replacements.

  • On PgBouncer 1.26 with PostgreSQL 17, is SET search_path safe in transaction mode?
    No. PostgreSQL 17 does not report `search_path` in `ParameterStatus`, so PgBouncer only knows a value sent in the startup packet and re-applies that one. A later `SET search_path` is invisible to PgBouncer and leaks to the next client that gets the backend. Set it in the startup packet, per role, or with `SET LOCAL`.
  • Why doesn't adding statement_timeout to PgBouncer's track_extra_parameters make SET statement_timeout safe?
    PgBouncer learns about `SET` changes only through `ParameterStatus` messages, and PostgreSQL does not report `statement_timeout`. PgBouncer would only apply a startup-packet value; changes made with `SET` after login still leak. Use `SET LOCAL` or `ALTER ROLE ... SET` instead.

  • PgBouncer runs DISCARD ALL after every transaction, so nothing can leak.
  • Every SET is safe in transaction mode because PgBouncer tracks all parameters.
  • SET search_path leaks under transaction pooling whatever the PgBouncer and PostgreSQL versions.
  • LISTEN works in transaction mode because PgBouncer forwards notifications to the client.
  • A session advisory lock is released when the server goes back to PgBouncer's pool.
  • Turning on server_reset_query_always makes session features work in transaction mode.

Assumes PgBouncer 1.26 and PostgreSQL 18: search_path is reported by the server only from 18 and tracked by PgBouncer by default only from 1.26.