Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Listing job kinds (used by River UI's kind autocomplete) on Postgres now probes the `kind` index once per distinct kind instead of reading an entry for every job, which makes it orders of magnitude faster on large `river_job` tables. [PR #1606](https://github.com/riverqueue/river/pull/1606).

## [0.51.2] - 2026-11-09

### Fixed
Expand Down
41 changes: 32 additions & 9 deletions riverdriver/riverdatabasesql/internal/dbsqlc/river_job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions riverdriver/riverdrivertest/job_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,56 @@ func exerciseJobRead[TTx any](ctx context.Context, t *testing.T, executorWithTx
require.Equal(t, []string{job2.Kind, job3.Kind}, jobKinds) // sorted by name
})

t.Run("DeduplicatesJobKinds", func(t *testing.T) {
t.Parallel()

exec, _ := setup(ctx, t)

for _, kind := range []string{"kind_b", "kind_a", "kind_c", "kind_a", "kind_b", "kind_a"} {
_ = testfactory.Job(ctx, t, exec, &testfactory.JobOpts{Kind: new(kind)})
}

jobKinds, err := exec.JobKindList(ctx, &riverdriver.JobKindListParams{
After: "",
Exclude: nil,
Match: "",
Max: 10,
Schema: "",
})
require.NoError(t, err)
require.Equal(t, []string{"kind_a", "kind_b", "kind_c"}, jobKinds)

jobKinds, err = exec.JobKindList(ctx, &riverdriver.JobKindListParams{
After: "kind_a",
Exclude: nil,
Match: "",
Max: 10,
Schema: "",
})
require.NoError(t, err)
require.Equal(t, []string{"kind_b", "kind_c"}, jobKinds)
})

t.Run("DeduplicatesJobKindsWithFilters", func(t *testing.T) {
t.Parallel()

exec, _ := setup(ctx, t)

for _, kind := range []string{"job_a", "job_b", "job_c", "other", "job_a", "job_b", "job_c", "other"} {
_ = testfactory.Job(ctx, t, exec, &testfactory.JobOpts{Kind: new(kind)})
}

jobKinds, err := exec.JobKindList(ctx, &riverdriver.JobKindListParams{
After: "",
Exclude: []string{"job_a"},
Match: "JOB",
Max: 1,
Schema: "",
})
require.NoError(t, err)
require.Equal(t, []string{"job_b"}, jobKinds)
})

t.Run("ExcludesJobKindsInExcludeList", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -991,6 +1041,25 @@ func exerciseJobRead[TTx any](ctx context.Context, t *testing.T, executorWithTx
require.NoError(t, err)
require.Equal(t, []string{job2.Kind, job3.Kind}, jobKinds)
})

t.Run("ReturnsNoJobKindsAfterLastKind", func(t *testing.T) {
t.Parallel()

exec, _ := setup(ctx, t)

_ = testfactory.Job(ctx, t, exec, &testfactory.JobOpts{Kind: new("job_aaa")})
_ = testfactory.Job(ctx, t, exec, &testfactory.JobOpts{Kind: new("job_bbb")})

jobKinds, err := exec.JobKindList(ctx, &riverdriver.JobKindListParams{
After: "job_zzz",
Exclude: nil,
Match: "",
Max: 10,
Schema: "",
})
require.NoError(t, err)
require.Empty(t, jobKinds)
})
})

t.Run("JobList", func(t *testing.T) {
Expand Down
31 changes: 27 additions & 4 deletions riverdriver/riverpgxv5/internal/dbsqlc/river_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,33 @@ FROM raw_job_data
RETURNING *;

-- name: JobKindList :many
SELECT DISTINCT ON (kind) kind
FROM /* TEMPLATE: schema */river_job
WHERE (@match = '' OR kind ILIKE '%' || @match || '%')
AND (@after = '' OR kind > @after)
-- Postgres has no loose index scan, so DISTINCT would read an index entry per
-- job. Hop between distinct kinds with one index probe each instead. Kinds are
-- never empty, so an empty `after` matches all of them without needing an OR
-- that would stop it being an index condition under a generic plan.
WITH RECURSIVE kind_cte AS (
(
SELECT kind
FROM /* TEMPLATE: schema */river_job
WHERE kind > @after::text
ORDER BY kind ASC
LIMIT 1
)
UNION ALL
SELECT (
SELECT river_job.kind
FROM /* TEMPLATE: schema */river_job
WHERE river_job.kind > kind_cte.kind
ORDER BY river_job.kind ASC
LIMIT 1
)
FROM kind_cte
WHERE kind_cte.kind IS NOT NULL
)
SELECT kind
FROM kind_cte
WHERE kind IS NOT NULL
AND (@match::text = '' OR kind ILIKE '%' || @match::text || '%')
AND (@exclude::text[] IS NULL OR kind != ALL(@exclude))
ORDER BY kind ASC
LIMIT @max;
Expand Down
41 changes: 32 additions & 9 deletions riverdriver/riverpgxv5/internal/dbsqlc/river_job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.