Skip to content

Cherry pick #118210 to 26.3: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state - #118743

Merged
robot-ch-test-poll2 merged 8 commits into
backport/26.3/118210from
cherrypick/26.3/118210
Sep 8, 2026
Merged

robot-ch-test-poll2 merged 8 commits into
backport/26.3/118210from
cherrypick/26.3/118210

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #118210

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 7 commits September 5, 2026 03:44
…sage

`Flush`, and any message type the server does not implement, is answered with
`ErrorResponse` and then sets `ignore_until_sync`. That state is left only at
`Sync`, and while it is set every other message is discarded, so a client that
sends one of those messages with no extended-query cycle open never receives
`ReadyForQuery` again and its next `Query` is dropped unexecuted. A simple-query
client never sends `Sync`, so its connection stays open and permanently silent.

Track whether an extended-query cycle is open, and enter the discard state only
then. Outside a cycle the client is owed a `ReadyForQuery` right away, which is
what the server answered before the discard state reached these two branches.

The integration test covers both branches that reach it, `Flush` and an
unimplemented message type, and asserts that the connection still runs a query
afterwards.
`test_flush_error_discards_until_sync` read the pipeline reply up to the first
`ReadyForQuery` and then accepted any later `CommandComplete`. A handler that
emitted `ReadyForQuery` immediately after the `Flush` error and only then ran the
queued `Execute` satisfied both assertions, because the `Execute` reply had not
been read yet and turned up in the following read. Measured with the cycle flag
forced to false at all of its assignments: the test passed.

Read an empty simple query next, which answers with `EmptyQueryResponse` and
nothing else, and require exactly that. With the same forced flag the test now
fails with `['T', 'D', 'C', 'Z', 'I', 'Z']`, naming the late `Execute` directly.
…of-cycle-rejection

#118097 moved the `ignore_until_sync` reset out of the `Sync` case in `run()` and
into `processSyncQuery()`, which conflicted with the `in_extended_query_cycle`
reset that sits beside it. Resolution keeps master's removal and the cycle reset,
so every cycle-flag assignment stays in `run()`'s switch and clearing
`ignore_until_sync` stays where master put it.
No test reddened if the `Sync` case stopped clearing `in_extended_query_cycle`:
the out-of-cycle test opens fresh connections whose flag was never set, and the
`FLUSH` test runs a simple query before any further rejection, which clears the
flag on its own. With the reset removed, a `Flush` sent after a completed
extended-query cycle waits again for a `Sync` that the client has no reason to
send.

Send that second `Flush` right after the cycle's `Sync` and require exactly
`ErrorResponse` and `ReadyForQuery`. Measured with the reset removed: the new
assertion fails with `['<no ReadyForQuery, client left waiting>']`, while the
pre-existing assertions and the out-of-cycle test still pass.
The out-of-cycle test ran its control `Query` on a connection whose cycle flag
was still false from the handshake, so removing the reset in the `QUERY` case
left the test green. A client that runs `Parse`, then a simple query, then a
standalone `Flush` would go deaf again.

Open a cycle with `Parse` before the control query, and require `ParseComplete`
so the arm cannot pass without one. Measured with that reset removed: the test
fails with `['<no ReadyForQuery, client left waiting>']`, while the FLUSH test,
which does not depend on this reset, still passes.

The flag's declaration named `Sync` as the only closing transition, which the
`QUERY` case has always contradicted; say so where the flag is declared.
`Sync` and a simple `Query` both end an extended-query cycle, and this handler
now treats them as equivalent boundaries, but only `Sync` cleared the cycle's
objects. After `Parse("")`, `Bind("")`, `Query(...)`, a bare `Execute("")` still
ran the portal bound before the simple query, and a `Bind("")` after it still
resolved the unnamed prepared statement. PostgreSQL rejects both cases
(`exec_simple_query` calls `drop_unnamed_stmt()` and recreates the unnamed
portal), so stale extended-query state stayed executable across a boundary that
should have destroyed it.

Measured on 7152cbb: `Execute("")` after the simple query returned the stale
portal's row instead of an error; it now answers `Execute without prior Bind`,
and a later `Bind("")` answers `Unknown statement`. Named statements are
unaffected, as is the SQL-level `PREPARE`/`EXECUTE` path.

The defect predates #108469: `resetBindQuery()` is called only from
`processSyncQuery` and `Close('P')` on master and at that PR's base alike. It is
fixed here because this PR is what makes the simple `Query` a cycle-ending
boundary.

Reported by the AI reviewer on #118210.

The `Parse` comment claiming a cycle ends only at `Sync` contradicted that, so it
now names both boundaries.

`test_out_of_cycle_rejection_keeps_connection_usable` also now sends the rejected
message as the connection's first message. Every other case opens and ends a cycle
first, so a handler that started out believing a cycle was in progress kept the
whole suite green while a fresh simple-query connection went deaf on its first
`Flush`.
…rejection

Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 8, 2026
…ypick/26.3/118210

# Conflicts:
#	src/Server/PostgreSQLHandler.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants