nautechsystems/nautilus_trader · error

Failed to load order position index

Error message

Failed to load order position index: {e}

What it means

This error is raised by `load_index_order_position` when the SQLX query that reads all rows of the `order_position_index` table fails. The library wraps the underlying sqlx error via `anyhow::anyhow!` so callers get a single anyhow::Error carrying the database failure message. It means the SELECT against the PostgreSQL pool could not be executed or fetched.

Solutions

  1. Verify PostgreSQL connectivity and credentials in the pool config (test with psql).
  2. Apply migrations so the `order_position_index` table exists (check the sqlx migrations directory).
  3. Inspect the inner sqlx error message embedded in `{e}` for the exact DB-level cause.
  4. Retry on transient errors; enable pool acquire_timeout and max_lifetime tuning if idle disconnects recur.

Example fix

// before
let map = load_index_order_position(&pool).await?;
// after
match load_index_order_position(&pool).await {
    Ok(map) => map,
    Err(e) => { tracing::error!("order position index load failed: {e:#}"); return Err(e); }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check table exists before loading
let exists: bool = sqlx::query_scalar(
    "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'order_position_index')")
    .fetch_one(pool).await?;
anyhow::ensure!(exists, "order_position_index table missing; run migrations");

Try / catch

match load_index_order_position(&pool).await {
    Ok(map) => map,
    Err(e) => {
        tracing::error!("index load failed: {e:#}");
        if is_transient(&e) { /* retry with backoff */ }
        return Err(e);
    }
}

Prevention

When it happens

Trigger: Calling `load_index_order_position(pool)` when the PostgreSQL server is unreachable, the `order_position_index` table does not exist (missing migration), the connection pool is exhausted/closed, credentials are wrong, or a transient network failure occurs during `fetch_all`.

Common situations: Running against a database where migrations were never applied or were partially applied; connection drops after idle timeouts; wrong DATABASE_URL or insufficient privileges to SELECT from the table; connecting to a database of a different schema version.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


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

Appendix: source

Thrown at crates/infrastructure/src/sql/queries.rs:1557

    ///
    /// # Errors
    ///
    /// Returns an error if the SQL SELECT or iteration fails.
    pub async fn load_index_order_position(
        pool: &PgPool,
    ) -> anyhow::Result<AHashMap<ClientOrderId, PositionId>> {
        let mut map: AHashMap<ClientOrderId, PositionId> = AHashMap::new();
        let result = sqlx::query_as::<_, OrderPositionIndexRow>(
            r#"
            SELECT
                client_order_id AS "client_order_id",
                position_id AS "position_id"
            FROM "order_position_index"
        "#,
        )
        .fetch_all(pool)
        .await
        .map_err(|e| anyhow::anyhow!("Failed to load order position index: {e}"))?;

        for row in result {
            map.insert(row.client_order_id, row.position_id);
        }
        Ok(map)
    }

    /// Inserts a `Signal` entry via the provided `pool`.
    ///
    /// # Errors
    ///
    /// Returns an error if the SQL INSERT operation fails.
    pub async fn add_signal(pool: &PgPool, signal: &Signal) -> anyhow::Result<()> {
        sqlx::query(
            r#"
            INSERT INTO "signal" (
                name, value, ts_event, ts_init, created_at, updated_at
            ) VALUES (

View on GitHub (pinned to 18893faf8b)