Skip to content

Extended Query: Prevent premature commit by deferring SYNC until pipeline end - PostgreSQL#5119

Merged
renecannao merged 3 commits intov3.0from
v3.0_extended_query_protocol_issue_5118
Sep 28, 2025
Merged

Extended Query: Prevent premature commit by deferring SYNC until pipeline end - PostgreSQL#5119
renecannao merged 3 commits intov3.0from
v3.0_extended_query_protocol_issue_5118

Conversation

@rahim-kanji
Copy link
Collaborator

Summary

Previously, each extended-query block was terminated with a SYNC, which caused implicit transactions to commit prematurely. As a result, earlier write operations (INSERT/UPDATE/DELETE) could not be rolled back if a later statement in the same sequence failed.

This change switches to libpq pipeline mode and replaces intermediate SYNC messages with FLUSH, ensuring that all client query frames execute as part of the same implicit transaction. A final SYNC is still issued to resynchronize the connection and make it safe for reuse in the pool.

Closes#5118

Previously, each extended-query block was terminated with a SYNC,
which caused implicit transactions to commit prematurely. As a result,
earlier write operations (INSERT/UPDATE/DELETE) could not be rolled
back if a later statement in the same sequence failed.

This change switches to libpq pipeline mode and replaces intermediate
SYNC messages with FLUSH, ensuring that all client query frames execute
as part of the same implicit transaction. A final SYNC is still issued
to resynchronize the connection and make it safe for reuse in the pool.
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 significantly refactors how PostgreSQL extended queries are handled to prevent premature transaction commits. By leveraging libpq's pipeline mode, it ensures that a series of operations within an extended query block are treated as a single implicit transaction, allowing for proper error handling and rollbacks. This change improves the robustness and reliability of PostgreSQL interactions, particularly in scenarios involving multiple write operations.

Highlights

  • PostgreSQL Extended Query Transaction Handling: The previous behavior of terminating each extended-query block with a SYNC message caused implicit transactions to commit prematurely, preventing rollbacks of earlier write operations if a later statement failed. This pull request addresses this by switching to libpq pipeline mode.
  • Pipeline Mode Implementation: Intermediate SYNC messages within an extended query sequence are now replaced with FLUSH messages. A final SYNC is issued only at the end of the pipeline to resynchronize the connection, ensuring all client query frames execute as part of the same implicit transaction.
  • Connection Resynchronization: New asynchronous states and methods (ASYNC_RESYNC_START, ASYNC_RESYNC_CONT, async_perform_resync) have been introduced to handle explicit connection resynchronization when errors occur within a pipeline, ensuring the connection returns to a ReadyForQuery state.
  • Connection Pooling Logic Update: The connection pooling mechanism has been updated to check if a PostgreSQL connection is in an active pipeline state (is_pipeline_active()) before returning it to the pool. If a pipeline is active, a new session is created and the connection is reset to prevent state contamination.
  • New Extended Query Flags: Two new flags, PGSQL_EXTENDED_QUERY_FLAG_SYNC and PGSQL_EXTENDED_QUERY_FLAG_IMPLICIT_PREPARE, have been added to control the SYNC and FLUSH behavior within extended query processing, allowing for more granular control over transaction boundaries.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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 pull request introduces a significant and correct change to handle PostgreSQL extended queries by using libpq's pipeline mode. This defers the SYNC message to the end of a query sequence, preventing premature commits within implicit transactions and ensuring atomicity. The implementation adds new states to the async state machine for resynchronization and correctly handles session resets for pipelined connections. The code is well-structured, and the changes are consistent across different files. I've found a few minor issues related to code style and a potential bug in an edge case, which I've commented on. Overall, this is a great improvement.

@sonarqubecloud
Copy link

@renecannao renecannao merged commit d37dc62 into v3.0 Sep 28, 2025
104 of 155 checks passed
@noizu noizu added this to the 2025-Q3-September milestone Sep 30, 2025
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.

3 participants