pgsql: json format for COPY TO

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: json format for COPY TO
Date: 2026-03-20 12:41:00
Message-ID: E1w3Z9v-000dj4-1t@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

json format for COPY TO

This introduces the JSON format option for the COPY TO command, allowing
users to export query results or table data directly as a stream of JSON
objects (one per line, NDJSON style).

The JSON format is currently supported only for COPY TO operations; it
is not available for COPY FROM.

JSON format is incompatible with some standard text/CSV formatting
options, including HEADER, DEFAULT, NULL, DELIMITER, FORCE QUOTE,
FORCE NOT NULL, and FORCE NULL.

Column list support is included: when a column list is specified, only
the named columns are emitted in each JSON object.

Regression tests covering valid JSON exports and error handling for
incompatible options have been added to src/test/regress/sql/copy.sql.

Author: Joe Conway <mail(at)joeconway(dot)com>
Author: jian he <jian(dot)universality(at)gmail(dot)com>
Co-Authored-By: Andrew Dunstan <andrew(at)dunslane(dot)net>
Reviewed-by: Andrey M. Borodin <x4mmm(at)yandex-team(dot)ru>
Reviewed-by: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Reviewed-by: Daniel Verite <daniel(at)manitou-mail(dot)org>
Reviewed-by: Davin Shearer <davin(at)apache(dot)org>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Reviewed-by: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Reviewed-by: Junwang Zhao <zhjwpku(at)gmail(dot)com>
Discussion: https://postgr.es/m/CALvfUkBxTYy5uWPFVwpk_7ii2zgT07t3d-yR_cy4sfrrLU%3Dkcg%40mail.gmail.com
Discussion: https://postgr.es/m/6a04628d-0d53-41d9-9e35-5a8dc302c34c@joeconway.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7dadd38cda95bf5bc0c4715d9ab71766d1693379

Modified Files
--------------
doc/src/sgml/ref/copy.sgml | 29 ++++++-
src/backend/commands/copy.c | 53 ++++++++----
src/backend/commands/copyto.c | 162 ++++++++++++++++++++++++++++++++++---
src/backend/parser/gram.y | 8 ++
src/backend/utils/adt/json.c | 5 +-
src/bin/psql/tab-complete.in.c | 2 +-
src/include/commands/copy.h | 1 +
src/include/utils/json.h | 2 +
src/test/regress/expected/copy.out | 146 +++++++++++++++++++++++++++++++++
src/test/regress/sql/copy.sql | 88 ++++++++++++++++++++
10 files changed, 463 insertions(+), 33 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Robert Haas 2026-03-20 12:53:59 pgsql: test_plan_advice: Set TAP test priority 50 in meson.build.
Previous Message Peter Eisentraut 2026-03-20 10:08:46 pgsql: Cleanup users and roles in graph_table_rls test