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 1qwlOq-00E80d-1R for pgsql-bugs@arkaria.postgresql.org; Sat, 28 Oct 2023 15:38:56 +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 1qwlOo-004jUB-Ig for pgsql-bugs@arkaria.postgresql.org; Sat, 28 Oct 2023 15:38:54 +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 1qwlOo-004jU3-57 for pgsql-bugs@lists.postgresql.org; Sat, 28 Oct 2023 15:38:54 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qwlOk-003OaP-G3 for pgsql-bugs@lists.postgresql.org; Sat, 28 Oct 2023 15:38:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=qLwLTKgqqebazr8hwGdWxnF6PwBrO1vGa5/cEcHigUQ=; b=hJZ0X go4iqPcHuWnh2cHBDIuON6aF8ExdDQsW+yrCK+gdTllWzWQcvNm5BaKzDCjy5BH9VFMDw13f8Mvso 5HV0ayiUv23zw9krSuzvLFQR8h6QYfmDH1CVfQdXfaVS2MSeJ5PrgnHWlspWw0JWFvGR9Roer0pTF w5kct6wXrwgPc7KXTaeXPW8pmI1Mt+ku7EXsePz99Egvyv+Lc9PqWHTJmhbhEHerVhPXo5H1N2SMC tQp+LOOXJApCfoLbwee5Z+MnoECa4Nz2ODU/cZGZexpXYo7fWX9Idw2eGtpXWElh41PE8hJlMlSiz 4j4CFmmzOADv1T/5gr/2brsmjtPSQ==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1qwlOX-008WZq-0m; Sat, 28 Oct 2023 11:38:37 -0400 Date: Sat, 28 Oct 2023 11:38:37 -0400 From: Bruce Momjian To: Tom Lane Cc: Noah Misch , Svante Richter , pgsql-bugs@lists.postgresql.org Subject: Re: COPY TO CSV produces data that is incompatible/unsafe for \COPY FROM CSV Message-ID: References: <20220814070053.GA92855@rfd.leadboat.com> <624693.1660486135@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="eStK6dm7IqJEB7x3" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --eStK6dm7IqJEB7x3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Aug 16, 2022 at 11:26:47AM -0400, Bruce Momjian wrote: > On Sun, Aug 14, 2022 at 10:08:55AM -0400, Tom Lane wrote: > > Noah Misch writes: > > > The psql documentation says [\copy ... from stdin] looks for "\.". It says > > > nothing about doing that for [\copy ... from filename]. I wonder if psql > > > should change the filename case to send the whole file to the server, ignoring > > > "\." inside. (That is to say, change to match the psql documentation.) > > > > This seems like a sensible solution. The need to use an in-band EOF > > marker when reading from the command source file is clear, and there's > > no non-kluge way there. But that doesn't mean we should extend it > > to separate files. (I'm surprised to realize we do, actually.) > > So, look what I found in psql/copy.c: > > /* check for EOF marker, but not on a partial line */ > if (at_line_begin) > { > /* > --> * This code erroneously assumes '\.' on a line alone > --> * inside a quoted CSV string terminates the \copy. > --> * https://www.postgresql.org/message-id/E1TdNVQ-0001ju-GO@wrigleys.postgresql.org > */ > if ((linelen == 3 && memcmp(fgresult, "\\.\n", 3) == 0) || > (linelen == 4 && memcmp(fgresult, "\\.\r\n", 4) == 0)) > { > copydone = true; > } > } > > I wondered who would reference an email thread in the source code, so I > looked at the whole 2012 thread: > > https://www.postgresql.org/message-id/flat/E1TdNVQ-0001ju-GO%40wrigleys.postgresql.org > > and it was, of course, me. :-( > > Tom's analysis was similar ten years ago: > > > Ugh. This seems like a rather fundamental oversight in the CSV feature. > > The problem is that psql has no idea whether the copy is being done in > > CSV mode or not --- and even if it did, it doesn't parse the data fully > > enough to realize whether a \. line is inside quotes or not. > > > > In the case of out-of-line data files, it might be reasonable to just > > dispense with the check for \. altogether and always ship the whole file > > to the backend; I think there's a \. check on the backend side. (Not > > sure this is safe in V2 protocol, but I doubt anyone cares anymore > > about that.) > > > > In the case of in-line data in a script file, CSV mode seems a bit > > broken in any case; there's no concept of a terminator in CSV, AFAIK. > > So maybe we don't have to worry about that. > > I think at this point we should either fix this or document it. I have decided to document this with the attached patch. I plan to apply it to all supported Postgres versions. If we ever fix FROM file so only FROM STDIN is a problem, we can update the documentation. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you. --eStK6dm7IqJEB7x3 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="copy_csv.diff" diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index d94e3cacfc..cc7d797159 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1119,6 +1119,10 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g destination, because all data must pass through the client/server connection. For large amounts of data the SQL command might be preferable. + Also, because of this pass-through method, \copy + ... from in CSV mode will erroneously + treat a \. data value alone on a line as an + end-of-input marker. diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index b3cc3d9a29..8d6ce4cedd 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -627,6 +627,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res) * This code erroneously assumes '\.' on a line alone * inside a quoted CSV string terminates the \copy. * https://www.postgresql.org/message-id/E1TdNVQ-0001ju-GO@wrigleys.postgresql.org + * https://www.postgresql.org/message-id/bfcd57e4-8f23-4c3e-a5db-2571d09208e2@beta.fastmail.com */ if ((linelen == 3 && memcmp(fgresult, "\\.\n", 3) == 0) || (linelen == 4 && memcmp(fgresult, "\\.\r\n", 4) == 0)) --eStK6dm7IqJEB7x3--