Fix incomplete selects during batch
An incompletely read select followed by an insert would fail. This was caused by query methods in the non-batch path always calling ensureConnectionReadyForQuery. This ensures that connections interrupted by context cancellation are still usable. However, in the batch case query methods are not being called while reading the result. A incompletely read select followed by another select would not manifest this error due to it starting by reading until row description. But when an incomplete select (which even a successful QueryRow would be considered) is followed by an Exec, the CommandComplete message from the select would be considered as the response to the subsequent Exec. The fix is the batch tracking whether a CommandComplete is pending and reading it before advancing to the next result. This is similar in principle to ensureConnectionReadyForQuery, just specific to Batch.
This commit is contained in:
+50
-1
@@ -477,7 +477,7 @@ func TestConnBeginBatchQuerySyntaxError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnBeginBatchSelectInsert(t *testing.T) {
|
||||
func TestConnBeginBatchQueryRowInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
@@ -525,3 +525,52 @@ func TestConnBeginBatchSelectInsert(t *testing.T) {
|
||||
|
||||
ensureConnValid(t, conn)
|
||||
}
|
||||
|
||||
func TestConnBeginBatchQueryPartialReadInsert(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
defer closeConn(t, conn)
|
||||
|
||||
sql := `create temporary table ledger(
|
||||
id serial primary key,
|
||||
description varchar not null,
|
||||
amount int not null
|
||||
);`
|
||||
mustExec(t, conn, sql)
|
||||
|
||||
batch := conn.BeginBatch()
|
||||
batch.Queue("select 1 union all select 2 union all select 3",
|
||||
nil,
|
||||
nil,
|
||||
[]int16{pgx.BinaryFormatCode},
|
||||
)
|
||||
batch.Queue("insert into ledger(description, amount) values($1, $2),($1, $2)",
|
||||
[]interface{}{"q1", 1},
|
||||
[]pgtype.OID{pgtype.VarcharOID, pgtype.Int4OID},
|
||||
nil,
|
||||
)
|
||||
|
||||
err := batch.Send(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rows, err := batch.QueryResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
ct, err := batch.ExecResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if ct.RowsAffected() != 2 {
|
||||
t.Errorf("ct.RowsAffected() => %v, want %v", ct.RowsAffected, 2)
|
||||
}
|
||||
|
||||
batch.Close()
|
||||
|
||||
ensureConnValid(t, conn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user