Skip to content

perf(database): add status + createdAt composite indexes - #477

Open
francescoricc1 wants to merge 1 commit into
ASTROIDX556:mainfrom
francescoricc1:perf/293-status-created-at-indexes
Open

francescoricc1 wants to merge 1 commit into
ASTROIDX556:mainfrom
francescoricc1:perf/293-status-created-at-indexes

Conversation

@francescoricc1

@francescoricc1 francescoricc1 commented Oct 22, 2026 •

Copy link
Copy Markdown

Summary

Adds the missing composite (scope, status, createdAt) indexes behind the slow status-filtered record queries:

  • transactions (organizationId, status, createdAt) — status-filtered listing and dashboard aggregates
  • transactions (agentId, status, createdAt) — daily/weekly/monthly and rolling spend windows
  • agents (organizationId, status, createdAt) — status-filtered agent listing
  • wallets (organizationId, status, createdAt) — status-filtered wallet listing
  • users (organizationId, status, createdAt) — active-member filters and broadcast fan-out

Each index ships in its own CREATE INDEX CONCURRENTLY migration so writes are not blocked, with a matching idempotent down.sql rollback (DROP INDEX CONCURRENTLY IF EXISTS). schema.prisma declares every index so there is no drift, and src/database/index-migrations.spec.ts keeps the up/down SQL in sync.

Verification

  • prisma validate and tsc --noEmit pass
  • Full suite: 183 test files / 1906 tests pass
  • scripts/verify-migrations.sh replays the full history on a fresh Postgres 16 shadow database with no drift
  • EXPLAIN (ANALYZE, BUFFERS) against 200k transactions and 20k agents/wallets/users shows index scans on each new index (no sequential scans); rollback removes all five and re-apply restores them

Closes #293
closes #294
closes #298
closes #301

Status-filtered listings and agent spend windows currently combine
single-column status/createdAt indexes or sort every match, which shows up
as high query latency and CPU on the transactions ledger.

Add composite (organizationId|agentId, status, createdAt) indexes for
transactions, agents, wallets and users, each in its own concurrent
migration so writes stay unblocked. Ship idempotent down.sql rollbacks and
a structural test that keeps the SQL, schema declarations and rollbacks in
sync.

Closes ASTROIDX556#293

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant