Convert Region Allocation to be a CTE#1733
Merged
Merged
Conversation
jmpesp
approved these changes
Sep 26, 2022
jmpesp
left a comment
Contributor
There was a problem hiding this comment.
This is awesome! A few questions, two suggestions, but none blocking.
Collaborator
Author
|
Just confirmed that this fixes the symptom of https://github.com/oxidecomputer/remote-access-preview/issues/23 |
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.
Fixes #1688
This PR can basically be read in two pieces - I could literally split it in two if that would be helpful, but figured I'd show "new utilities" with "how they are utilized for the region allocation use-case specifically".
Part 1: Utilities to build CTEs
nexus/src/db/subquery.rs- Adds the definition of traits likeSubquery, as well as aCteBuilderto help building arbitrary CTEs.nexus/db-macros/src/lib.rs- Adds macro to deriveSubquery(identifying that a query can be used in a CTE arm)nexus/db-macros/src/subquery.rs- Implementation ofSubquerymacronexus/src/db/alias.rs- Defines expression which allows field aliasing (FOO as BARin SELECT/RETURNING queries)Part 2: Using those utilities to implement the region allocation CTE
nexus/src/db/datastore/region.rs- Updates actual usage of region allocation querynexus/src/db/queries/region_allocation.rs- Defines and implements region allocation CTEnexus/db-model/src/queries/region_allocation.rs- Defines model of subqueries (usingtable!macro)Note: This particular CTE is actually kinda long, because the transaction was doing a lot. In the future, however, we'll be able to simplify the CTE significantly by relying on quotas/reservations and db constraints to help enforce some of the size limits.