fix(db): bound automation log truth-chain lookups
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
Your Name
2026-07-01 09:00:34 +08:00
parent 444de0b40c
commit 548127ffb0
5 changed files with 124 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
-- P0 post-reboot CPU pressure: bound truth-chain automation_operation_log lookups.
-- Run outside an explicit transaction because CREATE INDEX CONCURRENTLY requires it.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_incident_text
ON automation_operation_log ((incident_id::text))
WHERE incident_id IS NOT NULL;
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_input_text_trgm
ON automation_operation_log
USING gin ((coalesce(input::text, '')) gin_trgm_ops);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_output_text_trgm
ON automation_operation_log
USING gin ((coalesce(output::text, '')) gin_trgm_ops);

View File

@@ -0,0 +1,6 @@
-- Roll back only indexes owned by the P0 automation_operation_log truth-chain migration.
-- Do not drop pg_trgm; the extension may be shared by other indexes.
DROP INDEX CONCURRENTLY IF EXISTS idx_automation_operation_log_output_text_trgm;
DROP INDEX CONCURRENTLY IF EXISTS idx_automation_operation_log_input_text_trgm;
DROP INDEX CONCURRENTLY IF EXISTS idx_automation_operation_log_incident_text;

View File

@@ -5,6 +5,8 @@ MIGRATION = "awooop_conversation_event_hot_path_indexes_2026-07-01.sql"
ROLLBACK = "awooop_conversation_event_hot_path_indexes_2026-07-01_down.sql"
CONTENT_PREVIEW_MIGRATION = "awooop_conversation_event_content_preview_trgm_2026-07-01.sql"
CONTENT_PREVIEW_ROLLBACK = "awooop_conversation_event_content_preview_trgm_2026-07-01_down.sql"
AUTOMATION_LOG_MIGRATION = "automation_operation_log_truth_chain_hot_path_indexes_2026-07-01.sql"
AUTOMATION_LOG_ROLLBACK = "automation_operation_log_truth_chain_hot_path_indexes_2026-07-01_down.sql"
def _read(path: str) -> str:
@@ -27,6 +29,14 @@ def _content_preview_rollback_sql() -> str:
return _read(f"migrations/{CONTENT_PREVIEW_ROLLBACK}")
def _automation_log_migration_sql() -> str:
return _read(f"migrations/{AUTOMATION_LOG_MIGRATION}")
def _automation_log_rollback_sql() -> str:
return _read(f"migrations/{AUTOMATION_LOG_ROLLBACK}")
def test_hot_path_indexes_are_concurrent_and_transactionless() -> None:
sql = _migration_sql()
@@ -125,3 +135,34 @@ def test_content_preview_trgm_index_bounds_truth_chain_preview_fallback() -> Non
assert "DROP INDEX CONCURRENTLY IF EXISTS idx_awooop_conv_event_content_preview_trgm;" in rollback
assert "DROP INDEX CONCURRENTLY IF EXISTS idx_awooop_conv_event_project_content_preview_trgm;" in rollback
assert "DROP EXTENSION" not in rollback
def test_automation_operation_log_indexes_bound_truth_chain_lookups() -> None:
sql = _automation_log_migration_sql()
rollback = _automation_log_rollback_sql()
truth_chain_source = _read("src/services/awooop_truth_chain_service.py")
assert "BEGIN;" not in sql
assert "COMMIT;" not in sql
assert "CREATE EXTENSION IF NOT EXISTS pg_trgm" in sql
assert "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_incident_text" in sql
assert "ON automation_operation_log ((incident_id::text))" in sql
assert "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_input_text_trgm" in sql
assert "coalesce(input::text, '')" in sql
assert "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_automation_operation_log_output_text_trgm" in sql
assert "coalesce(output::text, '')" in sql
assert "gin_trgm_ops" in sql
assert "FROM automation_operation_log" in truth_chain_source
assert "incident_id::text" in truth_chain_source
assert "coalesce(input::text, '') LIKE" in truth_chain_source
assert "coalesce(output::text, '') LIKE" in truth_chain_source
assert "coalesce(array_to_string(tags, ','), '') LIKE" in truth_chain_source
for index_name in (
"idx_automation_operation_log_output_text_trgm",
"idx_automation_operation_log_input_text_trgm",
"idx_automation_operation_log_incident_text",
):
assert f"DROP INDEX CONCURRENTLY IF EXISTS {index_name};" in rollback
assert "DROP EXTENSION" not in rollback