Skip to content

Commit

Permalink
Merge #123562
Browse files Browse the repository at this point in the history
123562: sql: fix error with DROP VALUE and DROP SCHEMA in same txn r=rafiss a=rafiss

fixes #123539
fixes #123488
fixes #121828
fixes #122785

Release note (bug fix): Fixed a bug that would occur when `ALTER TYPE ... DROP VALUE` is followed by `DROP SCHEMA CASCADE ...` in the same transaction. Previously, the `ALTER TYPE` schema change would get queued up to run at commit time, but by that point, the type may have already been removed, so the commit could fail. This is fixed now.

Co-authored-by: Rafi Shamim <rafi@cockroachlabs.com>
  • Loading branch information
craig[bot] and rafiss committed May 3, 2024
2 parents bc8714c + dc0816e commit 98055f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/sql/drop_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ func (p *planner) dropTypeImpl(
return err
}

// For this type descriptor, delete queued schema change jobs from the job
// cache. If the job had already been scheduled, we would also need to mark
// these jobs as successful, but that should never happen for types.
//
// Note that we still wait for jobs removed from the cache to finish running
// after the transaction, since they're not removed from the jobsCollection.
// Also, changes made here do not affect schema change jobs created in this
// transaction with no mutation ID; they remain in the cache, and will be
// updated when writing the job record to drop the table.
delete(p.ExtendedEvalContext().jobs.uniqueToCreate, typeDesc.ID)

if err := p.removeBackRefsFromAllTypesInType(ctx, typeDesc); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/jobs_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type txnJobsCollection struct {
// job for a relation in one transaction. These jobs will be created and
// queued at commit time.
uniqueToCreate map[descpb.ID]*jobs.Record
// uniqueToCreate contains job records that are not unique to a descriptor
// nonUniqueToCreate contains job records that are not unique to a descriptor
// IDs. These jobs will be created and queued at commit time.
nonUniqueToCreate []*jobs.Record
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/drop_schema
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,18 @@ CREATE DATABASE test2

statement error pq: cannot drop schema "public"
DROP SCHEMA test2.public

# Regression test for ALTER TYPE ... DROP VALUE followed by DROP SCHEMA CASCADE.
# The type schema change should never be executed, since the DROP SCHEMA would
# have already removed the type.
statement ok
CREATE SCHEMA schema_123539;

statement ok
CREATE TYPE schema_123539.enum_123539 AS ENUM ('s', 't');

statement ok
BEGIN;
ALTER TYPE schema_123539.enum_123539 DROP VALUE 's';
DROP SCHEMA schema_123539 CASCADE;
COMMIT;

0 comments on commit 98055f0

Please sign in to comment.