Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uun2n-0005h1-RG for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Jul 2013 17:10:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Uun2n-0008Po-A5 for pgsql-bugs@arkaria.postgresql.org; Thu, 04 Jul 2013 17:10:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uun2l-0008Nl-R4 for pgsql-bugs@postgresql.org; Thu, 04 Jul 2013 17:10:07 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uun2h-0008TQ-2v for pgsql-bugs@postgresql.org; Thu, 04 Jul 2013 17:10:06 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1Uun2f-0008Af-Ga; Thu, 04 Jul 2013 13:10:01 -0400 Date: Thu, 4 Jul 2013 13:10:01 -0400 From: Bruce Momjian To: Tom Lane Cc: tgarnett@panjiva.com, pgsql-bugs@postgresql.org Subject: Re: BUG #7709: psql \copy csv terminates with \. in quoted region Message-ID: <20130704171001.GD17790@momjian.us> References: <24679.1354037624@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="WYTEVAkct0FjGQmd" Content-Disposition: inline In-Reply-To: <24679.1354037624@sss.pgh.pa.us> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.1 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --WYTEVAkct0FjGQmd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Nov 27, 2012 at 12:33:44PM -0500, Tom Lane wrote: > tgarnett@panjiva.com writes: > > psql \copy terminates at \. by itself in a line even if the format is csv > > and the \. is inside a quoted region. This means that some values can't be > > round-tripped by psql \copy. Tested and the native postgresql COPY handles > > this case correctly. > > 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 have added a C comment documenting this bug; patch attached. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. + --WYTEVAkct0FjGQmd Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="copy.diff" diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c new file mode 100644 index b5732c7..c1e7cfe *** a/src/bin/psql/copy.c --- b/src/bin/psql/copy.c *************** handleCopyIn(PGconn *conn, FILE *copystr *** 635,640 **** --- 635,645 ---- /* check for EOF marker, but not on a partial line */ if (firstload) { + /* + * This code erroneously assumes '\.' on a line alone + * inside a quoted CSV string terminates the \copy. + * http://www.postgresql.org/message-id/E1TdNVQ-0001ju-GO@wrigleys.postgresql.org + */ if (strcmp(buf, "\\.\n") == 0 || strcmp(buf, "\\.\r\n") == 0) { --WYTEVAkct0FjGQmd Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --WYTEVAkct0FjGQmd--