Skip to content

PostgreSQL's Extended Query Protocol Support in ProxySQL [WIP]#5044

Merged
renecannao merged 61 commits intov3.0from
v3.0_extended_query_protocol
Aug 19, 2025
Merged

PostgreSQL's Extended Query Protocol Support in ProxySQL [WIP]#5044
renecannao merged 61 commits intov3.0from
v3.0_extended_query_protocol

Conversation

@rahim-kanji
Copy link
Collaborator

@rahim-kanji rahim-kanji commented Jul 15, 2025

Closes#5018
Closes#5039

This commit lays the groundwork for handling PostgreSQL’s extended query
protocol (prepared statements) by introducing parsing, it's caching and
execution framework stubs.

- Implement Parse handling
- Implement caching of parse (reuse)
- Introduce Execute framework stub for future binding and execution logic.
Improved extended query frame handling
…s in a single frame will now use the same backend connection.

* Fixed few compilation warnings.
* Removed AutoCommit relate code from PostgreSQL session.
…er in PgSQL_Protocol.

* Moved get_uint32be() and get_uint16be() to gen_utils.h
* Split PgSQL_*_Message into distinct Message and Data components.
…ndling

- Always send Describe Portal to the backend
- Enforce check for unnamed portals in Bind, Describe, Close, and Execute; raise errors for named portals
- Improve error handling for extended query flow
- Introduce Extended_Query_Info struct to unify extended query-related members and reduce redundancy
* Introduced an error generator method to reduce code duplication
* Added several comments for clarity
* Addressed edge cases related to libpq limitations
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @rahim-kanji, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces foundational support for PostgreSQL's Extended Query Protocol within ProxySQL. It enables ProxySQL to handle prepared statements (Parse, Bind, Describe, Execute, Close messages) from PostgreSQL clients, allowing for more efficient and secure communication. The changes involve significant additions to the PostgreSQL-specific protocol handling, a new prepared statement management system, and refactoring of existing components to better align with PostgreSQL's unique characteristics.

Highlights

  • Extended Query Protocol Implementation: Introduced a comprehensive implementation of PostgreSQL's Extended Query Protocol (Parse, Describe, Bind, Execute, Close) within ProxySQL. This involves new message parsing, state machine handling, and response generation.
  • Prepared Statement Management: Added a robust system for managing PostgreSQL prepared statements, including global and session-local caches, reference counting, and metadata storage.
  • PostgreSQL-Specific Refactoring: Performed significant refactoring of existing MySQL-centric code paths (e.g., MySQL_Session, MySQL_Data_Stream) to align with PostgreSQL's specific protocol and transaction semantics, including renaming variables and functions.
  • Enhanced Query Information Tracking: Updated the PgSQL_Query_Info structure to support detailed tracking of extended query parameters and prepared statement metadata.
  • Improved Error Handling and Logging: Adjusted error handling and logging mechanisms to provide more specific information for PostgreSQL extended queries, including logging of client-side statement names.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is a substantial pull request that introduces support for PostgreSQL's Extended Query Protocol. The changes are extensive, adding new classes for message parsing, a manager for prepared statements, and significant modifications to the session and connection state machines. The overall architecture is sound and follows existing patterns in the codebase.

I've identified one critical bug in the parsing logic for bind formats that could lead to incorrect behavior. I also have a couple of medium-severity comments regarding potentially missing timeout logic and some unreachable code. Once these points are addressed, this will be a great addition to ProxySQL's capabilities.

…nded query frame. If it is an Execute message, indicating that client is sending a Bind/Describe/Execute sequence

 skip processing the Describe Portal. This is because libpq automatically includes a Describe Portal during execution, and sending it again would be redundant.
* Removed server_capabilities from PgSQL modules
…xperimentation <<

>> Do not use in production <<
This library provides basic functionality to connect to a PostgreSQL database and execute simple queries.
@rahim-kanji rahim-kanji force-pushed the v3.0_extended_query_protocol branch 3 times, most recently from e4178cf to 0638375 Compare July 16, 2025 11:52
@rahim-kanji rahim-kanji force-pushed the v3.0_extended_query_protocol branch from 3fce71c to 344ba25 Compare July 24, 2025 11:50
@rahim-kanji rahim-kanji force-pushed the v3.0_extended_query_protocol branch from f9af85f to 58eac30 Compare August 11, 2025 06:39
Added test_deallocate_non_existent_stmt
…ATE_SLEEP___PGSQL_QUERY_qpo.

* Fixed a crash occurring during session destruction.
* Query rules will now apply only to the first message in an extended query frame.
* OK message will apply to execute message.
* Query rewrite, error messages, and large packet handling will apply to parse message.
* Added query processing support for the Bind message.
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
21.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@renecannao renecannao marked this pull request as ready for review August 19, 2025 08:40
@renecannao renecannao merged commit b5fa8ca into v3.0 Aug 19, 2025
27 of 161 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.

2 participants