1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Add HEADER support to COPY text format

The COPY CSV format supports the HEADER option to output a header
line.  This patch adds the same option to the default text format.  On
input, the HEADER option causes the first line to be skipped, same as
with CSV.

Author: Rémi Lapeyre <remi.lapeyre@lenstra.fr>
Discussion: https://www.postgresql.org/message-id/flat/CAF1-J-0PtCWMeLtswwGV2M70U26n4g33gpe1rcKQqe6wVQDrFA@mail.gmail.com
This commit is contained in:
Peter Eisentraut
2022-01-28 09:22:53 +01:00
parent 5553cbd4fe
commit 43f33dc018
8 changed files with 29 additions and 9 deletions

View File

@ -555,10 +555,10 @@ ProcessCopyOptions(ParseState *pstate,
errmsg("COPY delimiter cannot be \"%s\"", opts_out->delim)));
/* Check header */
if (!opts_out->csv_mode && opts_out->header_line)
if (opts_out->binary && opts_out->header_line)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY HEADER available only in CSV mode")));
errmsg("cannot specify HEADER in BINARY mode")));
/* Check quote */
if (!opts_out->csv_mode && opts_out->quote != NULL)

View File

@ -863,8 +863,11 @@ DoCopyTo(CopyToState cstate)
colname = NameStr(TupleDescAttr(tupDesc, attnum - 1)->attname);
CopyAttributeOutCSV(cstate, colname, false,
if (cstate->opts.csv_mode)
CopyAttributeOutCSV(cstate, colname, false,
list_length(cstate->attnumlist) == 1);
else
CopyAttributeOutText(cstate, colname);
}
CopySendEndOfRow(cstate);

View File

@ -32,7 +32,7 @@ typedef struct CopyFormatOptions
bool binary; /* binary format? */
bool freeze; /* freeze rows on loading? */
bool csv_mode; /* Comma Separated Value format? */
bool header_line; /* CSV header line? */
bool header_line; /* header line? */
char *null_print; /* NULL marker string (server encoding!) */
int null_print_len; /* length of same */
char *null_print_client; /* same converted to file encoding */

View File

@ -120,6 +120,14 @@ copy copytest3 to stdout csv header;
c1,"col with , comma","col with "" quote"
1,a,1
2,b,2
create temp table copytest4 (
c1 int,
"colname with tab: " text);
copy copytest4 from stdin (header);
copy copytest4 to stdout (header);
c1 colname with tab: \t
1 a
2 b
-- test copy from with a partitioned table
create table parted_copytest (
a int,

View File

@ -160,6 +160,18 @@ this is just a line full of junk that would error out if parsed
copy copytest3 to stdout csv header;
create temp table copytest4 (
c1 int,
"colname with tab: " text);
copy copytest4 from stdin (header);
this is just a line full of junk that would error out if parsed
1 a
2 b
\.
copy copytest4 to stdout (header);
-- test copy from with a partitioned table
create table parted_copytest (
a int,