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 1r9o7K-002Py8-CI for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Dec 2023 15:10:46 +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 1r9o7I-00Cjpd-Sg for pgsql-hackers@arkaria.postgresql.org; Sun, 03 Dec 2023 15:10:44 +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 1r9o7I-00CjpV-Ct for pgsql-hackers@lists.postgresql.org; Sun, 03 Dec 2023 15:10:44 +0000 Received: from mail-qt1-x830.google.com ([2607:f8b0:4864:20::830]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r9o7F-008bJQ-1g for pgsql-hackers@postgresql.org; Sun, 03 Dec 2023 15:10:43 +0000 Received: by mail-qt1-x830.google.com with SMTP id d75a77b69052e-423f2d0c8baso26471721cf.2 for ; Sun, 03 Dec 2023 07:10:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1701616239; x=1702221039; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:subject:from:references:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=Yoc1sdhVYxaFzRNisSEAZTM9iwjvBsORzjNhHDfHrpc=; b=bdQDY15TG/n9L3Z7a3X9jA38T1P3Ms/y4R0Jz2kvANBNUo9Wn5gOsCFHFfJtc+ZBiI bADmfS7QNSEl+0Pu1f+8P/Bpnn3sIubyR8EhJvn74hTH9TrtxBqJFqZY9fSegpfSfa6a FW3qrTqp+L7F7NtLvefgu85RiHKT8opiDj7m9Db1L0LAEPzkVsl8heqZqwZsflgR1XZm YzIZsWAXFbAZpUJs7oEPndfV/WUTJHxCoa9Pss3ZzyWnpIfpPaVydKUsmzXX14dx2Oq+ 6nXpAmPb9HWPH9M7Qz6Ny5yVwsWQ0knqgtFCjz7QGDiPoIfDbs/NdgzctysrHKov+GJM UeMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701616239; x=1702221039; h=content-transfer-encoding:in-reply-to:subject:from:references:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Yoc1sdhVYxaFzRNisSEAZTM9iwjvBsORzjNhHDfHrpc=; b=t/YSyLH9rs6SPPtfBut3dNDebn3Lrnh6OnbuRVcOdLNzBZrGdpNTss3/G0VlL+lhW0 +0YIMDz8D3oHQHwOh6bsI5V/qpYFTutS6twOto4aYWIkRQTXjK8o/vrRs/cFUwRit6t2 Rzx7pGlbGUpM/S9Oa9QRESnn3bM8mmElHfSdHsXtRx+iAr1Lm9bYLWomJAr2hzk4KiVL J5nV9C48aLv9PV98CBwenkzAOSxEx3rx/fihC4BaVcDYzg2Aj3DvQolpe3pfLfAAqaol sy6RnD3iYyL/WCBM7W9b/6J3EI/F3S3UYT83btBZDwm6U3lWloVUXQc8djapyhBoGtz+ 4CNg== X-Gm-Message-State: AOJu0YxtV/AQrfgkS2S7FTyG7kFQdej4kitMPGRdk7f2VSNuTw57/Bis GxP2CD/RFwZz05JjH6W0siLtwQ== X-Google-Smtp-Source: AGHT+IEB8XU7D0nnfvpWhhGPlxKabHohPG7NkmkQHiz6WBwmDfx+1Wq6Yq8tf9+SKmwW9AyLF8BhQQ== X-Received: by 2002:a05:622a:1047:b0:425:4043:29ec with SMTP id f7-20020a05622a104700b00425404329ecmr3603103qte.103.1701616239476; Sun, 03 Dec 2023 07:10:39 -0800 (PST) Received: from ?IPV6:2605:a601:91bc:f600::2bb? ([2605:a601:91bc:f600::2bb]) by smtp.googlemail.com with ESMTPSA id f15-20020a0ccc8f000000b0067a276fd8d5sm2088523qvl.54.2023.12.03.07.10.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 03 Dec 2023 07:10:39 -0800 (PST) Message-ID: Date: Sun, 3 Dec 2023 10:10:38 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Content-Language: en-US To: Joe Conway , 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: Andrew Dunstan Subject: Re: Emitting JSON to file using COPY TO In-Reply-To: <24e3ee88-ec1e-421b-89ae-8a47ee0d2df1@joeconway.com> 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 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. I think JSON array format is sufficient. 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. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com