Skip to content

Commit

Permalink
sql: add a test for not doing retries if results were sent
Browse files Browse the repository at this point in the history
This test ensures that we do not do per-statement retries for READ
COMMITTED transactions if results were already sent to the client.

Release note: None
  • Loading branch information
rafiss committed Aug 16, 2023
1 parent 6ac84c4 commit 4e7f87e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/sql/conn_executor_exec.go
Expand Up @@ -1559,7 +1559,7 @@ func (ex *connExecutor) dispatchReadCommittedStmtToExecutionEngine(
// retry the statement.
res.SetError(errors.Wrapf(
maybeRetriableErr,
"cannot retry since some results were already sent to the client",
"cannot automatically retry since some results were already sent to the client",
))
break
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/sql/conn_executor_test.go
Expand Up @@ -1554,6 +1554,38 @@ func TestInjectRetryErrors(t *testing.T) {
require.NoError(t, tx.Rollback())
require.Equal(t, 2, readCommittedStmtRetries)
})

t.Run("read_committed_txn_already_sent_results", func(t *testing.T) {
readCommittedStmtRetries = 0

// Choose a small results_buffer_size and make sure the statement retry
// does not occur.
pgURL, cleanupFn := sqlutils.PGUrl(
t, s.AdvSQLAddr(), t.Name(), url.User(username.RootUser))
defer cleanupFn()
q := pgURL.Query()
q.Add("results_buffer_size", "4")
pgURL.RawQuery = q.Encode()
smallBufferDB, err := gosql.Open("postgres", pgURL.String())
require.NoError(t, err)
defer smallBufferDB.Close()

_, err = smallBufferDB.Exec("SET inject_retry_errors_enabled = 'true'")
require.NoError(t, err)

tx, err := smallBufferDB.BeginTx(ctx, &gosql.TxOptions{Isolation: gosql.LevelReadCommitted})
require.NoError(t, err)

var txRes int
err = tx.QueryRow("SELECT $1::int8", 3).Scan(&txRes)
require.Error(t, err)
pqErr := (*pq.Error)(nil)
require.ErrorAs(t, err, &pqErr)
require.Equal(t, "40001", string(pqErr.Code), "expected a transaction retry error code. got %v", pqErr)
require.ErrorContains(t, pqErr, "cannot automatically retry since some results were already sent to the client")
require.NoError(t, tx.Rollback())
require.Equal(t, 0, readCommittedStmtRetries)
})
}

func TestInjectRetryOnCommitErrors(t *testing.T) {
Expand Down

0 comments on commit 4e7f87e

Please sign in to comment.