Skip to content

Commit

Permalink
PostgreSQL Copy: Consume ReadyForQuery on error
Browse files Browse the repository at this point in the history
When a COPY statement was in error inside a subtransaction,
a Protocol Error used to be raised. By consuming the ReadyForQuery
message when there is an error, we no longer have this issue.
  • Loading branch information
feikesteenbergen committed Apr 4, 2024
1 parent e920682 commit 70c1876
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions sqlx-postgres/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,21 @@ async fn pg_begin_copy_out<'c, C: DerefMut<Target = PgConnection> + Send + 'c>(

let stream: TryAsyncStream<'c, Bytes> = try_stream! {
loop {
let msg = conn.stream.recv().await?;
match msg.format {
MessageFormat::CopyData => r#yield!(msg.decode::<CopyData<Bytes>>()?.0),
MessageFormat::CopyDone => {
let _ = msg.decode::<CopyDone>()?;
conn.stream.recv_expect(MessageFormat::CommandComplete).await?;
match conn.stream.recv().await {
Err(e) => {
conn.stream.recv_expect(MessageFormat::ReadyForQuery).await?;
return Ok(())
return Err(e);
},
_ => return Err(err_protocol!("unexpected message format during copy out: {:?}", msg.format))
Ok(msg) => match msg.format {
MessageFormat::CopyData => r#yield!(msg.decode::<CopyData<Bytes>>()?.0),
MessageFormat::CopyDone => {
let _ = msg.decode::<CopyDone>()?;
conn.stream.recv_expect(MessageFormat::CommandComplete).await?;
conn.stream.recv_expect(MessageFormat::ReadyForQuery).await?;
return Ok(())
},
_ => return Err(err_protocol!("unexpected message format during copy out: {:?}", msg.format))
}
}
}
};
Expand Down

0 comments on commit 70c1876

Please sign in to comment.