-- ndjson on the v4 COPY format API: round trip, then one block per point -- raised in the mail. Needs a superuser (server-side files, PROGRAM, -- pg_read_file of /proc) and a UTF8 database. The last block writes -- over 1 GB through COPY TO and is slow. \set ON_ERROR_STOP 0 LOAD 'ndjson'; -- Round trip: file, column list, frontend, PROGRAM, file_fdw. CREATE TABLE ndj (id int, name text, amount numeric, ok bool, ts date); INSERT INTO ndj VALUES (1, 'hello', 1.25, true, '2026-01-31'), (2, 'with "quotes" and \ backslash', -0.5, false, '1999-12-31'), (3, NULL, NULL, NULL, NULL), (4, E'tab\there and newline\nthere', 1e6, true, '2000-02-29'); CREATE TABLE ndj_back (LIKE ndj); COPY ndj TO '/tmp/ndj.ndjson' WITH (FORMAT ndjson); COPY ndj_back FROM '/tmp/ndj.ndjson' WITH (FORMAT ndjson); SELECT * FROM ndj EXCEPT ALL SELECT * FROM ndj_back; -- 0 rows SELECT * FROM ndj_back EXCEPT ALL SELECT * FROM ndj; -- 0 rows TRUNCATE ndj_back; COPY ndj (id, name) TO '/tmp/ndj_part.ndjson' WITH (FORMAT ndjson); COPY ndj_back (id, name) FROM '/tmp/ndj_part.ndjson' WITH (FORMAT ndjson); SELECT id, name, amount IS NULL AS amount_null FROM ndj_back ORDER BY id; TRUNCATE ndj_back; \copy ndj TO '/tmp/ndj_fe.ndjson' WITH (FORMAT ndjson) \copy ndj_back FROM '/tmp/ndj_fe.ndjson' WITH (FORMAT ndjson) SELECT count(*) AS diff FROM (SELECT * FROM ndj EXCEPT ALL SELECT * FROM ndj_back) d; TRUNCATE ndj_back; COPY ndj TO PROGRAM 'cat > /tmp/ndj_prog.ndjson' WITH (FORMAT ndjson); COPY ndj_back FROM PROGRAM 'cat /tmp/ndj_prog.ndjson' WITH (FORMAT ndjson); SELECT count(*) AS diff FROM (SELECT * FROM ndj EXCEPT ALL SELECT * FROM ndj_back) d; CREATE EXTENSION IF NOT EXISTS file_fdw; CREATE SERVER ndj_fs FOREIGN DATA WRAPPER file_fdw; CREATE FOREIGN TABLE ndj_ft (id int, name text, amount numeric, ok bool, ts date) SERVER ndj_fs OPTIONS (filename '/tmp/ndj.ndjson', format 'ndjson'); SELECT count(*) AS diff FROM (SELECT * FROM ndj EXCEPT ALL SELECT * FROM ndj_ft) d; -- ON_ERROR with a bad integer on line 3 of 5, and the per-row NOTICE. CREATE TABLE onerr (id int, v int); COPY onerr FROM STDIN WITH (FORMAT ndjson, ON_ERROR ignore, LOG_VERBOSITY verbose); {"id":"1","v":"10"} {"id":"2","v":"20"} {"id":"3","v":"abc"} {"id":"4","v":"40"} {"id":"5","v":"50"} \. -- REJECT_LIMIT 1 with two bad rows: fails on the second, line 4. COPY onerr FROM STDIN WITH (FORMAT ndjson, ON_ERROR ignore, REJECT_LIMIT 1); {"id":"1","v":"10"} {"id":"2","v":"bad"} {"id":"3","v":"30"} {"id":"4","v":"bad"} \. -- DEFAULT: a JSON string equal to the DEFAULT option is the marker. CREATE TABLE def (id int, v int DEFAULT 42, w text DEFAULT 'dw'); COPY def FROM STDIN WITH (FORMAT ndjson, DEFAULT '\D'); {"id":"1","v":"\\D","w":"a"} {"id":"2","v":"5","w":"\\D"} \. SELECT * FROM def ORDER BY id; -- typmod, converted by core and built by the format (typed). CREATE TABLE tm (a varchar(3), n numeric(5,2)); COPY tm FROM STDIN WITH (FORMAT ndjson); {"a":"abcd","n":"1.005"} \. COPY tm FROM STDIN WITH (FORMAT ndjson, typed true); {"a":"abc","n":"1.005"} {"a":"abcd","n":"1"} \. SELECT * FROM tm; -- SJIS: U+30BD and U+8868 are 0x83 0x5C and 0x95 0x5C, a backslash as -- the trailing byte. Splitting on \n and converting each record before -- parsing it works; parsing before converting (scan_raw) does not. CREATE TABLE sj (id int, s text); INSERT INTO sj VALUES (1, U&'\30BD'), (2, U&'\8868'), (3, U&'\30BD"\8868' || '\x'); CREATE TABLE sj_back (LIKE sj); COPY sj TO '/tmp/sj.ndjson' WITH (FORMAT ndjson, ENCODING 'SJIS'); COPY sj_back FROM '/tmp/sj.ndjson' WITH (FORMAT ndjson, ENCODING 'SJIS'); SELECT count(*) AS diff FROM (SELECT * FROM sj EXCEPT ALL SELECT * FROM sj_back) d; TRUNCATE sj_back; COPY sj_back FROM '/tmp/sj.ndjson' WITH (FORMAT ndjson, ENCODING 'SJIS', scan_raw true); -- CopyFromConvertToServer() rejects a partial character: copy_fixed -- (0009) slices 'abc' + U+30BD + 'xy' at byte 4, inside the character. LOAD 'copy_fixed'; COPY (SELECT 'abc' || U&'\30BD' || 'xy') TO '/tmp/split.dat' WITH (FORMAT text, ENCODING 'SJIS'); CREATE TABLE split (a text, b text); COPY split FROM '/tmp/split.dat' WITH (FORMAT fixed, widths '4,3', ENCODING 'SJIS'); -- Error context: a malformed record is quoted in UTF8, not in SJIS. COPY (SELECT '{"id":"1","s":"' || U&'\30BD' || '"') TO '/tmp/bad_sjis.ndjson' WITH (FORMAT text, ENCODING 'SJIS'); COPY (SELECT '{"id":"1","s":"' || U&'\30BD' || '"') TO '/tmp/bad_utf8.ndjson' WITH (FORMAT text); COPY sj_back FROM '/tmp/bad_sjis.ndjson' WITH (FORMAT ndjson, ENCODING 'SJIS'); COPY sj_back FROM '/tmp/bad_utf8.ndjson' WITH (FORMAT ndjson); -- text quotes the converted line for a record-level error COPY (SELECT 1, U&'\30BD', 'extra') TO '/tmp/bad_sjis.txt' WITH (FORMAT text, ENCODING 'SJIS'); COPY sj_back FROM '/tmp/bad_sjis.txt' WITH (FORMAT text, ENCODING 'SJIS'); -- typed: the format builds the Datums itself. Core applies the domain, -- and ON_ERROR covers it, although copyapi.h says it would have no effect. CREATE DOMAIN dpos AS int CHECK (VALUE > 0); CREATE TABLE dom (id int, x dpos); COPY dom FROM STDIN WITH (FORMAT ndjson, typed true, ON_ERROR ignore); {"id":"1","x":"5"} {"id":"2","x":"-1"} {"id":"3","x":"7"} \. SELECT * FROM dom ORDER BY id; -- Flushing only in CopyToEnd: the whole output stays in the backend. -- Peak RSS of a fresh backend after ~300 MB of output, per row vs at end. CREATE UNLOGGED TABLE big AS SELECT g AS id, repeat('z', 280) AS s FROM generate_series(1, 1000000) g; \c LOAD 'ndjson'; COPY big TO '/dev/null' WITH (FORMAT ndjson, flush 'row'); SELECT 'row' AS flush, (regexp_match(pg_read_file('/proc/' || pg_backend_pid() || '/status'), 'VmHWM:\s+(\d+) kB'))[1]::int / 1024 AS peak_rss_mb; \c LOAD 'ndjson'; COPY big TO '/dev/null' WITH (FORMAT ndjson, flush 'end'); SELECT 'end' AS flush, (regexp_match(pg_read_file('/proc/' || pg_backend_pid() || '/status'), 'VmHWM:\s+(\d+) kB'))[1]::int / 1024 AS peak_rss_mb; -- Over 1 GB of output, flushing only at the end fails. INSERT INTO big SELECT g, repeat('z', 280) FROM generate_series(1000001, 3600000) g; COPY big TO '/dev/null' WITH (FORMAT ndjson, flush 'end');