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

sql: resolve databases in high-priority transactions #46384

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
17 changes: 17 additions & 0 deletions pkg/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
Expand Down Expand Up @@ -178,6 +179,14 @@ func (dc *databaseCache) getDatabaseDesc(
}
if desc == nil {
if err := txnRunner(ctx, func(ctx context.Context, txn *kv.Txn) error {
// Run the descriptor read as high-priority, thereby pushing any intents out
// of its way. We don't want schema changes to prevent database lookup;
// we'd rather force them to refresh. Also this prevents deadlocks in cases
// where the name resolution is triggered by the transaction doing the
// schema change itself.
if err := txn.SetUserPriority(roachpb.MaxUserPriority); err != nil {
return err
}
a := UncachedPhysicalAccessor{}
desc, err = a.GetDatabaseDesc(ctx, txn, name,
tree.DatabaseLookupFlags{Required: required})
Expand Down Expand Up @@ -222,6 +231,14 @@ func (dc *databaseCache) getDatabaseID(
}
if dbID == sqlbase.InvalidID {
if err := txnRunner(ctx, func(ctx context.Context, txn *kv.Txn) error {
// Run the namespace read as high-priority, thereby pushing any intents out
// of its way. We don't want schema changes to prevent database acquisitions;
// we'd rather force them to refresh. Also this prevents deadlocks in cases
// where the name resolution is triggered by the transaction doing the
// schema change itself.
if err := txn.SetUserPriority(roachpb.MaxUserPriority); err != nil {
return err
}
var err error
dbID, err = getDatabaseID(ctx, txn, name, required)
return err
Expand Down
13 changes: 12 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/schema_change_in_txn
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ COMMIT
# namespace entries could block the schema resolution after the rollback (schema
# resolution uses different transactions to do its reads). We've fixed it by having those
# other transactions run at high priority, thus pushing the intents out of their way.
subtest no_schemachange_deadlock_after_savepoint_rollback
subtest no_table_schemachange_deadlock_after_savepoint_rollback

statement ok
begin; savepoint s; create table t(x int); rollback to savepoint s;
Expand All @@ -1787,3 +1787,14 @@ select * from t;

statement ok
commit;

subtest no_database_schemachange_deadlock_after_savepoint_rollback

statement ok
begin; savepoint s; create database d46224; rollback to savepoint s;

query error relation "d46224.t" does not exist
select * from d46224.t;

statement ok
commit;