{"record":{"id":"e496f7993bc4a068","repo":"nautechsystems/nautilus_trader","slug":"failed-to-set-work-mem-e","errorCode":null,"errorMessage":"Failed to set work_mem: {e}","messagePattern":"Failed to set work_mem: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":1416,"sourceCode":"    ///\n    /// # Errors\n    ///\n    /// Returns an error if the database operations fail.\n    pub async fn toggle_perf_sync_settings(&self, enable: bool) -> anyhow::Result<()> {\n        if enable {\n            log::debug!(\"Enabling performance sync settings for bulk operations\");\n\n            // Set synchronous_commit to OFF for maximum write performance\n            sqlx::query(\"SET synchronous_commit = OFF\")\n                .execute(&self.pool)\n                .await\n                .map_err(|e| anyhow::anyhow!(\"Failed to set synchronous_commit OFF: {e}\"))?;\n\n            // Increase work_mem for bulk operations\n            sqlx::query(\"SET work_mem = '256MB'\")\n                .execute(&self.pool)\n                .await\n                .map_err(|e| anyhow::anyhow!(\"Failed to set work_mem: {e}\"))?;\n\n            log::debug!(\"Performance settings enabled: synchronous_commit=OFF, work_mem=256MB\");\n        } else {\n            log::debug!(\"Restoring default safe database performance settings\");\n\n            // Restore synchronous_commit to ON for data safety\n            sqlx::query(\"SET synchronous_commit = ON\")\n                .execute(&self.pool)\n                .await\n                .map_err(|e| anyhow::anyhow!(\"Failed to set synchronous_commit ON: {e}\"))?;\n\n            // Reset work_mem to default\n            sqlx::query(\"RESET work_mem\")\n                .execute(&self.pool)\n                .await\n                .map_err(|e| anyhow::anyhow!(\"Failed to reset work_mem: {e}\"))?;\n        }\n","sourceCodeStart":1398,"sourceCodeEnd":1434,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L1398-L1434","documentation":"toggle_perf_sync_settings failed while issuing the SET work_mem statement to PostgreSQL while enabling bulk-sync performance settings; the database rejected or could not execute the session-level tuning statement, so the performance-sync toggle aborts.","triggerScenarios":"Calling the performance-toggling method with enabled=true when the SET work_mem statement errors: value exceeding the server's max allowed, insufficient role privileges, invalid syntax interpretation, or a broken connection.","commonSituations":"Managed Postgres instances capping work_mem below 256MB or restricting SET; roles lacking permission to raise session memory settings; connection dropped between the synchronous_commit and work_mem statements.","solutions":["Check the embedded `{e}`; lower the requested work_mem (e.g. '64MB') if the server rejects the value.","Grant the role permission or connect as a user allowed to change session-level settings.","Verify the connection is still alive; reconnect and reapply settings if the pool returned a dead connection.","Fall back to default work_mem if the environment disallows raising it."],"exampleFix":"// before\nsqlx::query(\"SET work_mem = '256MB'\").execute(&self.pool).await\n    .map_err(|e| anyhow::anyhow!(\"Failed to set work_mem: {e}\"))?;\n// after: use a server-safe value\nsqlx::query(\"SET work_mem = '64MB'\").execute(&self.pool).await\n    .map_err(|e| anyhow::anyhow!(\"Failed to set work_mem: {e}\"))?;","handlingStrategy":"fallback","validationCode":"// check the server's cap before requesting a work_mem value\nlet max_mem: Option<(String,)> = sqlx::query_as(\"SHOW max_wal_size\") /* or check role limits */\n    .fetch_optional(&pool).await?;","typeGuard":null,"tryCatchPattern":"let applied = sqlx::query(\"SET work_mem = '256MB'\").execute(&self.pool).await\n    .or_else(|_| sqlx::query(\"SET work_mem = '64MB'\").execute(&self.pool).await);\nif let Err(e) = applied {\n    log::warn!(\"work_mem tuning skipped: {e}\");\n}","preventionTips":["Use a work_mem value within the server's documented maximum.","Confirm the role may raise session-level settings.","Keep a conservative fallback value when tuning fails.","Issue tuning SETs immediately after acquiring a fresh connection."],"tags":["database","postgres","performance-tuning","configuration"],"backgroundTag":"invalid-config-value","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"}