Skip to content

perf(migration): qualify the ORDER BY so notification copies use their index - #1147

Merged
cameri merged 1 commit into
rewrite/mainfrom
perf/1124-qualify-notification-order-by
Nov 15, 2026
Merged

cameri merged 1 commit into
rewrite/mainfrom
perf/1124-qualify-notification-order-by

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

Description

scan_batch builds SELECT {columns} FROM {table} [WHERE key > $1] ORDER BY {key} LIMIT $2. For
notification_outbox and notification_delivery_log the column list starts id::text AS id, and in
PostgreSQL an ORDER BY name resolves to the output column before the table column — so ORDER BY id
sorted the text expression while the indexes cover the uuid column. Every batch read the
remaining rows and sorted their text ids before applying LIMIT.

The keyset predicate (id > $1::uuid) and the ORDER BY now both name the table, so the planner
reads the uuid index directly. The unit test asserts both qualified forms.

Related Issue

Closes #1124

Motivation and Context

The migration copies of both notification tables are the largest they get, and the unindexed sort
grows with the backlog: each batch costs a full read of the remaining rows instead of a bounded
index range scan.

How Has This Been Tested?

cargo fmt --check -p nostream-storage
cargo test -p nostream-storage --lib migration

4 passed, 0 failed (every_cursor_is_the_tables_own_key,
the_uuid_cursor_is_the_two_tables_the_schema_makes_uuid, a_reports_cursor_is_undone_before_it_is_bound,
the_first_page_of_every_table_binds_no_cursor).

The new assertion checks the exact predicate ({table}.id > $1::uuid) and the exact ordering
(ORDER BY {table}.id) for both uuid tables, so a regression to the unqualified form fails it.

Credit

The fix is @tirthraj01's, who worked it out in
#1124 (comment) and posted it on his fork
(tirthraj01:perf/1124-qualify-notification-order-by, off rewrite/main). This PR carries his commit
unchanged except for one rustfmt wrap of the format! call that cargo fmt --check required, and
he remains the author of the commit.

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • I added a changeset, or this is docs-only and I added an empty changeset.
  • All new and existing tests passed.

@changeset-bot

changeset-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4cf784e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2971538f-dede-493a-9ebd-5006a9f91407

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR makes notification migration queries order by the UUID column instead of the selected text alias.

  • Notification migration pages use the tables’ UUID IDs for paging and ordering.

Reviews (1) · Last reviewed commit: "perf(migration): qualify the ORDER BY so..." · Reviewed by Greptile

@chappie-daemon

Copy link
Copy Markdown
Collaborator Author

Checked the sibling cursor families for the same output-alias shadowing, since ORDER BY is where PostgreSQL prefers an output column over the table column:

  • Cursor::Uuid — notification_outbox, notification_delivery_log: id::text AS id, fixed here (also WHERE, which PostgreSQL resolves against the table, so it is consistent rather than strictly necessary).
  • Cursor::Bytes — events (EVENT_COLUMNS aliases events.id::text AS id, but the key is event_id, whose output column is the same expression), users.pubkey, nip05_verifications.pubkey, dvm_jobs.id: no key is shadowed.
  • Cursor::Text — invite_codes.code, invoices.id (plain id, no alias): no shadowing.
  • Cursor::Int32Key — reports.id (plain id): no shadowing.

So the two notification tables were the only ones where ORDER BY id sorted something other than the indexed column.

@cameri
cameri merged commit 0fe631b into rewrite/main Nov 15, 2026
17 checks passed
@cameri
cameri deleted the perf/1124-qualify-notification-order-by branch November 15, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants