mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 12:19:20 +01:00
Database performance audit (2026-05-28, four-lens expert pass) flagged
that the messages / notifications / pool_memberships / inventory_items
tables each have a query pattern that PostgreSQL can only satisfy
today by picking one single-column index and post-filtering — or by
doing a full sort after the index scan.
None of these is currently a visible bug — message volume per
conversation, notifications per user, members per pool, and pipeline
items per assignee are all modest. But each query path grows linearly
with platform activity, so add the right indexes now while it's cheap.
Adds:
- idx_messages_conversation_created (conversation_id, created_at DESC)
→ "last N messages in this chat" stops sorting in memory
- idx_notifications_user_read_at (user_id, read_at DESC NULLS FIRST)
→ dashboard bell + list serve from one index instead of two
- idx_pool_memberships_pool_status (pool_id, status)
→ the pool-join FOR UPDATE handler (a7d5c818) no longer scans every
historical membership when counting active seats
- idx_inventory_status_assigned (status, assigned_to)
→ erfassung admin's "my queue" + status filter combine cleanly
All four use IF NOT EXISTS so the migration is idempotent. No schema
changes, no data backfill, no locking concerns — pure index creation.
Operator note: the dev runner scripts/db/run-migration.sh targets
localhost:5433 and doesn't reach Neon prod. This migration file needs
to be applied to Neon manually (psql against DATABASE_URL, or the
Neon SQL editor) for the perf win to land in production. The dev
container picks it up on next `npm run db:migrate`.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
51 lines
2.7 KiB
SQL
51 lines
2.7 KiB
SQL
-- Migration 074: Composite indexes for hot list-query paths
|
|
--
|
|
-- Audit (2026-05-28, performance lens) flagged four query patterns that
|
|
-- currently force PostgreSQL to either pick one single-column index and
|
|
-- post-filter on the other column, or to do a full sort after an index
|
|
-- scan. None of the four tables has a workload large enough TODAY for
|
|
-- this to be visibly slow, but each is on a path that grows linearly
|
|
-- with platform activity (messages per chat, notifications per user,
|
|
-- pool members, inventory pipeline) — so the right move is to add the
|
|
-- composites now, before they become a real "page is sluggish" bug.
|
|
--
|
|
-- All four indexes use IF NOT EXISTS so the migration is idempotent
|
|
-- and safe to re-run.
|
|
|
|
-- ----------------------------------------------------------------------
|
|
-- messages: "fetch last N messages in conversation X" — currently uses
|
|
-- idx_messages_conversation (single column) then sorts the result by
|
|
-- created_at. The composite supports both filter and sort in one scan.
|
|
-- ----------------------------------------------------------------------
|
|
CREATE INDEX IF NOT EXISTS idx_messages_conversation_created
|
|
ON messages (conversation_id, created_at DESC);
|
|
|
|
-- ----------------------------------------------------------------------
|
|
-- notifications: "list user X's notifications, unread first" — currently
|
|
-- has idx_notifications_user and idx_notifications_read as two separate
|
|
-- single-column indexes. Composite enables the dashboard bell + list
|
|
-- queries to use one scan. read_at DESC NULLS LAST so unread (NULL
|
|
-- read_at) sorts to the top.
|
|
-- ----------------------------------------------------------------------
|
|
CREATE INDEX IF NOT EXISTS idx_notifications_user_read_at
|
|
ON notifications (user_id, read_at DESC NULLS FIRST);
|
|
|
|
-- ----------------------------------------------------------------------
|
|
-- pool_memberships: "active members of pool X" — used by the pool join
|
|
-- handler (which now also takes FOR UPDATE on subscription_pools) to
|
|
-- count active members before allowing a new join. Without this, the
|
|
-- count subquery scans every membership row for the pool regardless of
|
|
-- status.
|
|
-- ----------------------------------------------------------------------
|
|
CREATE INDEX IF NOT EXISTS idx_pool_memberships_pool_status
|
|
ON pool_memberships (pool_id, status);
|
|
|
|
-- ----------------------------------------------------------------------
|
|
-- inventory_items: "items assigned to me, filtered by status" — the
|
|
-- erfassung admin's pipeline view and "my queue" filters. Currently
|
|
-- relies on either idx_inventory_status or idx_inventory_assigned_to,
|
|
-- never both.
|
|
-- ----------------------------------------------------------------------
|
|
CREATE INDEX IF NOT EXISTS idx_inventory_status_assigned
|
|
ON inventory_items (status, assigned_to);
|