Repository navigation
Conversation
…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 <noreply@anthropic.com>
roji
approved these changes
Oct 29, 2026
roji
left a comment
Member
There was a problem hiding this comment.
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
BeginBinaryExport,BeginBinaryImportandBeginRawBinaryCopy(andBeginTextExportandBeginTextImport, 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.Initnow reads that ReadyForQuery before it throws.How a real server gets there
A caller passes
COPY t TO '<file>',COPY t FROM '<file>'or aPROGRAMvariant 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 theCOPYprefix, 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 (BeginTextImportwithCOPY … 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 42failed 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
Initmethods), with no API change.Dependencies and merge order
It depends on no other PR.
Fixes #6694