nautechsystems/nautilus_trader · error · anyhow::Error

Failed to set synchronous_commit OFF: {e}

Error message

Failed to set synchronous_commit OFF: {e}

What it means

Wraps failure of the `SET synchronous_commit = OFF` statement used to enable bulk-write performance during sync operations. It is a session-level Postgres tuning command, so failure means the database rejected the setting or the connection broke.

Source

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

    /// - `synchronous_commit` = OFF
    /// - `work_mem` increased for bulk operations
    ///
    /// When disabled (false), restores default safe settings:
    /// - `synchronous_commit` = ON (data safety)
    /// - `work_mem` back to default
    ///
    /// # 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

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the embedded `{e}`; for permission errors (42501) grant the role the needed setting rights or run sync as a privileged user.
  2. Verify connectivity and that the pool returns healthy connections before starting bulk sync.
  3. If the server forbids changing synchronous_commit, skip the optimization path (run with default settings).
  4. On managed databases, check provider docs for which SET commands are permitted.

Example fix

// before: fail sync when tuning cannot be applied
sqlx::query("SET synchronous_commit = OFF").execute(&self.pool).await
    .map_err(|e| anyhow::anyhow!("Failed to set synchronous_commit OFF: {e}"))?;
// after: treat tuning as best-effort
if let Err(e) = sqlx::query("SET synchronous_commit = OFF").execute(&self.pool).await {
    log::warn!("bulk sync tuning unavailable, continuing with defaults: {e}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// probe whether the session may change the setting before enabling bulk mode
let allowed: Result<(String,), _> = sqlx::query_as("SHOW synchronous_commit").fetch_one(&pool).await;
if allowed.is_err() { log::warn!("session tuning unavailable; bulk sync will use defaults"); }

Try / catch

if let Err(e) = self.set_performance_mode(true).await {
    log::warn!("performance mode unavailable, continuing safely: {e}");
    // proceed without bulk optimizations rather than aborting sync
}

Prevention

When it happens

Trigger: Calling the performance-toggling method (database.rs) with enabled=true when the SET statement errors: insufficient privileges (non-superuser/session settings restrictions), connection loss, or a pooled connection in a broken/aborted transaction state.

Common situations: Connecting as a role without permission to change synchronous_commit; managed Postgres (RDS/Cloud SQL) with restricted settings; sync start while the pool has stale idle connections that were dropped by the server or a firewall.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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