-
Notifications
You must be signed in to change notification settings - Fork 231
feat: queries benchmarking and optimization (#68) #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cameri
merged 5 commits into
cameri:main
from
archief2910:feature/68-queries-optimization
Apr 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b03ea0
feat(db): add hot-path indexes and query benchmark (#68)
archief2910 3310e84
Merge branch 'main' into feature/68-queries-optimization
archief2910 11f2180
docs: added changeset (#68)
archief2910 9ca3367
chore: addressed review comments (#68)
archief2910 1393f47
fix: bug fixes in benchmark-queries (#68)
archief2910 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| Add hot-path PostgreSQL indexes for subscription, vanish, retention, and invoice queries; add `db:benchmark` and `db:verify-index-impact` tooling; document index rationale and benchmarking. Closes #68. | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Add narrow, query-driven indexes to cover the hottest read paths. | ||
| * | ||
| * Each index is created with CREATE INDEX CONCURRENTLY so the migration can be | ||
| * applied to a running relay without taking an ACCESS EXCLUSIVE lock on the | ||
| * events table. CONCURRENTLY is not allowed inside a transaction, so this | ||
| * migration opts out of Knex's default transactional wrapper via | ||
| * `exports.config.transaction = false`. | ||
| * | ||
| * Rationale for each index is documented inline. See also: | ||
| * https://devcenter.heroku.com/articles/postgresql-indexes | ||
| */ | ||
|
|
||
| exports.config = { transaction: false } | ||
|
|
||
| exports.up = async function (knex) { | ||
| // Covers the hottest subscription / per-message reads: | ||
| // | ||
| // 1. NIP-01 REQ with `authors` + `kinds` ordered by created_at DESC | ||
| // (see EventRepository.findByFilters): | ||
| // WHERE event_pubkey = ? AND event_kind IN (...) | ||
| // ORDER BY event_created_at DESC, event_id ASC LIMIT N | ||
| // | ||
| // 2. `EventRepository.hasActiveRequestToVanish(pubkey)` — invoked on every | ||
| // inbound event via UserRepository.isVanished: | ||
| // WHERE event_pubkey = ? AND event_kind = 62 AND deleted_at IS NULL | ||
| // | ||
| // 3. `EventRepository.deleteByPubkeyExceptKinds(pubkey, kinds)`: | ||
| // WHERE event_pubkey = ? AND event_kind NOT IN (...) AND deleted_at IS NULL | ||
| // | ||
| // The index is intentionally NOT partial on `deleted_at IS NULL`: the REQ | ||
| // subscription path in findByFilters does not currently add that predicate, | ||
| // so a partial index would be ineligible for the most important query shape. | ||
| // Soft-deleted rows are a small fraction of total rows in practice (they get | ||
| // hard-deleted by the retention sweep), so the bloat is negligible compared | ||
| // to the benefit of the index being usable by the hot path. | ||
| // | ||
| // Including `event_id` as the final column makes the composite key match the | ||
| // full ORDER BY (created_at DESC, event_id ASC) used by findByFilters, so the | ||
| // planner can satisfy LIMIT N directly from the index without an extra sort | ||
| // step for the tie-breaker. | ||
| await knex.raw(` | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS events_active_pubkey_kind_created_at_idx | ||
| ON events (event_pubkey, event_kind, event_created_at DESC, event_id) | ||
| `) | ||
|
|
||
| // Supports the retention / purge scan in `deleteExpiredAndRetained` and the | ||
| // vanish hard-delete follow-up: | ||
| // WHERE deleted_at IS NOT NULL | ||
| // Partial index is tiny because well-maintained relays hard-delete these | ||
| // rows periodically and the vast majority of events have deleted_at IS NULL. | ||
| await knex.raw(` | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS events_deleted_at_partial_idx | ||
| ON events (deleted_at) | ||
| WHERE deleted_at IS NOT NULL | ||
| `) | ||
|
|
||
| // Supports `InvoiceRepository.findPendingInvoices`, which is polled by the | ||
| // maintenance worker to detect settled invoices: | ||
| // WHERE status = 'pending' ORDER BY created_at ASC OFFSET ? LIMIT ? | ||
| // Partial on status = 'pending' so the index only contains the rows the | ||
| // poller actually scans. Keyed on `created_at` so the planner can satisfy | ||
| // the ORDER BY straight from the index (FIFO polling, bounded tail latency | ||
| // even with large pending backlogs). | ||
| await knex.raw(` | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS invoices_pending_created_at_idx | ||
| ON invoices (created_at) | ||
| WHERE status = 'pending' | ||
| `) | ||
| } | ||
|
|
||
| exports.down = async function (knex) { | ||
| await knex.raw('DROP INDEX CONCURRENTLY IF EXISTS invoices_pending_created_at_idx') | ||
| await knex.raw('DROP INDEX CONCURRENTLY IF EXISTS events_deleted_at_partial_idx') | ||
| await knex.raw('DROP INDEX CONCURRENTLY IF EXISTS events_active_pubkey_kind_created_at_idx') | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.