Skip to content

[Bug]: health_storage_stats panics with SQLx decode error on empty user #179

@XuPeng-SH

Description

@XuPeng-SH

[Bug]: health_storage_stats panics with SQLx decode error on empty user

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Actual Behavior

When calling GET /v1/health/storage for a user with no memories, the endpoint returns HTTP 500:

Database error: error occurred while decoding column 1: unexpected null; try decoding as an `Option`

Expected Behavior

The endpoint should return a valid JSON response with zero counts:

{"total": 0, "active": 0, "inactive": 0, "avg_content_size": 0.0}

Root Cause

In memoria-storage/src/store.rs, health_storage_stats() uses:

let row: (i64, i64, f64) = sqlx::query_as(
    "SELECT COUNT(*) as total, \
     SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active, \
     AVG(LENGTH(content)) as avg_content_size \
     FROM mem_memories WHERE user_id = ?",
)

Per SQL standard, SUM() and AVG() return NULL on empty result sets (unlike COUNT(*) which returns 0). The non-Option types i64 and f64 cannot decode NULL, causing the SQLx decode panic.

Fix

Change the type to (i64, Option<i64>, Option<f64>) and unwrap with defaults:

let row: (i64, Option<i64>, Option<f64>) = sqlx::query_as(...);
let total = row.0;
let active = row.1.unwrap_or(0);
let avg_content_size = row.2.unwrap_or(0.0);

Steps to Reproduce

  1. Start Memoria with a fresh database
  2. Create a user (or use any user_id that has no memories)
  3. Call GET /v1/health/storage with that user's auth
  4. Observe HTTP 500 with decode error

Additional information

Discovered during astra-engine E2E testing with mock embedding provider. The health_hygiene endpoint is not affected because it only uses COUNT(*) queries.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions