{"record":{"id":"fff9d398655a4ab5","repo":"nautechsystems/nautilus_trader","slug":"failed-to-write-pool-swap-data-e","errorCode":null,"errorMessage":"Failed to write pool swap data: {e}","messagePattern":"Failed to write pool swap data: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/copy.rs","lineNumber":538,"sourceCode":"            row_data.write_all(spot_price_bytes)?;\n\n            let exec_price_decimal = trade_info.execution_price.as_decimal();\n            let exec_price_str = exec_price_decimal.to_string();\n            let exec_price_bytes = exec_price_str.as_bytes();\n            row_data.write_all(&(exec_price_bytes.len() as i32).to_be_bytes())?;\n            row_data.write_all(exec_price_bytes)?;\n        } else {\n            row_data.write_all(&(-1i32).to_be_bytes())?;\n            row_data.write_all(&(-1i32).to_be_bytes())?;\n            row_data.write_all(&(-1i32).to_be_bytes())?;\n            row_data.write_all(&(-1i32).to_be_bytes())?;\n            row_data.write_all(&(-1i32).to_be_bytes())?;\n        }\n\n        copy_in\n            .send(row_data)\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to write pool swap data: {e}\"))?;\n        Ok(())\n    }\n\n    /// Writes a single pool liquidity update in PostgreSQL binary format.\n    ///\n    /// Each row in binary format consists of:\n    /// - 2-byte field count\n    /// - For each field: 4-byte length followed by data (or -1 for NULL)\n    async fn write_pool_liquidity_update_binary(\n        &self,\n        copy_in: &mut sqlx::postgres::PgCopyIn<sqlx::pool::PoolConnection<sqlx::Postgres>>,\n        chain_id: u32,\n        update: &PoolLiquidityUpdate,\n    ) -> anyhow::Result<()> {\n        use std::io::Write;\n        let mut row_data = Vec::new();\n\n        row_data.write_all(&15u16.to_be_bytes())?;","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/copy.rs#L520-L556","documentation":"write_pool_swap_binary sends one serialized PoolSwap row over the COPY BINARY stream; failure from PgCopyIn::send is wrapped in this error and aborts the whole copy_pool_swaps batch. Indicates the connection dropped mid-copy or the server already aborted the COPY session (a prior row failed).","triggerScenarios":"Connection reset during a large swap batch, server-side abort propagating to the next send (constraint failure on an earlier swap), mismatch between serialized fields and the pool_swap COPY column list.","commonSituations":"High-volume swap backfills over flaky networks; swaps referencing uncached pools/tokens causing earlier-row failures that surface here; schema drift after adding a column to pool_swap.","solutions":["Check the wrapped sqlx error detail for the root cause (transport vs server abort)","Validate that all referenced pools/tokens exist before copying swaps","Re-verify write_pool_swap_binary's field order/count against the COPY statement","Chunk the swaps list and retry to narrow down the failing row"],"exampleFix":"// before\nhandler.copy_pool_swaps(chain_id, &swaps).await?;\n// after\nfor chunk in swaps.chunks(1000) {\n    handler.copy_pool_swaps(chain_id, chunk).await?;\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = handler.copy_pool_swaps(chain_id, &swaps).await {\n    if e.to_string().contains(\"Failed to write pool swap data\") {\n        tokio::time::sleep(Duration::from_secs(5)).await;\n        return handler.copy_pool_swaps(chain_id, &swaps).await; // COPY is atomic, safe to retry batch\n    }\n    return Err(e);\n}","preventionTips":["Ensure pools/tokens exist before swaps to avoid server-side aborts","Chunk large swap lists","Keep the serializer in sync with the COPY column list","Add connection health checks to PgPoolOptions"],"tags":["postgres","copy-binary","streaming"],"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-14T00:17:10.932Z"}