{"record":{"id":"a7f9009c5fdfc80b","repo":"nautechsystems/nautilus_trader","slug":"failed-to-write-pool-data-e","errorCode":null,"errorMessage":"Failed to write pool data: {e}","messagePattern":"Failed to write pool data: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/copy.rs","lineNumber":902,"sourceCode":"\n        if let Some(ref initial_sqrt_price) = pool.initial_sqrt_price_x96 {\n            write_copy_numeric(&mut row_data, initial_sqrt_price);\n        } else {\n            row_data.write_all(&(-1i32).to_be_bytes())?; // NULL\n        }\n\n        if let Some(ref hooks) = pool.hooks {\n            let hooks_bytes = hooks.to_string().as_bytes().to_vec();\n            row_data.write_all(&(hooks_bytes.len() as i32).to_be_bytes())?;\n            row_data.write_all(&hooks_bytes)?;\n        } else {\n            row_data.write_all(&(-1i32).to_be_bytes())?; // NULL\n        }\n\n        copy_in\n            .send(row_data)\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to write pool data: {e}\"))?;\n        Ok(())\n    }\n\n    /// Writes the PostgreSQL COPY binary format trailer.\n    ///\n    /// The trailer is a 2-byte value of -1 to indicate end of data.\n    async fn write_copy_trailer(\n        &self,\n        copy_in: &mut sqlx::postgres::PgCopyIn<sqlx::pool::PoolConnection<sqlx::Postgres>>,\n    ) -> anyhow::Result<()> {\n        // Binary trailer: -1 as i16 to indicate end of data\n        let trailer = (-1i16).to_be_bytes();\n        copy_in\n            .send(trailer.to_vec())\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to write COPY trailer: {e}\"))?;\n        Ok(())\n    }","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/copy.rs#L884-L920","documentation":"write_pool_binary serializes a single pool row in PostgreSQL COPY binary format and sends it through the copy_in sink; failures are wrapped in this anyhow error. NULL fields are encoded as -1 i32 length prefixes, so errors here typically indicate sink/connection issues rather than value problems.","triggerScenarios":"copy_in.send(row_data) fails during copy_pools — the PostgreSQL connection dropped, the server aborted the COPY stream, or the row bytes no longer match the pool table's binary column layout.","commonSituations":"Long-running pool cache sync killed by network timeout; DDL change on the pool table invalidating the hard-coded binary field order; database connection pool closed by the application early.","solutions":["Check the wrapped source error to distinguish connectivity from encoding problems","Confirm the pools table schema still matches write_pool_binary's field order and types","Keep the connection/pool alive for the duration of the COPY (no early drops, adequate timeouts)","Re-run copy_pools; the aborted COPY is rolled back so retrying is safe"],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"// ensure pool table exists with expected columns before copy\nsqlx::query(\"SELECT 1 FROM information_schema.tables WHERE table_name='pool'\")\n    .fetch_optional(&pool).await?\n    .ok_or_else(|| anyhow::anyhow!(\"pool table missing; run migrations\"))?;","typeGuard":null,"tryCatchPattern":"async fn copy_pools_guarded(db: &CacheDatabase, chain: &Chain, pools: &[Pool]) -> anyhow::Result<()> {\n    db.copy_pools(chain, pools).await.map_err(|e| {\n        anyhow::anyhow!(\"copy_pools failed (partitions created? schema current?): {e:#}\")\n    })\n}","preventionTips":["Call create_block_partition/create_token_partition during cache init, before any copy","Keep the hard-coded binary field order in sync with schema migrations","Avoid dropping the connection pool while copy operations are in flight","Set generous statement timeouts for bulk load sessions"],"tags":["database","postgres","copy","binary-format"],"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"}