-
Notifications
You must be signed in to change notification settings - Fork 1k
Refactored Prepared-Statement Cache Design (Lock-Free Hot Path) - Part 2 #5225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
renecannao
merged 13 commits into
v3.0
from
v3.0_refactor_prepared_statement_cache_design_5211
Dec 5, 2025
Merged
Refactored Prepared-Statement Cache Design (Lock-Free Hot Path) - Part 2 #5225
renecannao
merged 13 commits into
v3.0
from
v3.0_refactor_prepared_statement_cache_design_5211
Dec 5, 2025
+1,232
−950
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Backport PQsendPipelineSync to PostgreSQL 16.3, enabling pipeline synchronization without flushing the send buffer. - Replace calls to PQPipelineSync in code with PQsendPipelineSync to use the new functionality.
… if resultset threshold is reached.
…nd uint32-to-string conversion
…ctions, since response data is buffered during extended queries. Fixed TAP test
Previously, the parser always tokenized the full command, even when we only needed to check whether it was a transaction command. Now, it first extracts the first word to determine relevance and performs full tokenization only when necessary.
… construction for selected PostgreSQL protocol messages to reduce overhead and improve performance.
#5211 Concurrency and Memory Management * Lock-Free Ref Counting: Replaced global mutex-protected integer reference counts with `std::atomic<uint32_t>` within `PgSQL_STMT_Global_info`, eliminating lock contention during statement referencing. * Modern Ownership: Adopted std::shared_ptr<const PgSQL_STMT_Global_info> for global and local storage, providing automatic, thread-safe memory and lifecycle management. * Memory Optimization: Removed redundant auxiliary maps `global_id_to_stmt_names` and `map_stmt_id_to_info` from local and global statement managers respectively, reducing overall memory overhead. * Optimized Purging: Statement removal logic was simplified for efficiently identifying and cleaning up unused statements. Hot Path Performance (`BIND`, `DESCRIBE`, `EXECUTE`) * Bypassed Global Lookups: Local session maps now store the `shared_ptr` directly, removing the need to acquire the global lock and search the global map during hot path operations. * Direct Refcount Manipulation: Refcount modification functions now operate directly on the passed statement object, eliminating the overhead of searching the global map to find the object pointer based on statement id. Safety and Protocol Logic (`PARSE`) * Efficient Statement Reuse: Implemented a **local fast path** check for the unnamed statement (`""`), allowing immediate reuse of an identical query (same hash) upon re-parse, which bypasses global processing and locks. Cleanup * Cleaned up and class rename `PgSQL_STMT_Manager_v14` -> `PgSQL_STMT_Manager`.
Contributor
|
retest this please |
|
Collaborator
Author
|
retest this please |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



Summary
Concurrency and Memory Management
std::atomic<uint32_t>withinPgSQL_STMT_Global_info. This eliminates lock contention during statement referencing.std::shared_ptr<const PgSQL_STMT_Global_info>for global and local storage, providing automatic, thread-safe memory and lifecycle management.global_id_to_stmt_names(from local statement managers)map_stmt_id_to_info(from global statement manager)This significantly reduces overall memory overhead.
Hot Path Performance (
BIND,DESCRIBE,EXECUTE)The refactor targets operations that occur frequently to minimize latency:
shared_ptrdirectly, removing the need to acquire the global lock and search the global map during hot path operations.Safety and Protocol Logic (
PARSE)""). This allows immediate reuse of an identical query (same hash) upon re-parse, bypassing global processing and locks.Cleanup
Class Rename:
PgSQL_STMT_Manager_v14->PgSQL_STMT_ManagerPgSQL_STMTs_local_v14->PgSQL_STMTs_LocalRemoved unused variables from PostgreSQL module:
pgsql-client_session_track_gtidpgsql-enable_client_deprecate_eofpgsql-enable_server_deprecate_eofpgsql-enable_load_data_local_infilepgsql-log_mysql_warnings_enabledpgsql-default_session_track_gtidspgsql-handle_warningsIntroduced Xoshiro128++ RNG
Switched from
rand()to theXoshiro128++ RNGto improve performance and randomness.Prepared Statement Cache Refactoring - Benchmark Results
Benchmark setup:
pgbenchvia ProxySQL, 16 clients, 60s duration, query mode: prepared.Metrics: TPS (transactions per second) and average latency (ms). Each value is averaged over two runs.
Observations
Throughput (TPS) improvements:
pgsql-thread=1: +28–31%pgsql-thread=2: +14–27%Latency improvements:
pgsql-thread=1: -21–24%pgsql-thread=2: -13–21%Merged optimizations from PR #5196:
This PR includes several performance improvements and protocol handling enhancements:
Buffered response handling (9eb934e)
PostgreSQL pipeline sync backport (9fa3d75)
Closes #5211