Skip to content

Conversation

@rahim-kanji
Copy link
Collaborator

@rahim-kanji rahim-kanji commented Nov 24, 2025

Summary

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. This eliminates 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 (from local statement managers)
    • map_stmt_id_to_info (from global statement manager)
      This significantly reduces overall memory overhead.
  • Optimized Purging: Statement removal logic was simplified for efficiently identifying and cleaning up unused statements.

Hot Path Performance (BIND, DESCRIBE, EXECUTE)

The refactor targets operations that occur frequently to minimize latency:

  • 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: Reference count 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 (Local Fast Path): Implemented a local fast path check for the unnamed statement (""). 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_Manager
  • PgSQL_STMTs_local_v14 -> PgSQL_STMTs_Local

Removed unused variables from PostgreSQL module:

  • pgsql-client_session_track_gtid
  • pgsql-enable_client_deprecate_eof
  • pgsql-enable_server_deprecate_eof
  • pgsql-enable_load_data_local_infile
  • pgsql-log_mysql_warnings_enabled
  • pgsql-default_session_track_gtids
  • pgsql-handle_warnings

Introduced Xoshiro128++ RNG

Switched from rand() to the Xoshiro128++ RNG to improve performance and randomness.

Prepared Statement Cache Refactoring - Benchmark Results

Benchmark setup: pgbench via ProxySQL, 16 clients, 60s duration, query mode: prepared.
Metrics: TPS (transactions per second) and average latency (ms). Each value is averaged over two runs.

pgsql-thread (ProxySQL) Threads (pgbench) TPS Before (Pre-Refactor) TPS After (Post-Refactor) Latency Before (ms) Latency After (ms)
1 1 3944 5078 4.06 3.16
1 2 3737 4751 4.28 3.37
1 4 3435 4454 4.66 3.59
1 8 3381 4327 4.73 3.70
1 16 3286 4312 4.87 3.71
2 1 6263 7377 2.56 2.17
2 2 5784 6622 2.77 2.42
2 4 5180 6557 3.10 2.44
2 8 4913 6199 3.26 2.58
2 16 4902 6219 3.26 2.57

Observations

  1. Throughput (TPS) improvements:

    • pgsql-thread=1: +28–31%
    • pgsql-thread=2: +14–27%
    • Gains are largest at higher thread counts, showing better concurrency handling.
  2. Latency improvements:

    • pgsql-thread=1: -21–24%
    • pgsql-thread=2: -13–21%
    • Lower latency indicates more efficient reuse of prepared statements.

Merged optimizations from PR #5196:

This PR includes several performance improvements and protocol handling enhancements:

  • Replaced std::string with char[] in critical path.
  • Introduced inline functions for ASCII whitespace detection and uint32 to string conversion.

Buffered response handling (9eb934e)

  • Buffer query responses until the Extended Query frame completes.
  • Early sending of results occurs only if the resultset threshold is reached, reducing partial responses and improving consistency.

PostgreSQL pipeline sync backport (9fa3d75)

  • Backported PQsendPipelineSync from PostgreSQL 17.

Closes #5211

- 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.
…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`.
@noizu noizu added this to the Release 3.0.4 milestone Nov 25, 2025
@renecannao
Copy link
Contributor

retest this please

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 1, 2025

@rahim-kanji rahim-kanji changed the title Improved Prepared-Statement Cache Design (Lock-Free Hot Path) Improved Prepared-Statement Cache Design (Lock-Free Hot Path) - Part 2 Dec 2, 2025
@rahim-kanji
Copy link
Collaborator Author

retest this please

@rahim-kanji rahim-kanji marked this pull request as ready for review December 2, 2025 09:10
@rahim-kanji rahim-kanji changed the title Improved Prepared-Statement Cache Design (Lock-Free Hot Path) - Part 2 Refactored Prepared-Statement Cache Design (Lock-Free Hot Path) - Part 2 Dec 2, 2025
@renecannao renecannao merged commit 3c4e09f into v3.0 Dec 5, 2025
142 of 154 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Improved Prepared-Statement Cache Design (Lock-Free Hot Path) - PostgreSQL

4 participants