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: fix drop database - sequence ownership bug #50744

Merged
merged 1 commit into from
Jul 6, 2020
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/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (n *alterTableNode) startExec(params runParams) error {
return err
}

if err := params.p.dropSequencesOwnedByCol(params.ctx, colToDrop); err != nil {
if err := params.p.dropSequencesOwnedByCol(params.ctx, colToDrop, true /* queueJob */); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/drop_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (p *planner) dropTableImpl(

// Drop sequences that the columns of the table own
for _, col := range tableDesc.Columns {
if err := p.dropSequencesOwnedByCol(ctx, &col); err != nil {
if err := p.dropSequencesOwnedByCol(ctx, &col, queueJob); err != nil {
return droppedViews, err
}
}
Expand Down Expand Up @@ -338,7 +338,7 @@ func (p *planner) initiateDropTable(
drainName bool,
) error {
if tableDesc.Dropped() {
return fmt.Errorf("table %q is being dropped", tableDesc.Name)
return errors.Errorf("table %q is already being dropped", tableDesc.Name)
}

// If the table is not interleaved , use the delayed GC mechanism to
Expand Down
87 changes: 87 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/sequences
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# LogicTest: !3node-tenant(50840)
# see also files `drop_sequence`, `alter_sequence`, `rename_sequence`

# USING THE `lastval` FUNCTION
Expand Down Expand Up @@ -1095,3 +1096,89 @@ DROP SEQUENCE seq_50649

statement ok
DROP TABLE t_50649

subtest regression_50712

statement ok
CREATE DATABASE db_50712

statement ok
CREATE TABLE db_50712.t_50712(a INT PRIMARY KEY)

statement ok
CREATE SEQUENCE db_50712.seq_50712 OWNED BY db_50712.t_50712.a

statement ok
DROP DATABASE db_50712 CASCADE

# Same test like above, except the table is lexicographically less than the
# sequence, which results in drop database dropping the table before the
# sequence.
statement ok
CREATE DATABASE db_50712

statement ok
CREATE TABLE db_50712.a_50712(a INT PRIMARY KEY)

statement ok
CREATE SEQUENCE db_50712.seq_50712 OWNED BY db_50712.a_50712.a

statement ok
DROP DATABASE db_50712 CASCADE

# Same test like above, except the db is switched as the current db
statement ok
CREATE DATABASE db_50712

statement ok
SET DATABASE = db_50712

statement ok
CREATE TABLE a_50712(a INT PRIMARY KEY)

statement ok
CREATE SEQUENCE seq_50712 OWNED BY a_50712.a

statement ok
DROP DATABASE db_50712

statement ok
SET DATABASE = test

# Tests db drop.
# Sequence: outside db.
# Owner: inside db.
# The sequence should be automatically dropped.
statement ok
CREATE DATABASE db_50712

statement ok
CREATE TABLE db_50712.t_50712(a INT PRIMARY KEY)

statement ok
CREATE SEQUENCE seq_50712 OWNED BY db_50712.t_50712.a

statement ok
DROP DATABASE db_50712 CASCADE

statement error pq: relation "seq_50712" does not exist
SELECT * FROM seq_50712

# Tests db drop.
# Sequence: inside db
# Owner: outside db
# It should be possible to drop the table later.
statement ok
CREATE DATABASE db_50712

statement ok
CREATE TABLE t_50712(a INT PRIMARY KEY)

statement ok
CREATE SEQUENCE db_50712.seq_50712 OWNED BY t_50712.a

statement ok
DROP DATABASE db_50712 CASCADE

statement ok
DROP TABLE t_50712
13 changes: 11 additions & 2 deletions pkg/sql/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func removeSequenceOwnerIfExists(
if err != nil {
return err
}
// If the table descriptor has already been dropped, there is no need to
// remove the reference.
if tableDesc.Dropped() {
return nil
}
col, err := tableDesc.FindColumnByID(opts.SequenceOwner.OwnerColumnID)
if err != nil {
return err
Expand Down Expand Up @@ -469,17 +474,21 @@ func maybeAddSequenceDependencies(
// dropSequencesOwnedByCol drops all the sequences from col.OwnsSequenceIDs.
// Called when the respective column (or the whole table) is being dropped.
func (p *planner) dropSequencesOwnedByCol(
ctx context.Context, col *sqlbase.ColumnDescriptor,
ctx context.Context, col *sqlbase.ColumnDescriptor, queueJob bool,
) error {
for _, sequenceID := range col.OwnsSequenceIds {
seqDesc, err := p.Tables().GetMutableTableVersionByID(ctx, sequenceID, p.txn)
if err != nil {
return err
}
// This sequence is already getting dropped. Don't do it twice.
if seqDesc.Dropped() {
continue
}
jobDesc := fmt.Sprintf("removing sequence %q dependent on column %q which is being dropped",
seqDesc.Name, col.ColName())
if err := p.dropSequenceImpl(
ctx, seqDesc, true /* queueJob */, jobDesc, tree.DropRestrict,
ctx, seqDesc, queueJob, jobDesc, tree.DropRestrict,
); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package sql

import (
"context"
"fmt"
"strings"

"github.com/cockroachdb/cockroach/pkg/clusterversion"
Expand Down Expand Up @@ -209,7 +208,8 @@ func (p *planner) writeSchemaChange(
}
if tableDesc.Dropped() {
// We don't allow schema changes on a dropped table.
return fmt.Errorf("table %q is being dropped", tableDesc.Name)
return errors.Errorf("no schema changes allowed on table %q as it is being dropped",
tableDesc.Name)
}
if err := p.createOrUpdateSchemaChangeJob(ctx, tableDesc, jobDesc, mutationID); err != nil {
return err
Expand All @@ -225,7 +225,8 @@ func (p *planner) writeSchemaChangeToBatch(
}
if tableDesc.Dropped() {
// We don't allow schema changes on a dropped table.
return fmt.Errorf("table %q is being dropped", tableDesc.Name)
return errors.Errorf("no schema changes allowed on table %q as it is being dropped",
tableDesc.Name)
}
return p.writeTableDescToBatch(ctx, tableDesc, b)
}
Expand Down