nautechsystems/nautilus_trader · error · anyhow::Error

Failed to call create_token_partition for chain {}: {e}

Error message

Failed to call create_token_partition for chain {}: {e}

What it means

create_token_partition calls the PostgreSQL function create_token_partition($1) for a chain; failures of the sqlx query_as call are wrapped in this anyhow error with the chain_id embedded. Like the block variant, the cause may be a missing function, permission, or a server-side partitioning error.

Source

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

                )
            })?;

        Ok(result.0)
    }

    /// Creates a table partition for the token table specific to the given chain
    /// by calling the existing PostgreSQL function `create_token_partition`.
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    pub async fn create_token_partition(&self, chain: &Chain) -> anyhow::Result<String> {
        let result: (String,) = sqlx::query_as("SELECT create_token_partition($1)")
            .bind(chain.chain_id as i32)
            .fetch_one(&self.pool)
            .await
            .map_err(|e| {
                anyhow::anyhow!(
                    "Failed to call create_token_partition for chain {}: {e}",
                    chain.chain_id
                )
            })?;

        Ok(result.0)
    }

    /// Returns the highest block number that maintains data continuity in the database.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub async fn get_block_consistency_status(
        &self,
        chain: &Chain,
    ) -> anyhow::Result<CachedBlocksConsistencyStatus> {
        log::debug!("Fetching block consistency status");

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the wrapped {e}: 'does not exist' points to missing migrations
  2. Run database migrations to install create_token_partition
  3. Grant EXECUTE on the function to the app DB user
  4. Confirm the parent token table exists before creating partitions

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

// check function and parent token table exist before creating partitions
let ok: (bool,) = sqlx::query_as(
    "SELECT EXISTS (SELECT 1 FROM pg_proc WHERE proname='create_token_partition') \
     AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='token')"
).fetch_one(&pool).await?;
if !ok.0 { return Err(anyhow::anyhow!("token partition prerequisites missing; run migrations")); }

Try / catch

db.create_token_partition(&chain).await.map_err(|e| {
    if e.to_string().contains("does not exist") {
        anyhow::anyhow!("missing create_token_partition; migrations not applied: {e:#}")
    } else {
        anyhow::anyhow!("token partition failed for chain {}: {e:#}", chain.chain_id)
    }
})?;

Prevention

When it happens

Trigger: Calling CacheDatabase::create_token_partition when the token partition function is absent (migrations pending), the role lacks EXECUTE, or the server-side function errors for the given chain_id.

Common situations: Fresh environment where schema migrations weren't applied; version mismatch between adapter and database schema; restricted DB role in production.

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/e8d35268b3b94e6f. Report an issue: GitHub.