Repository navigation
Description
Missing feature
Three gaps in the table-copy path added by #211 (read_rows / insert_records / table_copy) that the host (Tabularis, TabularisDB/tabularis#1625) needs closed before it can consume it without risking data loss or dropping a feature PostgreSQL users have today.
Plugin version
main at 5715a20 (#211 merged), manifest still 1.0.0-rc.14.
Tabularis app version
0.34.0 (host work in TabularisDB/tabularis#1625, which removes the built-in PostgreSQL driver and wants the old PostgreSQL to PostgreSQL / PostgreSQL to SQLite table copy back through this RPC pair).
Additional context
Thanks for #211. Reading the merged handlers/copy.rs and extract.rs against what the host has to plan a copy from, these three things are still open. They are ordered by how much I would block on them.
1. Wide NUMERIC and NaN read back as NULL (data loss)
extract_value_lossless delegates NUMERIC to the display path (extract.rs), which decodes with rust_decimal::Decimal and maps the decoded value to a string. Decimal holds a 96-bit mantissa, so about 28 to 29 significant digits, and cannot represent NaN. In both cases Decimal's PostgreSQL decoder returns None/an error. try_extract then tries a String fallback, but String::accepts is limited to VARCHAR/TEXT/BPCHAR/NAME/UNKNOWN and does not accept NUMERIC, so the fallback fails too and the cell becomes JsonValue::Null. The from_sql path (extract.rs:722) has the same unwrap_or(JsonValue::Null).
PostgreSQL allows far wider values than that. Values I checked against a real PostgreSQL 16:
SELECT 12345678901234567890123456789012345678.1234567890::numeric(48,10), -- 38 integer digits
'NaN'::numeric,
123456789012345678901234567890::numeric; -- 30 digitsA numeric(38,10) column is a common money/measurement type, and #211's round-trip test covers numeric(38,10) only for values that fit in 96 bits. For a copy this is the worst failure mode: no error, and the host writes NULL over real data. read_rows is the "lossless" path, so a lossy NULL here is the one thing it must not do.
I could not run the plugin binary locally to confirm the NULL end to end, so this is read from the source above. Please check it with a live test.
Suggested fix: in the lossless path only, read NUMERIC as its exact decimal text (the wire format is base-10000 digits plus weight, sign and scale, so a small decoder that builds the decimal string needs no big-number crate), and return "NaN", "Infinity" and "-Infinity" as strings. If a value ever cannot be decoded, return an error for the row instead of NULL.
2. read_rows has no column metadata on an empty page, and get_columns does not return the type OID
fetch_lossless builds columns from rows[0], and its comment explains why an empty page reports columns: []: query_typed's unnamed protocol has no prepared-statement handle to introspect, and the host is expected to call get_columns for an empty source. That is a reasonable choice, and the host can follow it. The catch is that the host then has two different type vocabularies for the same table:
read_rowscolumns carrytype(the PostgreSQL type name, for exampletimestamptz) andoidget_columnsreturnsdata_type/full_typedisplay strings and no OID
The host plans the copy from the column types before it reads any row, so it needs one mapping that works for both. A zero-row table, or a copy that plans first and reads later, is a normal case, and a type like numeric(38,10) or a user-defined enum is spelled differently in the two.
Either of these would close it, in order of preference:
- return the canonical PostgreSQL type name (the same string
read_rowsuses, for example frompg_catalog.format_typeorType::name()) and theoidfromget_columnsas well, or - make
read_rowsreturn the column list for an empty page by running a zero-row probe on the same connection, for example the statement wrapped asSELECT * FROM (<query>) q LIMIT 0through a prepared statement, since a prepared statement does expose its result columns.
If you would rather keep both as they are, say so and the host will map get_columns display types itself, but then the two vocabularies have to stay in step with each other.
3. insert_records has no skip-on-conflict option
The host's table copy has two conflict modes today, fail and skip ("skip rows that already exist in the target"). The removed built-in driver implemented skip as INSERT ... ON CONFLICT DO NOTHING. insert_records can only fail, so a PostgreSQL target would lose skip, which is a feature users of the built-in driver have now.
Suggested fix: an optional on_conflict parameter ("fail" default, "skip" for ON CONFLICT DO NOTHING) on insert_records, with the response reporting both affected_rows (rows actually inserted) and how many were skipped, so the host's rows_written and rows_skipped stay honest. If you would rather not add it, the host can drop skip for PostgreSQL targets; please say which, so the host does not guess.
Not asking for (host handles these)
- Arrays, ranges and composites of temporals staying lossy: the host will refuse those columns before writing.
- An uncapped single-row
bytea: now tracked in Table copy: bound the size of a read_rows response (uncapped bytea) #215 (it was only mentioned in feat: add lossless read_rows RPC for table-to-table copy (#207) #211's description, with no issue).
Related
- feat: remove built-in PostgreSQL driver (force-migrate + delete) tabularis#1625 (the host PR)
- Feature parity gap: lossless row read/write for host table-to-table copy #207 and feat: add lossless read_rows RPC for table-to-table copy (#207) #211 (this feature), Feature parity gap: transactional SQL import (import_database_sql) #201, Feature parity gap: protected read-only queries (read_only_queries / execute_read_only_query) #202, Bug: pagination appends LIMIT/OFFSET inside a trailing -- comment, so every page returns the same rows #204 (siblings, merged)
- The host will pin its CI fixture and its migration gate to the release that carries this plus the unix-socket fix, so the release number is the only other thing it needs from the plugin.
Activity
aesslinger commentedon Dec 1, 2026
One more item for this follow-up, found while planning the host side of the copy. It is about how
read_rowspages, and it matters for large tables.4.
read_rowspages withLIMIT/OFFSET, which is quadratic and has no stable row orderrun_read_rowsbuilds the paged statement withbuild_paginated_query(query, page_size, page), so every page re-runs the query and discards all earlier rows. For a copy that reads the whole table, the total work grows with the square of the table size. There is also noORDER BY, so even inside aREPEATABLE READsnapshot the row order of two pages is not guaranteed to be consistent (the snapshot fixes which rows are visible, not the order the executor returns them in). The host will have to add anORDER BYto make pages deterministic, which makes each page more expensive still.Measured on PostgreSQL 16 with a 2,000,000-row table (
id intprimary key, a 30 charactertextcolumn), reading the whole table 1,000 rows at a time, server side with no network or JSON:OFFSETpages,ORDER BY id(what a deterministicread_rowspage sequence costs)MOVE FORWARD 1000, so rows are scanned but not returned)The cost of a single page also grows with its offset. The last page (offset about 2M) took 62 ms with no
ORDER BY, 186 ms ordered by primary key, and 504 ms ordered byctid(a table with no primary key), against a fraction of a millisecond for the first page. These are server-side numbers only. The cursor figure usesMOVE, which advances without returning rows, so it measures the cost of scanning the table once, not of fetching and encoding it; a real copy adds per-row JSON encoding and an RPC round trip to both approaches, so the end-to-end gap would be smaller than the ratio above, but the growth with table size is the same.The removed built-in driver read the whole table through one
DECLARE ... NO SCROLL CURSORinside aREPEATABLE READ READ ONLYtransaction andFETCH FORWARDper batch, for this reason.Suggested fix: when
read_rowsis called with asession_id(the snapshot path), keep a server-side cursor open on the pinned connection and answer each page withFETCH FORWARD <page_size>instead of re-running the statement withOFFSET. That is linear in the table size, gives one stable order, and fits the existingsession::Slotmachinery (the cursor lives and dies with the pinned transaction, sorelease_sessionalready cleans it up). The first call wouldDECLAREthe cursor from the host'squery, and later calls would only fetch. The no-session_idpath can keepLIMIT/OFFSET, since it makes no consistency promise.If you would rather not add the cursor in this round, the alternative is documenting that the host must supply a deterministic
ORDER BYinquery, and treatingOFFSETpaging as best effort for small tables. The host would then have to refuse large tables or accept the slowdown, so the cursor is the better fix.fix: address pre-handoff ensemble review findings for #214
fix: close four table-copy gaps in read_rows/insert_records (#214) (#216
chore: update rc.15 CHANGELOG with #214/#216 and #215/#217 entries
chore: release 1.0.0-rc.15 (#213)