Repository navigation
Description
When an ErrorResponse ends a binary export, e.g. a division by zero in a row, the read throws, but the exporter stays in its Ready state (NpgsqlBinaryExporter.cs:118-119, 179). The error can also come after the last row: after the trailer and CopyDone, PostgreSQL runs the statement's AFTER triggers, such as the immediate foreign key check of COPY (INSERT ... RETURNING ...) TO STDOUT, and a deferred constraint fails at commit, both before CommandComplete (postgres copyto.c:1282, execMain.c:452-454, postgres.c:1308-1321). The read of CommandComplete then throws (:191), with the same result.
The exporter's dispose then skips _endOfMessagePos - CumulativeReadPosition bytes, which is negative here, and waits for CopyDone (:505, 507). BeginBinaryExport runs that dispose itself when the header read fails (NpgsqlConnection.cs:1189-1203), and closing the connection runs it for an undisposed exporter (NpgsqlConnector.cs:2163-2206).
- In Release builds, which NuGet ships, the negative skip usually poisons the connection. PostgreSQL flushes the error and the ReadyForQuery separately (postgres
elog.c:4141,dest.c:283), so the client often reads them into a refilled buffer, and the skip then moves the read position before the start of the buffer. The connection stays open, but every later command on it fails with IndexOutOfRangeException (NpgsqlConnector.cs:1456), including the first one after it went back to the pool, which then breaks it. - In Debug builds the skip fails an assertion; under the test host, which turns a failed assertion into an exception, the dispose swallows it, so the connection stays usable by accident.
- After a message longer than the read buffer (a long notice before CopyOutResponse, or, in Release builds, a long notice or error at the failing read), the skip goes to a buffer the connector no longer reads from, and the dispose waits for a CopyDone that never comes: until Command Timeout, which breaks the connection, or forever with Command Timeout=0 (inferred for 0).
- If the connection breaks during that dispose, it throws NullReferenceException instead of the reason (
NpgsqlConnector.cs:2429-2441,NpgsqlBinaryExporter.cs:521,528,540).
Calls after the error don't report it either: StartRow throws "Already in the middle of a row" (:182-183), and Read, IsNull and Skip read a column length from the idle connection (:440-445, 416) until Command Timeout breaks it.
-- PostgreSQL 18.6, Release build: the 22012 is thrown by StartRow at row 2, and after the dispose
-- the next command on the connection failed with IndexOutOfRangeException (40 of 40 fresh connections;
-- an error at the first row, 1 / (i - 1), did the same through BeginBinaryExport's own dispose, 40 of 40)
COPY (SELECT 1 / (i - 2) FROM generate_series(1, 2) i) TO STDOUT (FORMAT BINARY)
-- The wait: the IMMUTABLE function with a constant argument raises its notice at planning time,
-- before CopyOutResponse; in a Debug build with Command Timeout=5, BeginBinaryExport threw 22012 after 5.0 s
-- and left the connection broken (with noisy_planned(10): after 24 ms, connection usable, through the swallowed assertion)
CREATE FUNCTION pg_temp.noisy_planned(len int) RETURNS int LANGUAGE plpgsql IMMUTABLE AS $$
BEGIN RAISE NOTICE '%', repeat('x', len); RETURN 1; END $$;
COPY (SELECT pg_temp.noisy_planned(20000), 1 / (i - 1) FROM generate_series(1, 2) i) TO STDOUT (FORMAT BINARY)The same code is on the 10.0 hotfix branch. 8.0 and 9.0 have the same dispose and the same later calls, but their BeginBinaryExport doesn't dispose the exporter when the header read fails, so an error before the first row doesn't reach that dispose there (read, not run).
Activity