{"record":{"id":"85fa0a748d0449ee","repo":"nautechsystems/nautilus_trader","slug":"failed-to-inspect-legacy-execution-transactions","errorCode":null,"errorMessage":"Failed to inspect legacy execution transactions: {e}","messagePattern":"Failed to inspect legacy execution transactions: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":3247,"sourceCode":"        if let Some(installed_version) = installed_version\n            && installed_version > EXECUTION_SCHEMA_VERSION\n        {\n            anyhow::bail!(\n                \"Execution schema version {} is newer than supported version {EXECUTION_SCHEMA_VERSION}\",\n                installed_version\n            );\n        }\n\n        let unresolved_legacy = sqlx::query_scalar::<_, i64>(\n            \"\n            SELECT COUNT(*)\n            FROM execution_transaction\n            WHERE status IN ('pending', 'included', 'reverted')\n            \",\n        )\n        .fetch_one(&mut *transaction)\n        .await\n        .map_err(|e| anyhow::anyhow!(\"Failed to inspect legacy execution transactions: {e}\"))?;\n        anyhow::ensure!(\n            unresolved_legacy == 0,\n            \"Cannot safely migrate {unresolved_legacy} unresolved execution schema version 1 transaction(s); resolve them with the prior version before enabling version {EXECUTION_SCHEMA_VERSION}\"\n        );\n\n        for statement in [\n            \"\n            CREATE OR REPLACE FUNCTION execution_transaction_v2_fence()\n            RETURNS TRIGGER AS $$\n            BEGIN\n                RAISE EXCEPTION 'Legacy execution writer refused after schema version 2 activation';\n            END;\n            $$ LANGUAGE plpgsql\n            \",\n            \"DROP TRIGGER IF EXISTS execution_transaction_v2_fence ON execution_transaction\",\n            \"\n            CREATE TRIGGER execution_transaction_v2_fence\n            BEFORE INSERT OR UPDATE OR DELETE ON execution_transaction","sourceCodeStart":3229,"sourceCodeEnd":3265,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/2114cf6f761429e0adb5ca9596fcd7b895b16011/crates/adapters/blockchain/src/cache/database.rs#L3229-L3265","documentation":"Thrown when the COUNT(*) over legacy execution_transaction rows with status in ('pending','included','reverted') fails inside ensure_execution_transaction_schema. The table was locked ACCESS EXCLUSIVE moments earlier, so the failure is transport-level: dropped connection, statement timeout scanning a very large table, or the backend being terminated by an admin.","triggerScenarios":"A large legacy execution_transaction table combined with a low statement_timeout; pg_terminate_backend invoked while the count runs; network interruption while the exclusive lock is held.","commonSituations":"Long-lived nodes with years of transaction history; timeout values tuned for short OLTP queries.","solutions":["Re-run the migration once connectivity is confirmed stable","Temporarily raise statement_timeout for the migration session","Archive or trim legacy execution_transaction rows before upgrading"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"-- Estimate scan cost before upgrading\nSELECT reltuples::bigint AS approx_rows FROM pg_class WHERE relname = 'execution_transaction';","typeGuard":null,"tryCatchPattern":"match db.ensure_execution_transaction_schema().await {\n    Err(e) if e.to_string().contains(\"statement timeout\") || e.to_string().contains(\"canceling statement\") => {\n        // raise statement_timeout for the session and retry once\n    }\n    other => other?,\n}","preventionTips":["Trim or archive legacy execution_transaction rows before major upgrades","Set statement_timeout appropriate to table size for the migration session","Monitor connection stability (error rates) before scheduling migrations"],"tags":["rust","sqlx","postgresql","query-timeout","migration"],"backgroundTag":"database-query-failed","analyzedSha":"2114cf6f761429e0adb5ca9596fcd7b895b16011","analyzedAt":"2026-08-21T11:28:30.864Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}