nautechsystems/nautilus_trader · error · anyhow::Error

Failed to set synchronous_commit ON: {e}

Error message

Failed to set synchronous_commit ON: {e}

What it means

Wraps failure of the `SET synchronous_commit = ON` statement used to restore safe durability settings after bulk sync completes. Failing here leaves the session possibly still in the unsafe OFF state, so it also signals that data-safety restoration did not complete.

Source

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

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

        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,

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the embedded `{e}`; on connection-closed errors reconnect and re-issue SET synchronous_commit = ON before other work.
  2. Avoid transaction-pooling modes (pgbouncer transaction pool) that do not preserve session-level SETs.
  3. Run restore inside a short window immediately after bulk writes, or issue periodic keepalives during long syncs.
  4. Verify role permissions if the error is a privilege failure (42501).

Example fix

// before: restore assumed to run on the same live connection
sqlx::query("SET synchronous_commit = ON").execute(&self.pool).await
    .map_err(|e| anyhow::anyhow!("Failed to set synchronous_commit ON: {e}"))?;
// after: verify connection health before restoring
sqlx::query("SELECT 1").execute(&self.pool).await?;
sqlx::query("SET synchronous_commit = ON").execute(&self.pool).await
    .map_err(|e| anyhow::anyhow!("Failed to set synchronous_commit ON: {e}"))?;
Defensive patterns

Strategy: retry

Validate before calling

// verify the connection is alive before attempting to restore settings
sqlx::query("SELECT 1").execute(&pool).await
    .map_err(|e| anyhow!("connection dead before settings restore: {e}"))?;

Try / catch

for attempt in 0..3 {
    match sqlx::query("SET synchronous_commit = ON").execute(&self.pool).await {
        Ok(_) => break,
        Err(e) if attempt == 2 => return Err(anyhow!("Failed to set synchronous_commit ON: {e}")),
        Err(_) => tokio::time::sleep(Duration::from_secs(2)).await,
    }
}

Prevention

When it happens

Trigger: Calling the performance-toggling method with enabled=false when restoring defaults: connection lost after the bulk sync window (most common — long syncs often outlive idle timeouts), role lacks permission to SET, or the pooled connection was returned/recycled.

Common situations: Very long bulk syncs whose connection was dropped by an idle firewall/LB timeout, so the restore SET fails; switching database roles between enable and restore; pgbouncer transaction pooling discarding session-level SET state.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


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