Re: Emitting JSON to file using COPY TO - Mailing list pgsql-hackers

From jian he
Subject Re: Emitting JSON to file using COPY TO
Date
Msg-id CACJufxELGPA86veVNNBoqUDLwwQbQKdiYLKtwLb7DoSgV6f8sw@mail.gmail.com
Whole thread Raw
In response to Re: Emitting JSON to file using COPY TO  (Andrew Dunstan <andrew@dunslane.net>)
Responses Re: Emitting JSON to file using COPY TO
List pgsql-hackers
COPY (SELECT 1 UNION ALL SELECT 2) TO stdout WITH (format json);

still fails with v25-0002, json_tupledesc_ready is not helpful.
I think I figured it out. We need to use BlessTupleDesc in BeginCopyTo.
Then let slot->tts_tupleDescriptor point to cstate->queryDesc->tupDesc
in CopyToJsonOneRow

 * CSV, text and json formats share the same TextLike routines except for the
 * one-row callback.
This comment is not useful, I want to delete it.

CopyToTextLikeStart
+ /* JSON-specific initialization */
+ if (cstate->opts.format == COPY_FORMAT_JSON)
+ {
+ MemoryContext oldcxt;
+
+ /* Allocate reusable JSON output buffer in long-lived context */
+ oldcxt = MemoryContextSwitchTo(cstate->copycontext);
+ initStringInfo(&cstate->json_buf);
+ MemoryContextSwitchTo(oldcxt);
+ }
We ca just add

cstate->json_buf = makeStringInfo();

in BeginCopyTo.

v25-0004-COPY-TO-JSON-build-JSON-per-column-support-colum.patch
added several fields to the CopyToStateData.
Actually, there is a simpler way (construct a new Tupdesc and let
composite_to_json do the job), please see my v26-0004.



--
jian
https://www.enterprisedb.com/

Attachment

pgsql-hackers by date:

Previous
From: Amit Kapila
Date:
Subject: Re: Dead code in logical decoding of speculative insertions
Next
From: Antonin Houska
Date:
Subject: Re: Dead code in logical decoding of speculative insertions