GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Keep the connection usable when a COPY API command completes without copying by Brar · Pull Request #6706 · npgsql/npgsql

Conversation

@Brar

@Brar Brar commented Oct 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

BeginBinaryExport, BeginBinaryImport and BeginRawBinaryCopy (and BeginTextExport and BeginTextImport, which use the raw stream) throw InvalidOperationException when the server completes the command right away, e.g. for a COPY to or from a file on the server. They left the ReadyForQuery that follows the CommandComplete unread, so the next command on the connection read it as its own response and failed with "Received backend message ReadyForQuery while expecting ParseCompleteMessage", which broke the connection. Init now reads that ReadyForQuery before it throws.

How a real server gets there

A caller passes COPY t TO '<file>', COPY t FROM '<file>' or a PROGRAM variant to one of these APIs; that needs superuser or one of the roles pg_read_server_files, pg_write_server_files and pg_execute_server_program. Npgsql only checks the COPY prefix, so the server runs the command, sends no copy response, and ends it like any other statement with CommandComplete and ReadyForQuery. This is what the user in #1673 did (BeginTextImport with COPY … FROM 'C:\temp\test.csv'), which is why the exception exists.

On PostgreSQL 18.6, all six calls (binary export and import, raw export and import, text export and import) threw the intended exception. Then SELECT 42 failed as above and the connection was broken, also after Close and a reopen from the pool, which handed out the same physical connection. The imports did import. With this PR both following commands returned their values. The released 8.0.9, 9.0.5 and 10.0.3 behave like main.

Tests

CopyTests.Copy_api_with_command_that_completes_without_copying (mock, for the three classes): on main all three cases fail with the error above; with the fix they pass.

For the reviewer

  • A core change in the COPY code (one more read in each of the three Init methods), with no API change.
  • It isn't meant for the hotfix branch.

Dependencies and merge order

It depends on no other PR.

Fixes #6694

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Brar @claude
Keep the connection usable when a COPY API command completes without …
4041f4c
…copying

BeginBinaryExport, BeginBinaryImport and BeginRawBinaryCopy (and the
text APIs built on it) throw InvalidOperationException when the server
completes the command right away, e.g. for a COPY to or from a file on
the server. They left the ReadyForQuery that follows the
CommandComplete unread, so the next command on the connection read it
as its own response and failed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brar
Brar requested review from roji and vonzshik as code owners October 25, 2026 21:38
This was referenced Oct 25, 2026
Fix TIMELINE_HISTORY, SHOW ALL and CommandTimeout on replication connections #6709
Merged
Read COPY data from the connector's current read buffer #6712
Open
Fix NpgsqlBinaryExporter's dispose after an error or a connection break #6713
Open
roji
roji approved these changes Oct 29, 2026
View reviewed changes

@roji roji left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable even if low-value - calling the BeginCopy APIs with non-STDIN/STDOUT options is a programmer bug, as these APIs are for importing/exporting data from the client application; so these aren't ever errors that are supposed to happen in a running application, which would recover and continue. Making the connection work after such programmer errors isn't really needed, but as the fix is very trivial, why not.

I wouldn't backport.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Brar
Brar merged commit 5108962 into npgsql:main Nov 1, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@Brar
Brar deleted the core/copy-without-copying branch November 1, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@roji roji roji approved these changes
@vonzshik vonzshik Awaiting requested review from vonzshik vonzshik is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Brar @roji
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.