Skip to content

perf(notification): use the annotations GIN index for balance hash lookups - #5498

Merged
chrisgacsal merged 1 commit into
mainfrom
perf/notification-balance-hash-lookup
Nov 12, 2026
Merged

chrisgacsal merged 1 commit into
mainfrom
perf/notification-balance-hash-lookup

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

The balance worker reads the latest matching Event in a namespace and usage period using either its V1 or V2 hash. Previously, the adapter extracted the annotation as text, so PostgreSQL had to filter candidate Events after scanning the namespace index.

Use JSONB containment on the existing annotations column for each hash and combine the predicates with OR. This lets PostgreSQL use the existing GIN index for both the pagination count and the newest-Event lookup.

Notes for reviewer

The adapter tests cover V1, V2, mixed history, an unknown hash, and exclusion by annotation key, namespace and usage period. The mixed-history case checks that two Events count as matches while only the latest is returned.

On a local PostgreSQL 15 fixture with 100,000 Events, the old plans read 25,000 candidates through the namespace index and discarded 24,980. With the new predicate, both plans combined two scans on notificationevent_annotations using BitmapOr and fetched 20 matches.

Query Before After
Count 7.657 ms 0.405 ms
Newest Event 4.650 ms 0.312 ms

Timings are from a single local fixture run.

The PostgreSQL-backed Notification adapter suite passed with -race -tags=dynamic, and package lint reported zero issues.

Summary by CodeRabbit

  • Bug Fixes
    • Event listings now correctly match requested deduplication hashes, including when multiple hashes are provided.
    • Filtering continues to respect namespace and time range, with accurate result counts and newest-first ordering.

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Changes balance hash lookups from extracted-text comparisons to JSONB containment, allowing PostgreSQL to use the existing GIN index.

  • Balance event lookups can use the annotations index for either hash.

Reviews (1) · Last reviewed commit: "perf(notification): use the annotations ..." · Reviewed by Greptile

@chrisgacsal
chrisgacsal requested a review from a team as a code owner November 12, 2026 13:38
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

ListEvents now matches each requested deduplication hash against a complete JSON object in event annotations. Tests cover hash combinations, namespace and time filters, result counts, and ordering.

Changes: Event deduplication hash filtering

Layer / File(s) Summary
Update and verify hash filtering
openmeter/notification/adapter/event.go, openmeter/notification/adapter/event_test.go
ListEvents combines whole-object JSON containment matches for requested hashes with OR. Tests check legacy and current hashes, unknown hashes, namespace and time filters, total counts, and newest-first results with a one-item page.

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

Change: Refactor

Suggested reviewers: gergely-kurucz-konghq, borosr, galexihu

Merge Risk: Minimal · up to 69621

This change switches balance hash lookups to a query form that can use the existing annotations index. No behavioral risk was found. The release-note label check still needs a label applied before merge.

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: balance hash lookups in the notification event adapter now use the annotations GIN index. The raw summary confirms ListEvents switched from entutils.JSONBIn on the ded…
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: balance hash lookups in the notification event adapter now use the annotations GIN index. The raw summary confirms ListEvents switched from entutils.JSONBIn on the dedup-hash key to per-hash JSON containment matches combined with OR, and the objectives state the same goal. The title is specific and matches the changeset, and it follows the conventional commit style used in the repository.

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 a209bda and 69621fd.

Files selected for processing: 2

  • openmeter/notification/adapter/event.go
  • openmeter/notification/adapter/event_test.go

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

@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label Nov 12, 2026
@chrisgacsal chrisgacsal self-assigned this Nov 12, 2026
@chrisgacsal
chrisgacsal merged commit 5bc6c56 into main Nov 12, 2026
31 of 32 checks passed
@chrisgacsal
chrisgacsal deleted the perf/notification-balance-hash-lookup branch November 12, 2026 15:12
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.

2 participants