Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r9owH-002Urv-93 for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Dec 2023 16:03:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r9owF-00CtPD-JF for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Dec 2023 16:03:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r9owE-00CtP5-Rw for pgsql-hackers@lists.postgresql.org; Sun, 03 Dec 2023 16:03:23 +0000 Received: from mail-yb1-xb2f.google.com ([2607:f8b0:4864:20::b2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r9ow9-008blY-87 for pgsql-hackers@postgresql.org; Sun, 03 Dec 2023 16:03:21 +0000 Received: by mail-yb1-xb2f.google.com with SMTP id 3f1490d57ef6-db3fa47c2f7so2008013276.0 for ; Sun, 03 Dec 2023 08:03:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; t=1701619396; x=1702224196; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=jTmYOYNXCjjv2pNtxpFmHeeL7Te8NYMgaeaTD4/Rx0s=; b=VIH6ff7pnAJH2Sf0wJQ8Odn8uLyNBkJYUT5/IX06WyymMsd1ocJZMu75+yr/m17KA+ LLtlDXlRbTzi0sEGwEwo9Y5B/t1J7sqEcfjnZRarrDGAM8EZ4dx+O1HzLHosMHxo/3w9 X8oj9Z8JxE4auGBhkzdr7qvOwh4wWpZQPU8rK54kgrNE+CRjuy3+F+7EyjdNpNuFcaJD jDNHMUpvmJYIH/fmdtl1ytoCDGiVMoQgFWgaILcwhwWrtTgQXosiZIXR+ZMgxQywQd+M kzECGx57znsx88TLVy5S8iF3nh4U59WBcPqFr2zHz/MoY1eQOoe9jPuOYmP1Viwyk2ss 1rYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701619396; x=1702224196; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jTmYOYNXCjjv2pNtxpFmHeeL7Te8NYMgaeaTD4/Rx0s=; b=w5cHzj96ElXxPm0aWFlvw8iCJS2QR8YYshPTNybBWHF6ZNBBsCJQQlACCsaUgW19jQ WvFextgcV6lJszZtxUEREDH0QkINq4RCMTMHVXmC0BTiK7pRScDVvPDsEsIYd9W4xdy2 AMaNfqljT9ZdrOOieWeG1ZiYafrnWqR0OKv/Bj8bwnALcMEtfqGeEV8RxthjX0LC4vdR 6vck18AkxiuhxATy94tuRPMhKHsPPLhUd2AlWPR6ol/830ch9fqqrFnzjqMRUBX5dTKx 9d1FkujohiWLY2f/7A3XP7gMACXuTwtPeXao4116kKpg8LwR/45JNFhYc4n8pJmgV1tE E2/A== X-Gm-Message-State: AOJu0YxBX1FaOtM/sAS7g50RQw1m7jQSQpcaZmJqr5duRGQfuFn9SP8A jnZRfvfwi/Q45r/Fe0Jyw4FXEw== X-Google-Smtp-Source: AGHT+IHot4Be70dBaqKMaEqmpASHc2OPwpZ70SAjcipfOrPqk3EvlDBgI2apMYq2/9lONf7SQwDUkg== X-Received: by 2002:a05:690c:dd0:b0:5d7:1941:2c1b with SMTP id db16-20020a05690c0dd000b005d719412c1bmr2164987ywb.72.1701619395817; Sun, 03 Dec 2023 08:03:15 -0800 (PST) Received: from [192.168.4.41] (162-239-31-113.lightspeed.dybhfl.sbcglobal.net. [162.239.31.113]) by smtp.gmail.com with ESMTPSA id v11-20020a81a54b000000b005d781a2e123sm957276ywg.109.2023.12.03.08.03.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 03 Dec 2023 08:03:15 -0800 (PST) Message-ID: <8c88da85-c197-4765-96f8-a9a1c78305a6@joeconway.com> Date: Sun, 3 Dec 2023 11:03:14 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Emitting JSON to file using COPY TO Content-Language: en-US To: Andrew Dunstan , Davin Shearer , PostgreSQL-development References: <3853387.1701096982@sss.pgh.pa.us> <3a98decf-3fe3-4b49-9b68-fda01338872c@sedlakovi.org> <24e3ee88-ec1e-421b-89ae-8a47ee0d2df1@joeconway.com> From: Joe Conway Autocrypt: addr=mail@joeconway.com; keydata= xsFNBEpXMCsBEADDnXUQzjlyi/cX02Gtdy2CLcroE5CsC7DJKdOBDbfgn0kfiIYoV5JniG4l VyzZUodY8yUAagqLYolh0UkBzs9N+qkm7erde4ypw3jzVQ37BuzIvk3nMUbuDZDgxWqX+nVS sKc+BQ5BpzgCHg48leoRO2ohjvYnUhgH3j2rFZCzaj6qQ7mv+XoxOJmUlVQtG06Jwkk7Vu14 7U9nMMM6hyUKzVnmCphnlcMNo26UyVU70MwFfFJgcI0c5fpp8byN56eD6VJVnufO5WAuEhzE qcrSJR2FAlmM90GBY+6vP29twLDCHuSFvrnujNCx/BvCC/a3/gPvyAFp4JtMm9eXAmq3m/Kw 94nTJXVdcbQeQQDp3KIG7MmWS4lnGvPn8v0CjgNaLvZXFLo1FgmUVsyEq1Lww4iRLa6sbpXJ ESx15UEue1k1YZM9C+4F/o3aeKNsAienjw2EXFzcaxIg/C4P493VMi3Qa8ycVxR5iYhUbYdo DFIUQhbFNsYfrtW/qZAELT3FCYFpZYG01e9Hj+cBrXXgyDDkQ5Lq4mlvmkRvuxn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ljpq6ZL4FQUsM0RSRjs 5egE3szPcIf5SyPK6WDOApoAq6M7BBFMGDZwEylYMtr0YekA1u86UA9D2xwLHEbBBp/uiby1 c9JbPJ1Pn8zJP8WZNeRw4Q9TtqVK09+oLirMUSpIDd6KdZ1VgRxOK2re7tjDvkVuYsSrsiJ+ 1iJNEnp9iK0ok0DlJpSCe6KhkxpaTdeoWMXdKuJWec0NIqoAd54ZgBPnr+UPxTixgPq/p6Q= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/3/23 10:10, Andrew Dunstan wrote: > > On 2023-12-01 Fr 14:28, Joe Conway wrote: >> On 11/29/23 10:32, Davin Shearer wrote: >>> Thanks for the responses everyone. >>> >>> I worked around the issue using the `psql -tc` method as Filip >>> described. >>> >>> I think it would be great to support writing JSON using COPY TO at >>> some point so I can emit JSON to files using a PostgreSQL function >>> directly. >>> >>> -Davin >>> >>> On Tue, Nov 28, 2023 at 2:36 AM Filip Sedlák >> > wrote: >>> >>>     This would be a very special case for COPY. It applies only to a >>> single >>>     column of JSON values. The original problem can be solved with psql >>>     --tuples-only as David wrote earlier. >>> >>> >>>     $ psql -tc 'select json_agg(row_to_json(t)) >>>                    from (select * from public.tbl_json_test) t;' >>> >>>        [{"id":1,"t_test":"here's a \"string\""}] >>> >>> >>>     Special-casing any encoding/escaping scheme leads to bugs and harder >>>     parsing. >> >> (moved to hackers) >> >> I did a quick PoC patch (attached) -- if there interest and no hard >> objections I would like to get it up to speed for the January commitfest. >> >> Currently the patch lacks documentation and regression test support. >> >> Questions: >> ---------- >> 1. Is supporting JSON array format sufficient, or does it need to >> support some other options? How flexible does the support scheme need >> to be? >> >> 2. This only supports COPY TO and we would undoubtedly want to support >> COPY FROM for JSON as well, but is that required from the start? >> >> Thanks for any feedback. > > I  realize this is just a POC, but I'd prefer to see composite_to_json() > not exposed. You could use the already public datum_to_json() instead, > passing JSONTYPE_COMPOSITE and F_RECORD_OUT as the second and third > arguments. Ok, thanks, will do > I think JSON array format is sufficient. The other formats make sense from a completeness standpoint (versus other databases) and the latest patch already includes them, so I still lean toward supporting all three formats. > I can see both sides of the COPY FROM argument, but I think insisting on > that makes this less doable for release 17. On balance I would stick to > COPY TO for now. WFM. From your earlier post, regarding constructing the aggregate -- not extensive testing but one data point: 8<-------------------------- test=# copy foo to '/tmp/buf' (format json, force_array); COPY 10000000 Time: 36353.153 ms (00:36.353) test=# copy (select json_agg(foo) from foo) to '/tmp/buf'; COPY 1 Time: 46835.238 ms (00:46.835) 8<-------------------------- -- Joe Conway PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com