Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-22.2: sql: handle AOST in multi-stmt implicit txn #97146

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ccl/logictestccl/testdata/logic_test/as_of
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BEGIN AS OF SYSTEM TIME follower_read_timestamp(); SELECT * FROM t
statement ok
ROLLBACK

statement error pgcode 0A000 cannot specify AS OF SYSTEM TIME with different timestamps
statement error pgcode 0A000 inconsistent AS OF SYSTEM TIME timestamp
SELECT * from t AS OF SYSTEM TIME '-1μs'; SELECT * from t AS OF SYSTEM TIME '-2μs'

statement ok
Expand Down
20 changes: 12 additions & 8 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,10 @@ func (ex *connExecutor) execStmtInOpenState(
p.stmt = stmt
p.cancelChecker.Reset(ctx)

// Auto-commit is disallowed during statement execution, if we previously executed any DDL.
// This is because may potentially create jobs and do other operations rather than
// a KV commit. Insteadand carry out any extra operations needed for DDL.he auto-connection executor will commit after this statement,
// in this scenario.
// This prevents commit during statement execution, but the connection executor,
// Auto-commit is disallowed during statement execution if we previously
// executed any DDL. This is because may potentially create jobs and do other
// operations rather than a KV commit.
// This prevents commit during statement execution, but the conn_executor
// will still commit this transaction after this statement executes.
p.autoCommit = canAutoCommit &&
!ex.server.cfg.TestingKnobs.DisableAutoCommitDuringExec && ex.extraTxnState.numDDL == 0
Expand Down Expand Up @@ -760,7 +759,10 @@ func (ex *connExecutor) handleAOST(ctx context.Context, stmt tree.Statement) err
if asOf == nil {
return nil
}
if ex.implicitTxn() {

// Implicit transactions can have multiple statements, so we need to check
// if one has already been executed.
if ex.implicitTxn() && !ex.extraTxnState.firstStmtExecuted {
if p.extendedEvalCtx.AsOfSystemTime == nil {
p.extendedEvalCtx.AsOfSystemTime = asOf
if !asOf.BoundedStaleness {
Expand Down Expand Up @@ -801,9 +803,11 @@ func (ex *connExecutor) handleAOST(ctx context.Context, stmt tree.Statement) err
)
}
if readTs := ex.state.getReadTimestamp(); asOf.Timestamp != readTs {
err = pgerror.Newf(pgcode.Syntax,
err = pgerror.Newf(pgcode.FeatureNotSupported,
"inconsistent AS OF SYSTEM TIME timestamp; expected: %s, got: %s", readTs, asOf.Timestamp)
err = errors.WithHint(err, "try SET TRANSACTION AS OF SYSTEM TIME")
if !ex.implicitTxn() {
err = errors.WithHint(err, "try SET TRANSACTION AS OF SYSTEM TIME")
}
return err
}
p.extendedEvalCtx.AsOfSystemTime = asOf
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/as_of
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,14 @@ SET TRANSACTION AS OF system time '-1s'

statement ok
ROLLBACK

# Check that AOST in multi-stmt implicit transaction has the proper error.
statement error inconsistent AS OF SYSTEM TIME timestamp
insert into t values(1); select * from t as of system time '-1s';

statement error inconsistent AS OF SYSTEM TIME timestamp
select * from t as of system time '-1s'; select * from t as of system time '-2s';

# Specifying the AOST in the first statement (and no others) is allowed.
statement ok
select * from t as of system time '-1s'; select * from t;