You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(notification): index delivery status lookups by event ID - #5510
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.
Confidence 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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
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
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.
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 insupplemental_schema.sqlso 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.Before migration
After migration
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


Confidence 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.
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