Skip to content

Commit

Permalink
Fix error when using BatchResults.Exec
Browse files Browse the repository at this point in the history
...on a select that returns an error after some rows.

This was initially found in by a failure with CockroachDB because it
seems to send a RowDescription before an error even when no rows are
returned. PostgreSQL doesn't.
  • Loading branch information
jackc committed Apr 21, 2023
1 parent a23a423 commit 6defa2a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func (br *batchResults) Exec() (pgconn.CommandTag, error) {
}

commandTag, err := br.mrr.ResultReader().Close()
br.err = err
if err != nil {
br.err = err
br.mrr.Close()
}

if br.conn.batchTracer != nil {
br.conn.batchTracer.TraceBatchQuery(br.ctx, br.conn, TraceBatchQueryData{
Expand Down
21 changes: 21 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,27 @@ func TestSendBatchErrorWhileReadingResultsWithoutCallback(t *testing.T) {
})
}

func TestSendBatchErrorWhileReadingResultsWithExecWhereSomeRowsAreReturned(t *testing.T) {
t.Parallel()

pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
batch := &pgx.Batch{}
batch.Queue("select 4 / n from generate_series(-2, 2) n")

batchResult := conn.SendBatch(ctx, batch)

_, execErr := batchResult.Exec()
require.Error(t, execErr)

closeErr := batchResult.Close()
require.Equal(t, execErr, closeErr)

// Try to use the connection.
_, err := conn.Exec(ctx, "select 1")
require.NoError(t, err)
})
}

func TestConnBeginBatchDeferredError(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 6defa2a

Please sign in to comment.