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: dropping a sequence with a sequence owners corrupts the table that owns the sequence #50649

Closed
jordanlewis opened this issue Jun 25, 2020 · 7 comments · Fixed by #50665
Closed
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-1 High impact: many users impacted, serious risk of high unavailability or data loss

Comments

@jordanlewis
Copy link
Member

Repro:

root@:26257/defaultdb> create table a (a int primary key);
CREATE TABLE

Time: 49.454ms

root@:26257/defaultdb> create sequence foo owned by a.a;
CREATE SEQUENCE

Time: 162.234ms

root@:26257/defaultdb> drop sequence foo;
DROP SEQUENCE

Time: 251.705ms

root@:26257/defaultdb> drop table a;
ERROR: table is being dropped

It looks like there is no code to remove the sequence owner ID from the column descriptor that owns the sequence when the sequence is dropped.

@jordanlewis jordanlewis added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-1 High impact: many users impacted, serious risk of high unavailability or data loss labels Jun 25, 2020
@rohany
Copy link
Contributor

rohany commented Jun 25, 2020

Huh, @RichardJCai found this a bit ago -- #48249

@RichardJCai
Copy link
Contributor

RichardJCai commented Jun 25, 2020

Huh, @RichardJCai found this a bit ago -- #48249

I didn't realize that it actually prevents dropping the table that owns the sequence after.

arulajmani added a commit to arulajmani/cockroach that referenced this issue Jun 25, 2020
Previously, when a sequence that was owned by a column was being
dropped, we would not remove the sequence ID from the column descriptor
of the column that owned it. As a result, there was a bug where if the
sequence was dropped manually before the table, it would be impossible
to drop the table.

This patch addresses this problem by removing the ownership dependency
on sequence drops.

Fixes cockroachdb#50649

Release note (bug fix): there was a bug previously where if a user
created a sequence owned by a table's column and dropped the sequence,
it would become impossible to drop the table after. This is now fixed.
See attached issue for repro steps.
craig bot pushed a commit that referenced this issue Jun 25, 2020
50665: sql: remove sequence ownership dependency when dropping sequences r=solongordon a=arulajmani

Previously, when a sequence that was owned by a column was being
dropped, we would not remove the sequence ID from the column descriptor
of the column that owned it. As a result, there was a bug where if the
sequence was dropped manually before the table, it would be impossible
to drop the table.

This patch addresses this problem by removing the ownership dependency
on sequence drops.

Fixes #50649

Release note (bug fix): there was a bug previously where if a user
created a sequence owned by a table's column and dropped the sequence,
it would become impossible to drop the table after. This is now fixed.
See attached issue for repro steps.

Co-authored-by: arulajmani <[email protected]>
@craig craig bot closed this as completed in fe78658 Jun 25, 2020
arulajmani added a commit to arulajmani/cockroach that referenced this issue Jun 26, 2020
Previously, when a sequence that was owned by a column was being
dropped, we would not remove the sequence ID from the column descriptor
of the column that owned it. As a result, there was a bug where if the
sequence was dropped manually before the table, it would be impossible
to drop the table.

This patch addresses this problem by removing the ownership dependency
on sequence drops.

Fixes cockroachdb#50649

Release note (bug fix): there was a bug previously where if a user
created a sequence owned by a table's column and dropped the sequence,
it would become impossible to drop the table after. This is now fixed.
See attached issue for repro steps.
@thoszhang
Copy link
Contributor

I'm reopening the issue to discuss how we want to get users' table descriptors out of this state, if they're already in it. This problem (dropped/nonexistent sequence descriptors) is a special case of #50651, but it seems worth bringing up separately.

Is there anything targeted we can do to avoid this failure if we hit a dropped/nonexistent sequence while dropping a column or table? Based on a very cursory look at the code, I was wondering if we can just check for ErrDescriptorNotFound and not return an error in one of these places (not sure which one is the problem):

seqLookup, err := p.LookupTableByID(ctx, sequenceID)

seqDesc, err := p.Tables().GetMutableTableVersionByID(ctx, sequenceID, p.txn)

@ajwerner
Copy link
Contributor

ajwerner commented Jul 2, 2020

We've got a related and big problem which is sort of the opposite. Dropping a database which has tables which utilizes sequences corrupts, well, everything. I'll be creating another issue to track that but it's bad.

@arulajmani
Copy link
Collaborator

I looked into the issue of dropping databases which have tables that utilize sequences and it seems like the table that uses the sequence isn't getting dropped. I don't think that problem is related to this issue though, as there is no "ownership" in that case.

The root cause of that issue seems to be filterCascadedTables, which seems to be designed for table-view / view-view dependency relationships so that we don't try to unnecessarily drop views that would be dropped in other table/view's drop cascade. This doesn't seem to play well with sequences, which don't drop the tables that depend on them during cascade (aside we currently don't support cascade for sequences).

I think we can fix this by ensuring filterCascadedTables skips sequences when accumulating dependencies, so that it returns both the table descriptor for the sequence that uses it and the sequence itself so that they both can be cleaned up. Feel free to assign me the issue, I'm happy to take it on!

@ajwerner
Copy link
Contributor

ajwerner commented Jul 6, 2020

@arulajmani thanks for taking a look last week. I've written the issue up as #50997 and tentatively assigned it to you. Let me know if you run into any trouble or would like me to look.

@arulajmani
Copy link
Collaborator

I've opened #51170 for discussion about how to get users' table descriptors out of invalid states if they're already in it. Fixes for a few issues could get users' into invalid states and I've tagged all of them in the issue above.

Closing this.

arulajmani added a commit to arulajmani/cockroach that referenced this issue Jul 21, 2020
Previously, when a sequence that was owned by a column was being
dropped, we would not remove the sequence ID from the column descriptor
of the column that owned it. As a result, there was a bug where if the
sequence was dropped manually before the table, it would be impossible
to drop the table.

This patch addresses this problem by removing the ownership dependency
on sequence drops.

Fixes cockroachdb#50649

Release note (bug fix): there was a bug previously where if a user
created a sequence owned by a table's column and dropped the sequence,
it would become impossible to drop the table after. This is now fixed.
See attached issue for repro steps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-1 High impact: many users impacted, serious risk of high unavailability or data loss
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants