Skip to content

fix: bound read_rows response size for uncapped bytea - #217

Merged
aesslinger merged 2 commits into
mainfrom
fix/issue-215-bound-read-rows-response
Dec 2, 2026
Merged

aesslinger merged 2 commits into
mainfrom
fix/issue-215-bound-read-rows-response

Conversation

@aesslinger

@aesslinger aesslinger commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Closes #215 — bound the size of a read_rows response so a table with large bytea values 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 READ snapshot) now bounds a single response by a byte budget instead of only by row count:

  • SCROLL cursor: the snapshot cursor's DECLARE switches from NO SCROLL to SCROLL, so the over-budget row can be rewound with MOVE BACKWARD.
  • Byte-budget fetch loop (fetch_cursor): rows are fetched one at a time (FETCH FORWARD 1) and accumulated until adding the next row would exceed MAX_RESPONSE_BYTES (16 MB). The over-budget row is rewound with MOVE BACKWARD 1 so the next page picks it up, and has_more reports true.
  • Always at least one row: a single row larger than the budget is still returned as its own response — only that one row is in flight, not a page of them (the issue's requirement).
  • has_more when budget hit: true (we rewound unreturned rows — there are definitely more). The existing rows_returned >= page_size rule applies when the budget is not hit.

The one-shot path (no session_id) is unchanged: it returns exactly limit rows and has no byte budget. The host does not use the one-shot path for copies.

Host contract

Confirmed with the fix-sqlite-type-bugs session (which opened #215): the host always passes a session_id for copy reads and loops on has_more only, never on rows.len() < limit. page_size in the response is the requested limit, not the shortened count. The host needs no change.

Why SCROLL is cheap here

A SCROLL cursor on a 200K-row SELECT ... FROM table ORDER BY id (the common copy query) streams — it does not materialize. FETCH FORWARD 1 is the natural cursor read; MOVE BACKWARD 1 is a single-row rewind. The PostgreSQL docs note SCROLL may materialize for some plans (hash joins, etc.), but a simple ordered table scan — the copy case — streams.

Test plan

  • Unit (no live DB): 713 lib tests pass (0 failed); cargo clippy --all-targets -- -D warnings clean; cargo fmt --all -- --check clean.
  • Live (podman): four session-cursor tests pass —
    • read_rows_session_cursor_pages_a_large_table_linearly (2500 small rows, 3 pages — the normal case, budget never hit) — confirms SCROLL + the byte budget doesn't break the common case.
    • read_rows_session_cursor_bounds_response_by_bytes_for_large_bytea — 10 rows of 5 MB bytea. Verifies every row appears exactly once across pages (no skips from a missing MOVE BACKWARD, no repeats), and each page stays under the 16 MB budget.
    • read_rows_session_cursor_returns_one_oversized_row_alone — a single 20 MB bytea row (larger than the 16 MB budget) returns as one row on its own page.
    • existing read_rows_session_cursor snapshot-idempotency test still passes.

Notes for review

  • The one-row-at-a-time fetch (FETCH FORWARD 1 + MOVE BACKWARD 1) is deliberate: a batch FETCH FORWARD N + MOVE BACKWARD N rewind 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.
  • This is the issue's option 1 (bound by bytes). Option 2 (stream large values separately via 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.

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>
@aesslinger aesslinger added enhancement New feature or request prerelease:rc Version suggestion targets a release candidate labels Dec 2, 2026
@aesslinger aesslinger self-assigned this Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Version suggestion

Based on this PR's title (fix) and the prerelease:rc label:

Current 1.0.0-rc.14
Suggested next tag v1.0.0-rc.15

This is informational only — no tag or release is created automatically yet.

@aesslinger
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
aesslinger merged commit 2441429 into main Dec 2, 2026
8 checks passed
@aesslinger
aesslinger deleted the fix/issue-215-bound-read-rows-response branch December 2, 2026 15:05
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request prerelease:rc Version suggestion targets a release candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table copy: bound the size of a read_rows response (uncapped bytea)

1 participant