Skip to content

Automatic Prepared Statement Caching

Jack Christensen edited this page Sep 17, 2022 · 2 revisions

Automatic Prepared Statement Caching

By default pgx automatically prepares and caches queries. pgx also supports manually preparing and executing statements, but it should rarely be necessary.

While this automatic caching typically provides a significant performance improvement it does impose a restriction that the same SQL query always has the same parameter and result types. Otherwise an error will occur.

For example, the query select * from foo will fail if a column is added or removed to foo after the statement is cached.

This error can also be triggered by cursors that return different result types.

Another incompatibility is with PgBouncer. PgBouncer does not support prepared statements.

In any case, statement caching can be disabled by changing QueryExecMode in the ConnConfig.