{"record":{"id":"275df2a59445f44d","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-bars-e","errorCode":null,"errorMessage":"Failed to load bars: {e}","messagePattern":"Failed to load bars: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1398,"sourceCode":"    }\n\n    /// Loads all `Bar` entries for `instrument_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT or deserialization fails.\n    pub async fn load_bars(\n        pool: &PgPool,\n        instrument_id: &InstrumentId,\n    ) -> anyhow::Result<Vec<Bar>> {\n        sqlx::query_as::<_, BarRow>(\n            r#\"SELECT * FROM \"bar\" WHERE instrument_id = $1 ORDER BY ts_event ASC\"#,\n        )\n        .bind(instrument_id.to_string())\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 bars: {e}\"))\n    }\n\n    /// Loads all distinct client order IDs from order events via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT or iteration fails.\n    pub async fn load_distinct_order_event_client_ids(\n        pool: &PgPool,\n    ) -> anyhow::Result<AHashMap<ClientOrderId, ClientId>> {\n        let mut map: AHashMap<ClientOrderId, ClientId> = AHashMap::new();\n        let result = sqlx::query_as::<_, OrderEventOrderClientIdCombination>(\n            r#\"\n            SELECT DISTINCT ON (client_order_id)\n                client_order_id AS \"client_order_id\",\n                client_id AS \"client_id\"\n            FROM \"order_event\"\n            WHERE client_id IS NOT NULL","sourceCodeStart":1380,"sourceCodeEnd":1416,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1380-L1416","documentation":"load_bars wraps any sqlx error from the SELECT of bars for an instrument_id into anyhow, discarding the typed sqlx error. It indicates the query `SELECT * FROM \"bar\" WHERE instrument_id = $1 ORDER BY ts_event ASC` failed at execution or row-decoding time. Rows are decoded into the Bar model via row.0, so a schema/type mismatch will surface here.","triggerScenarios":"Calling load_bars when the `bar` table doesn't exist, when stored rows have columns incompatible with the Bar row type (e.g. wrong column count/types after schema change), or when the pool connection fails.","commonSituations":"Database created by an older nautilus version whose `bar` table lacks columns expected by the current Bar model; connecting to the wrong database; DB offline mid-session.","solutions":["Inspect the chained sqlx error (`{:?}`) to distinguish connection failure from decode failure.","Run the crate's schema creation/migrations so the `bar` table matches the current model.","If decode errors occur, re-derive or migrate old rows to the current schema (column names/types/count).","Verify the instrument_id string format matches what was stored at insert time."],"exampleFix":"// before\nlet bars = queries::postgres::load_bars(&pool, instrument_id).await?;\n// after\nsqlx::query(\"SELECT 1 FROM \\\"bar\\\" LIMIT 1\").fetch_optional(&pool).await\n    .map_err(|e| anyhow::anyhow!(\"bar table unreadable, run migrations: {e}\"))?;\nlet bars = queries::postgres::load_bars(&pool, instrument_id).await?;","handlingStrategy":"try-catch","validationCode":"let ok = sqlx::query(r#\"SELECT 1 FROM \\\"bar\\\" LIMIT 1\"#).fetch_optional(pool).await.is_ok();\nanyhow::ensure!(ok, \"bar table missing or unreadable; run migrations first\");","typeGuard":null,"tryCatchPattern":"match queries::postgres::load_bars(&pool, instrument_id) {\n    Ok(bars) if bars.is_empty() => tracing::warn!(\"no bars for {instrument_id}\"),\n    Ok(bars) => use_bars(bars),\n    Err(e) => fallback_to_alternate_source(e),\n}","preventionTips":["Keep the DB schema in lockstep with your nautilus version (migrate on upgrade)","Probe the table with a cheap SELECT before bulk loads","Treat empty results and errors distinctly in callers","Verify instrument_id formatting matches stored values"],"tags":["rust","database","sqlx","anyhow","query"],"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"}