nautechsystems/nautilus_trader · error · anyhow::Error

Failed to reset work_mem: {e}

Error message

Failed to reset work_mem: {e}

What it means

Wraps a sqlx error raised while executing `RESET work_mem` on the PostgreSQL connection pool. This library call runs after a large analytical query that temporarily raised `work_mem` via `SET LOCAL`/`SET`, and this step restores the session default. If the reset fails, the session may keep an inflated work_mem, risking memory pressure on subsequent queries.

Source

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

                .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}"))?;
        }

        Ok(())
    }

    /// Saves the checkpoint block number indicating the last completed pool synchronization for a specific DEX.
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    pub async fn update_dex_last_synced_block(
        &self,
        chain_id: u32,
        dex: &DexType,
        block_number: u64,
    ) -> anyhow::Result<()> {
        sqlx::query(
            "

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check database connectivity and pool health; ensure the Postgres server was not restarted mid-operation
  2. Log the underlying sqlx error (`{e}`) to identify whether it is a connection error vs a permission error
  3. Wrap the reset in a best-effort match that logs instead of failing the whole sync, since the following queries still work
  4. Verify the user role is allowed to RESET work_mem on managed Postgres services

Example fix

// before
.map_err(|e| anyhow::anyhow!("Failed to reset work_mem: {e}"))?;
// after
if let Err(e) = sqlx::query("RESET work_mem").execute(&self.pool).await {
    tracing::warn!("Failed to reset work_mem: {e}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify DB connectivity before session-tuning
sqlx::query("SELECT 1").execute(&pool).await?;

Try / catch

match sqlx::query("RESET work_mem").execute(&pool).await {
    Ok(_) => {}
    Err(e) => tracing::warn!("work_mem reset failed (non-fatal): {e}"),
}

Prevention

When it happens

Trigger: The `RESET work_mem` statement fails when executing `set_session_work_mem`-style cleanup: pool connections dropped mid-operation, the connection returning the error (e.g. server restarting, connection broken), or a superuser-only setting being reset on a restricted session in some managed setups.

Common situations: Postgres restarted or failed over between the SET and RESET; connection pooled and closed by the server (idle timeout); network blip between app and database; running against a managed Postgres (RDS/Cloud SQL) that restricts parameter changes.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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