Skip to content

perf(proxy): track the first reply to each simple query - #531

Merged
tobyhede merged 2 commits into
mainfrom
fix/simple-query-reply-tracking
Dec 3, 2026
Merged

tobyhede merged 2 commits into
mainfrom
fix/simple-query-reply-tracking

Conversation

@tobyhede

@tobyhede tobyhede commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Proxy releases the unnamed prepared statement when PostgreSQL first answers a simple Query.
Tracking forwarded Queries in wire order lets unrelated backend replies skip the write lock used for that release.

Changes

Verification

On commit 55c961fe, 797 workspace unit tests passed and six were skipped.
Formatting, Clippy and all 15 release-metadata checks passed.
The regression for a Query received after an extended-protocol error failed before the model change and passed afterward.

Related

Follow-up to #497.

Acknowledgment

By submitting this pull request, I confirm that CipherStash can use, modify, copy, and redistribute this contribution, under the terms of CipherStash's choice.

@tobyhede
tobyhede marked this pull request as ready for review December 2, 2026 05:08

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review attestation

Reviewed 55c961fe · posted from Claude Code · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review · claude-opus-5-5; rust-security · claude-opus-5-5 (no findings)
Skills: code-review, company-skills:rust-security, mattpocock-skills:code-review, company-skills:review-cross-check

No blocking findings. The reply tracking is correct for pg-proto 0.12.0, and the changelog move is correct because #497 merged after v3.1.0. cargo test -p cipherstash-proxy --lib postgresql::middleware passed: 210 tests.

Findings (5): 5 minor: 4 inline, 1 below.

  • packages/cipherstash-proxy/src/postgresql/middleware/settings_rollback_tests.rs:104 (outside diff) minor This harness still builds Frontend and Backend separately and calls frontend.intercept directly, so query_forwarded never runs and a simple Query never releases the unnamed statement, unlike production. rollback_restores_original_keyset (extended) and pipelined_batch_that_changes_settings_and_rolls_back_restores_them mix the two paths, so their state differs from production. Route Client through CipherStashMiddleware::intercept_frontend, as middleware/mod.rs now does.
    Source: code-review · claude-opus-5-5

Generated by Claude Code

@cipherstash-bot cipherstash-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Recommendation: 🟢 merge as it is

Nothing must change before merging. Two optional test gaps can wait for a follow-up.

How this review was made
Agent Model Review type Result
claude claude-opus-5-5 test-gap 2 found, 2 posted
claude claude-opus-5-5 rust 0 found, 0 posted
codex gpt-5.6-terra test-gap 0 found, 0 posted
codex gpt-5.6-terra rust 0 found, 0 posted

Synthesis: claude-sonnet-5-5 merged the findings, removed duplicates and dropped findings it could not confirm in the code. 0 posted finding(s) were raised by two or more models.

Plain language: claude-sonnet-5-5 read every comment as a new reader would. 1 comment(s) had a problem that stopped the reader acting; it rewrote 0.

Stack: not part of a stack.

Context loaded: the description, 0 linked issue(s) and 5 discussion entries.


Other findings not posted as comments

  • packages/cipherstash-proxy/src/postgresql/middleware/mod.rs:1546 — Optional: No test checks that a Query that Proxy refuses is not added to pending_queries.

{
self.context.operation_answered(operation)?;
if let Some(query) = operation.filter(|&operation| self.take_first_query_reply(operation)) {
self.context.release_unnamed_statement(query)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: No test forwards an unnamed Parse after a simple Query and before that Query's first reply.

Impact: release_unnamed_statement now runs when the Query's first reply arrives. By then, Proxy may already track a new unnamed statement from a pipelined Parse "". If deallocate_statements stops comparing operation order, Proxy deletes the new statement. No test would fail. The next Bind to "" would then use missing metadata.

Evidence: reparse_pipelined_after_deallocate_finishes_the_old_metrics in middleware/mod.rs covers the same order for a named statement after DEALLOCATE. No test covers a Parse "" after the Query.

Fix: Add a test next to reparse_pipelined_after_deallocate_finishes_the_old_metrics in middleware/mod.rs. It sends a Query, then Parse "", then Sync. It then delivers the Query replies, the ParseComplete and the Sync reply. It asserts connection.holds_statement(b"") and connection.sizes().pending_queries == 0.

Found by 1 model: claude

@auxesis auxesis left a comment

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.

Thanks @tobyhede!

One small test gap to fix, but approving in anticipation of that.

Track forwarded simple queries in wire order so unrelated replies avoid the protocol-state write lock. Exercise a query discarded after an extended error and document the model's batch boundary.

Signed-off-by: Toby Hede <[email protected]>
@tobyhede
tobyhede force-pushed the fix/simple-query-reply-tracking branch from 7670abe to e61d0d7 Compare December 3, 2026 08:58
@tobyhede
tobyhede merged commit 4bc4a96 into main Dec 3, 2026
10 checks passed
@tobyhede
tobyhede deleted the fix/simple-query-reply-tracking branch December 3, 2026 09:13
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.

4 participants