Repository navigation
perf(database): index install_events.click_id so deleting clicks stays fast - #104
Merged
Merged
Conversation
…s fast install_events.click_id references click_events ON DELETE SET NULL, so Postgres looks up the installs that point at every click it deletes. With no index on click_id each lookup is a full scan of install_events, and a retention job deleting clicks in batches spends minutes per batch once that table is large. The index is built with the existing concurrent helper, so an established install_events table keeps taking writes while it builds, a start that finds the index already valid sends nothing, and a failed build is logged rather than failing the boot.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
install_events.click_idreferencesclick_events(id) ON DELETE SET NULL. On every click deletion Postgres looks up the installs that point at that click so it can null theirclick_id, and with no index on the column each lookup is a full scan ofinstall_events. A retention job that deletes old clicks in batches then spends minutes per batch onceinstall_eventsis large, and anything scheduled after it in the same run starves.This adds
idx_installs_click_id ON install_events(click_id), built through the existingensureConcurrentIndexhelper (same asidx_fingerprints_ipandidx_in_app_events_client_event):CONCURRENTLY, so an establishedinstall_eventstable keeps taking writes during the build;Type of Change
Changes Made
src/lib/database.ts: newINSTALL_CLICK_INDEX_LOCK_KEY(4977264) and anensureConcurrentIndexcall foridx_installs_click_id; the helper's doc comment lists why the index exists.src/lib/database.test.ts: real-database test that two starts leave a single valid btree index oninstall_events(click_id).Testing
DATABASE_URL=… npx vitest run: 43 files, 735 tests pass.tsc --noEmitclean.