Skip to content

perf(notification): index delivery status lookups by event ID - #5510

Merged
chrisgacsal merged 1 commit into
mainfrom
perf/notification-delivery-status-index
Nov 16, 2026
Merged

chrisgacsal merged 1 commit into
mainfrom
perf/notification-delivery-status-index

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Notification eagerly loads delivery statuses when listing or fetching Events. Ent queries the join table by Event ID and reads the corresponding status IDs. The table’s existing primary key starts with the status ID, which made PostgreSQL scan the join table for a single Event.

Add a covering B-tree index on (notification_event_id, notification_event_delivery_status_id). The migration creates and drops the index concurrently. The schema exporter includes the index alongside deferred foreign keys in supplemental_schema.sql so future Atlas diffs retain it.

Notes for reviewer

I replayed the SQL emitted by Ent using EXPLAIN (ANALYZE, BUFFERS) on a local PostgreSQL 15 database with 100,000 delivery statuses and 100,000 join rows. The same Event returned one delivery in both runs. The before plan was captured after rollback, and the after plan with the migration applied.

Measurement Before After
Join-table access Sequential scan, 99,999 rows discarded Index-only scan, 0 heap fetches
Shared buffer hits for the query 1,035 8
Execution time on this fixture 5.693 ms 0.043 ms
Before migration
Nested Loop  (cost=0.42..2289.44 rows=1 width=271) (actual time=3.631..5.669 rows=1 loops=1)
   Buffers: shared hit=1035
   ->  Seq Scan on notification_event_delivery_status_events t1  (cost=0.00..2281.00 rows=1 width=54) (actual time=3.611..5.649 rows=1 loops=1)
         Filter: (notification_event_id = '00000000000000000000054321'::bpchar)
         Rows Removed by Filter: 99999
         Buffers: shared hit=1031
   ->  Index Scan using notificationeventdeliverystatus_id on notification_event_delivery_status  (cost=0.42..8.44 rows=1 width=244) (actual time=0.017..0.017 rows=1 loops=1)
         Index Cond: (id = t1.notification_event_delivery_status_id)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=391
 Planning Time: 1.463 ms
 Execution Time: 5.693 ms
After migration
Nested Loop  (cost=0.83..12.87 rows=1 width=271) (actual time=0.027..0.028 rows=1 loops=1)
   Buffers: shared hit=8
   ->  Index Only Scan using notification_status_events_event_status_idx on notification_event_delivery_status_events t1  (cost=0.42..4.44 rows=1 width=54) (actual time=0.013..0.014 rows=1 loops=1)
         Index Cond: (notification_event_id = '00000000000000000000054321'::bpchar)
         Heap Fetches: 0
         Buffers: shared hit=4
   ->  Index Scan using notificationeventdeliverystatus_id on notification_event_delivery_status  (cost=0.42..8.44 rows=1 width=244) (actual time=0.013..0.013 rows=1 loops=1)
         Index Cond: (id = t1.notification_event_delivery_status_id)
         Buffers: shared hit=4
 Planning:
   Buffers: shared hit=385 read=23
 Planning Time: 13.683 ms
 Execution Time: 0.043 ms

Verified migration apply, rollback and reapply. Atlas validation and schema parity checks passed. Focused Notification adapter, schema-export and migration parity tests passed with the race detector.

Summary by CodeRabbit

  • Improvements
    • Added a database index to help look up notification delivery status events by notification event and delivery status.
    • Updated schema generation to include supplemental database definitions.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Adds a covering index for notification delivery-status lookups by Event ID.

  • Notification event reads find delivery statuses through an event-first index.

No actionable issues were found. The previous-review checks failed, so this review covered the full PR. No previous findings were supplied. Tests were not run.

Reviews (2) · Last reviewed commit: "perf(notification): index delivery statu..." · Reviewed by Greptile

@chrisgacsal
chrisgacsal requested a review from a team as a code owner November 13, 2026 10:40
@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label Nov 13, 2026
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The schema generator now reads supplemental_schema.sql as SupplementalDDL. That file defines a notification event status index, which a new reversible migration also creates and drops concurrently.

Changes: Supplemental schema and index

Layer / File(s) Summary
Load supplemental schema and define the index
tools/migrate/cmd/entschema/main.go, tools/migrate/supplemental_schema.sql, tools/migrate/migrations/20261116081710_notification_delivery_status_event_lookup.*.sql, tools/migrate/README.md, openmeter/ent/schema/ledger_breakage_record.go
The generator reads supplemental_schema.sql as SupplementalDDL. The file and migration define the notification event status index. The README and schema comment reference the supplemental schema file.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Suggested reviewers: galexihu

Merge Risk: Minimal · up to f8e46

This PR adds a covering index to speed up delivery-status lookups by event ID. The migration is reversible and follows the pattern of earlier concurrent-index migrations. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a performance-focused index that speeds up delivery-status lookups by event ID. The changeset adds a covering index on (notification_event_id, notification_event_deliv…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a performance-focused index that speeds up delivery-status lookups by event ID. The changeset adds a covering index on (notification_event_id, notification_event_delivery_status_id), a concurrent migration to create and drop it, and a supplemental schema entry. The title matches the primary objective in the PR summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d59c781 and f8e4671.

Files ignored due to path filters: 1

  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing: 2

  • tools/migrate/migrations/20261116081710_notification_delivery_status_event_lookup.down.sql
  • tools/migrate/migrations/20261116081710_notification_delivery_status_event_lookup.up.sql

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

turip
turip previously approved these changes Nov 13, 2026
@chrisgacsal
chrisgacsal merged commit 75f3ac4 into main Nov 16, 2026
32 checks passed
@chrisgacsal
chrisgacsal deleted the perf/notification-delivery-status-index branch November 16, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants