Details
-
New Feature
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Won't Do
-
None
-
None
Description
Current Implementation
The driver currently uses an LRU cache (size: 256) to store prepared statements. When useServerPrepStmts is enabled, every SQL statement immediately triggers a PREPARE command on the PostgreSQL server, regardless of execution frequency. This approach creates significant overhead:
Extra processing and memory consumption on the server side
Additional network roundtrips for each prepared statement
Resource allocation for queries that may execute only once or twice
Proposed Solution: prepareThreshold Implementation
Implement a threshold-based approach option (default: 5 executions).
Mechanism:
- Track execution count for each unique SQL statement
- Send PREPARE command only after the statement has been executed N times
- Use client-side execution until the threshold is reached
- Cache prepared statements in the existing LRU cache once the threshold is met
Benefits:
- Server-side preparation limited to frequently executed statements by default
- Fewer roundtrips for infrequently-used queries
Configuration Options
The prepareThreshold parameter provides flexible control:
- 0: Always prepare immediately (equivalent to current useServerPrepStmts=true behavior)
- 1-N: Prepare after N executions (default: 5, recommended)
- -1: Never auto-prepare (equivalent to current useServerPrepStmts=false behavior)
replacing useServerPrepStmts option completly, (even if alias will be keeped for compatibility)