sql: improve pg_table_is_visible performance#59880
Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom Feb 9, 2021
Merged
sql: improve pg_table_is_visible performance#59880craig[bot] merged 1 commit intocockroachdb:masterfrom
craig[bot] merged 1 commit intocockroachdb:masterfrom
Conversation
Member
b758054 to
d593785
Compare
otan
reviewed
Feb 8, 2021
pkg/sql/sem/builtins/pg_builtins.go
Outdated
| "SELECT nspname FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON c.relnamespace=n.oid "+ | ||
| "WHERE c.oid=$1 AND nspname=ANY(current_schemas(true))", oid) | ||
| oid := tree.MustBeDOid(args[0]) | ||
| isVisibe, err := ctx.Planner.IsTableVisible(ctx.Context, ctx.SessionData.SearchPath, int64(oid.DInt)) |
pkg/sql/resolver.go
Outdated
|
|
||
| // IsTableVisible is part of the tree.EvalDatabase interface. | ||
| func (p *planner) IsTableVisible( | ||
| ctx context.Context, searchPath sessiondata.SearchPath, tableId int64, |
| return false, nil | ||
| } | ||
| iter := searchPath.Iter() | ||
| for scName, ok := iter.Next(); ok; scName, ok = iter.Next() { |
Contributor
There was a problem hiding this comment.
do we need to check database too? what if db is in dropping state?
Contributor
There was a problem hiding this comment.
uhh i guess not since it relies on search path.
Collaborator
Author
There was a problem hiding this comment.
hmm actually my code won't work because you could have two schemas in different databases, but with the same name.
Collaborator
Author
There was a problem hiding this comment.
i guess the old code had the same bug..
Collaborator
Author
There was a problem hiding this comment.
HMMM though actually when cross-database references are fully banned, then we don't need to worry about it... (#55791)
otan
reviewed
Feb 8, 2021
pkg/sql/sem/tree/eval.go
Outdated
|
|
||
| // IsTableVisible checks if the table with the given ID belongs to a schema | ||
| // on the given sessiondata.SearchPath. | ||
| IsTableVisible(ctx context.Context, searchPath sessiondata.SearchPath, tableId int64) (bool, error) |
d593785 to
57340dd
Compare
Collaborator
Author
|
RFAL -- i updated to explicitly account for cross-db references (and match the PG behavior) |
This is motivated by a query that Django makes in order to retrieve all the tables visible in the current session. The query uses the pg_table_is_visible function. Previously this was implemented by using the internal executor to inspect the pg_catalog. This was expensive because the internal executor does not share the same descriptor collection as the external context, so it ended up fetching every single table descriptor one-by-one. Now, we avoid the internal executor and lookup the table descriptor directly. There are other builtins that use the internal executor that may face the same problem. Perhaps an approach for the future is to allow builtin functions to be implemented using user-defined scalar functions. I added a benchmark that shows the original Django query performing a constant number of descriptor lookups with this new implementation. Previously, the same benchmark would make hundreds of roundtrips. Release note (performance improvement): Improve performance of the pg_table_is_visible builtin function.
57340dd to
061e90e
Compare
otan
approved these changes
Feb 9, 2021
Collaborator
Author
|
tftr! bors r=otan |
Collaborator
Author
|
i mean bors r=otan |
Contributor
|
Build succeeded: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is motivated by a query that Django makes in order to retrieve all
the tables visible in the current session. The query uses the
pg_table_is_visible function. Previously this was implemented by using
the internal executor to inspect the pg_catalog. This was expensive
because the internal executor does not share the same descriptor
collection as the external context, so it ended up fetching every single
table descriptor one-by-one.
Now, we avoid the internal executor and lookup the table descriptor
directly.
There are other builtins that use the internal executor that may face
the same problem. Perhaps an approach for the future is to allow builtin
functions to be implemented using user-defined scalar functions.
I added a benchmark that shows the original Django query performing a
constant number of descriptor lookups with this new implementation.
fixes #57924
Release note (performance improvement): Improve performance of the
pg_table_is_visible builtin function.