Skip to content

Commit eb1d23a

Browse files
authored
Merge pull request #2391 from bonnefoa/pipeline-close-error
Close batch pipeline after a query error
2 parents 61d3c96 + 452f07b commit eb1d23a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

batch.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ func (br *pipelineBatchResults) Close() error {
404404

405405
if br.err == nil && br.lastRows != nil && br.lastRows.err != nil {
406406
br.err = br.lastRows.err
407-
return br.err
408407
}
409408

410409
if br.closed {

batch_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,44 @@ func TestConnSendBatchErrorDoesNotLeaveOrphanedPreparedStatement(t *testing.T) {
10221022
})
10231023
}
10241024

1025+
func TestSendBatchStatementTimeout(t *testing.T) {
1026+
t.Parallel()
1027+
1028+
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
1029+
defer cancel()
1030+
1031+
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
1032+
batch := &pgx.Batch{}
1033+
batch.Queue("SET statement_timeout='1ms'")
1034+
batch.Queue("SELECT pg_sleep(10)")
1035+
1036+
br := conn.SendBatch(context.Background(), batch)
1037+
// set statement_timeout
1038+
_, err := br.Exec()
1039+
assert.NoError(t, err)
1040+
1041+
// get pg_sleep results
1042+
rows, err := br.Query()
1043+
assert.NoError(t, err)
1044+
1045+
// Consume rows and check error
1046+
for rows.Next() {
1047+
}
1048+
err = rows.Err()
1049+
assert.ErrorContains(t, err, "(SQLSTATE 57014)")
1050+
rows.Close()
1051+
1052+
// The last error should be repeated when closing the batch
1053+
err = br.Close()
1054+
assert.ErrorContains(t, err, "(SQLSTATE 57014)")
1055+
1056+
// Connection should be usable after the statement timeout in pipeline
1057+
_, err = conn.Exec(context.Background(), "Select 1")
1058+
assert.NoError(t, err)
1059+
})
1060+
1061+
}
1062+
10251063
func ExampleConn_SendBatch() {
10261064
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
10271065
defer cancel()

0 commit comments

Comments
 (0)