Re: Make COPY format extendable: Extract COPY TO format implementations

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>, Sutou Kouhei <kou(at)clear-code(dot)com>, Tomas Vondra <tomas(at)vondra(dot)me>, Michael Paquier <michael(at)paquier(dot)xyz>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Make COPY format extendable: Extract COPY TO format implementations
Date: 2026-11-25 03:02:49
Message-ID: 179557576917.3709987.16350159766839841750@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Masahiko,

Short version: v4 is enough to write a format outside core. I ported
my ndjson module to it (master b4cc5e924b7) and everything I tried
works. The rough spots are in what copyapi.h says, not in the code.

What works: round trips through a file, with column lists, through
\copy, TO/FROM PROGRAM, file_fdw and with ENCODING 'SJIS' all give
back the same rows. ON_ERROR, REJECT_LIMIT, LOG_VERBOSITY, DEFAULT,
typmods and domains behave as the commit messages say, as with text.

Where I had to guess:

On Tue, Nov 24, 2026 at 12:10 PM Masahiko Sawada
<sawada(dot)mshk(at)gmail(dot)com> wrote:
> 0004 exports thin wrappers around them, CopyToSendData(),
> CopyToFlushData() and CopyFromGetData(), and copyapi.h says that they
> don't add any line termination.

1. When to flush. copyapi.h only asks a format to flush "by the end
of its CopyToEnd callback". A format that does just that keeps the
whole output in backend memory: for 1M rows of about 300 bytes, peak
RSS was 320 MB, against 33 MB when flushing after each row as text
does. Past 1 GB the COPY fails with "string buffer exceeds maximum
allowed length". I'd have copyapi.h say to flush after each row.

2. When to convert. copyapi.h says the format converts the data, but
not at which point. Parsing first breaks in SJIS: U+30BD is 0x83
0x5C, and 0x5C is a backslash, so a JSON parser sees an escape in the
middle of the character. Converting first doesn't work either, since
CopyFromConvertToServer() rejects a character cut in half. So a
format has to split records on the raw bytes and convert each record
afterwards. A sentence on which bytes are safe to split on would
help; the fields that tell (need_transcoding, encoding_embeds_ascii)
are in the internal part of copy_state.h.

> However, core still applies the typmod and the domain constraints of
> the columns to them, and ON_ERROR covers the errors there too.

3. It does: with ON_ERROR ignore, a row whose value fails a domain
CHECK is skipped, even when the format builds the Datum itself. But
the comment on the validate callback still says such a format "should
reject ON_ERROR here, since it would have no effect".

4. Smaller things. CopyFromSetErrorLine() drops the line whenever the
data is transcoded, so with ENCODING 'SJIS' the error context shows
only the line number, where text shows the converted line. And
copyapi.h doesn't mention opts->format_private_opts (I found it in
copy_fixed), or that the DEFAULT marker is opts->default_print.

Attached: the ported module and a script that reproduces each point.
Its scan_raw, typed and flush options exist only for these tests.

Regards,
Manu

Attachment Content-Type Size
nocfbot-test-copy-ndjson-v4.c.txt text/plain 10.2 KB
nocfbot-test-copy-ndjson-v4.sql.txt text/plain 6.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message John Naylor 2026-11-25 03:35:46 Re: Should we skip foreign key checks in binary upgrade mode?
Previous Message Zhijie Hou 2026-11-25 02:52:22 Re: Fix LogStandbySnapshot() race during logical decoding activation