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: DROP DATABASE CASCADE leaves tables which utilize sequences #50997

Closed
ajwerner opened this issue Jul 6, 2020 · 7 comments · Fixed by #51813
Closed

sql: DROP DATABASE CASCADE leaves tables which utilize sequences #50997

ajwerner opened this issue Jul 6, 2020 · 7 comments · Fixed by #51813
Assignees
Labels
A-schema-descriptors Relating to SQL table/db descriptor handling. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. S-3-wrong-metadata Issues causing erroneous metadata or monitoring stats to be returned.

Comments

@ajwerner
Copy link
Contributor

ajwerner commented Jul 6, 2020

Describe the problem

It seems that tables which utilize sequences are not properly dropped. It is interesting to note that this is only a problem if the sequence is also in the database being dropped. If the sequence is part of a different database, it will not be dropped.

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).

// filterCascadedTables takes a list of table descriptors and removes any
// descriptors from the list that are dependent on other descriptors in the
// list (e.g. if view v1 depends on table t1, then v1 will be filtered from
// the list).
func (p *planner) filterCascadedTables(ctx context.Context, tables []toDelete) ([]toDelete, error) {

To Reproduce

Set up CockroachDB cluster using v20.1.3 or earlier in the 20.1 release and do the following:

CREATE DATABASE good_stuff;
CREATE TABLE good_stuff.data_to_backup ();

SET experimental_serial_normalization = 'sql_sequence';
CREATE DATABASE to_corrupt;
USE to_corrupt;
CREATE TABLE uses_sequence ( k SERIAL PRIMARY KEY );

USE good_stuff;
DROP DATABASE to_corrupt CASCADE;
BACKUP DATABASE good_stuff to 'nodelocal://1/this_wont_work';

ERROR: table "uses_sequence" has unknown ParentID 57

At this point we'll have failed to drop the table uses_sequence and manually repairing it is painful.

Expected behavior
The drop will work.

Additional context

It's not yet clear whether this bug affects earlier versions.

@blathers-crl
Copy link

blathers-crl bot commented Jul 6, 2020

Hi @ajwerner, I've guessed the C-ategory of your issue and suitably labeled it. Please re-label if inaccurate.

While you're here, please consider adding an A- label to help keep our repository tidy.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Jul 6, 2020
@ajwerner
Copy link
Contributor Author

ajwerner commented Jul 6, 2020

@arulajmani if you'd be willing to pick this up, that'd be great!

@ajwerner ajwerner added the A-schema-descriptors Relating to SQL table/db descriptor handling. label Jul 6, 2020
@knz knz added S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. S-3-wrong-metadata Issues causing erroneous metadata or monitoring stats to be returned. labels Jul 6, 2020
@knz
Copy link
Contributor

knz commented Jul 6, 2020

Nice catch. I'm sorry I didn't catch this last time I was spelunking in that area.

arulajmani added a commit to arulajmani/cockroach that referenced this issue Jul 23, 2020
There were a few things going on here because of the way we filtered
tables during `DROP DATABASE CASCADE`. The intention was to filter
out objects that depended on other objects also being deleted (and would
therefore be deleted by the CASCADE nature of object drops). This
assumption is correct for table-view and view-view dependencies -- but
not for sequences. We also switched from individual schema change jobs
during a database drop to a single job which doesn't play well with
this filtering -- as no new jobs are queued, all objects that were
filtered would leave orphaned namespace/descriptor entries behind.
It's interesting to note that the filtering didn't directly cause this
issue, it just made the underlying issue more visible -- the single drop
database schema change job relies on knowing about all object
descriptors that need to be dropped upfront. Orphaned entries which
would have only occured for cross-database dependencies can occur in
the same database because of the filtering. This leads to why the fix
is what it is.

As part of this patch, I've tried to make the preperation steps for
dropping databases more explicit. First, we accumalate all objects
that need to be deleted. This includes objects that will be implicitly
deleted, such as owned sequences, dependent views (both in the database
being deleted and other databases that aren't being deleted). The
schema change job uses this new list to ensure entries are appropriately
cleaned up. We still perform the filter step as before to identify
objects which are the "root" of a drop and only call drop on these
objects. The only change here is that instead of accumulating dependent
objects, we explicitly accumulate cascading views.

Fixes cockroachdb#51782
Fixes cockroachdb#50997

Release note (bug fix): Before this change, we would leave orphaned
system.namespace/system.descriptor entries if we ran a
`DROP DATABASE CASCADE` and the database contained "dependency"
relations. For example, if the database included a view which
depended on a table in the database, dropping the database would result
in an orphaned entry for the view. Same thing for a sequence that was
used by a table in the database. (See cockroachdb#51782 for repro steps). This bug
is now fixed, and cleanup happens as expected.
arulajmani added a commit to arulajmani/cockroach that referenced this issue Jul 24, 2020
There were a few things going on here because of the way we filtered
tables during `DROP DATABASE CASCADE`. The intention was to filter
out objects that depended on other objects also being deleted (and would
therefore be deleted by the CASCADE nature of object drops). This
assumption is correct for table-view and view-view dependencies -- but
not for sequences. We also switched from individual schema change jobs
during a database drop to a single job which doesn't play well with
this filtering -- as no new jobs are queued, all objects that were
filtered would leave orphaned namespace/descriptor entries behind.
It's interesting to note that the filtering didn't directly cause this
issue, it just made the underlying issue more visible -- the single drop
database schema change job relies on knowing about all object
descriptors that need to be dropped upfront. Orphaned entries which
would have only occured for cross-database dependencies can occur in
the same database because of the filtering. This leads to why the fix
is what it is.

As part of this patch, I've tried to make the preperation steps for
dropping databases more explicit. First, we accumalate all objects
that need to be deleted. This includes objects that will be implicitly
deleted, such as owned sequences, dependent views (both in the database
being deleted and other databases that aren't being deleted). The
schema change job uses this new list to ensure entries are appropriately
cleaned up. We still perform the filter step as before to identify
objects which are the "root" of a drop and only call drop on these
objects. The only change here is that instead of accumulating dependent
objects, we explicitly accumulate cascading views.

Fixes cockroachdb#51782
Fixes cockroachdb#50997

Release note (bug fix): Before this change, we would leave orphaned
system.namespace/system.descriptor entries if we ran a
`DROP DATABASE CASCADE` and the database contained "dependency"
relations. For example, if the database included a view which
depended on a table in the database, dropping the database would result
in an orphaned entry for the view. Same thing for a sequence that was
used by a table in the database. (See cockroachdb#51782 for repro steps). This bug
is now fixed, and cleanup happens as expected.
craig bot pushed a commit that referenced this issue Jul 24, 2020
51813: sql: correctly identify and cleanup dependent objects in drop database r=ajwerner a=arulajmani

There were a few things going on here because of the way we filtered
tables during `DROP DATABASE CASCADE`. The intention was to filter
out objects that depended on other objects also being deleted (and would
therefore be deleted by the CASCADE nature of object drops). This
assumption is correct for table-view and view-view dependencies -- but
not for sequences. We also switched from individual schema change jobs
during a database drop to a single job which doesn't play well with
this filtering -- as no new jobs are queued, all objects that were
filtered would leave orphaned namespace/descriptor entries behind.
It's interesting to note that the filtering didn't directly cause this
issue, it just made the underlying issue more visible -- the single drop
database schema change job relies on knowing about all object
descriptors that need to be dropped upfront. Orphaned entries which
would have only occured for cross-database dependencies can occur in
the same database because of the filtering. This leads to why the fix
is what it is.

As part of this patch, I've tried to make the preperation steps for
dropping databases more explicit. First, we accumalate all objects
that need to be deleted. This includes objects that will be implicitly
deleted, such as owned sequences, dependent views (both in the database
being deleted and other databases that aren't being deleted). The
schema change job uses this new list to ensure entries are appropriately
cleaned up. We still perform the filter step as before to identify
objects which are the "root" of a drop and only call drop on these
objects. The only change here is that instead of accumulating dependent
objects, we explicitly accumulate cascading views.

Fixes #51782
Fixes #50997

Release note (bug fix): Before this change, we would leave orphaned
system.namespace/system.descriptor entries if we ran a
`DROP DATABASE CASCADE` and the database contained "dependency"
relations. For example, if the database included a view which
depended on a table in the database, dropping the database would result
in an orphaned entry for the view. Same thing for a sequence that was
used by a table in the database. (See #51782 for repro steps). This bug
is now fixed, and cleanup happens as expected.

Co-authored-by: arulajmani <[email protected]>
@craig craig bot closed this as completed in 5063b61 Jul 24, 2020
arulajmani added a commit to arulajmani/cockroach that referenced this issue Jul 24, 2020
There were a few things going on here because of the way we filtered
tables during `DROP DATABASE CASCADE`. The intention was to filter
out objects that depended on other objects also being deleted (and would
therefore be deleted by the CASCADE nature of object drops). This
assumption is correct for table-view and view-view dependencies -- but
not for sequences. We also switched from individual schema change jobs
during a database drop to a single job which doesn't play well with
this filtering -- as no new jobs are queued, all objects that were
filtered would leave orphaned namespace/descriptor entries behind.
It's interesting to note that the filtering didn't directly cause this
issue, it just made the underlying issue more visible -- the single drop
database schema change job relies on knowing about all object
descriptors that need to be dropped upfront. Orphaned entries which
would have only occured for cross-database dependencies can occur in
the same database because of the filtering. This leads to why the fix
is what it is.

As part of this patch, I've tried to make the preperation steps for
dropping databases more explicit. First, we accumalate all objects
that need to be deleted. This includes objects that will be implicitly
deleted, such as owned sequences, dependent views (both in the database
being deleted and other databases that aren't being deleted). The
schema change job uses this new list to ensure entries are appropriately
cleaned up. We still perform the filter step as before to identify
objects which are the "root" of a drop and only call drop on these
objects. The only change here is that instead of accumulating dependent
objects, we explicitly accumulate cascading views.

Fixes cockroachdb#51782
Fixes cockroachdb#50997

Release note (bug fix): Before this change, we would leave orphaned
system.namespace/system.descriptor entries if we ran a
`DROP DATABASE CASCADE` and the database contained "dependency"
relations. For example, if the database included a view which
depended on a table in the database, dropping the database would result
in an orphaned entry for the view. Same thing for a sequence that was
used by a table in the database. (See cockroachdb#51782 for repro steps). This bug
is now fixed, and cleanup happens as expected.
@sitapriyanka
Copy link

Still this issue persists. I am facing this issue in my local after using drop database cascade.
cockroach-v20.2.0-alpha.2.windows-6.2-amd64
i am using wondows 10 enterprise.

@knz knz reopened this Aug 27, 2020
@knz
Copy link
Contributor

knz commented Aug 27, 2020

Arul can you interact with @sitapriyanka to get more data about the situation? Or maybe have support look into it? We probably want to iron this out thoroughly before the release.

@arulajmani
Copy link
Collaborator

arulajmani commented Aug 27, 2020

I don't think this change made alpha2. The release issue's SHA is from 7/21 and the PR that fixed this issue went in on 7/24. I ran the steps listed in the issue description above and this is indeed fixed on master, so this should be fixed in alpha3.

@sitapriyanka could you please elaborate on the steps you took to run into this issue? I just wanna make sure you're not getting into a bad state even after the fix.

@solongordon
Copy link
Contributor

@sitapriyanka I'm closing this for now but please comment if this is still an issue in alpha3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-descriptors Relating to SQL table/db descriptor handling. C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. S-3-erroneous-edge-case Database produces or stores erroneous data without visible error/warning, in rare edge cases. S-3-wrong-metadata Issues causing erroneous metadata or monitoring stats to be returned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants