Skip to content

chore: optimize GetPrebuiltWorkspaces query #18717

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Jul 2, 2025

Hopefully fixes coder/internal#715 this time.

Second attempt at this.
Previous attempt incorrectly returned all rows for which there existed a prebuild that had previously had a successful start transition at some point: #18588

Explain before (71.1ms): https://explain.dalibo.com/plan/9be18ab833b7a000
Explain after (9.8ms): https://explain.dalibo.com/plan/b4a94742gaha229g (EDIT: not selecting from correct CTE)
Explain after (11.2ms): https://explain.dalibo.com/plan/bea42b563ff7fbe7

Manually verified against dogfood db:

$ psql '<db url>' -f a.sql > a.txt
$ psql '<db url>' -f b.sql > b.txt
$ diff a.txt b.txt
28d27
<  18aa3c11-c1eb-4fc6-ae3b-8cbd01f81f21 | prebuild-hiyghekkb3gv2vq | 99064381-8750-407f-9d30-91f38b7911cc | 5d91f37e-5872-4821-b6ed-90574082628e | bdc27adb-7a8e-4a6f-9922-5a0f4a3885da | f     | 2025-07-02 13:34:58.537656+00
32a32
>  18aa3c11-c1eb-4fc6-ae3b-8cbd01f81f21 | prebuild-hiyghekkb3gv2vq | 99064381-8750-407f-9d30-91f38b7911cc | 5d91f37e-5872-4821-b6ed-90574082628e | bdc27adb-7a8e-4a6f-9922-5a0f4a3885da | f     | 2025-07-02 13:34:58.537656+00
$ diff <(sort a.txt) <(sort b.txt)
$ 

Diff is only due to unstable row order, also added additional testing in coderd/database/querier_test.go to validate changes.

If we want to be super careful about this, I can instead break out the updated query into a new function and diff the old versus the new on each reconcile call.

@johnstcn johnstcn self-assigned this Jul 2, 2025
@johnstcn johnstcn marked this pull request as ready for review July 2, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: GetRunningPrebuiltWorkspaces creates lots of DB load
1 participant