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 1t18nk-00Eu0O-Cn for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Oct 2024 18:31: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 1t18ni-007ypX-Lj for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Oct 2024 18:31: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 1t18ni-007yoT-CA for pgsql-hackers@lists.postgresql.org; Wed, 16 Oct 2024 18:31:14 +0000 Received: from fout-a4-smtp.messagingengine.com ([103.168.172.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t18ne-001E7m-Jf for pgsql-hackers@lists.postgresql.org; Wed, 16 Oct 2024 18:31:13 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id A3A6113801A8; Wed, 16 Oct 2024 14:31:09 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-11.internal (MEProxy); Wed, 16 Oct 2024 14:31:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1729103469; x=1729189869; bh=vMtAlOkUsKLLgjp5PZYpEm9D3vCmLDMQTv6hexRnRwA=; b= PuTig+i6t4ryEYouFKXcfx5lkDGQu7/NIbvQJQMXHG4SqHxzT66BkJz4g38Og2wa nFArDXYc6rx2keTZEBQBWHrUXvkfRoMYkPEz7zIGXzsR5XoUQFUK6hCSCFsx8t4P YGksS36IJegM+99x42Fdq+J2EB84hlULaG2ej1ijIO3iRvXx9wbl+rRbGpseSsAU gXov7qsO54O3V+p8IdLuoFp+VdICrazL9jMHtKLNsxxb70IMhwTQWq12IjwKnTpE DdYiQ27v+8Ng34taiwIi9s9f3bOFl9+z4nBtndDfhMENf4hBz47jZj1c1kYZFiHO rUGTMCEg2INbQKiyMFx8MA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1729103469; x= 1729189869; bh=vMtAlOkUsKLLgjp5PZYpEm9D3vCmLDMQTv6hexRnRwA=; b=T ls7rg8fNuTxk2M33N7MUVQAJ75mIQ8jo4adG3Z87CFwXhe9QozJbtchxOgOIl62+ ws/vUrgBROA976eLmjjRU7bJFVDQw5m04fyUGOoBOYfNgRSnXfc/QRW6wEfm7qDj Thwj49UyZ3jV9AJVbKrOApRx5sLejfw/aNHYZuJFgbOkh70xVg1Y14oocZh49bjQ ZNkOi25sCDuLqtK6S3SReB6w2y15aebcBLUx3OUidrF0bMOVnqes2+XRDrVtBUKq hFQyVccyEi4TUWIrfW9sonVeb8tWDhw2ZuoMLp9zLaqb/bK84DhWlPzx0wcJkJWX 6DX1sds/06DhdhsxoXu9g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdegledguddvjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvvefkjghfufgtgfesthejredtredt tdenucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestghomhhpih hlvghrrdhorhhgqeenucggtffrrghtthgvrhhnpeevkefgfeejfedtkeeggfdugfegvefh keduteekhffhgfejueefhfduvdfftdeuvdenucffohhmrghinhepphhoshhtghhrrdgvsh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehjohgv lhestghomhhpihhlvghrrdhorhhgpdhnsggprhgtphhtthhopeegpdhmohguvgepshhmth hpohhuthdprhgtphhtthhopehjrggtohgsrdgthhgrmhhpihhonhesvghnthgvrhhprhhi shgvuggsrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsheslhhishhtsh drphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepuggrnhhivghlsehmrghnihht ohhuqdhmrghilhdrohhrghdprhgtphhtthhopehishhhihhisehpohhsthhgrhgvshhqlh drohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 2D556B00068; Wed, 16 Oct 2024 14:31:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 Date: Wed, 16 Oct 2024 20:30:47 +0200 From: "Joel Jacobson" To: "Daniel Verite" Cc: "Jacob Champion" , "Tatsuo Ishii" , pgsql-hackers@lists.postgresql.org Message-Id: <5d7c38c6-e3de-4861-b045-0cf4abee1a14@app.fastmail.com> In-Reply-To: References: Subject: Re: New "raw" COPY format Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Oct 16, 2024, at 18:34, Daniel Verite wrote: > Joel Jacobson wrote: > >> However, I thinking rejecting such column data seems like the >> better alternative, to ensure data exported with COPY TO >> can always be imported back using COPY FROM, >> for the same format. > > On the other hand, that might prevent cases where we > want to export, for instance, a valid json array: > > copy (select json_agg(col) from table ) to 'file' RAW > > This is a variant of the discussion in [1] where the OP does: > > copy (select json_agg(row_to_json(t)) from t) TO 'file' > > and he complains that both text and csv "break the JSON". > That discussion morphed into a proposed patch adding JSON > format to COPY, but RAW would work directly as the OP > expected. > > That is, unless happens to include JSON fields with LF/CRLF > in them, and the RAW format says this is an error condition. > In that case it's quite annoying to make it an error, rather than > simply let it pass. > > > [1] > https://postgr.es/m/CALvfUkBxTYy5uWPFVwpk_7ii2zgT07t3d-yR_cy4sfrrLU=kcg@mail.gmail.com Thanks for finding this related thread. Very good example, that would be solved with RAW. I've used a different hack myself many times to export text "as is", which is to use COPY TO ... BINARY, and then to manually edit the file using vim, stripping away the header and footer from the file. :D But I don't see how JSON fields that come from PostgreSQL could contain LF/CRLF though, could they? Since LF/CR must be escaped inside a JSON field, and when casting JSONB to text, there are no newlines injected anywhere in between fields. I can only see how a value of the legacy JSON type could have newlines in it. That doesn't matter though, this is still a very good example on the need to export text "as is" from PostgreSQL to a file. I like Jacob's idea of letting the user specify a DELIMITER also for the RAW format, to override the automagical newline detection. This would e.g. allow importing values containing e.g. \r when the newline delimiter is \n, which would otherwise be reject with an "inconsistent newline style" error. I think it would also be useful to allow specifying DELIMITER NONE, which would allow importing an entire text file, "as is", into a single column and single row. To export a single column single row, the delimiter wouldn't matter, since there wouldn't be any. But DELIMITER NONE would be useful also for COPY TO, if wanting to concatenate text "as is" without adding newlines in between, to a file, similar to the UNIX "cat" command. Regarding the name for the format, I thought SINGLE was nice before I read this message, but now since the need for more rawness seems desirable, I think I like RAW the most again, since if the automagical newline detection can be overridden, then it's really raw for real. A final thought is to maybe consider just skipping the automagical newline detection for RAW? Instead of the automagical detection, the default newline delimiter could be the OS default, similar to how COPY TO works. That way, it would almost always just work for most users, as long as processing files within their OS, and when not, they would just need to specify the DELIMITER. /Joel