Skip to content

perf(database): index install_events.click_id so deleting clicks stays fast - #104

Merged
onamfc merged 1 commit into
mainfrom
perf/index-install-click-id
Nov 12, 2026
Merged

onamfc merged 1 commit into
mainfrom
perf/index-install-click-id

Conversation

@onamfc

@onamfc onamfc commented Nov 12, 2026

Copy link
Copy Markdown
Member

Description

install_events.click_id references click_events(id) ON DELETE SET NULL. On every click deletion Postgres looks up the installs that point at that click so it can null their click_id, and with no index on the column each lookup is a full scan of install_events. A retention job that deletes old clicks in batches then spends minutes per batch once install_events is 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 existing ensureConcurrentIndex helper (same as idx_fingerprints_ip and idx_in_app_events_client_event):

  • built CONCURRENTLY, so an established install_events table keeps taking writes during the build;
  • a start that finds the index already valid sends no DDL and takes no lock on the table;
  • an invalid leftover from a failed build is dropped and rebuilt; a failure is logged, not thrown.

Type of Change

  • ⚡ Performance improvement
  • ✅ Test addition or update

Changes Made

  • src/lib/database.ts: new INSTALL_CLICK_INDEX_LOCK_KEY (4977264) and an ensureConcurrentIndex call for idx_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 on install_events(click_id).

Testing

  • DATABASE_URL=… npx vitest run: 43 files, 735 tests pass.
  • tsc --noEmit clean.

…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

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@onamfc
onamfc merged commit 15203aa into main Nov 12, 2026
13 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant