Sub topic 8
Prepared statements and PgBouncer
Assume you know basic SQL, can use psql, and understand transaction pooling. This chapter focuses on how prepared statements interact with PgBouncer when clients and PostgreSQL server connections do not stay paired.
Why prepared statements need session state
A prepared statement is a SQL statement that PostgreSQL parses and plans before execution. The client gives it a name, and later sends that name with new parameter values.
PREPARE order_count(integer) AS
SELECT count(*) FROM orders WHERE customer_id = $1;
EXECUTE order_count(42);
DEALLOCATE order_count;The statement named order_count exists inside one PostgreSQL session. It is stored on one server connection. A different server connection does not know that name.
Session pooling preserves this relationship. Each client keeps the same PostgreSQL server connection for its entire session. Transaction pooling does not. PgBouncer can give a client server connection A for one transaction and connection B for the next transaction.
The failure in transaction mode
With older PgBouncer versions, a named prepared statement created through transaction pooling can fail when the next transaction uses another server connection. PostgreSQL reports an error similar to:
prepared statement "order_count" does not existThe same problem applies to other session state, such as temporary tables and session-level settings. The application may work in testing because both transactions happen to use the same backend, then fail under load.
Statement pooling is even more restrictive. PgBouncer may assign a different server connection after every statement, so session-level prepared statements cannot be relied on.
Enabling PgBouncer support
Recent PgBouncer versions can track protocol-level prepared statements in transaction pooling mode. Set max_prepared_statements to a nonzero value:
[pgbouncer]
pool_mode = transaction
max_prepared_statements = 100PgBouncer then tracks prepared statements and creates them on the server connection that needs them. The value limits how many prepared statements PgBouncer tracks for each client connection. It uses a least-recently-used cache when the limit is reached.
Use a value large enough for the statements each client commonly uses. A value that is too small causes repeated prepare operations. A value that is too large uses more memory and can create many prepared statements across backend connections.
This feature supports protocol-level preparation used by many database drivers. SQL commands such as PREPARE and EXECUTE are session commands and should still be treated as unsafe in transaction pooling mode.
Choosing a workaround
- Use session pooling when the application depends heavily on SQL
PREPARE, temporary tables, session variables, or other session state. - Use transaction pooling with
max_prepared_statementswhen the driver uses supported protocol-level preparation. - Configure the driver to avoid server-side prepared statements when compatibility is more important than their planning benefit.
- Keep parameterized queries even when server-side preparation is disabled. Parameterization protects query values from SQL injection and is separate from statement caching.
Exercise
An application uses transaction pooling and receives “prepared statement does not exist.” Name two valid remedies.
Show answer
Enable a supported PgBouncer version with a suitable maxpreparedstatements value, or disable server-side prepared statements in the driver. Session pooling is another valid remedy when the application requires session state.