Skip to content

Table copy follow-up: wide NUMERIC reads as NULL, column types on empty pages, skip-on-conflict #214

Done

Description

@aesslinger

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 digits

A 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_rows columns carry type (the PostgreSQL type name, for example timestamptz) and oid
  • get_columns returns data_type / full_type display 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_rows uses, for example from pg_catalog.format_type or Type::name()) and the oid from get_columns as well, or
  • make read_rows return the column list for an empty page by running a zero-row probe on the same connection, for example the statement wrapped as SELECT * FROM (<query>) q LIMIT 0 through 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)

Related

Activity

  1. added
    capability-gap
    on Dec 1, 2026
  2. self-assigned this
    on Dec 1, 2026
  3. aesslinger commented on Dec 1, 2026

    @aesslinger
    CollaboratorAuthor

    One more item for this follow-up, found while planning the host side of the copy. It is about how read_rows pages, and it matters for large tables.

    4. read_rows pages with LIMIT/OFFSET, which is quadratic and has no stable row order

    run_read_rows builds the paged statement with build_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 no ORDER BY, so even inside a REPEATABLE READ snapshot 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 an ORDER BY to make pages deterministic, which makes each page more expensive still.

    Measured on PostgreSQL 16 with a 2,000,000-row table (id int primary key, a 30 character text column), reading the whole table 1,000 rows at a time, server side with no network or JSON:

    How the table is read Total time
    2,000 OFFSET pages, ORDER BY id (what a deterministic read_rows page sequence costs) 120.7 s
    One cursor advanced through the table in 1,000-row steps (MOVE FORWARD 1000, so rows are scanned but not returned) 0.085 s

    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 by ctid (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 uses MOVE, 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 CURSOR inside a REPEATABLE READ READ ONLY transaction and FETCH FORWARD per batch, for this reason.

    Suggested fix: when read_rows is called with a session_id (the snapshot path), keep a server-side cursor open on the pinned connection and answer each page with FETCH FORWARD <page_size> instead of re-running the statement with OFFSET. That is linear in the table size, gives one stable order, and fits the existing session::Slot machinery (the cursor lives and dies with the pinned transaction, so release_session already cleans it up). The first call would DECLARE the cursor from the host's query, and later calls would only fetch. The no-session_id path can keep LIMIT/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 BY in query, and treating OFFSET paging 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

capability-gap

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions