{"record":{"id":"5c9ce9e864a46ac7","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-order-position-index-e","errorCode":null,"errorMessage":"Failed to load order position index: {e}","messagePattern":"Failed to load order position index: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1557,"sourceCode":"    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT or iteration fails.\n    pub async fn load_index_order_position(\n        pool: &PgPool,\n    ) -> anyhow::Result<AHashMap<ClientOrderId, PositionId>> {\n        let mut map: AHashMap<ClientOrderId, PositionId> = AHashMap::new();\n        let result = sqlx::query_as::<_, OrderPositionIndexRow>(\n            r#\"\n            SELECT\n                client_order_id AS \"client_order_id\",\n                position_id AS \"position_id\"\n            FROM \"order_position_index\"\n        \"#,\n        )\n        .fetch_all(pool)\n        .await\n        .map_err(|e| anyhow::anyhow!(\"Failed to load order position index: {e}\"))?;\n\n        for row in result {\n            map.insert(row.client_order_id, row.position_id);\n        }\n        Ok(map)\n    }\n\n    /// Inserts a `Signal` entry via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL INSERT operation fails.\n    pub async fn add_signal(pool: &PgPool, signal: &Signal) -> anyhow::Result<()> {\n        sqlx::query(\n            r#\"\n            INSERT INTO \"signal\" (\n                name, value, ts_event, ts_init, created_at, updated_at\n            ) VALUES (","sourceCodeStart":1539,"sourceCodeEnd":1575,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1539-L1575","documentation":"This error is raised by `load_index_order_position` when the SQLX query that reads all rows of the `order_position_index` table fails. The library wraps the underlying sqlx error via `anyhow::anyhow!` so callers get a single anyhow::Error carrying the database failure message. It means the SELECT against the PostgreSQL pool could not be executed or fetched.","triggerScenarios":"Calling `load_index_order_position(pool)` when the PostgreSQL server is unreachable, the `order_position_index` table does not exist (missing migration), the connection pool is exhausted/closed, credentials are wrong, or a transient network failure occurs during `fetch_all`.","commonSituations":"Running against a database where migrations were never applied or were partially applied; connection drops after idle timeouts; wrong DATABASE_URL or insufficient privileges to SELECT from the table; connecting to a database of a different schema version.","solutions":["Verify PostgreSQL connectivity and credentials in the pool config (test with psql).","Apply migrations so the `order_position_index` table exists (check the sqlx migrations directory).","Inspect the inner sqlx error message embedded in `{e}` for the exact DB-level cause.","Retry on transient errors; enable pool acquire_timeout and max_lifetime tuning if idle disconnects recur."],"exampleFix":"// before\nlet map = load_index_order_position(&pool).await?;\n// after\nmatch load_index_order_position(&pool).await {\n    Ok(map) => map,\n    Err(e) => { tracing::error!(\"order position index load failed: {e:#}\"); return Err(e); }\n}","handlingStrategy":"try-catch","validationCode":"// check table exists before loading\nlet exists: bool = sqlx::query_scalar(\n    \"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'order_position_index')\")\n    .fetch_one(pool).await?;\nanyhow::ensure!(exists, \"order_position_index table missing; run migrations\");","typeGuard":null,"tryCatchPattern":"match load_index_order_position(&pool).await {\n    Ok(map) => map,\n    Err(e) => {\n        tracing::error!(\"index load failed: {e:#}\");\n        if is_transient(&e) { /* retry with backoff */ }\n        return Err(e);\n    }\n}","preventionTips":["Run migrations at startup before any query functions","Validate DATABASE_URL connectivity at boot","Set pool acquire_timeout and test connections","Monitor for idle connection drops"],"tags":["database","sqlx","postgresql"],"backgroundTag":"sql-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}