| From: | Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> |
|---|---|
| To: | Joao Rito <joaovictor(dot)rito(at)gmail(dot)com> |
| Cc: | Manu <manuelreyesbravo(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-24 20:10:12 |
| Message-ID: | CAD21AoCTigSo=Ux0k-EvbJJbm3-kL2PSyEMekBoJTnRQFDCV=A@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Tue, Nov 10, 2026 at 4:11 AM Joao Rito <joaovictor(dot)rito(at)gmail(dot)com> wrote:
>
> Hi,
>
> Thank you all for keeping this going. I looked at this patch set from the
> user side: people often ask for COPY to and from Parquet, and as I
> understand the thread, this API is how such a format would live in an
> extension instead of in core.
>
> I reviewed v3 (Sawada-san, 2026-06-24), together with Manu's export diff and
> ndjson module (2026-09-22).
>
> Tested against master c074737f687f604b64f6ee830fccbbe854719823 (2026-11-09),
> Debian bookworm container, ./configure --enable-cassert --enable-debug
> --enable-tap-tests.
>
> Applying and building
> - v3 no longer applies with git apply on current master. git am -3 works for
> 0001 to 0003 (copyto.c, copyfrom.c, copyfromparse.c and file_fdw.c merge
> automatically, no conflicts). In 0004 only the typedefs.list hunk fails;
> the rest applies.
> - Build is clean, with no compiler warnings.
>
> Tests
> - make check: 243 tests, PASS. contrib/file_fdw check: PASS.
> - test_copy_custom_format fails on current master, as Manu reported. I think
> the cause is in psql, not in the patch. Commit 3045a25ba8 (2026-08-10,
> "Teach psql to skip in-line COPY ... FROM STDIN data after a failure")
> makes psql skip input up to "\." after a COPY FROM STDIN that fails. The
> test has a COPY FROM STDIN that is expected to fail (the disallow_freeze
> case) with no "\." after it, so psql reads the next COPY command as data,
> and its output is missing. The server raises the error in
> ProcessCopyOptions(), before CopyInResponse, so the backend side is fine.
> The same happens with a built-in format, for example COPY t FROM stdin
> WITH (format 'text', bogus 1). Adding one line "\." after the failing
> command makes the test pass, with the expected file unchanged.
> - Manu's ndjson module: without the export diff it compiles with implicit
> declaration warnings, and LOAD fails with "undefined symbol:
> CopySendData". With the export diff, his round-trip script passes (file,
> column lists), and so do \copy through the frontend protocol and COPY TO
> PROGRAM.
Thank you for testing the patches.
I've attached a new series of patches. I'll reply to these points
followed by summarizing what changed and the overall design.
> Comments
>
> 1. Exporting CopySendData(), CopySendEndOfRow() and CopyGetData(): +1.
> Without them a format can be registered but not implemented, so I think they
> belong in 0002. As Manu said, the comment should mention that
> CopySendEndOfRow() does not add a newline.
0004 exports thin wrappers around them, CopyToSendData(),
CopyToFlushData() and CopyFromGetData(), and copyapi.h says that they
don't add any line termination.
> 2. Core options that a custom format does not use are accepted silently.
> With FORMAT ndjson, all of these run without an error and do not change the
> output: DELIMITER '|', HEADER true, NULL 'x', FORCE_QUOTE *. Since the
> text/CSV checks are now skipped for custom formats (CopyFormatIsBuiltins()),
> each format has to reject these in its validate callback, and if it does
> not, the user's option is ignored without notice. Maybe core could reject
> the text/CSV-only options for a custom format unless the format says it
> accepts them. This may be related to the open question about an
> InitializeCopyOptions callback.
I think the position of a custom format is that it can define options
of its own in addition to the core options, as EXPLAIN options of
extensions do. Some core options, such as ON_ERROR and FREEZE, are
implemented by core, so they work the same way for any format, and
core validates them. On the other hand, options like DELIMITER and
QUOTE mean something only if the format implements them, and a
combination that is invalid for a built-in format might be valid for
another format. So I think it should be up to the format to accept or
reject them in its validation callback, and copyapi.h now says so.
Core still rejects things not depending on the format such as
malformed option values, combinations that can't work in a direction
like FORCE_NOT_NULL with COPY TO.
I'm not sure that letting a format declare the core options it accepts
is worth the complexity. Whether a format accepts an option can depend
on its own options or on the direction, so it would need the
validation callback anyway.
>
> 3. ON_ERROR and line numbers. COPY ... FROM ... WITH (FORMAT ndjson,
> ON_ERROR ignore) is accepted, but a bad row still fails with "invalid input
> syntax for type integer", and the context line says "COPY ndj2, line 0".
> This is up to the module (it would need InputFunctionCallSafe() and to
> update cur_lineno), but it shows that the contract around on_error,
> reject_limit, log_verbosity and cur_lineno is not written down. A short
> paragraph in copyapi.h, or a way for a format to say it does not support
> ON_ERROR so that core rejects it, would help extension authors.
I believe it's fixed in the latest patch, see below.
>
> 4. The library has to be loaded first. In a new session without LOAD, FORMAT
> ndjson fails with 'COPY format "ndjson" not recognized'. This is expected
> with registration in _PG_init(), so users will need shared_preload_libraries
> or session_preload_libraries. An errhint pointing to that could save some
> confusion. Small point.
Hmm, but we don't know which library listed in
shared_preload_libraries provides the custom format, no?
>
> 5. Parquet as a use case. I tried to picture a Parquet format on top of this
> API:
> - COPY TO seems to fit: buffer rows in CopyToOneRow, write the row groups
> and the footer in CopyToEnd through CopySendData(), and keep the state
> in format_private.
> - COPY FROM is harder, because the Parquet footer is at the end of the
> file, so the reader needs random access. For a file source the
> extension could use cstate->copy_file, which copy_state.h now exposes.
> For STDIN or PROGRAM it would have to read everything through
> CopyGetData() first. That is fine, but it would help to document
> whether a format may use copy_file directly or should only use
> CopyGetData().
In v4, copy_state.h has a section of the fields that extensions may
use, and copy_file is not in it, so a format should use
CopyFromGetData(). I think it's reasonable to target formats that can
be read as a stream in the first version, and to leave random access
out of scope.
> - A target like 's3://bucket/x.parquet' is rejected by core ("relative
> path not allowed for COPY to file") before the format sees it. So an
> extension that writes to object storage would still need its own hook
> for that part. I think that is fine and out of scope here; the API
> covers the format, not the destination.
Agreed.
>
> 6. file_fdw works with a custom format with no extra code: a foreign table
> with format 'ndjson' reads the file correctly. A nice side effect. If it is
> intended, a test or a line in the docs would make it explicit.
Yes, it's intended. Will add a test.
>
> 7. Small things:
> - Typo "Sturct" in copyapi.h, and "Add an hook" in the 0003 subject ("a
> hook").
> - The commit messages of 0002 and 0003 have empty Author, Reviewed-by and
> Discussion lines.
> - There are no SGML docs yet; copy.sgml still lists only the built-in
> formats.
> - I only built with autoconf, not meson.
Fixed the typos. I'll fill in the trailers when committing. I haven't
prepared the doc changes yet, as the API is still under discussion.
While working on the patches, I found that we would need more
refactoring before adding the custom copy format, since some COPY FROM
features work only with the built-in formats: ON_ERROR, the line
number shown in the error context, and the DEFAULT option. In addition
to that, another problem was that a format that builds Datums by
itself had to apply the typmod and the domain constraints of the
columns on its own. For the built-in formats the input functions do
that, for example varcharin() checks the length and domain_in() checks
the domain constraints. I think this should be core's job rather than
each format's.
So I've refactored the COPY FROM code and introduced some helper
functions for extensions, so that they can convert one or more
attributes without taking care of ON_ERROR, REJECT_LIMIT and
LOG_VERBOSITY, or the DEFAULT marker. For instance, a format extension
can call the CopyFromConvertFields() helper function, which calls the
input function of each column, applies the ON_ERROR behavior (stop,
ignore, or set_null), handles the DEFAULT marker, and maintains the
error context. For the line number, core counts the records, and the
format can set the current record to be shown in the error context
with CopyFromSetErrorLine(). Extensions that convert the input data to
PostgreSQL's data types by themselves don't need to call these helper
functions; they can convert the input data and simply pass the Datums.
However, core still applies the typmod and the domain constraints of
the columns to them, and ON_ERROR covers the errors there too.
Also, when refactoring the COPY FROM code, I found that we can
optimize the input function calls by setting up the function call
information for each column once. It saves the cost of setting up the
FunctionCallInfo for every field. The attached 0001 patch does it. I
think it can be pushed independently. I'll share the performance
results separately.
The attached 0009 patch implements a module called copy_fixed that
supports a fixed-width format. Note that it's fully AI-generated code,
so I'm not planning to push it at least at this stage. It's just a PoC
to verify the newly introduced COPY APIs.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
| Attachment | Content-Type | Size |
|---|---|---|
| v4-0005-Allow-extensions-to-register-custom-COPY-formats.patch | application/x-patch | 23.1 KB |
| v4-0008-Let-custom-COPY-formats-process-their-own-options.patch | application/x-patch | 27.5 KB |
| v4-0007-Apply-typmod-and-domain-constraints-to-Datums-bui.patch | application/x-patch | 26.6 KB |
| v4-0006-Support-COPY-FROM-in-custom-COPY-formats.patch | application/x-patch | 46.5 KB |
| v4-0009-Add-contrib-copy_fixed-a-fixed-width-COPY-format.patch | application/x-patch | 83.9 KB |
| v4-0003-Factor-out-per-attribute-input-conversion-for-COP.patch | application/x-patch | 13.2 KB |
| v4-0004-Expose-the-COPY-data-I-O-functions-and-state-fiel.patch | application/x-patch | 9.3 KB |
| v4-0002-Move-Copy-From-To-StateData-to-copy_state.h.patch | application/x-patch | 26.9 KB |
| v4-0001-Set-up-the-input-function-call-information-once-i.patch | application/x-patch | 5.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tomas Vondra | 2026-11-24 20:11:07 | Re: Parallel INSERT SELECT take 2 |
| Previous Message | surya poondla | 2026-11-24 20:02:11 | Re: Long reloption values are truncated by pg_dump and pg_get_indexdef() |