sql: fix auto-retry behavior with autocommit_before_ddl #141116
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the autocommit_before_ddl setting was enabled, we were
sending a notice to the client without any buffering. This prevents
auto-retry logic for retriable errors from kicking in, since if results
have already sent to the client, it's not safe to retry the current
statement.
The fix is to buffer the notice for sending instead. It will get sent
whenever results are flushed back to the client. In order to achieve
this, I introduced a new BufferClientNoticeInConnection function. The
existing BufferClientNotice function is not sufficient since that only
buffers notices in the command result, and that buffer is discarded due
to how the connExecutor state transitions are defined. Namely, the
connExecutor will execute the schema change command twice: once to
autocommit the current transaction, and another time to run the schema
change. To avoid losing the notice from the autocommit, it must be
buffered all the way into the client connection, and not just the
command result.
Running with this patch makes logic tests less flaky, since schema
changes that encounter retryable errors are way more likely to be able
to be automatically retried now. This allows us to remove the testing
knob that overrode the transaction liveness threshold. I verified the
flakiness is gone by using:
informs #133180
Release note (bug fix): Fixed a bug that prevented transaction retry
errors encountered during implicit transactions from being automatically
retried internally if the autocommit_before_ddl session variable was
enabled and the statement was a schema change.