Skip to content

Table copy follow-up to #220: arrays, intervals and bit(n) still lose or reject data; #221 left several #220 items open #222

Done

Description

@aesslinger

Summary

#221 closed #220, but several of its items are still broken on main, and a wider sweep of arrays, intervals and bit(n) found more. Everything below was re-run against a release build of main at 7672f6a (the #221 merge) over JSON-RPC against PostgreSQL 16, not taken from the PR description. The host (TabularisDB/tabularis#1625) works around what it can and fails loudly on the rest, so none of this is blocking, but a copy of an affected column fails or is changed.

A lot of #220 is fixed and verified: the duplicate-row rewind (2000 rows now return exactly 2000), NaN/±Infinity/infinity/24:00:00 on scalar reads, money as text, insert_records for inet/cidr/macaddr/xml/varbit/tsvector/point/ranges/money, a BLOB-shaped text value, the query-mismatch error on a reused session_id, ;+whitespace trimming, redacted bind errors, is_generated, full_type, datetime_precision, materialized views and domain types. Thank you. What is left:

1. Still returns null for a value that exists

read_rows returns null with no error, so a caller cannot tell it from SQL NULL. These were in #220 (item 2) and #221 did not cover them.

Value Returned
multi-dimensional array, '{{1,2},{3,4}}'::int[] null (the whole cell)
'null'::jsonb (JSON null) null
'1e400'::jsonb null
time[], array['01:02:03.123456'::time] [null]
float8[] / float4[] containing NaN or Infinity [null]
numeric[] containing NaN [null]
date[], timestamp[] containing infinity [null]

The scalar versions of the float, date, timestamp and time cases are fixed. The same specials inside an array still go through the old display path, so the array element conversion needs the same lossless handling.

2. Returns a changed value

  • timestamp[] and timestamptz[] lose precision and offset. array['2024-01-02 03:04:05.123456+00'::timestamptz] reads as ['2024-01-02 03:04:05']. The server's text is {"2024-01-02 03:04:05.123456+00"}. The scalar column is fine (2024-06-01 06:30:00+00:00). tstzrange also drops the offset (["2024-01-01 00:00:00", ...) where the server has +00).
  • interval is rewritten. interval '48 hours' is stored as 48:00:00 (a time part, not days) and reads back as 2 days. The two compare equal, but they are not the same value: across a DST change '2024-03-09 12:00'::timestamptz + interval '2 days' is 2024-03-11 12:00:00-04 and + interval '48 hours' is 2024-03-11 13:00:00-04 in America/New_York. 1000000 hours reads as 41666 days 16:00:00. After a read and an insert, the target's text differs from the source's. The interval needs to be sent in the form PostgreSQL printed it (interval::text gives 48:00:00).
  • Array lower bounds are dropped. '[2:3]={1,2}'::int[] reads as [1, 2] (the server prints [2:3]={1,2}).
  • jsonb numbers pass through f64. 12345678901234567890123.5 reads as 1.2345678901234568e+22. fix: close seven read_rows/insert_records copy gaps (#220) #221 lists this as a known limitation (it needs arbitrary_precision).
  • json is parsed. {"a":1,"a":2} reads as {"a": 2}.
  • money[] is still integer cents. array[12.5::money] reads as [1250], while scalar money is fixed ('12.50').

3. insert_records cannot write back what read_rows emits

All of these fail loudly and roll the batch back.

  • bit(n) for any n > 1: {"v": "101"} into bit(3) fails with bit string length 1 does not match type bit(3). bit(1) works, and so does varbit. read_rows emits '101'. The value is bound with the wrong length (or as bit(1)).
  • Array columns, JSON array form. read_rows emits a JSON array, which works for int4[] but fails for timestamptz[], timestamp[], date[], uuid[], numeric[], int4range[] and inet[] with column "v" is of type X[] but expression is of type text[], and jsonb[] with Unsupported array element type. A PostgreSQL-literal string ({1.5}) is accepted only for inet[] and int4range[], not for numeric[], uuid[], date[] or the timestamp arrays.
  • Composite types: read_rows emits {"x": 1, "y": 2}; insert_records fails with Cannot bind a JSON object to a non-JSON column. A composite[] reads as an array of objects.
  • BC dates and large years: read_rows emits -0043-03-15 (and +5874897-12-31); insert_records fails with time zone displacement out of range for date, timestamp and timestamptz. 0044-03-15 BC is accepted, but it is not the form read_rows produces. (Also in Table copy: rc.15 read_rows repeats rows after a size-budget cut, returns null for NaN/infinity, and insert_records can't write back what read_rows emits #220 item 4.)

4. json/jsonb: the read and write sides do not form a round trip

For a jsonb holding the JSON string "{\"a\":1}", read_rows returns the bare string {"a":1}; for "hello" it returns hello. insert_records treats every string for a json column as JSON text, so a string that happens to be valid JSON comes back as a different type, and any other string fails:

Source jsonb read_rows After insert_records
"{\"a\":1}" (string) '{"a":1}' {"a": 1} (object)
"123" (string) '123' 123 (number)
"true" (string) 'true' true (boolean)
"hello" (string) 'hello' fails: invalid input syntax for type json
null (JSON null) null becomes SQL NULL

Sending JSON text for a json column is exact, so a caller can avoid this by selecting col::text (the host does). It is worth documenting that read_rows for json columns is not meant to be fed back, or having read_rows return text for json/jsonb columns.

5. Smaller

  • -- comment on the session path. select 1 -- c fails with DECLARE cursor failed: syntax error at end of input; the one-shot path accepts it. fix: close seven read_rows/insert_records copy gaps (#220) #221 says trailing comments are trimmed; only ;+whitespace is.
  • GUC leak. select set_config('probe.leak','yes',false) on the one-shot path leaves current_setting('probe.leak') at yes for the next call on the pooled connection.
  • limit: -1 is accepted and becomes page_size: 1000.
  • Identity columns. A GENERATED ALWAYS AS IDENTITY column reports is_auto_increment: true but is_generated is not set (is_generated is correct for GENERATED ALWAYS AS (...) STORED). A caller that skips only generated columns will try to write it.

Suggested fix

Most of section 1, 2 and 3 share one cause: array elements and a handful of scalar types go through the display extractors and the JSON-shape guessing binder rather than the per-type lossless path. Reading every non-simple cell as value::text (and binding by format_type/udt_name from the target column) would remove the need for a case per type.

What the host does meanwhile

SELECTs float, date, time, timestamp, timestamptz and json columns as ::text, and refuses before any write a text value shaped like a blob, JSON it cannot write exactly, and a page the plugin cut short on a pre-fix plugin. Array, range, composite, bit, interval, money, inet, xml and timetz columns are not in the host's supported type list, so a copy of such a column is blocked at plan time with unsupportedSourceType. That keeps the above from corrupting data, but it also means those columns cannot be copied at all until both sides support them. BC dates are the exception: the host reads date, timestamp and timestamptz as ::text, which prints 0044-03-15 BC, and insert_records accepts that form, so only the -0043-03-15 form that a direct read_rows emits (section 3) is affected.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions