nautechsystems/nautilus_trader · error · anyhow::Error

Failed to set work_mem: {e}

Error message

Failed to set work_mem: {e}

What it means

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.

Source

Thrown at crates/adapters/blockchain/src/cache/database.rs:1416

    ///
    /// # Errors
    ///
    /// Returns an error if the database operations fail.
    pub async fn toggle_perf_sync_settings(&self, enable: bool) -> anyhow::Result<()> {
        if enable {
            log::debug!("Enabling performance sync settings for bulk operations");

            // Set synchronous_commit to OFF for maximum write performance
            sqlx::query("SET synchronous_commit = OFF")
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to set synchronous_commit OFF: {e}"))?;

            // Increase work_mem for bulk operations
            sqlx::query("SET work_mem = '256MB'")
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to set work_mem: {e}"))?;

            log::debug!("Performance settings enabled: synchronous_commit=OFF, work_mem=256MB");
        } else {
            log::debug!("Restoring default safe database performance settings");

            // Restore synchronous_commit to ON for data safety
            sqlx::query("SET synchronous_commit = ON")
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to set synchronous_commit ON: {e}"))?;

            // Reset work_mem to default
            sqlx::query("RESET work_mem")
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to reset work_mem: {e}"))?;
        }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the embedded `{e}`; lower the requested work_mem (e.g. '64MB') if the server rejects the value.
  2. Grant the role permission or connect as a user allowed to change session-level settings.
  3. Verify the connection is still alive; reconnect and reapply settings if the pool returned a dead connection.
  4. Fall back to default work_mem if the environment disallows raising it.

Example fix

// before
sqlx::query("SET work_mem = '256MB'").execute(&self.pool).await
    .map_err(|e| anyhow::anyhow!("Failed to set work_mem: {e}"))?;
// after: use a server-safe value
sqlx::query("SET work_mem = '64MB'").execute(&self.pool).await
    .map_err(|e| anyhow::anyhow!("Failed to set work_mem: {e}"))?;
Defensive patterns

Strategy: fallback

Validate before calling

// check the server's cap before requesting a work_mem value
let max_mem: Option<(String,)> = sqlx::query_as("SHOW max_wal_size") /* or check role limits */
    .fetch_optional(&pool).await?;

Try / catch

let applied = sqlx::query("SET work_mem = '256MB'").execute(&self.pool).await
    .or_else(|_| sqlx::query("SET work_mem = '64MB'").execute(&self.pool).await);
if let Err(e) = applied {
    log::warn!("work_mem tuning skipped: {e}");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/e496f7993bc4a068. Report an issue: GitHub.