From 63d1402a6a57536e40438f4017e40de782e8767b Mon Sep 17 00:00:00 2001 From: Gowdham M Date: Wed, 28 Oct 2026 15:07:51 +0530 Subject: [PATCH] Avoid usage of stale error context in COPY FROM after ON_ERROR ignore skips a row --- src/backend/commands/copyfromparse.c | 8 ++++++++ src/test/regress/expected/copy2.out | 4 ++++ src/test/regress/sql/copy2.sql | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index 0645053577..f7fb8b3bdc 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -873,6 +873,14 @@ NextCopyFromRawFieldsInternal(CopyFromState cstate, char ***fields, int *nfields return false; } + /* + * Clear the current attribute for error messages. A row skipped by + * ON_ERROR leaves it set, with cur_attval pointing into attribute_buf, + * which is about to be reset and possibly enlarged. + */ + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + cstate->cur_lineno++; /* Actually read the line into memory here */ diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index df6fcd9179..353f54e600 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -883,6 +883,10 @@ CONTEXT: COPY hard_err, line 1, column foo: "1" COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: missing data for column "k" CONTEXT: COPY check_ign_err, line 1: "1 {1}" +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2 {2} 2 extra" -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: extra data after last expected column diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index ec6f5e8cfd..d2119b337f 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -669,6 +669,12 @@ COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} \. +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +1 {1} a +2 {2} 2 extra +\. + -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc -- 2.54.0