Repository navigation
[go] Make JobKindList scale with distinct kinds, not jobs - #1606
Open
jacobmolby wants to merge 3 commits into
Open
jacobmolby wants to merge 3 commits into
jacobmolby wants to merge 3 commits into
Conversation
Postgres has no loose index scan, so SELECT DISTINCT ON (kind) read an index entry (and often a heap tuple) for every job to produce a few dozen kinds. Replace it with a recursive CTE that hops between distinct kinds with one index probe each, applying the match and exclude filters to the small set of kinds it produces.
jacobmolby
marked this pull request as ready for review
November 26, 2026 09:51
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.
River UI's job kind autocomplete calls
JobKindList, which ran:Postgres has no loose index scan, so even with
river_job_kindthis walks one index entry per job. On a busy table, the index-only scan also falls back to the heap for a large share of those rows. Once a search string is typed,ILIKEis evaluated against every row too. The cost grows with the number of jobs, while the result is a few dozen kinds.This replaces it with a recursive CTE that emulates a skip scan. Each step is a single
kind > previous ORDER BY kind LIMIT 1probe intoriver_job_kind. Thematchandexcludefilters then run on the distinct kinds it produces, so the cost depends on the number of distinct kinds, not the number of jobs.afteris pushed into the CTE's starting probe askind > @after. Kinds are never empty, so an emptyafterstill matches everything, andafterstays an index condition rather than anORfilter under a generic plan.The SQLite query is unchanged.
One open question is YugabyteDB, which I couldn't test. Yugabyte hash-shards an index's first column unless it's declared
ASC. Ifriver_job_kindends up hash-sharded there, thekind > ... ORDER BY kind LIMIT 1probes can't use ordered index access and may scan the table instead. That would make this query slower than the old one on Yugabyte. If that turns out to be the case, the driver already detects Yugabyte, so it could keep theDISTINCTquery there.Numbers
These come from
EXPLAIN (ANALYZE, BUFFERS)against a production table on Postgres 18 with about 1.3M rows inriver_joband 45 distinct kinds:On a small table (8k rows, 26 kinds) both versions finish in about a millisecond.
Tests
I added three cases to
riverdrivertest, because the existing cases insert one job per kind:DeduplicatesJobKindscovers several jobs per kind, with and withoutafter.DeduplicatesJobKindsWithFilterscombines duplicate kinds with an uppercasematch,exclude, andmaxset below the number of distinct kinds, since those filters now run on the CTE output.ReturnsNoJobKindsAfterLastKindcovers anafterthat sorts past every kind, so the CTE's starting probe finds nothing.The
JobKindListsuite passes on every driver.I regenerated the sqlc code with sqlc v1.31.1, the version CI pins. The regenerated params are now typed
stringwhere they wereinterface{}, and the driver call sites didn't need changes.