Files
sam 0033933a58 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.
2026-11-11 02:49:37 +11:00

20 lines
1012 B
SQL

-- +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;