-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: re-vamp the view dependency analysis #17310
Conversation
66fa9dc
to
d2699e3
Compare
04fd2a6
to
646c05d
Compare
okay so:
|
I'm not sure dropping the view's dependencies and checking that the views don't work without them was really needed, but the test looks reasonable to me. Reviewed 2 of 33 files at r1. Comments from Reviewable |
pkg/migrations/migrations_test.go
Outdated
defer s.Stopper().Stop(ctx) | ||
e := s.Executor().(*sql.Executor) | ||
|
||
t.Run("create test tables", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me that these subtests are providing any value, since they can't be run independently of one another. Plus, each subtest creates its own session, which adds quite a bit of boilerplate. @knz, does this exercise some codepath that wouldn't be executed if you used the same session throughout?
pkg/migrations/migrations_test.go
Outdated
}) | ||
|
||
// Now, corrupt the descriptors by breaking their dependency information. | ||
t.Run("break descriptors", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E.g., this will segfault if someone runs make test PKG=./pkg/migration TESTS=TestUpdateViewDependenciesMigration/break_descriptors
. I feel like you might be better off with t.Logf("break descriptors")
.
646c05d
to
809b813
Compare
Test simplified thanks to generous suggestions by Jordan and Nikhil. Review status: 1 of 29 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed. pkg/migrations/migrations_test.go, line 539 at r1 (raw file): Previously, benesch (Nikhil Benesch) wrote…
Done. pkg/migrations/migrations_test.go, line 598 at r1 (raw file): Previously, benesch (Nikhil Benesch) wrote…
Done. Comments from Reviewable |
b5e81a7
to
285fb22
Compare
LGTM The second "fixes" in the PR comment links to a PR. Is that what you wanted? After we talked, I thought some more: for the future, I still think that having a plan construction mode where all optimizations are disabled, and then collecting the dependencies by visiting that, seems like a cleaner way than your proposed followed: the recording Review status: 0 of 19 files reviewed at latest revision, 11 unresolved discussions, all commit checks successful. pkg/sql/create.go, line 334 at r2 (raw file):
why is this a pointer? pkg/sql/create.go, line 382 at r2 (raw file):
I was confused about what the reference to pretty-printing is telling me. pkg/sql/create.go, line 398 at r2 (raw file):
nit: I don't know anything about anything, but this line suggests to me that pkg/sql/create.go, line 414 at r2 (raw file):
I don't understand what this does and how it's "requesting" anything. You're restoring pkg/sql/create.go, line 423 at r2 (raw file):
pkg/sql/create.go, line 423 at r2 (raw file):
isn't pkg/sql/create.go, line 464 at r2 (raw file):
bleah. I think we should move this function to another file that nobody needs to see, specific to migrations. pkg/sql/create.go, line 669 at r2 (raw file):
why didn't the ctor populate this field? Is it didn't know its own id? pkg/sql/planner.go, line 77 at r2 (raw file):
please expand the comment saying something about the responsibilities of the node ctors when this is non-nil: to record what descriptors they're using. And then get rid of this member soon :P Comments from Reviewable |
285fb22
to
660c16f
Compare
Review status: 0 of 19 files reviewed at latest revision, 11 unresolved discussions, all commit checks successful. pkg/sql/create.go, line 334 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Added a clarifying comment. pkg/sql/create.go, line 382 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Done. pkg/sql/create.go, line 398 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
I agree. Filed this separately as #17374. pkg/sql/create.go, line 414 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Yes! This is a general pattern since this planNode constructor is really a visitor, and there may be a parent with a different idea of what planDeps should be. We do the same with pkg/sql/create.go, line 423 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Read the code above :) pkg/sql/create.go, line 423 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Indeed. Changed to pkg/sql/create.go, line 464 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
I am going to refactor this code in the next review iteration to put all the view-related stuff in its own file. How does that sound? pkg/sql/create.go, line 669 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Explained in the comment above the definition of the field. pkg/sql/planner.go, line 77 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Done. Comments from Reviewable |
660c16f
to
19a8af5
Compare
Review status: 0 of 23 files reviewed at latest revision, 11 unresolved discussions. pkg/sql/create.go, line 464 at r2 (raw file): Previously, knz (kena) wrote…
Ok done already. Comments from Reviewable |
a9affb5
to
83f873c
Compare
Reviewed 1 of 33 files at r2. pkg/migration/sqlmigrations/migrations.go, line 420 at r3 (raw file):
this comment talks about all columns but pkg/migration/sqlmigrations/migrations.go, line 422 at r3 (raw file):
I don't know what this comment about the privileged user is telling me. Is it about this code, or an assumption about the caller? I don't see any use anywhere around here. pkg/migration/sqlmigrations/migrations.go, line 423 at r3 (raw file):
is this comment about retrying necessary (and talking about panicking at the wrong level - this code doesn't panic)? Nobody would be tempted to try something forever (particularly since this interface supports returning an error), would they? pkg/sql/create.go, line 414 at r2 (raw file): Previously, knz (kena) wrote…
ummm isn't this horrible? If there were a parent which cares about planDeps, wouldn't it also care about the deps accumulated under this view? I don't know the answer to that question, so: pkg/sql/create.go, line 669 at r2 (raw file): Previously, knz (kena) wrote…
which field? pkg/sql/views.go, line 35 at r3 (raw file):
well but what about if someone else wants to modify the descriptor. If there's no good reason, save me from thinking about anything and make a copy of the proto. This is just used for schema operations, right? So performance doesn't matter. pkg/sql/views.go, line 79 at r3 (raw file):
I spent a bunch of time being confused around here; I think this comment misses the point: what's important is not (only) that we use the current version of descriptors, but that the reads and writes of descriptors are transactional. The comment should reference Also, I think that function needs a comment about how the transaction would better have read the descriptor that it's writing. And to make it safe, I'd also change it to be a CPut and test the existing value. I'd also comment on the definition of We should also assert here that the transaction is SERIALIZABLE and refuse to allow creation of views in SNAPSHOT transactions :S Comments from Reviewable |
83f873c
to
a98b7f8
Compare
Thank you for your insightful comment, especially the very valuable insight on avoidCachedDescriptors. I will investigate this further, I think we may have some more bugs lurking. Review status: 0 of 23 files reviewed at latest revision, 12 unresolved discussions. pkg/migration/sqlmigrations/migrations.go, line 420 at r3 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Moved the comment to RecomputeViewDependencies where it belongs. pkg/migration/sqlmigrations/migrations.go, line 422 at r3 (raw file): Previously, andreimatei (Andrei Matei) wrote…
The comment was stale, removed it. Thanks for noticing. pkg/migration/sqlmigrations/migrations.go, line 423 at r3 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Yes you're right. I simplified the code instead. pkg/sql/create.go, line 414 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
I am going to respectfully and politely ignore this comment. This is really a common pattern in tree traversals. We can discuss more offline. pkg/sql/create.go, line 669 at r2 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Extended this comment to clarify. pkg/sql/views.go, line 35 at r3 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Peter and I thoroughly checked together that referenced descriptors are not being modified anywhere, see the discussion on #17309. pkg/sql/views.go, line 79 at r3 (raw file): Previously, andreimatei (Andrei Matei) wrote…
Aha! This very comment reminds me why I love it when you review a PR. I did not really understand what was this thing with "avoiding cached descriptors" before. Now I understand! Thank you! Also I understand that:
This is worth another commit on its own, which I might insert in-between master and this one. I will investigate further. Thank you for the patient explanation. Comments from Reviewable |
a98b7f8
to
ceaf228
Compare
ceaf228
to
724d983
Compare
The views in pg_catalog and information_schema provide introspection over the logical schema (pure SQL view) however when testing/debugging it is also important to see the descriptor IDs and also other fields of descriptors. This patch provides help for debugging with the following new virtual tables: - `crdb_internal.table_columns`: detailed column IDs and properties - `crdb_internal.table_indexes`: detailed index IDs and index properties - `crdb_internal.index_columns`: what columns are indexed and how - `crdb_internal.backward_dependencies`: which other descriptors each descriptor depends on - `crdb_internal.forward_dependencies`: which other descriptors each descriptor is depended on by These virtual tables are intended to display the contents of descriptors without any data transformation, so as to reveal any error in the descriptors, if any. Debugging/fsck tools can later use these to check schema validity.
724d983
to
e1dcadd
Compare
Rebased on top of #17422 to make the fix more obvious. |
8b4efac
to
a4de72a
Compare
Review status: 0 of 30 files reviewed at latest revision, 6 unresolved discussions, all commit checks successful. Comments from Reviewable |
... because it was incomplete (missing FK relationships). The two new tables `crdb_internal.forward_dependencies` and `crdb_internal.backward_dependencies` can be used instead.
Prior to this patch, view dependency analysis during CREATE VIEW was broken because it would only check dependencies after plan optimization, i.e. possibly after some dependency were lost. For example, with the queries: ```sql CREATE VIEW v AS SELECT k FROM (SELECT k,v FROM kv) -- loses dependency on kv.v CREATE VIEW v AS SELECT k,v FROM kv WHERE FALSE -- loses dependency on kv ``` This patch addresses the issue as follows: - the dependencies are now collected during the initial construction of the query plan, before any optimization are applied. This way we ensure the dependency tracking is complete. - a migration is implemented that will fix any view descriptor and corresponding dependency information that was populated prior to this fix.
a4de72a
to
e28811c
Compare
TFYR |
Review status: 0 of 30 files reviewed at latest revision, 6 unresolved discussions, all commit checks successful. pkg/sql/views.go, line 79 at r3 (raw file): Previously, knz (kena) wrote…
This was further discussed in #17404 Comments from Reviewable |
Prior to this patch, view dependency analysis during CREATE VIEW was broken because it would only check dependencies after plan optimization, i.e. possibly after some dependency were lost. For example, with the queries:
This patch addresses the issue as follows:
Fixes #17306.
Fixes #17290.
Replaces 74c7b0f (reverted via #17305)
Needed for #15388.