Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/conn-prepared-statements.md
1---2title: Use Prepared Statements Correctly with Pooling3impact: HIGH4impactDescription: Avoid prepared statement conflicts in pooled environments5tags: prepared-statements, connection-pooling, transaction-mode6---78## Use Prepared Statements Correctly with Pooling910Prepared statements are tied to individual database connections. In transaction-mode pooling, connections are shared, causing conflicts.1112**Incorrect (named prepared statements with transaction pooling):**1314```sql15-- Named prepared statement16prepare get_user as select * from users where id = $1;1718-- In transaction mode pooling, next request may get different connection19execute get_user(123);20-- ERROR: prepared statement "get_user" does not exist21```2223**Correct (use unnamed statements or session mode):**2425```sql26-- Option 1: Use unnamed prepared statements (most ORMs do this automatically)27-- The query is prepared and executed in a single protocol message2829-- Option 2: Deallocate after use in transaction mode30prepare get_user as select * from users where id = $1;31execute get_user(123);32deallocate get_user;3334-- Option 3: Use session mode pooling (port 5432 vs 6543)35-- Connection is held for entire session, prepared statements persist36```3738Check your driver settings:3940```sql41-- Many drivers use prepared statements by default42-- Node.js pg: { prepare: false } to disable43-- JDBC: prepareThreshold=0 to disable44```4546Reference: [Prepared Statements with Pooling](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pool-modes)47