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 1rAzS9-0090bU-C9 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2023 21:29:09 +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 1rAzS7-00Cdaw-7g for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2023 21:29:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAzS6-00CdaC-C9 for pgsql-hackers@lists.postgresql.org; Wed, 06 Dec 2023 21:29:06 +0000 Received: from mail-yw1-x112b.google.com ([2607:f8b0:4864:20::112b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rAzS1-00AS4c-QK for pgsql-hackers@postgresql.org; Wed, 06 Dec 2023 21:29:05 +0000 Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-5b383b4184fso2240977b3.1 for ; Wed, 06 Dec 2023 13:29:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; t=1701898140; x=1702502940; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=tCpy5Zz+6LLARYFN7sOXL3IbhLOd+AqfMHj6xWGomyA=; b=jz57bv05xqOkPZ/e7/OJjqEF8PYG6CUOnxVRqudZXWGPik7rPkoiDCsB/qlGp/MNQS elOBcX0ZvQaut/oAUgpXQK3BH+EcrUjbf3bOTaP3IhSzc16OGzkRgSqt2qSdU1ogXfdr 8L4fqIvj/1wS4p0/vVPLS7EdzWFS6Ov2G2aiIb4O8LdqzlfePb+IO4sS7h2JYrevtZHo GdQwSz3IgRRrPtnlavDY4LHVSU7cHAn8OpQXa8rsPGnqPV/xQG76dMwBxRXTuvgQA1MN eo79CBp0bDFYcOby9Ik//SGWkeZ+NLpjEVMjgKQ27N/xDzwyu9O+hh+DGsjkrKVUyWjd hj7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701898140; x=1702502940; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :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=tCpy5Zz+6LLARYFN7sOXL3IbhLOd+AqfMHj6xWGomyA=; b=mAtqmJOxInGxno/XDmSpK5A/yzBUdTBs7AXf9fHi0/oHburvh6pDhxzHQzfEmOgt9l Hn0q9FhQmL734xJahm1+tVoM5VW1zlcE06FKBBRMlftyyYafcZxTp39w1q/acwWdCVFf 1RXl9JdvYB3Zr0Ksq9iVhu5sv8tN2jYKInUiW9NGV2lzk46NKM3JDvRCFpI5yDlhNWcA anshwhBEIpgcthlJb9Ls5nFCNW7dOELz0NC/zqhvwo2MWeTvLH/DFqgTbG1jjvGeV39a Z/D0HhHoWoXfdZ97Vs71L8xBTztV8EM+m3NY8w4jj83UXaVTTTEXYk3XJVRQn2wcGihl RMXA== X-Gm-Message-State: AOJu0YyBYYQWg4/FJ82wnXzaUSOF0fVClpZDXIUKWElMm7G18D4C094n du37Vgw8og3PAUrMSZ5I3GNDwA== X-Google-Smtp-Source: AGHT+IENn6Vnt/uyWHO6hwWSJ+MGKnhdbyjKcBywEY1WCVAQU2Ax/1Z+4Urn8rjabg1WfgiO4Rnmeg== X-Received: by 2002:a81:4846:0:b0:5d6:c5e6:fa4d with SMTP id v67-20020a814846000000b005d6c5e6fa4dmr1516018ywa.31.1701898140060; Wed, 06 Dec 2023 13:29:00 -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 x66-20020a817c45000000b005add997ae53sm247617ywc.81.2023.12.06.13.28.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Dec 2023 13:28:59 -0800 (PST) Message-ID: <4d5688f4-9582-4093-8448-e1867bc9e2bc@joeconway.com> Date: Wed, 6 Dec 2023 16:28:59 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Emitting JSON to file using COPY TO Content-Language: en-US To: Sehrope Sarkuni , Andrew Dunstan Cc: Tom Lane , Davin Shearer , PostgreSQL-development References: <5c84b70b-ba18-c45d-dbbe-612fa229b2ce@dunslane.net> <398c22f6-4299-4b17-80bf-2f14f4afd592@joeconway.com> <46cc4507-a0d9-4044-b2ce-5a8bca8015c0@joeconway.com> <2554e520-e103-8978-dcb5-807dfeb77402@dunslane.net> <926ff917-8371-40ec-b5e6-ab7b0e09bdc5@joeconway.com> <315b81d4-4b67-7828-0355-3808cd14acd1@dunslane.net> <7a60faf6-e7f1-419d-aee6-10a78ea2fe81@joeconway.com> <2e7ff718-895d-83fc-46f7-be25e23b23b4@dunslane.net> <1104915.1701877459@sss.pgh.pa.us> <19a5f9d8-bd1f-9e51-0f5b-510c1189a8a7@dunslane.net> From: Joe Conway Autocrypt: addr=mail@joeconway.com; keydata= xsFNBEpXMCsBEADDnXUQzjlyi/cX02Gtdy2CLcroE5CsC7DJKdOBDbfgn0kfiIYoV5JniG4l VyzZUodY8yUAagqLYolh0UkBzs9N+qkm7erde4ypw3jzVQ37BuzIvk3nMUbuDZDgxWqX+nVS sKc+BQ5BpzgCHg48leoRO2ohjvYnUhgH3j2rFZCzaj6qQ7mv+XoxOJmUlVQtG06Jwkk7Vu14 7U9nMMM6hyUKzVnmCphnlcMNo26UyVU70MwFfFJgcI0c5fpp8byN56eD6VJVnufO5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DWFav9+xMz9rKP4N+HffoBIZ E0C1xIE43nD4eLsbycte9sVIrmlNuUti3qUxJAQw8HwfJ6ZbBInHxquApR16uD1u99o6Xlnd FrDlY22tRmHCM0bR81GfGNdcU3Uo+rG/R/k4qa7s9/dgKvMbyH3fHhp/ceKag80Xo8IFurRl 0ZJP3sHJ2QDHCVLat7jRZ+43hi1WlIhFbrgn6IyI0i7XR/W8JjrC5MsKq4TUwGH077sU/kcH YebVJZRbUUst2hAGHDFVBcG12qoKf+ltL9qXJc1y7BGeCoUW6QjOpljpq6ZL4FQUsM0RSRjs 5egE3szPcIf5SyPK6WDOApoAq6M7BBFMGDZwEylYMtr0YekA1u86UA9D2xwLHEbBBp/uiby1 c9JbPJ1Pn8zJP8WZNeRw4Q9TtqVK09+oLirMUSpIDd6KdZ1VgRxOK2re7tjDvkVuYsSrsiJ+ 1iJNEnp9iK0ok0DlJpSCe6KhkxpaTdeoWMXdKuJWec0NIqoAd54ZgBPnr+UPxTixgPq/p6Q= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/6/23 11:28, Sehrope Sarkuni wrote: > Big +1 to this overall feature. cool! > Regarding the defaults for the output, I think JSON lines (rather than a > JSON array of objects) would be preferred. It's more natural to combine > them and generate that type of data on the fly rather than forcing > aggregation into a single object. So that is +2 (Sehrope and me) for the status quo (JSON lines), and +2 (Andrew and Davin) for defaulting to json arrays. Anyone else want to weigh in on that issue? > Couple more features / use cases come to mind as well. Even if they're > not part of a first round of this feature I think it'd be helpful to > document them now as it might give some ideas for what does make that > first cut: > > 1. Outputting a top level JSON object without the additional column > keys. IIUC, the top level keys are always the column names. A common use > case would be a single json/jsonb column that is already formatted > exactly as the user would like for output. Rather than enveloping it in > an object with a dedicated key, it would be nice to be able to output it > directly. This would allow non-object results to be outputted as well > (e.g., lines of JSON arrays, numbers, or strings). Due to how JSON is > structured, I think this would play nice with the JSON lines v.s. array > concept. > > COPY (SELECT json_build_object('foo', x) AS i_am_ignored FROM > generate_series(1, 3) x) TO STDOUT WITH (FORMAT JSON, > SOME_OPTION_TO_NOT_ENVELOPE) > {"foo":1} > {"foo":2} > {"foo":3} Your example does not match what you describe, or do I misunderstand? I thought your goal was to eliminate the repeated "foo" from each row... > 2. An option to ignore null fields so they are excluded from the output. > This would not be a default but would allow shrinking the total size of > the output data in many situations. This would be recursive to allow > nested objects to be shrunk down (not just the top level). This might be > worthwhile as a standalone JSON function though handling it during > output would be more efficient as it'd only be read once. > > COPY (SELECT json_build_object('foo', CASE WHEN x > 1 THEN x END) FROM > generate_series(1, 3) x) TO STDOUT WITH (FORMAT JSON, > SOME_OPTION_TO_NOT_ENVELOPE, JSON_SKIP_NULLS) > {} > {"foo":2} > {"foo":3} clear enough I think > 3. Reverse of #2 when copying data in to allow defaulting missing fields > to NULL. good to record the ask, but applies to a different feature (COPY FROM instead of COPY TO). -- Joe Conway PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com