Skip to content

Update prefix usage query to improve index usage#3413

Merged
lunkwill42 merged 1 commit intomasterfrom
bugfix/improve-prefix-usage-lookups
Aug 6, 2025
Merged

Update prefix usage query to improve index usage#3413
lunkwill42 merged 1 commit intomasterfrom
bugfix/improve-prefix-usage-lookups

Conversation

@lunkwill42
Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 commented Aug 4, 2025

Scope and purpose

On some installations with huge ARP tables, we recommend adding local gist indexes or partial indexes for queries against open arp records (end_time = 'infinity') to improve the lookup speeds.

In order to ensure these indexes can be made as btree indexes, their conditional is usually configured as applying to records where end_time >= 'infinity' (as = matches will often map to a hash index rather than a btree index, >= is better).

However, PostgreSQL's planner cannot detect and properly utilize these indexes if a query includes a where clause that limits it to end_time = 'infinity'. These queries should explicitly be for end_time >= 'infinity' to ensure the indexes can be used.

This query was found to cause severe timeout issues on a customer installation, because of repeated queries to the prefix usage API endpoint that would never complete, causing more and more requests to stack on and eat up all available PostgreSQL connections.

Contributor Checklist

Every pull request should have this checklist filled out, no matter how small it is.
More information about contributing to NAV can be found in the
Hacker's guide to NAV.

  • Added a changelog fragment for towncrier
  • [ ] Added/amended tests for new/changed code
  • [ ] Added/changed documentation
  • Linted/formatted the code with black and ruff, easiest by using pre-commit
  • The first line of the commit message continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See https://cbea.ms/git-commit/
  • This pull request is based on the correct upstream branch: For a patch/bugfix affecting the latest stable version, it should be based on that version's branch (<major>.<minor>.x). For a new feature or other additions, it should be based on master.
  • [ ] If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done
  • [ ] If this results in changes in the UI: Added screenshots of the before and after
  • [ ] If this adds a new Python source code file: Added the boilerplate header to that file

On some installations with huge ARP tables, we recommend adding
local gist indexes or partial indexes for queries against open arp
records (`end_time = 'infinity'`) to improve the lookup speeds.

In order to ensure these indexes can be made as btree indexes, their
conditional is usually configured as applying to records where
`end_time >= 'infinity'` (as `=` matches will often map to a hash
index rather than a btree index, `>=` is better).

However, PostgreSQL's planner cannot detect and properly utilize these
indexes if a query includes a where clause that limits it to
`end_time = 'infinity'`.  These queries should explicitly be for
`end_time >= 'infinity'` to ensure the indexes can be used.

This query was found to cause severe timeout issues on a customer
installation, because of repeated queries to the prefix usage API
endpoint that would never complete, causing more and more requests
to stack on and eat up all available PostgreSQL connections.
@lunkwill42 lunkwill42 requested a review from a team August 4, 2025 13:00
@lunkwill42 lunkwill42 self-assigned this Aug 4, 2025
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Aug 4, 2025

@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 4, 2025

Test results

   12 files     12 suites   12m 3s ⏱️
2 206 tests 2 206 ✅ 0 💤 0 ❌
6 093 runs  6 093 ✅ 0 💤 0 ❌

Results for commit b5360b6.

@lunkwill42 lunkwill42 merged commit 25432b2 into master Aug 6, 2025
15 checks passed
@lunkwill42 lunkwill42 deleted the bugfix/improve-prefix-usage-lookups branch August 6, 2025 06:26
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (37521e7) to head (b5360b6).
⚠️ Report is 21 commits behind head on master.

Additional details and impacted files
@@      Coverage Diff       @@
##   master   #3413   +/-   ##
==============================
==============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants