From 53e67e3030625fc5bf43e536e1b489f8f3d9003e Mon Sep 17 00:00:00 2001 From: Simon Muller Date: Sun, 13 May 2018 21:35:01 +0200 Subject: [PATCH] Allow COPY's 'text' format to output a header --- doc/src/sgml/ref/copy.sgml | 2 +- src/backend/commands/copy.c | 11 +++++++---- src/test/regress/input/copy.source | 2 ++ src/test/regress/output/copy.source | 4 ++++ 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/ref/copy.sgml b/doc/src/sgml/ref/copy.sgml index 13a8b68d95..4db97589fd 100644 --- a/doc/src/sgml/ref/copy.sgml +++ b/doc/src/sgml/ref/copy.sgml @@ -279,7 +279,7 @@ COPY { table_name [ ( CSV format. + This option is not allowed when using binary format. diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 770c75fe2c..3eb72a1308 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -1293,10 +1293,10 @@ ProcessCopyOptions(ParseState *pstate, errmsg("COPY delimiter cannot be \"%s\"", cstate->delim))); /* Check header */ - if (!cstate->csv_mode && cstate->header_line) + if (cstate->binary && cstate->header_line) ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY HEADER available only in CSV mode"))); + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("cannot specify HEADER in BINARY mode"))); /* Check quote */ if (!cstate->csv_mode && cstate->quote != NULL) @@ -2033,8 +2033,11 @@ CopyTo(CopyState cstate) colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname); - CopyAttributeOutCSV(cstate, colname, false, + if (cstate->csv_mode) + CopyAttributeOutCSV(cstate, colname, false, list_length(cstate->attnumlist) == 1); + else + CopyAttributeOutText(cstate, colname); } CopySendEndOfRow(cstate); diff --git a/src/test/regress/input/copy.source b/src/test/regress/input/copy.source index cb13606d14..f9301b5e6b 100644 --- a/src/test/regress/input/copy.source +++ b/src/test/regress/input/copy.source @@ -133,3 +133,5 @@ this is just a line full of junk that would error out if parsed \. copy copytest3 to stdout csv header; + +copy copytest3 to stdout with (format text, header true); diff --git a/src/test/regress/output/copy.source b/src/test/regress/output/copy.source index b7e372d61b..686c61d71d 100644 --- a/src/test/regress/output/copy.source +++ b/src/test/regress/output/copy.source @@ -95,3 +95,7 @@ copy copytest3 to stdout csv header; c1,"col with , comma","col with "" quote" 1,a,1 2,b,2 +copy copytest3 to stdout with (format text, header true); +c1 col with , comma col with " quote +1 a 1 +2 b 2 -- 2.15.1.windows.2