{"record":{"id":"4cc403ecaae64e56","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-position-events-e","errorCode":null,"errorMessage":"Failed to load position events: {e}","messagePattern":"Failed to load position events: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":925,"sourceCode":"    ///\n    /// Returns an error if the SQL SELECT or deserialization fails.\n    pub async fn load_position_events(\n        pool: &PgPool,\n        position_id: &PositionId,\n    ) -> anyhow::Result<Vec<OrderFilled>> {\n        sqlx::query_as::<_, OrderFilledRow>(\n            r#\"\n            SELECT *\n            FROM \"position_event\"\n            WHERE position_id = $1\n            ORDER BY event_sequence ASC\n        \"#,\n        )\n        .bind(position_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 position events: {e}\"))\n    }\n\n    /// Loads and replays a complete `Position` for a `position_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if loading events, loading instruments, or replaying fills fails.\n    pub async fn load_position(\n        pool: &PgPool,\n        position_id: &PositionId,\n    ) -> anyhow::Result<Option<Position>> {\n        if let Some(snapshot) = Self::load_position_snapshot(pool, position_id).await?\n            && let Some(replay_state) = snapshot.replay_state\n        {\n            return serde_json::from_value(replay_state)\n                .map(Some)\n                .map_err(|e| anyhow::anyhow!(\"Failed to decode position replay state: {e}\"));\n        }","sourceCodeStart":907,"sourceCodeEnd":943,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L907-L943","documentation":"load_position_events runs a SELECT returning the fill events (JSON) for a position_id; when the query fails the sqlx error is wrapped in this message. The caller cannot replay the position because its events could not be read.","triggerScenarios":"Calling load_position_events(pool, position_id) where the fetch_all on the position events query errors: table missing, connection failure, or JSON column type mismatch.","commonSituations":"Schema not migrated; database unreachable; corrupted JSONB values causing decode errors during row fetch; wrong database pointed to by configuration.","solutions":["Inspect the wrapped sqlx error for the exact cause (relation missing, decode error, connection).","Apply the nautilus SQL migrations to create the position_event table.","Verify the database connection settings point at the intended schema/database.","If a row fails to decode as JSON, inspect and repair or remove the offending position_event row."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"sqlx::query(\"SELECT 1 FROM position_event LIMIT 1\").fetch_optional(&pool).await?;","typeGuard":null,"tryCatchPattern":"match load_position_events(&pool, &pid).await {\n    Err(e) if is_transient_db_error(&e) => retry_with_backoff(3),\n    other => other,\n}","preventionTips":["Apply migrations before reading positions.","Validate JSONB integrity of position_event rows after manual data operations.","Point configuration at the correct database/schema."],"tags":["rust","database","sqlx","postgres","json"],"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"}