You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It appears that the extended protocol doesn't work very well for CopyIn or CopyBoth mode. The main problem is described in the protocol docs:
"The backend will ignore Flush and Sync messages received during copy-in mode. Receipt of any other non-copy message type constitutes an error that will abort the copy-in state as described above. (The exception for Flush and Sync is for the convenience of client libraries that always send Flush or Sync after an Execute message, without checking whether the command to be executed is a COPY FROM STDIN.)"
If the COPY ... FROM STDIN command is sent using the extended protocol, along with a Sync, then the server's behavior depends on when the Sync is consumed. There are two scenarios once in CopyIn mode:
Scenario 1
Server encounters error and sends ErrorResponse
Server consumes Sync (and treats it as acknowledgement of the ErrorResponse)
Scenario 2
Server consumes Sync (ignoring it)
Server encounters error and sends ErrorResponse
There's no way for the client to tell which scenario happened. The client can either wait indefinitely to see if it gets a ReadyForQuery, or it can send an extra Sync message (in which case it may receive an extra ReadyForQuery message).
Instead, it's better for the client to just not send a Sync message along with the COPY ... FROM STDIN command. It only needs to send the Sync after it receives an ErrorResponse or after it sends a CopyDone/CopyFail.
This sentence seems to imply that the server will respect Sync barriers in the event of an error if the query was sent in extended query mode:
In the event of a backend-detected error during copy-in mode (including receipt of a CopyFail message), the backend will issue an ErrorResponse message. If the COPY command was issued via an extended-query message, the backend will now discard frontend messages until a Sync message is received, then it will issue ReadyForQuery and return to normal processing.
If you read the linked message in the archives, Robert is pointing out that the protocol is badly designed. libpq can't do much about it because it doesn't know whether the command is SELECT or COPY ... FROM STDIN. But rust-postgres does know, because there is a separate entry point for copy_in().
I'm discussing on pgsql-hackers, and it's possible that a server change will improve things. Let me figure it out first. (If you want you can close this until I do, but hopefully that's soon.)
Ah, I see. It seems like it would be safer for copy_in to take a raw query string and use the simple protocol then. Copy statements can't take parameters anyway so there's not too much of a use for prepared statements in that context. My concern with just not sending a Sync is that if a user sent a not-COPY FROM STDIN query through copy_in the connection would deadlock (I think?).
Unfortunately switching to the simple protocol is a breaking change...
I posted a server patch that may address the issue. If it's accepted, there's no reason for a change. Let's wait and see -- I'll know in a few weeks I think. If open issues bother you, you can close this one until I have more information.
Activity
sfackler commentedon Jun 17, 2021
This sentence seems to imply that the server will respect Sync barriers in the event of an error if the query was sent in extended query mode:
jeff-davis commentedon Jun 18, 2021
If you read the linked message in the archives, Robert is pointing out that the protocol is badly designed. libpq can't do much about it because it doesn't know whether the command is
SELECTorCOPY ... FROM STDIN. But rust-postgres does know, because there is a separate entry point forcopy_in().I'm discussing on pgsql-hackers, and it's possible that a server change will improve things. Let me figure it out first. (If you want you can close this until I do, but hopefully that's soon.)
sfackler commentedon Jun 18, 2021
Ah, I see. It seems like it would be safer for copy_in to take a raw query string and use the simple protocol then. Copy statements can't take parameters anyway so there's not too much of a use for prepared statements in that context. My concern with just not sending a
Syncis that if a user sent a not-COPY FROM STDINquery throughcopy_inthe connection would deadlock (I think?).Unfortunately switching to the simple protocol is a breaking change...
jeff-davis commentedon Jun 18, 2021
I posted a server patch that may address the issue. If it's accepted, there's no reason for a change. Let's wait and see -- I'll know in a few weeks I think. If open issues bother you, you can close this one until I have more information.
sfackler commentedon Jun 18, 2021
Seems fine to leave this open for now!