Skip to content

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

Done

Description

@aesslinger

Missing feature

A bound on how much bytea one read_rows response can carry, so a table with large binary values can be copied without exhausting memory in the plugin or the host.

Plugin version

main at 5715a20 (#211 merged); also in the 1.0.0-rc.15 release PR (#213).

Tabularis app version

0.34.0 (host work in TabularisDB/tabularis#1625).

Additional context

This is the "uncapped single-row bytea" item. #211's description lists it under known limitations as a possible follow-up, but there was no issue for it, so this is the tracker. (Correcting my own wording in #214, which said it was "noted as a follow-up in #211": it is mentioned in the PR description only, not in the README or CHANGELOG.)

What happens today. read_rows returns bytea in full, base64-encoded, inside the JSON response (README, "Table copy": "bytea base64-encoded in full (not the 10 KB preview)"). That is the point of the lossless path, and it is correct for typical values. There is no size cap, so:

  • PostgreSQL stores a bytea up to 1 GB per value. A 100 MB value (I created one in a test table) becomes a base64 string of about 133 MB.
  • A page is one JSON-RPC response, and the host reads one line per response into a buffer and parses it whole (reader.read_line then serde_json::from_str, src/plugins/driver.rs). A page holds up to limit rows (default 1000), so a table with large values can produce a single response far larger than either process should hold. On the plugin side the page's rows are also built as a JsonValue tree before being written, which I have not measured, so I do not claim a number for the peak; it is at least the size of the response on the host side.
  • I could not find a response size limit in the host's plugin reader (it uses read_line), so nothing stops it.

The old execute_query path avoided this by truncating to a 10 KB preview. The lossless path removed the truncation, which is right for correctness but trades it for this memory risk.

Why it is not a blocker for the first release. Most tables have small or no bytea columns, and the copy still works for them. It becomes a problem for tables that hold files, images or serialized blobs, which are the ones users are most likely to want to copy.

Suggested fix. Either of these keeps the copy lossless:

  1. Bound the page by bytes, not only by rows. Stop adding rows to a page once its encoded size passes a budget (for example 8 to 16 MB), always returning at least one row, and report has_more as usual. The host already pages until has_more is false, so it needs no change. A single row larger than the budget still goes out as one response, but then only that one row is in flight rather than a page of them.
  2. Stream large values separately. For a bytea over a threshold, return a reference in the row (for example the primary key plus a marker) and let the host fetch it through the existing fetch_blob_as_data_url / save_blob_to_file path, which already handles one value at a time. This needs a way to identify the row, so it only works for tables with a primary key.

Option 1 is small and fixes the common case (many medium-sized values). Option 2 is needed only if single values can be in the hundreds of megabytes.

Host side. Independently of this issue, the host will refuse or warn when a source column is bytea and the plugin reports a value over a size it will accept, so a copy fails with a clear error instead of running out of memory. The plugin-side bound is what makes that rare.

Related

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions