v1-0001-Reject-HEADER-with-binary-and-json-COPY-formats-b.patch
application/octet-stream
Filename: v1-0001-Reject-HEADER-with-binary-and-json-COPY-formats-b.patch
Type: application/octet-stream
Part: 0
From 51f90d594cf068c98d07448bc8c8c51966ef3648 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Sun, 31 May 2026 09:51:04 +0800
Subject: [PATCH v1] Reject HEADER with binary and json COPY formats by option
presence
The documentation says that the HEADER option is not allowed with binary
or json format. However, COPY option processing checked the interpreted
header value instead of whether the option was specified, so HEADER 0 was
accepted even though the option was present.
Check header_specified when rejecting HEADER with binary or json format,
so all explicit HEADER values are handled consistently with the documented
rule. Add a file_fdw test for the HEADER 0 case with binary format.
Author: Chao Li <lic@highgo.com>
---
contrib/file_fdw/expected/file_fdw.out | 2 ++
contrib/file_fdw/sql/file_fdw.sql | 1 +
src/backend/commands/copy.c | 2 +-
3 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/contrib/file_fdw/expected/file_fdw.out b/contrib/file_fdw/expected/file_fdw.out
index 640986528ae..41ce83139f1 100644
--- a/contrib/file_fdw/expected/file_fdw.out
+++ b/contrib/file_fdw/expected/file_fdw.out
@@ -60,6 +60,8 @@ CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', escape ':
ERROR: COPY ESCAPE requires CSV mode
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', header 'true'); -- ERROR
ERROR: cannot specify HEADER in BINARY mode
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', header '0'); -- ERROR
+ERROR: cannot specify HEADER in BINARY mode
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', quote ':'); -- ERROR
ERROR: COPY QUOTE requires CSV mode
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', escape ':'); -- ERROR
diff --git a/contrib/file_fdw/sql/file_fdw.sql b/contrib/file_fdw/sql/file_fdw.sql
index 56bfc926c00..2892e420ff1 100644
--- a/contrib/file_fdw/sql/file_fdw.sql
+++ b/contrib/file_fdw/sql/file_fdw.sql
@@ -61,6 +61,7 @@ CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'xml'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', quote ':'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', escape ':'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', header 'true'); -- ERROR
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', header '0'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', quote ':'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', escape ':'); -- ERROR
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter 'a'); -- ERROR
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 003b70852bb..b89e4e0b1cc 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -877,7 +877,7 @@ ProcessCopyOptions(ParseState *pstate,
errmsg("COPY delimiter cannot be \"%s\"", opts_out->delim)));
/* Check header */
- if (opts_out->header_line != COPY_HEADER_FALSE &&
+ if (header_specified &&
(opts_out->format == COPY_FORMAT_BINARY ||
opts_out->format == COPY_FORMAT_JSON))
ereport(ERROR,
--
2.50.1 (Apple Git-155)