Skip to content

COPY APIs leave the connection out of sync when the COPY command doesn't copy through the client #6694

Closed

Description

@Brar

BeginBinaryExport, BeginBinaryImport, BeginRawBinaryCopy, BeginTextExport and BeginTextImport throw the intended InvalidOperationException ("This API only supports import/export from the client …") when the server completes the COPY right away, e.g. for COPY t TO '/some/file' (NpgsqlBinaryExporter.cs:93-97, NpgsqlBinaryImporter.cs:102-106, NpgsqlRawCopyStream.cs:113-117; the text APIs use the raw stream, NpgsqlConnection.cs:1247-1250, 1313-1316). They don't read the ReadyForQuery that follows the CommandComplete, and their cleanup doesn't either (NpgsqlBinaryExporter.cs:489-493, 528, NpgsqlBinaryImporter.cs:532-535, NpgsqlRawCopyStream.cs:419, 453). So the next command on the connection reads that ReadyForQuery where it expects its own response. On PostgreSQL 18.6, SELECT 42 after the exception failed with "Received backend message ReadyForQuery while expecting ParseCompleteMessage", and the connection was broken; the same happened after Close and a reopen from the pool, which handed out the same physical connection. The imports did import.

await using var conn = await dataSource.OpenConnectionAsync();
// needs the pg_write_server_files role
Assert.Throws<InvalidOperationException>(() => conn.BeginBinaryExport("COPY (SELECT 1) TO '/tmp/x.bin' (FORMAT BINARY)"));
await using var cmd = new NpgsqlCommand("SELECT 1", conn);
cmd.ExecuteScalar(); // fails: it reads the leftover ReadyForQuery first

The released 8.0.9, 9.0.5 and 10.0.3 behave the same way. The exception was added in 2017 for #1673 (2a5029e), where a user had passed COPY … FROM 'C:\temp\test.csv' to BeginTextImport.

Activity

  1. added this to the 11.0.0 milestone on Oct 25, 2026
  2. self-assigned this
    on Oct 25, 2026
  3. added theissue type on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions