Skip to content

Use partial indexes for nullable relationships#3

Merged
jechol merged 1 commit into
mainfrom
partial-reference-indexes
Jul 19, 2026
Merged

Use partial indexes for nullable relationships#3
jechol merged 1 commit into
mainfrom
partial-reference-indexes

Conversation

@jechol

@jechol jechol commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Nullable belongs_to relationships now get partial indexes excluding NULL rows: references use index_where: :not_nil, custom indexes use where: "fk_id IS NOT NULL". FK lookups are equality matches (always IS NOT NULL), so the smaller index serves them fully. allow_nil? false relationships keep full indexes.
  • On multitenant resources, the tenant attribute gets its own single-column index when every other FK index is partial, since partial FK indexes cannot serve tenant-only lookups.
  • Combined with fix: generate full foreign key indexes #2 on the multitenant single-column index: all_tenants?: true + include_base_filter?: false + stable {table}_{fk_id}_fkey_index name, plus the partial where when nullable.
  • Bumps version to 0.5.0 with changelog and README updates.

Requires ash_postgres with index_where on references (ash-project/ash_postgres#795) — not yet in a hex release, dependency stays on GitHub main.

Test plan

  • mix test (22 tests, including new test/partial_index_test.exs)

@jechol
jechol merged commit 4f21ce7 into main Jul 19, 2026
1 check passed
@jechol
jechol deleted the partial-reference-indexes branch July 19, 2026 10: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.

1 participant