From 0033933a58ba4c0ce9bc540f3e12044018bdebb0 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 11 Nov 2026 02:49:37 +1100 Subject: [PATCH] perf(store): index the six unindexed foreign keys (#1860) Postgres does not index the referencing side of a foreign key, so deleting or updating a parent row scanned the child table once per referencing row for the six keys that had no matching index: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. Migration 0074 adds one supporting index per key; the foreign-key integrity checks on the restored snapshot pass, with every one of the 17 constraints now covered. --- CHANGELOG.md | 4 ++++ .../postgres/migrations/0074_fk_indexes.sql | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 internal/infrastructure/postgres/migrations/0074_fk_indexes.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index f201e3b17..12e73c3d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,10 @@ release's per-component sections are labelled beneath its heading. - Conversation titles are written from your message and Archie's reply, name what the conversation is about rather than the mood, and wait for an exchange with a subject. - Session memory never records passwords, tokens or other secrets, and adds only facts it does not already hold, so memory stops filling with restated duplicates. +### archie-state-store + +- Six foreign keys now have an index on the referencing column, so deleting a parent row looks its children up instead of scanning the child table: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. + ## [1.62.0] - 2026-11-10 ### archied diff --git a/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql b/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql new file mode 100644 index 000000000..79953fd98 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql @@ -0,0 +1,19 @@ +-- +goose Up +-- Postgres does not index the referencing side of a foreign key, so a parent +-- delete or key update scans the child table for each referencing row. These +-- indexes back the six child lookups that had no matching index. +CREATE INDEX idx_identity_aliases_identity_id ON identity_aliases (identity_id); +CREATE INDEX idx_identity_subjects_identity_id ON identity_subjects (identity_id); +CREATE INDEX idx_memberships_org_id ON memberships (org_id); +CREATE INDEX idx_org_agents_org_id ON org_agents (org_id); +CREATE INDEX idx_installed_package_requirements_org_id_required_name ON installed_package_requirements (org_id, required_name); +CREATE INDEX idx_tasks_handoff_from_task_id ON tasks (handoff_from_task_id); + +-- +goose Down + +DROP INDEX idx_tasks_handoff_from_task_id; +DROP INDEX idx_installed_package_requirements_org_id_required_name; +DROP INDEX idx_org_agents_org_id; +DROP INDEX idx_memberships_org_id; +DROP INDEX idx_identity_subjects_identity_id; +DROP INDEX idx_identity_aliases_identity_id;