Skip to content

copy_in shouldn't send Sync message #789

Open

Description

@jeff-davis

https://www.postgresql.org/message-id/CA+Tgmoa4eA+cPXaiGQmEBp9XisVd3ZE9dbvnbZEvx9UcMiw2tg@mail.gmail.com

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:

  1. Scenario 1
    1. Server encounters error and sends ErrorResponse
    2. Server consumes Sync (and treats it as acknowledgement of the ErrorResponse)
  2. Scenario 2
    1. Server consumes Sync (ignoring it)
    2. 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.

Activity

  1. sfackler commented on Jun 17, 2021

    @sfackler
    Collaborator

    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.

  2. jeff-davis commented on Jun 18, 2021

    @jeff-davis
    ContributorAuthor

    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.)

  3. sfackler commented on Jun 18, 2021

    @sfackler
    Collaborator

    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...

  4. jeff-davis commented on Jun 18, 2021

    @jeff-davis
    ContributorAuthor

    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.

  5. sfackler commented on Jun 18, 2021

    @sfackler
    Collaborator

    Seems fine to leave this open for now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions