{"record":{"id":"c331881866a3bcad","repo":"nautechsystems/nautilus_trader","slug":"failed-to-commit-order-client-origins-e","errorCode":null,"errorMessage":"Failed to commit order client origins: {e}","messagePattern":"Failed to commit order client origins: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1504,"sourceCode":"                WHERE client_order_id = $1\n                  AND (client_id IS NULL OR client_id = $2)\n            \"#,\n            )\n            .bind(client_order_id.to_string())\n            .bind(client_id.to_string())\n            .execute(&mut *transaction)\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to index order client origin: {e}\"))?;\n\n            if result.rows_affected() == 0 {\n                anyhow::bail!(\"No persisted order events found for {client_order_id}\");\n            }\n        }\n\n        transaction\n            .commit()\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to commit order client origins: {e}\"))\n    }\n\n    /// Inserts or updates an order ID to position ID index entry via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL INSERT or UPDATE operation fails.\n    pub async fn index_order_position(\n        pool: &PgPool,\n        client_order_id: ClientOrderId,\n        position_id: PositionId,\n    ) -> anyhow::Result<()> {\n        sqlx::query(\n            r#\"\n            INSERT INTO \"order_position_index\" (\n                client_order_id, position_id, created_at, updated_at\n            ) VALUES (\n                $1, $2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP","sourceCodeStart":1486,"sourceCodeEnd":1522,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1486-L1522","documentation":"At the end of index_order_clients the database transaction is committed; any sqlx commit failure is wrapped as 'Failed to commit order client origins'. The prior statements succeeded but finalizing them atomically failed — usually a lost connection or serialization/lock conflict at commit time. All changes roll back, so callers can safely retry the whole operation.","triggerScenarios":"Calling index_order_clients when the connection drops between the UPDATEs and commit, the DB is shutting down, or a constraint/concurrency conflict surfaces only at COMMIT.","commonSituations":"Long-running index rebuild against a remote Postgres with an idle-timeout killing the connection; DB restart; network partition between nautilus and the cache DB.","solutions":["Simply retry index_order_clients — the rolled-back transaction leaves no partial state.","Check DB server logs for the commit failure cause (connection reset, serialization failure).","Keep the transaction short; batch large rebuilds into smaller transactions if timeouts occur.","Ensure pool timeouts and keepalives are configured for long operations."],"exampleFix":"// before\nqueries::postgres::index_order_clients(&mut tx, &client_order_id, &client_id).await?;\n// after\nfor attempt in 0..3 {\n    match try_index_clients(&pool, &client_order_id, &client_id).await {\n        Ok(_) => break,\n        Err(e) if attempt < 2 && is_transient(&e) => tokio::time::sleep(backoff(attempt)).await,\n        Err(e) => return Err(e),\n    }\n}","handlingStrategy":"retry","validationCode":"let healthy = pool.acquire().await.is_ok();\nanyhow::ensure!(healthy, \"database pool unhealthy before index commit\");","typeGuard":null,"tryCatchPattern":"for attempt in 0..3 {\n    match index_clients_in_tx(&pool, &client_order_id, &client_id).await {\n        Ok(_) => break,\n        Err(e) if attempt < 2 && e.to_string().contains(\"commit\") => {\n            tokio::time::sleep(std::time::Duration::from_millis(250 * (attempt + 1))).await;\n        }\n        Err(e) => return Err(e),\n    }\n}","preventionTips":["Retry whole index operations — rollback makes them idempotent","Keep transactions short; batch large rebuilds","Configure DB idle/connection timeouts for long-running jobs","Monitor DB server logs for commit-time connection resets"],"tags":["rust","database","transaction","commit","anyhow"],"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"}