{"record":{"id":"31f1a02c6eaba73a","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-instruments-e","errorCode":null,"errorMessage":"Failed to load instruments: {e}","messagePattern":"Failed to load instruments: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":239,"sourceCode":"            .fetch_optional(pool)\n            .await\n            .map(|instrument| instrument.map(|row| row.0))\n            .map_err(|e| {\n                anyhow::anyhow!(\"Failed to load instrument with id {instrument_id},error is: {e}\")\n            })\n    }\n\n    /// Loads all `InstrumentAny` entries via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SELECT operation fails.\n    pub async fn load_instruments(pool: &PgPool) -> anyhow::Result<Vec<InstrumentAny>> {\n        sqlx::query_as::<_, InstrumentAnyRow>(\"SELECT * FROM instrument\")\n            .fetch_all(pool)\n            .await\n            .map(|rows| rows.into_iter().map(|row| row.0).collect())\n            .map_err(|e| anyhow::anyhow!(\"Failed to load instruments: {e}\"))\n    }\n\n    /// Inserts or replaces an `InstrumentClose`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL INSERT or UPDATE fails.\n    pub async fn add_instrument_close(\n        pool: &PgPool,\n        close: &InstrumentClose,\n    ) -> anyhow::Result<()> {\n        sqlx::query(\n            r#\"\n            INSERT INTO \"instrument_close\" (\n                instrument_id, close_price, close_type, ts_event, ts_init, created_at\n            ) VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)\n            ON CONFLICT (instrument_id) DO UPDATE\n            SET close_price = EXCLUDED.close_price,","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L221-L257","documentation":"This error is raised by `load_instruments` in the SQL queries module when the `SELECT * FROM instrument` query against PostgreSQL fails. The library wraps any sqlx error returned by `fetch_all` into an `anyhow::Error` with this message, so the underlying cause (connection issue, missing table, bad column mapping) is embedded in the `{e}` text. It means the instruments could not be read from the database, and the caller receives no instrument data.","triggerScenarios":"Calling `load_instruments(&pool)` when: the database connection is down or the pool is exhausted; the `instrument` table does not exist (migrations not run); a column type in the table does not match `InstrumentAnyRow`'s expected types (sqlx decode failure); or the query times out.","commonSituations":"Running against a database without the Nautilus schema migrations applied; wrong DATABASE_URL pointing at an empty/unrelated database; schema drift after upgrading the crate where a new/renamed column breaks the row decoding; PostgreSQL temporarily unreachable in a containerized environment.","solutions":["Inspect the wrapped `{e}` message to identify the root cause (connection vs missing table vs decode error).","Run the project's SQL migrations so the `instrument` table exists with the expected schema.","Verify the PostgreSQL connection string / pool configuration and that the database is reachable.","If it is a decode/type error, align the database column types with `InstrumentAnyRow` (upgrade schema or crate together)."],"exampleFix":"// before: calls the query and panics/unwraps elsewhere\nlet instruments = load_instruments(&pool).await.unwrap();\n\n// after: handle the error explicitly\nlet instruments = match load_instruments(&pool).await {\n    Ok(v) => v,\n    Err(e) => {\n        tracing::error!(\"instrument load failed: {e:#}\");\n        return Err(e);\n    }\n};","handlingStrategy":"try-catch","validationCode":"// Before calling: verify DB reachable and table exists\nlet ok = sqlx::query_scalar::<_, i64>(\n    \"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'instrument'\")\n    .fetch_one(pool).await? > 0;\nif !ok { return Err(anyhow::anyhow!(\"instrument table missing: run migrations\")); }","typeGuard":null,"tryCatchPattern":"match load_instruments(&pool).await {\n    Ok(instruments) => instruments,\n    Err(e) => {\n        tracing::error!(\"load_instruments failed: {e:#}\");\n        return Err(anyhow::anyhow!(\"instruments unavailable: {e:#}\"));\n    }\n}","preventionTips":["Run SQL migrations as a startup step before any query functions are used.","Validate DATABASE_URL and connectivity with a `SELECT 1` at application start.","Keep the crate version and database schema in lockstep when upgrading.","Enable sqlx logging (`RUST_LOG=sqlx=debug`) to see root-cause errors early."],"tags":["database","postgres","sqlx","rust","instruments"],"backgroundTag":"database-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}