-
Notifications
You must be signed in to change notification settings - Fork 435
db: add completed env_build_assignments tip-lookup index and harden its statistics #3403
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/db/migrations/20260727032500_env_build_assignments_tip_lookup_index.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| -- +goose Up | ||
| -- +goose NO TRANSACTION | ||
|
|
||
| -- CREATE INDEX CONCURRENTLY and ANALYZE cannot run inside a transaction; | ||
| -- the migrator's session default (3h, scripts/migrator.go) bounds them. | ||
|
|
||
| -- The current-tip lookup orders by (created_at DESC, build_id DESC) under an | ||
| -- (env_id, tag) equality. That exact shape belongs to an internal | ||
| -- first-party consumer of this table whose queries are not in this | ||
| -- repository's tree (searching only this repo won't find it); the | ||
| -- latest-build laterals here order by created_at alone and ride the same | ||
| -- prefix unchanged. The existing index stops at created_at: the planner | ||
| -- still needs a top-N sort, so its plan choice rides on per-column | ||
| -- estimates that every autoanalyze re-samples. Completing the index to the | ||
| -- full sort key turns the lookup into a descend-and-stop ordered index scan | ||
| -- (LIMIT 1, no sort), which stays the cheapest plan under any statistics | ||
| -- roll; newer-assignment probes ride the same prefix. | ||
| -- This migration is deliberately ADDITIVE: the superseded 3-column index | ||
| -- (idx_env_build_assignments_env_tag_created, identical ordering prefix) is | ||
| -- dropped by a separate follow-up migration once the new index has soaked. | ||
| -- A failed CONCURRENTLY build leaves an INVALID index that a plain retry | ||
| -- would trip over; drop it first. | ||
| DROP INDEX CONCURRENTLY IF EXISTS idx_env_build_assignments_env_tag_created_build; | ||
| CREATE INDEX CONCURRENTLY idx_env_build_assignments_env_tag_created_build | ||
| ON public.env_build_assignments (env_id, tag, created_at DESC, build_id DESC); | ||
|
|
||
| -- Stabilize the estimate inputs themselves: larger per-column samples cut the | ||
| -- roll-to-roll variance of n_distinct/MCV on the lookup's key columns (same | ||
| -- treatment as env_builds.status_group). | ||
| ALTER TABLE public.env_build_assignments ALTER COLUMN env_id SET STATISTICS 2000; | ||
| ALTER TABLE public.env_build_assignments ALTER COLUMN tag SET STATISTICS 2000; | ||
|
|
||
| -- Rebuild stats immediately rather than waiting for the next autoanalyze. | ||
| ANALYZE public.env_build_assignments; | ||
|
|
||
| -- +goose Down | ||
| -- +goose NO TRANSACTION | ||
|
|
||
| DROP INDEX CONCURRENTLY IF EXISTS idx_env_build_assignments_env_tag_created_build; | ||
|
|
||
| ALTER TABLE public.env_build_assignments ALTER COLUMN env_id SET STATISTICS -1; | ||
| ALTER TABLE public.env_build_assignments ALTER COLUMN tag SET STATISTICS -1; | ||
|
|
||
| ANALYZE public.env_build_assignments; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.