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: populated pg_depend with table and view dependencies #57240

Merged
merged 1 commit into from
Dec 11, 2020

Conversation

mnovelodou
Copy link
Contributor

Previously, pg_depend only contained dependencies from foreign keys,
but this table in postgres also cointains dependencies from table-views,
To address this, this patch populates this dependency information

Fixes #56115

Release note (sql change): added table-view dependency information
in pg_depend to improve compatibility with postgresql.

@blathers-crl
Copy link

blathers-crl bot commented Nov 30, 2020

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

I have added a few people who may be able to assist in reviewing:

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

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Nov 30, 2020
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss requested review from rafiss and removed request for jayshrivastava December 1, 2020 18:08
@blathers-crl
Copy link

blathers-crl bot commented Dec 2, 2020

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

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

@blathers-crl
Copy link

blathers-crl bot commented Dec 2, 2020

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • When CI has completed, please ensure no errors have appeared.

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

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good! i have a few requests for testing and commenting

@@ -992,6 +1002,35 @@ JOIN pg_constraint ON objid=pg_constraint.oid AND refobjid=pg_constraint.conindi
contype
f

# Testing table-view dependencies in pg_depend, This query will not work the same way
# In PostgreSQL as pg_depend.classid points to pg_rewrite then in pg_rewrite
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these Postgres docs also say that pg_depend.classid points directly to pg_class.oid
https://www.postgresql.org/docs/current/catalog-pg-depend.html

but it looks like in Postgres, pg_depend.objid points to pg_rewrite.ev_class. can you update this comment?

i looked into this further.. i think soon we should try to populate pg_rewrite. i created #57417 -- can you include this link in the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# pg_rewrite yet.

statement ok
CREATE TABLE source_table(a INT, b INT, c INT);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this CREATE TABLE source_table(a INT PRIMARY KEY, b INT, c INT);

then i think fewer other tests will be affected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -1153,6 +1153,31 @@ https://www.postgresql.org/docs/9.5/catalog-pg-depend.html`,
depTypeAuto, // deptype
)
}

reportViewDependency := func(dep *descpb.TableDescriptor_Reference) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this code handle views that depend on other views? can you add a test for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop already pick views and descriptor already have dependencies at other views, but this is a nice catch, I tried it and it works, I am going to add a test case for this as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CREATE TABLE source_table(a INT, b INT, c INT);
CREATE VIEW depend_view AS SELECT b, c FROM source_table;

query TTTTT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this query TTTTT colnames so that the output includes the column names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

SELECT
depend_cat_class.relname depend_catalog,
source_cat_class.relname source_catalog,
depend_class.relname depend,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also check for source_class.relname?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

reportViewDependency := func(dep *descpb.TableDescriptor_Reference) error {
for _, colID := range dep.ColumnIDs {
if err := addRow(
pgClassTableOid, //classid
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a comment here that described how we don't use pg_rewrite (and link to issue #57417)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rafiss
Copy link
Collaborator

rafiss commented Dec 2, 2020

I noticed that you merged master into this branch. the recommended way to update the PR is to rebase your branch on top of master, then force push. that way the git history is easier to read. for example:

git pull --rebase origin master
git push <your remote name> --force pg-depend-views

(in this case you should also remove your merge commit too)

@blathers-crl
Copy link

blathers-crl bot commented Dec 3, 2020

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

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

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

bors r+

Reviewed 2 of 2 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)

@craig
Copy link
Contributor

craig bot commented Dec 10, 2020

Merge conflict.

@rafiss
Copy link
Collaborator

rafiss commented Dec 10, 2020

bors r-

@mnovelodou i just noticed there is a merge conflict. can you please update it when you have a chance?

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marking as requested change

Previously, pg_depend only contained dependencies from foreign keys,
but this table in postgres also cointains dependencies from table-views,
To address this, this patch populates this dependency information

Fixes cockroachdb#56115

Release note (sql change): added table-view dependency information
in pg_depend to improve compatibility with postgresql.
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

bors r+

@rafiss rafiss removed the O-community Originated from the community label Dec 11, 2020
@craig
Copy link
Contributor

craig bot commented Dec 11, 2020

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: store dependencies in pg_depend
3 participants