Skip to content

fix: read non-native copy columns as server text, close remaining copy gaps - #227

Merged
aesslinger merged 3 commits into
mainfrom
fix/issue-225-read-rows-copy-gaps-2
Dec 3, 2026
Merged

aesslinger merged 3 commits into
mainfrom
fix/issue-225-read-rows-copy-gaps-2

Conversation

@aesslinger

@aesslinger aesslinger commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #225

Why a different approach

#220 → #222 → #225 were three rounds of the same bug class. tokio-postgres always asks for binary results, so every read_rows value came from a hand-written Rust copy of a PostgreSQL output function (array_out, interval_out, jsonb_out, range_out, BC dates…). Each retest found the next mismatch, for example interval '14 months' (PostgreSQL normalizes months to years but never hours to days). This PR stops imitating the server and lets it produce the text.

Read side

Inside the read transaction, read_rows calls prepare to get the column metadata. It then wraps the query as SELECT c1, c2::text, … FROM (\n<query>\n) AS tabularis_q(c1..cN):

  • Native (binary, unchanged): bool, int2/4/8, oid, float4/8 (with "NaN"/"Infinity"), bytea (full base64), text/varchar/bpchar/name, uuid
  • money: ::numeric::text, so it stays "123.45"
  • Everything else: ::text

The transaction pins DateStyle='ISO, MDY', IntervalStyle='postgres' and extra_float_digits=3 with SET LOCAL.

This one change closes:

  • multi-dimensional arrays
  • [2:3]= lower bounds
  • 1e400, wide jsonb numbers and json duplicate keys
  • tstzrange offsets
  • interval text, BC dates and years past 9999

The lossless binary decoders are deleted: PgNumeric, the full temporal decoders, PgIntervalText, PgMoneyText and the lossless array decoder. extract.rs now differs from rc.15 only in extract_value_lossless.

Smaller items

  • The one-shot read always ends with ROLLBACK. The transaction is read-only, so there is nothing to commit, and ROLLBACK reverts any set_config(…, false) the query issued, which fixes the GUC leak. The startup script's settings are applied before BEGIN, so they survive.
  • Trailing comments and ; are trimmed with the pagination lexer (trim_statement_end), replacing the hand-rolled strip_trailing_line_comment. select 1 -- c\n now works on the session path, and user SQL is always placed on its own lines inside the wrapping parens.
  • limit: -1 (or any non-integer) now returns -32602 instead of the default 1000.

Write side

  • After all existing steps, the binding cascade has a new last step: a value no earlier step recognizes is bound as text and wrapped in CAST($N AS <udt_schema>.<udt_name>), cast to the column's qualified type. This fixes composites and lower-bound arrays. Text-family columns are never cast, and the BLOB decode is still bytea-only.
  • JSON objects inside a jsonb[] array are written as quoted JSON text.
  • A JSON-array-shaped string ('[1,2,3]') going into a known text-family column is stored as is. It used to be rewritten to {1,2,3}, a bug that predates this PR and was found in the host retest. Array and unknown columns keep the array conversion.

Review fixes (ensemble review + host retest)

  • bytea: a bytea column takes only the BLOB: wire format. Any other string, such as an unresolved BLOB_FILE_REF: from the file picker or corrupt base64, is an error again. Before this fix, the new cast step stored the string's own bytes.
  • Transaction pooler: behind a transaction pooler, read_rows runs on a one-shot connection and starts with DEALLOCATE ALL, so its describe prepare can't collide with statement names left on the pooler's backend (42P05).
  • hstore: the text read_rows returns for an hstore column is cast back to hstore on write.
  • Custom ranges: the JSON-array guess applies only to array columns or columns of unknown type, so a user-defined range like [1.5,2.5] reaches the cast to its type.
  • Output settings: the read also pins bytea_output='hex' and TimeZone='UTC'.
  • Text columns: the raw SQL, WKT and UUID shape guesses no longer apply to known text columns, so an md5 hash in a text column copies as is. This bug predates the PR, but it silently changed copied text.
  • Docs and tests: stale docs are corrected, and new live tests cover each fix (each fails without its fix), the settings under a hostile startup script, session columns on later pages and for empty results, a trailing ;, a failed first page, composite row editing, and the pooler collision.

Not changed: money[] keeps the server's locale form (documented), and user types named like built-ins still get cast to the built-in type, which predates this PR.

Contract changes (host)

  • Arrays, ranges, multiranges and composites are returned as PostgreSQL text strings, not JSON arrays or objects. The host blocks these types today, so nothing breaks.
  • timestamptz is read in UTC (+00).
  • interval follows IntervalStyle=postgres.
  • Zero-row results include columns.
  • I checked with the host session and no host change is needed. The host e2e postgres_plugin_table_copy passes 3/3 against the release build.

Verification

…y gaps (#225)

read_rows now projects every column that is not a native JSON type
through ::text inside the read transaction (money via ::numeric::text),
so values are the server's own output instead of hand-written binary
decoders. This closes multi-dimensional arrays, array lower bounds,
1e400 and wide jsonb numbers, json duplicate keys, tstzrange offsets and
interval normalization in one mechanism, and deletes the lossless
decoders that kept diverging. Column metadata comes from prepare, so a
zero-row result reports its columns.

- one-shot reads always ROLLBACK, so set_config(..., false) cannot leak
  into the pooled connection
- trailing comments and semicolons are trimmed with the pagination
  lexer, fixing `select 1 -- c\n` on the session path
- a negative or non-integer limit is a -32602 error, not 1000
- insert_records casts otherwise-unrecognized values to the column's
  qualified type (composites, lower-bound arrays) and accepts objects in
  jsonb[] arrays

Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
insert_records turned a text value like '[1,2,3]' into the array
literal {1,2,3} whatever the target column, so a copied text cell
holding a JSON array arrived changed. Skip the array guess when the
column is a known text-family type; array and unknown columns keep it.

Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
@aesslinger aesslinger self-assigned this Dec 3, 2026
- a bytea column takes only the BLOB wire format; any other string
  (an unresolved BLOB_FILE_REF, corrupt base64) is an error again
  instead of being cast to bytea and stored as its own text
- behind a transaction pooler read_rows runs on a one-shot connection
  and starts with DEALLOCATE ALL, so its prepare can't collide with
  statement names another client left on the backend (42P05)
- hstore text from read_rows casts to the column's hstore type
- the JSON-array guess applies only to array or unknown columns, so a
  user-defined range's [a,b] text reaches the cast to its type
- the read also pins bytea_output = 'hex' and TimeZone = 'UTC'
- raw SQL, WKT and UUID shape guesses no longer apply to text columns,
  so an md5 hash in a text column is copied as is
- stale docs, a split doc comment, and tests for the settings, session
  columns on later pages, a trailing ';', a failed first page, row
  editing of a composite and the pooler collision

Co-Authored-By: Adam J Esslinger <esslinger@gmail.com>
@aesslinger
aesslinger merged commit db64490 into main Dec 3, 2026
14 checks passed
@aesslinger
aesslinger deleted the fix/issue-225-read-rows-copy-gaps-2 branch December 3, 2026 17:45
aesslinger added a commit that referenced this pull request Dec 3, 2026
Bumps version to 1.0.0-rc.16 in .tabularium, Cargo.toml, Cargo.lock, and
the CHANGELOG. This release includes manual commit mode (#219), the
read_rows server-text read path (#225, #227), and the copy fixes from
#220/#221, #222/#223 and #224/#226. Known limitations: money[] locale text
form and composite types named like built-ins.
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 follow-up to #222: multi-dimensional arrays, jsonb 1e400, session-path comment newline, GUC leak, jsonb[]/composite inserts

1 participant