Repository navigation
fix: bound read_rows response size for uncapped bytea - #217
Merged
Merged
Conversation
The session-pinned cursor path of read_rows returns bytea in full, base64-encoded, with no size cap — a page of large binary values can produce a single JSON-RPC response far larger than either side should hold in memory. Switch the snapshot cursor from NO SCROLL to SCROLL so the over-budget row can be rewound with MOVE BACKWARD. fetch_cursor now fetches one row at a time, accumulating the JSON-serialized byte size, and stops when the next row would exceed the 16 MB MAX_RESPONSE_BYTES budget — rewinding that row so the next page picks it up and reporting has_more = true. A single row larger than the budget is still returned as its own response (at least one row always goes out). The one-shot path is unchanged: it returns exactly limit rows and has no byte budget. Document the byte budget in the README (cursor path only; host loops on has_more alone, not rows.len() < limit). Live tests: 10×5 MB bytea rows page across several responses with every row appearing exactly once; a single 20 MB row returns alone. Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
Version suggestionBased on this PR's title (
This is informational only — no tag or release is created automatically yet. |
aesslinger
marked this pull request as ready for review
December 2, 2026 14:22
23-agent ensemble review (11 dimensions × 2 passes + /code-review)
found 13 confirmed findings. All fixed:
Performance (round-trip regression, score 78):
- Rewrite fetch_cursor to batch FETCH FORWARD {page_size} (1 round-trip,
the common case) with a single MOVE BACKWARD {rewind_count + 1} (also
via query_typed) when the byte budget is hit — 2 round-trips for the
budget case vs the ~1000 round-trips of the per-row FETCH FORWARD 1
loop. The +1 compensates for the extended-protocol cursor position
(the cursor sits *on* the last fetched row, not *after* it, so MOVE
BACKWARD N moves N-1). The per-row loop's only advantage was a
reliable single-row rewind; keeping both FETCH and MOVE on
query_typed (extended protocol) makes MOVE BACKWARD N exact.
- Add MAX_PAGE_SIZE = 100_000 cap on limit to prevent a hostile
limit: u32::MAX from driving millions of round-trips (the byte
budget only caps large rows; small rows sail through).
Test quality (score 85):
- Fix oversized-row test: the "20 MB" row was 10 MB (10M bytes →
13.3 MB base64, under the 16 MB budget). Increased to 13 MB (13M
bytes → ~17.3 MB base64 > 16 MB) so the always-at-least-one rule
is actually exercised.
- Add saw_short_page assertion to the bounds test: assert at least
one page returns fewer rows than limit (proving the byte budget
threshold fires, not just the MOVE BACKWARD rewind).
Docs-vs-code-drift (6 fixes):
- run_snapshot doc: FETCH FORWARD <page_size> (batch) → per-row
description (now batch FETCH + MOVE BACKWARD).
- MAX_RESPONSE_BYTES doc: "rows" (plural) → "single unreturned
over-budget row".
- README: "exactly limit rows" → "at most limit rows".
- Comment typo: NO SCOLL → NO SCROLL.
- Test comments: "FETCHes the first/next batch" → "page" (stale
batch terminology from pre-#215 design).
Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
aesslinger
added a commit
that referenced
this pull request
Dec 2, 2026
Merge main (PRs #216, #217) into release/rc.15 and update the CHANGELOG: - Added: session-pinned cursor paging, on_conflict skip, get_columns type+oid (#214, #216) - Fixed: wide NUMERIC/NaN data-loss on read_rows, read_rows byte-budget bounding (#215, #217) - Test count: 682 → 713 Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
aesslinger
added a commit
that referenced
this pull request
Dec 2, 2026
* chore: release 1.0.0-rc.15 Bumps version to 1.0.0-rc.15 in .tabularium, Cargo.toml, Cargo.lock, and the CHANGELOG. This release includes: - feat: lossless table-to-table copy path (#207) — read_rows RPC (lossless, optionally-snapshot read) + insert_records RPC (batch typed insert) + table_copy capability - fix: replace unmaintained chrono with custom wire-decode (#200) — full Postgres date range parity, jiff dependency removed - fix: avoid SQL pagination for data-modifying CTE statements (#212) — WITH ... INSERT/UPDATE/DELETE no longer gets LIMIT appended or re-executed - fix: paginate queries that end in a comment (#204) — comment-aware lexer - fix: large limit/page no longer wraps to LIMIT 0 Co-authored-by: Adam J. Esslinger <14128427@aesslinger@users.noreply.github.com> * chore: update rc.15 CHANGELOG with #214/#216 and #215/#217 entries Merge main (PRs #216, #217) into release/rc.15 and update the CHANGELOG: - Added: session-pinned cursor paging, on_conflict skip, get_columns type+oid (#214, #216) - Fixed: wide NUMERIC/NaN data-loss on read_rows, read_rows byte-budget bounding (#215, #217) - Test count: 682 → 713 Co-Authored-By: Adam J Esslinger <esslinger@gmail.com> --------- Co-authored-by: Adam J. Esslinger <14128427@aesslinger@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #215 — bound the size of a
read_rowsresponse so a table with largebyteavalues can be copied without exhausting memory in the plugin or the host. The issue's suggested fix #1 (bound the page by bytes, not only by rows) is what this implements.What changed
The session-pinned cursor path (the one the host uses for copies — it needs one consistent
REPEATABLE READsnapshot) now bounds a single response by a byte budget instead of only by row count:SCROLLcursor: the snapshot cursor'sDECLAREswitches fromNO SCROLLtoSCROLL, so the over-budget row can be rewound withMOVE BACKWARD.fetch_cursor): rows are fetched one at a time (FETCH FORWARD 1) and accumulated until adding the next row would exceedMAX_RESPONSE_BYTES(16 MB). The over-budget row is rewound withMOVE BACKWARD 1so the next page picks it up, andhas_morereportstrue.has_morewhen budget hit:true(we rewound unreturned rows — there are definitely more). The existingrows_returned >= page_sizerule applies when the budget is not hit.The one-shot path (no
session_id) is unchanged: it returns exactlylimitrows and has no byte budget. The host does not use the one-shot path for copies.Host contract
Confirmed with the
fix-sqlite-type-bugssession (which opened #215): the host always passes asession_idfor copy reads and loops onhas_moreonly, never onrows.len() < limit.page_sizein the response is the requestedlimit, not the shortened count. The host needs no change.Why SCROLL is cheap here
A
SCROLLcursor on a 200K-rowSELECT ... FROM table ORDER BY id(the common copy query) streams — it does not materialize.FETCH FORWARD 1is the natural cursor read;MOVE BACKWARD 1is a single-row rewind. The PostgreSQL docs noteSCROLLmay materialize for some plans (hash joins, etc.), but a simple ordered table scan — the copy case — streams.Test plan
cargo clippy --all-targets -- -D warningsclean;cargo fmt --all -- --checkclean.read_rows_session_cursor_pages_a_large_table_linearly(2500 small rows, 3 pages — the normal case, budget never hit) — confirmsSCROLL+ the byte budget doesn't break the common case.read_rows_session_cursor_bounds_response_by_bytes_for_large_bytea— 10 rows of 5 MBbytea. Verifies every row appears exactly once across pages (no skips from a missingMOVE BACKWARD, no repeats), and each page stays under the 16 MB budget.read_rows_session_cursor_returns_one_oversized_row_alone— a single 20 MBbytearow (larger than the 16 MB budget) returns as one row on its own page.read_rows_session_cursorsnapshot-idempotency test still passes.Notes for review
FETCH FORWARD 1+MOVE BACKWARD 1) is deliberate: a batchFETCH FORWARD N+MOVE BACKWARD Nrewind was unreliable across the extended/simple protocol mix (moved N-1 positions, losing every 3rd row). Single-row rewind is exact.MAX_RESPONSE_BYTES = 16 * 1024 * 1024(16 MB): the issue suggests 8–16 MB; 16 MB is generous for a single response while keeping a page of 1000 small rows well under it.fetch_blob_as_data_url/save_blob_to_file) is not needed for the common case and is left as a future follow-up if single values in the hundreds of megabytes become a real use case.