Repository navigation
Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state - #118210
antonio2368 merged 6 commits into
Conversation
…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.
|
Workflow [PR], commit [6359a44] Summary: ✅
AI ReviewSummaryThis PR makes Missing context / blind spots
Final Verdict✅ No remaining review findings in the current head. The prior bot thread about stale unnamed extended-query state is fixed and was resolved. LLVM Coverage ReportMeasured on commit 6359a44.
Changed lines: Changed C/C++ lines covered: 56/56 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units11 translation units recompiled, 45 s compile time in total, 11 of them have a recent master baseline. |
|
@groeneai merge master |
…of-cycle-rejection ClickHouse#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.
|
Merged master at Two test commits ride along, because re-checking the merged tree showed that neither cycle reset was pinned by an assertion: with the reset in |
`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 ClickHouse#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 ClickHouse#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`.
|
The finding is real, and it is older than this PR: It also reproduces on released builds, not just master. On 26.7.6.57 and 26.8.2.7, Since that half is user-visible in a stable release, I moved the changelog category from Separately, New head: |
Cherry pick #118210 to 26.3: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
…y and stale unnamed extended-query state
Cherry pick #118210 to 26.8: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
…y and stale unnamed extended-query state
…y and stale unnamed extended-query state
Backport #118210 to 26.6: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
Backport #118210 to 26.7: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
Backport #118210 to 26.3: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
Backport #118210 to 26.8: Fix PostgreSQL out-of-cycle message recovery and stale unnamed extended-query state
…ge recovery and stale unnamed extended-query state
Flush, and any message type this handler does not implement, is answered withErrorResponseand then setsignore_until_sync, which is left only atSync. While it is set every other message is discarded, so a client that sends one of those with no extended-query cycle open never receivesReadyForQueryagain and its nextQueryis dropped unexecuted. A simple-query client never sendsSync, so its connection stays open and permanently silent.This tracks whether a cycle is open and enters the discard state only then; outside a cycle the client is owed a
ReadyForQueryat once. Errors inside a real cycle still recover atSync, so a pipelinedExecuteafter a rejected message is still discarded and oneSyncstill yields exactly oneReadyForQuery.Making the simple
Querya cycle-ending boundary exposed an older defect at the same site: unlikeSync, it did not destroy the unnamed prepared statement or the unnamed portal.Parse(""),Bind(""),Query(...),Execute("")therefore ran the stale portal instead of failing, and aBind("")after the simple query still resolved a statement that should no longer exist. PostgreSQL destroys both (exec_simple_querycallsdrop_unnamed_stmt()); this now does too, leaving named statements and the SQL-levelPREPARE/EXECUTEpath untouched. That half is reachable on released builds (observed on 26.7.6.57 and 26.8.2.7), which is why the category isBug Fix.Each new or strengthened test fails on pristine master and passes here; the module's remaining failure set is unchanged.
Two things this does not change, both noted in the issue: the
Flusherror text still says the extended query mechanism is unsupported, andFlushis still rejected rather than implemented as an output flush.Closes: #118202
Related: #108469
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A simple
Queryin the PostgreSQL wire protocol now destroys the unnamed prepared statement and the unnamed portal, as PostgreSQL does, so a laterBindorExecuteon them is rejected instead of silently reusing stale extended-query state. AFlushor unimplemented message sent outside an extended-query cycle also no longer leaves the connection unable to answer further queries; that half only affects unreleased code from #108469.Workflow [PR]
Sync PR [sync-upstream/pr/118210]
Version info
26.9.1.948(included in26.9and later)26.8.3.69,26.7.7.49,26.6.5.77,26.3.33.17