Skip to content

[go] Make JobKindList scale with distinct kinds, not jobs - #1606

Open
jacobmolby wants to merge 3 commits into
riverqueue:masterfrom
jacobmolby:job-kind-list-skip-scan
Open

jacobmolby wants to merge 3 commits into
riverqueue:masterfrom
jacobmolby:job-kind-list-skip-scan

Conversation

@jacobmolby

@jacobmolby jacobmolby commented Nov 26, 2026 •

Copy link
Copy Markdown

River UI's job kind autocomplete calls JobKindList, which ran:

SELECT DISTINCT ON (kind) kind
FROM river_job
WHERE (@match = '' OR kind ILIKE '%' || @match || '%') ...
ORDER BY kind
LIMIT @max;

Postgres has no loose index scan, so even with river_job_kind this 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, ILIKE is 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 1 probe into river_job_kind. The match and exclude filters then run on the distinct kinds it produces, so the cost depends on the number of distinct kinds, not the number of jobs. after is pushed into the CTE's starting probe as kind > @after. Kinds are never empty, so an empty after still matches everything, and after stays an index condition rather than an OR filter 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. If river_job_kind ends up hash-sharded there, the kind > ... ORDER BY kind LIMIT 1 probes 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 the DISTINCT query there.

Numbers

These come from EXPLAIN (ANALYZE, BUFFERS) against a production table on Postgres 18 with about 1.3M rows in river_job and 45 distinct kinds:

Before After
No match string 350 ms, 57k buffers, 210k heap fetches 0.87 ms, 188 buffers, 31 heap fetches
Match string with no hits 1,034 ms (3 parallel workers) 0.79 ms, 187 buffers

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:

  • DeduplicatesJobKinds covers several jobs per kind, with and without after.
  • DeduplicatesJobKindsWithFilters combines duplicate kinds with an uppercase match, exclude, and max set below the number of distinct kinds, since those filters now run on the CTE output.
  • ReturnsNoJobKindsAfterLastKind covers an after that sorts past every kind, so the CTE's starting probe finds nothing.

The JobKindList suite passes on every driver.

I regenerated the sqlc code with sqlc v1.31.1, the version CI pins. The regenerated params are now typed string where they were interface{}, and the driver call sites didn't need changes.

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
jacobmolby marked this pull request as ready for review November 26, 2026 09:51
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.

1 participant