Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f906e07

Browse files
committed
Make sure we wait for protocol-level EOF when ending binary COPY IN.
The previous coding just terminated the COPY immediately after seeing the EOF marker (-1 where a row field count is expected). The expected CopyDone or CopyFail message just got thrown away later, since we weren't in COPY mode anymore. This behavior complicated matters for the JDBC driver, and arguably was the wrong thing in any case since a CopyFail message after the marker wouldn't be honored. Note that there is a behavioral change here: extra data after the EOF marker was silently ignored before, but now it will cause an error. Hence not back-patching, although this is arguably a bug. Per report and patch by Kris Jurka.
1 parent af0161e commit f906e07

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/backend/commands/copy.c

+28-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.329 2010/08/13 20:10:50 rhaas Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.330 2010/09/18 20:10:15 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2058,9 +2058,34 @@ CopyFrom(CopyState cstate)
20582058
int16 fld_count;
20592059
ListCell *cur;
20602060

2061-
if (!CopyGetInt16(cstate, &fld_count) ||
2062-
fld_count == -1)
2061+
if (!CopyGetInt16(cstate, &fld_count))
20632062
{
2063+
/* EOF detected (end of file, or protocol-level EOF) */
2064+
done = true;
2065+
break;
2066+
}
2067+
2068+
if (fld_count == -1)
2069+
{
2070+
/*
2071+
* Received EOF marker. In a V3-protocol copy, wait for
2072+
* the protocol-level EOF, and complain if it doesn't come
2073+
* immediately. This ensures that we correctly handle
2074+
* CopyFail, if client chooses to send that now.
2075+
*
2076+
* Note that we MUST NOT try to read more data in an
2077+
* old-protocol copy, since there is no protocol-level EOF
2078+
* marker then. We could go either way for copy from file,
2079+
* but choose to throw error if there's data after the EOF
2080+
* marker, for consistency with the new-protocol case.
2081+
*/
2082+
char dummy;
2083+
2084+
if (cstate->copy_dest != COPY_OLD_FE &&
2085+
CopyGetData(cstate, &dummy, 1, 1) > 0)
2086+
ereport(ERROR,
2087+
(errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
2088+
errmsg("received copy data after EOF marker")));
20642089
done = true;
20652090
break;
20662091
}

0 commit comments

Comments
 (0)