Skip to content

Commit

Permalink
sql/schemachanger: block legacy schema changer operations earlier
Browse files Browse the repository at this point in the history
Previously, it was possible for ADD/DROP CONSTRAINT and
other declarative schema changer operations to collide with
DROP TABLE in the legacy schema changer, since our gate
was not early enough. So, some work would be done before
the schema change was blocked with a retry error.
This patch prevents DROP TABLE/VIEW/SEQUENCE from
executing until a declarative schema change on such
objects is completed earlier.

Fixes: #99379

Release note: None
  • Loading branch information
fqazi committed Mar 23, 2023
1 parent 909e50a commit ac74d8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pkg/sql/drop_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/iterutil"
Expand Down Expand Up @@ -83,6 +84,12 @@ func (n *dropSequenceNode) startExec(params runParams) error {
ctx := params.ctx
for _, toDel := range n.td {
droppedDesc := toDel.desc
// Exit early with an error if the table is undergoing a declarative schema
// change, before we try to get job IDs and update job statuses later. See
// createOrUpdateSchemaChangeJob.
if catalog.HasConcurrentDeclarativeSchemaChange(droppedDesc) {
return scerrors.ConcurrentSchemaChangeError(droppedDesc)
}
err := params.p.dropSequenceImpl(
ctx, droppedDesc, true /* queueJob */, tree.AsStringWithFQNames(n.n, params.Ann()), n.n.DropBehavior,
)
Expand Down
14 changes: 6 additions & 8 deletions pkg/sql/drop_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ func (p *planner) dropTableImpl(
behavior tree.DropBehavior,
) ([]string, error) {
var droppedViews []string

// Exit early with an error if the table is undergoing a declarative schema
// change, before we try to get job IDs and update job statuses later. See
// createOrUpdateSchemaChangeJob.
if catalog.HasConcurrentDeclarativeSchemaChange(tableDesc) {
return nil, scerrors.ConcurrentSchemaChangeError(tableDesc)
}
// Remove foreign key back references from tables that this table has foreign
// keys to.
// Copy out the set of outbound fks as it may be overwritten in the loop.
Expand Down Expand Up @@ -353,13 +358,6 @@ func (p *planner) initiateDropTable(
return errors.Errorf("table %q is already being dropped", tableDesc.Name)
}

// Exit early with an error if the table is undergoing a declarative schema
// change, before we try to get job IDs and update job statuses later. See
// createOrUpdateSchemaChangeJob.
if catalog.HasConcurrentDeclarativeSchemaChange(tableDesc) {
return scerrors.ConcurrentSchemaChangeError(tableDesc)
}

// Use the delayed GC mechanism to schedule usage of the more efficient
// ClearRange pathway.
if tableDesc.IsTable() {
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/drop_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -236,6 +237,12 @@ func (p *planner) dropViewImpl(
) ([]string, error) {
var cascadeDroppedViews []string

// Exit early with an error if the table is undergoing a declarative schema
// change, before we try to get job IDs and update job statuses later. See
// createOrUpdateSchemaChangeJob.
if catalog.HasConcurrentDeclarativeSchemaChange(viewDesc) {
return nil, scerrors.ConcurrentSchemaChangeError(viewDesc)
}
// Remove back-references from the tables/views this view depends on.
dependedOn := append([]descpb.ID(nil), viewDesc.DependsOn...)
for _, depID := range dependedOn {
Expand Down

0 comments on commit ac74d8c

Please sign in to comment.