Speed up introspection of system objects like Database and Role #6665
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.
Currently if there are a lot of roles, doing a query like
select sys::Role { member_of }
is quite slow. This is because the views forboth
Role
andmember_of
need to extract metadata for every Roleand pase it as json, and postgres is planning this as a nested loop.
Unfortunately, startup needs to query all these objects, so having
lots of databases or roles is a problem.
Fix this by forcing sys objects and links to always be materialized
into CTEs. This ensures that we do at most one linear scan of each of
the views. Since there is no way to hit an index when querying any of
these system object views, always doing a linear scan is no worse.
For the objects themselves we can leverage the rewrites mechanism. For
link tables, we add a slightly lighter weight mechanism to handle it.
Tested using 100 roles.
Reverts the now-unnecessary main part of #6633 but keeps the extension
to the patch system. We could actually put annotations on databases now
and have it not be too slow.
This can be backported with an empty
edgeql+schema+globalonly
patch.