Skip to content

chore(relay): drop the live-basis partial index that the planner never picks - #21305

Merged
Jinwoo-H merged 1 commit into
mainfrom
relay-drop-live-basis-index
Sep 17, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
relay-drop-live-basis-index

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​6 $\color{#cf222e}{\Huge{\mathbf{−}}}$​21 $\color{#cf222e}{\Huge{\mathbf{−}}}$​15
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2

ELI5

In the previous change we added an extra index to help one relay housekeeping query find the small number of rows it cares about. We then measured it on real data instead of trusting the reasoning, and it turned out to help nothing: it gets just as cluttered as the index we already had, the database never chooses it, and it makes every write slightly more expensive. This removes it. The thing that actually fixes the problem, deleting old rows and reclaiming the space, already shipped.

What Changed

Removes relay_connection_bases_live_deadline from SCHEMA, added in #21301 and merged as cbd0470.

Before and after, as the user experiences it: no difference. This index was never chosen by the planner, so removing it changes no query plan. The user-visible win from #21301, the fleet no longer stalling every ten minutes, came from the other four indexes, the role gating, and the reapers, all of which stay.

Mechanism. The index was added on the reasoning that the existing composite index spans all 6.65M rows to find a few hundred live ones, so a partial index on active = 1 would hold only the rows the sweep wants. Measured after the merge against production-shaped history, the reasoning does not survive:

  • A basis row is inserted active = 1 and later flipped to 0. That update moves the row out of the partial index's predicate, which in Postgres means deleting the index entry, and the dead entry stays until vacuum. So the partial index accumulates exactly one dead entry per deactivation, the same as the composite one. It is only small on a table that has already been vacuumed, which is the state where nothing needed help.
  • The planner picks the composite index in every state. Scan buffers with both indexes present are identical to composite-only:
State Buffers
Cold 11,099
Warm 2,522
After VACUUM 9
  • It costs about 65 bytes of extra WAL per basis insert, roughly 22% more, for that nothing.

The cure for this table is the reaper plus vacuum, which #21301 ships. The 9-buffer row above is what that looks like.

Also in this PR:

  • postgres-maintenance-sweep-plans.test.ts goes back to pinning relay_connection_bases_active_deadline by name. perf(relay): index and gate the credential cleanup sweeps that seq-scan relay_invites #21301 had widened it to accept either index, which was correct while both existed and is now just looser than it needs to be.
  • The sibling assertion in credential-cleanup-sweep-postgres.test.ts is pinned to the composite index for the same reason.
  • A -- Why not comment sits beside the composite index in SCHEMA recording the measurement, so the next person reading "6.65M rows scanned to find a few hundred" does not re-derive the same idea and re-add the same index.

Operator step, and the order matters

Deploy this schema change first, then drop the index in production:

DROP INDEX CONCURRENTLY relay_connection_bases_live_deadline;

Not the other way round. The boot-time catalog pre-check is name-only: it skips a CREATE INDEX once an index of that name exists. Dropping the index while the current image is still serving would make it absent, so the next boot of any director would send the CREATE INDEX IF NOT EXISTS statement, take its relation lock, and build it non-concurrently over a 1.5 GB table, with all 28 directors reaching it at once. That is the exact failure mode the rule beside SCHEMA exists to prevent. Once this image is live the statement is gone from SCHEMA entirely, and the drop is safe.

DROP INDEX CONCURRENTLY takes no lock that blocks readers or writers, so there is no window to schedule around.

Why

Why remove it rather than leave it? It is not free. It adds ~22% WAL per basis insert and a second index to maintain on every insert and every deactivation, on the hottest write path this table has. An index that is never chosen and never smaller than the one beside it is pure cost.

Why not instead drop the composite index and keep this one? The composite index is the only one covering active = 0, which is what the reaper matches. #21301 measured that directly: the drained reaper costs 5 buffers with the composite index and 1,274 without it. Keeping the partial one and dropping the composite would break the reaper to keep an index the planner does not use.

Why did the original reasoning look right? Because the row-count ratio is real: a few hundred live rows against 6.65M. What it missed is that the ratio describes live tuples, and index bloat is driven by dead ones. A predicate that a row leaves as part of its normal lifecycle does not keep an index small, it just relocates where the dead entries land. That is the generalisable lesson and it is now in the schema comment.

Alternative considered: keep it and rely on more frequent vacuum. Rejected. It would make the partial index useful only in the state where the composite index is also fast (the 9-buffer row), so it would still never be picked. Tuning autovacuum for this table is worth doing on its own merits and does nothing for this index.

Linked Issue

Follow-up to #21301.

Fixes #

Visual Proof

N/A — schema-only change with no UI surface, and by construction no change to any query plan.

Testing

Ran from cloud/ against PostgreSQL 16 in a container on a fresh database, since the relay Postgres suite leaves state behind and fails on a re-used one.

The three plan and schema tests this change touches:

 Test Files  3 passed (3)
      Tests  18 passed (18)

Full relay suite:

 Test Files  88 passed (88)
      Tests  815 passed | 1 skipped (816)

pnpm -F @orca-cloud/relay typecheck clean. oxlint does not cover this code: cloud/** is in the root config's ignorePatterns. No lint suppressions were added.

The plan assertions are the real check here and they are not vacuous: with the partial index gone they still resolve to using relay_connection_bases_active_deadline on a table seeded 20,000 settled to 50 live, and the not.toMatch(/Seq Scan on relay_connection_bases/) half is unchanged from before #21301.

One note for whoever runs these locally: npx vitest on these files fails to resolve @orca-cloud/postgres-schema, because the package is consumed from dist and the pretest script is what builds it. Run them through pnpm -F @orca-cloud/relay test <paths>.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Review

Cross-platform: schema text only, no paths, shells, or processes. SQLite is unaffected either way, since it applies SCHEMA in order and simply has one fewer statement. SSH and mobile are unaffected: no wire field, stream opcode, or published content changes. Backwards compatible in both directions, with one ordering constraint on the operator drop, described above and the only thing in this PR that needs care. Security is unchanged: no predicate, no read path, and no retention policy is touched.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Nothing in this PR is urgent. The index is inert, so the only cost of leaving it in place a while longer is the WAL overhead on basis inserts. The ordering constraint on the production drop is the one thing that must not be got wrong.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…r picks

Added in #21301 on the reasoning that the composite (active, deadline)
index spans all 6.65M rows to find a few hundred live ones. Measured
post-merge against production-shaped history, that reasoning does not
hold: a basis is inserted active and flipped to 0, so the partial index
accumulates one dead entry per deactivation exactly as the composite one
does. Scan buffers are identical to composite-only in every state, 11,099
cold, 2,522 warm, 9 after VACUUM, and the planner picks the composite
index throughout. The extra index costs ~65 bytes of WAL per basis insert,
about 22% more.

The relief is the reaper plus vacuum, which #21301 already ships. Removes
the statement from SCHEMA, restores the plan test to pinning the composite
index by name, and records why a narrower index is not the cure so the
next reader does not re-derive it.
@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: f4ae875c-ab8f-4b7f-afa1-a807db9d7ed7

📥 Commits

Reviewing files that changed from the base of the PR and between 40b2230 and 50c0220.

📒 Files selected for processing (4)
  • cloud/apps/relay/src/credential-cleanup-sweep-postgres.test.ts
  • cloud/apps/relay/src/database.ts
  • cloud/apps/relay/src/postgres-maintenance-sweep-plans.test.ts
  • cloud/apps/relay/src/relay-schema-lock-targets.test.ts
💤 Files with no reviewable changes (1)
  • cloud/apps/relay/src/relay-schema-lock-targets.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The relay schema removes the relay_connection_bases_live_deadline partial index. Its lock-target expectations are removed. The retained relay_connection_bases_active_deadline composite index is documented as covering the sweep behavior. PostgreSQL planner tests now require this composite index and update their descriptions and comments.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 50c02

The retained composite index continues to support the cleanup sweeps, and the related planner and schema expectations are aligned with its removal.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed and covers the change, rationale, testing, operational ordering, and checklist. However, the required Linked Issue field is incomplete: it contains Fixes # without an iss… Complete the Linked Issue section with the correct issue reference, such as Fixes #21301`` if that issue should be closed, or use the repository-approved reference format and clarify that this PR is a follow-up if #21301 should remain open.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely states that the unused live-basis partial index is being removed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
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: Description check

Explanation

The description is detailed and covers the change, rationale, testing, operational ordering, and checklist. However, the required Linked Issue field is incomplete: it contains Fixes # without an issue number, although #21301 is mentioned elsewhere.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR

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

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Drops relay_connection_bases_live_deadline from SCHEMA — removes the CREATE INDEX ... WHERE active = 1 statement and its schema-deferrable comment block; replaces them with a -- Why not a partial index on active = 1 note above the surviving composite index that records the measurement.
  • Re-pins the maintenance-sweep plan test — postgres-maintenance-sweep-plans.test.ts narrows its regex back to relay_connection_bases_active_deadline instead of accepting either index.
  • Re-pins the credential-sweep plan test — credential-cleanup-sweep-postgres.test.ts swaps toMatch(/..._(active|live)_deadline/) for toContain('using relay_connection_bases_active_deadline') and updates the explanatory comment to say why the partial index was a dead end.
  • Updates the lock-target golden — removes the live-deadline entry from GOLDEN_LOCK_TAKING and from the expected deferrable-statement list in relay-schema-lock-targets.test.ts.

Checked repo-wide: no remaining references to relay_connection_bases_live_deadline in code, tests, docs, or workflow YAML. relayPostgresSchemaStatements() is SCHEMA.split(';') plus migrations, so the golden census and the pre-check test (which derives its counts at runtime) both lose exactly one entry consistently. The new comment correctly omits the schema-deferrable marker, which the composite index never had. The one operational hazard — deploy this image before running DROP INDEX CONCURRENTLY, so the old image's name-only pre-check cannot send a non-concurrent build over the 1.5 GB table — is well covered in the PR description, and is moot once this image is live.

I was unable to run the relay suite locally to confirm the plan assertions: the cloud/ workspace keeps its own node_modules that the root pnpm install does not populate (ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL … node_modules missing), which is environmental and unrelated to this diff. The change is schema text with no behavioral surface and the author reports the three touched tests and the full relay suite green.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@Jinwoo-H
Jinwoo-H merged commit ddbad22 into main Sep 17, 2026
26 checks passed
ivanvykopal pushed a commit to ivanvykopal/orca that referenced this pull request Sep 20, 2026
…r picks (stablyai#21305)

Added in stablyai#21301 on the reasoning that the composite (active, deadline)
index spans all 6.65M rows to find a few hundred live ones. Measured
post-merge against production-shaped history, that reasoning does not
hold: a basis is inserted active and flipped to 0, so the partial index
accumulates one dead entry per deactivation exactly as the composite one
does. Scan buffers are identical to composite-only in every state, 11,099
cold, 2,522 warm, 9 after VACUUM, and the planner picks the composite
index throughout. The extra index costs ~65 bytes of WAL per basis insert,
about 22% more.

The relief is the reaper plus vacuum, which stablyai#21301 already ships. Removes
the statement from SCHEMA, restores the plan test to pinning the composite
index by name, and records why a narrower index is not the cure so the
next reader does not re-derive it.
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.

1 participant