Skip to content

Keep PostgreSQL message recovery aligned after parse errors - #118097

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix/postgresql-message-framing
Sep 5, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix/postgresql-message-framing

Conversation

@antonio2368

@antonio2368 antonio2368 commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Parse and Bind rejected negative count fields before consuming the rest of the declared PostgreSQL message payload. The extended-query recovery loop could then interpret residual payload bytes as new frontend frames and react to a fake Sync before the client sent the real protocol-level message.

Read both payloads through an exact-size LimitReadBuffer. If deserialization fails, consume the declared remainder before propagating the exception to the recovery handler. The regression coverage puts a complete-looking Sync frame after each invalid count, inside the malformed message payload, and verifies that recovery waits for the real Sync and keeps the connection usable.

Related: #108469

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Workflow [PR]
Sync PR [sync-upstream/pr/118097]

`Parse` and `Bind` errors could leave bytes from the declared payload in the connection buffer. The recovery loop then treated those bytes as new frontend messages before it reached the real `Sync`.

Read both message payloads through an exact-size `LimitReadBuffer` and consume the declared remainder before propagating an exception. Extend the socket and unit coverage with an embedded fake `Sync` frame.

Review: #108469 (comment)
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a78879d]

Summary: ✅


AI Review

Summary

This PR hardens PostgreSQL frontend framing for Query and the extended-protocol messages by forcing exact-size payload reads, draining malformed payloads before recovery, and rejecting non-empty Sync / CopyDone. The recovery bug from the earlier review threads is fixed, but one auth-path framing hole remains, so I would not merge this revision yet.

Findings
⚠️ Majors
  • [src/Core/PostgreSQLProtocol.h:292] The new exact-size framing contract still does not cover authentication. SASLInitialResponse::deserialize and PasswordMessage::deserialize still read NUL-terminated fields directly from the live socket after consuming the size field, so a truncated auth frame can consume bytes from the next frontend message or block waiting for a terminator beyond the declared payload.
    Suggested fix: route both auth parsers through the same exact-size helper (or an auth-specific variant that also consumes the leading type byte) and add a regression test that proves the next message stays unread when the auth payload is malformed.
Tests
  • ⚠️ Add a focused auth-path regression test for cleartext and/or SCRAM authentication that sends size = 4 plus a following frame, and verifies the handler rejects the malformed auth message without reading into that next frame.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit a78879d.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.10% 81.10% +0.00%

Changed lines: Changed C/C++ lines covered: 244/246 (99.19%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 4, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 4, 2026
Comment thread src/Core/PostgreSQLProtocol.h Resolved
Comment thread src/Core/PostgreSQLProtocol.h Resolved
Comment thread src/Core/PostgreSQLProtocol.h Resolved

protected:
template <typename F>
static void deserializePayload(ReadBuffer & in, std::string_view message_name, F && deserialize_payload)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new exact-size helper closes the recovery hole for Query / extended-protocol messages, but the same framing invariant is still open during authentication. SASLInitialResponse::deserialize (src/Core/PostgreSQLProtocol.h:566) and PasswordMessage::deserialize (src/Core/PostgreSQLProtocol.h:672) still call readNullTerminated on the live socket after reading the length field, without a LimitReadBuffer. A malformed auth frame can therefore still read past its declared payload into the next frontend message or wait indefinitely for a terminator that is not inside the declared frame. I think these two parsers need the same exact-size treatment as the query messages (possibly via an auth-specific helper that also consumes the leading type byte), plus a regression test that the next message stays unread when the auth payload is truncated.

@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a78879ddb with master 35a0d6f0c (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 323d42647; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

3 object files changed (+21.69 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Server/PostgreSQLHandler.cpp.o 599.25 KiB 620.95 KiB +21.70 KiB (+3.62%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

19 translation units recompiled, 66 s compile time in total, 19 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +8.3 s (+14%) in total before that adjustment.

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 5, 2026
Merged via the queue into master with commit 5177274 Sep 5, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/postgresql-message-framing branch September 5, 2026 16:05
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 7, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants