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 1rA9sq-0054kf-RN for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Dec 2023 14:25:16 +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 1rA9sp-00H9S0-HF for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Dec 2023 14:25:15 +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 1rA9sp-00H9Rd-2M for pgsql-hackers@lists.postgresql.org; Mon, 04 Dec 2023 14:25:15 +0000 Received: from mail-qk1-x735.google.com ([2607:f8b0:4864:20::735]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rA9sh-008lCE-PH for pgsql-hackers@postgresql.org; Mon, 04 Dec 2023 14:25:13 +0000 Received: by mail-qk1-x735.google.com with SMTP id af79cd13be357-77d895c298eso287533385a.3 for ; Mon, 04 Dec 2023 06:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1701699906; x=1702304706; 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=B9TKZbhZa1BtVtIhj1PspN+hIML/iFjgfwAhmqDKjKE=; b=dWc8dIngia39/3J6iKFsFmXDdJTyly7ZaannY7RrOIqd8pMu6Irb60g1Rp3/FTSdBP gkqBnPED3x0NTdG6Nyym5jrmrxxle9vaeT678Sk/4+AnSWK6lclp1kHKulNWxaXjRA2w gj1onqrQcRQSz/T7ox9Q6Fqlj4X8KZuGPRkSXuEX+6r729u/9iF37KflUjqDLl11A9b6 MHuYtl6EQsBbeiGern4rmnvREITVUuc7hju9zYGHR2vABujzXpdylQI2eL4rYtB1wSQC WXH+2MPkLZeIMTkWSZGYF+BgrGGml4kw7s7YNslGS7BaW6jKHoiM89eDO8CQeUqCca8U X6kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701699906; x=1702304706; 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=B9TKZbhZa1BtVtIhj1PspN+hIML/iFjgfwAhmqDKjKE=; b=RWPDbkj3Dh4Ogp0VfYp+Ca7rKubuhbCD+qN0IETX5/eeib3FGi1IizruH7kuYwfFfB byEViWk2YEtdVS9P7faJEC93jeiH0Sc0D5Kf9/b17ovhDHWpjT/tn+KGDndLLSM7A3Pa 4NE79hD7SlIyHkZIquBRh1LIR11kXCZ3U2mbrT8H83j/D4MYYqnBNHmv/X36Rrb3ONo8 M8xrUUGt2XR97KyVU5leAtglX1okO4BPkklaWksew9ztodKLQqter6pDSViNJqPkVAqf 9uIEbPPWWAKaCAT1al4Wrrv6mJFap23ob05XEPNjRRPwYCFuKwdHEbRyZcI/GvhS5T+x lVVA== X-Gm-Message-State: AOJu0YwuLgS0nwEPdTHC207MoEWfntvYhKXw71stqBMcVs3eHLFj7rY1 h2vqjDdt5T2U9GM2kaVcdznfWQ== X-Google-Smtp-Source: AGHT+IEEK5AwYUs175MTFnYE3/Fe3UXmI10OTRGCZ+6C8CTReqAGf2zxC2bO9lj6UoxgL5n/PGgrXQ== X-Received: by 2002:a05:620a:6182:b0:77e:fba3:4f0a with SMTP id or2-20020a05620a618200b0077efba34f0amr4952471qkn.96.1701699906290; Mon, 04 Dec 2023 06:25:06 -0800 (PST) Received: from ?IPV6:2605:a601:91bc:f600::2bb? ([2605:a601:91bc:f600::2bb]) by smtp.googlemail.com with ESMTPSA id bs44-20020a05620a472c00b0077d7eaefc08sm4262537qkb.87.2023.12.04.06.25.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Dec 2023 06:25:05 -0800 (PST) Message-ID: <41dcba92-1075-e5e5-cb99-36711abf6cec@dunslane.net> Date: Mon, 4 Dec 2023 09:25:04 -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> <7117a356-916c-4cf3-bad8-861490e65dcf@joeconway.com> <9c77b6fa-ee88-b2e6-0fa7-4fc81721da35@dunslane.net> From: Andrew Dunstan Subject: Re: Emitting JSON to file using COPY TO 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 2023-12-04 Mo 08:37, Joe Conway wrote: > On 12/4/23 07:41, Andrew Dunstan wrote: >> >> On 2023-12-03 Su 20:14, Joe Conway wrote: >>> (please don't top quote on the Postgres lists) >>> >>> On 12/3/23 17:38, Davin Shearer wrote: >>>> " being quoted as \\" breaks the JSON. It needs to be \".  This has >>>> been my whole problem with COPY TO for JSON. >>>> >>>> Please validate that the output is in proper format with correct >>>> quoting for special characters. I use `jq` on the command line to >>>> validate and format the output. >>> >>> I just hooked existing "row-to-json machinery" up to the "COPY TO" >>> statement. If the output is wrong (just for for this use case?), >>> that would be a missing feature (or possibly a bug?). >>> >>> Davin -- how did you work around the issue with the way the built in >>> functions output JSON? >>> >>> Andrew -- comments/thoughts? >> >> I meant to mention this when I was making comments yesterday. >> >> The patch should not be using CopyAttributeOutText - it will try to >> escape characters such as \, which produces the effect complained of >> here, or else we need to change its setup so we have a way to inhibit >> that escaping. > > > Interesting. > > I am surprised this has never been raised as a problem with COPY TO > before. > > Should the JSON output, as produced by composite_to_json(), be sent > as-is with no escaping at all? If yes, is JSON somehow unique in this > regard? Text mode output is in such a form that it can be read back in using text mode input. There's nothing special about JSON in this respect - any text field will be escaped too. But output suitable for text mode input is not what you're trying to produce here; you're trying to produce valid JSON. So, yes, the result of composite_to_json, which is already suitably escaped, should not be further escaped in this case. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com