Skip to content

fix(pglite-sync): serialize COPY initial sync with Postgres TEXT format#1035

Draft
AntonOfTheWoods wants to merge 1 commit into
electric-sql:mainfrom
AntonOfTheWoods:copybis
Draft

fix(pglite-sync): serialize COPY initial sync with Postgres TEXT format#1035
AntonOfTheWoods wants to merge 1 commit into
electric-sql:mainfrom
AntonOfTheWoods:copybis

Conversation

@AntonOfTheWoods

Copy link
Copy Markdown
Contributor

The COPY-based initial insert path (initialInsertMethod: 'csv' / the deprecated useCopy) hand-rolled a CSV encoder that broke on many column types: arrays were emitted via Array.toString as 1,2,3 instead of the {1,2,3} literal, json/jsonb objects became [object Object], and embedded delimiters, newlines and backslashes were mishandled.

Replace it with a faithful port of PostgreSQL's own COPY TEXT serialization — CopyAttributeOutText (copyto.c) for field escaping and array_out (arrayfuncs.c) for array literals — driven by the JS types the Electric client produces. Column types are looked up from information_schema to disambiguate json/jsonb (whose parsed values are indistinguishable from SQL arrays by runtime type alone).

Adds unit tests plus round-trip tests that push the generated stream through a real PGlite COPY ... FROM for scalars, strings with special characters, arrays (incl. multi-dimensional), json/jsonb (incl. jsonb[]), bytea and timestamps.

Claude-Session: https://claude.ai/code/session_01VkNJeV2Ju2TnjxSxSzgL9i

@AntonOfTheWoods AntonOfTheWoods marked this pull request as draft June 23, 2026 00:38
The COPY-based initial insert path (`initialInsertMethod: 'csv'` / the
deprecated `useCopy`) hand-rolled a CSV encoder that broke on many column
types: arrays were emitted via `Array.toString` as `1,2,3` instead of the
`{1,2,3}` literal, `json`/`jsonb` objects became `[object Object]`, and
embedded delimiters, newlines and backslashes were mishandled.

Replace it with a faithful port of PostgreSQL's own COPY TEXT
serialization — `CopyAttributeOutText` (copyto.c) for field escaping and
`array_out` (arrayfuncs.c) for array literals — driven by the JS types the
Electric client produces. Column types are looked up from
`information_schema` to disambiguate `json`/`jsonb` (whose parsed values are
indistinguishable from SQL arrays by runtime type alone).

Adds unit tests plus round-trip tests that push the generated stream through
a real PGlite `COPY ... FROM` for scalars, strings with special characters,
arrays (incl. multi-dimensional), json/jsonb (incl. `jsonb[]`), bytea and
timestamps.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VkNJeV2Ju2TnjxSxSzgL9i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants