nautechsystems/nautilus_trader · error · anyhow::Error

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

Error message

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

What it means

create_block_partition calls the PostgreSQL function create_block_partition($1) for a chain and this error wraps any sqlx failure of that SELECT. The partition function itself may also fail server-side (e.g. partition already exists in a conflicting way), which surfaces through sqlx as a database error. The chain_id is included in the message to identify the failing chain.

Source

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

        .execute(&self.pool)
        .await
        .map(|_| ())
        .map_err(|e| anyhow::anyhow!("Failed to seed chain table: {e}"))
    }

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

        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)

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the wrapped {e} — 'function create_block_partition(...) does not exist' means migrations were not run
  2. Apply the latest schema migrations before initializing the cache
  3. Grant EXECUTE on the partition functions to the application DB role
  4. Verify the parent block table and partition scheme exist for the chain_id

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

// ensure the partition function exists before calling it
let exists: (bool,) = sqlx::query_as(
    "SELECT EXISTS (SELECT 1 FROM pg_proc WHERE proname='create_block_partition')"
).fetch_one(&pool).await?;
if !exists.0 {
    return Err(anyhow::anyhow!("create_block_partition missing; run migrations first"));
}

Try / catch

db.create_block_partition(&chain).await.map_err(|e| {
    if e.to_string().contains("does not exist") {
        anyhow::anyhow!("schema not migrated: {e:#}")
    } else {
        anyhow::anyhow!("partition creation failed for chain {}: {e:#}", chain.chain_id)
    }
})?;

Prevention

When it happens

Trigger: Calling CacheDatabase::create_block_partition during cache initialization and either the query fails (connection, function missing) or the server-side function raises an error for that chain_id.

Common situations: Migrations not applied so create_block_partition() function doesn't exist; DB user lacks EXECUTE on the function; calling it for a chain whose partition state conflicts.

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