{"record":{"id":"01131bbf73e95a34","repo":"nautechsystems/nautilus_trader","slug":"failed-to-delete-position-event-rows-e","errorCode":null,"errorMessage":"Failed to delete position_event rows: {e}","messagePattern":"Failed to delete position_event rows: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":865,"sourceCode":"    ) -> anyhow::Result<()> {\n        let event_position_id = Self::event_position_id(event)?;\n        if event_position_id != position_id {\n            anyhow::bail!(\n                \"Cannot persist position event {} for mismatched position_id: expected {}, was {}\",\n                event.event_id,\n                position_id,\n                event_position_id\n            );\n        }\n\n        let mut transaction = pool.begin().await?;\n\n        sqlx::query(r#\"DELETE FROM \"position_event\" WHERE position_id = $1\"#)\n            .bind(position_id.to_string())\n            .execute(&mut *transaction)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to delete position_event rows: {e}\"))?;\n\n        Self::insert_position_event(&mut transaction, event).await?;\n        transaction\n            .commit()\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to commit transaction: {e}\"))\n    }\n\n    /// Appends a fill event for a `Position` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the fill is invalid or if the SQL operations fail.\n    pub async fn update_position(pool: &PgPool, event: &OrderFilled) -> anyhow::Result<()> {\n        let mut transaction = pool.begin().await?;\n\n        Self::insert_position_event(&mut transaction, event).await?;\n        transaction","sourceCodeStart":847,"sourceCodeEnd":883,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L847-L883","documentation":"add_position opens a transaction that deletes existing position_event rows for the position_id before inserting the new snapshot event; when the DELETE fails the sqlx error is wrapped in this message. The transaction is then dropped (rolled back), leaving the position log unchanged.","triggerScenarios":"Calling add_position(pool, position_id, event) when the DELETE FROM position_event statement errors: table missing, connection lost, lock timeout, or insufficient privileges.","commonSituations":"Schema not migrated; concurrent writers holding row locks on the same position_id; database failover mid-transaction; read-only replica used by mistake.","solutions":["Inspect the wrapped sqlx error to identify the DELETE failure cause.","Run the nautilus migrations so the position_event table exists.","Ensure writers target the primary database and that concurrent add_position calls for the same position_id are serialized.","Check permissions for the DB user (DELETE on position_event)."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"sqlx::query(\"SELECT 1 FROM position_event LIMIT 1\").fetch_optional(&pool).await?;","typeGuard":null,"tryCatchPattern":"match add_position(&pool, &pid, &event).await {\n    Err(e) if is_transient_db_error(&e) => retry_with_backoff(3),\n    other => other,\n}","preventionTips":["Serialize add_position calls for the same position_id to avoid lock contention.","Always write to the primary, never a read replica.","Ensure the position_event table exists via migrations."],"tags":["rust","database","sqlx","transaction","postgres"],"backgroundTag":"database-write-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"}