Skip to content

feat(ash): pg_ash history integration — /ash reads ash.samples when pg_ash is installed #761

@NikolayS

Description

@NikolayS

Goal

When pg_ash is installed on the server, /ash should immediately show the full history available in ash.samples, then continue streaming live data. Today it always uses the live ring buffer (600 samples max, ~10 min).

Current behavior

AshState.pg_ash_installed is detected but ViewMode::History never activates — the code falls back to live ring buffer unconditionally (documented TODO in state.rs:102).

Desired behavior

  1. On connect — detect pg_ash via SELECT 1 FROM pg_extension WHERE extname = 'pg_ash'
  2. If installed — query ash.samples for the configured window (default 10 min, up to 24h via zoom)
  3. Merge — history snapshots pre-populate the ring buffer; live pg_stat_activity polls append new samples
  4. Seamless — left side of timeline = history, right side = live. No gap, no jump.
  5. Zoom — when user zooms out beyond ring buffer capacity, re-query ash.samples for larger window

Implementation plan

Sampler changes (src/ash/sampler.rs)

Add query_ash_history():

select
    wait_event_type,
    wait_event,
    query_id,
    left(query, 80) as q,
    count(*) as count,
    bucket_ts  -- truncated to bucket_secs
from ash.samples
where sample_ts >= now() - interval '10 minutes'
group by 1, 2, 3, 4, bucket_ts
order by bucket_ts

Note: ash.samples schema — verify column names against pg_ash v1.2 (sample_ts, wait_event_type, wait_event, query_id, query).

State changes (src/ash/state.rs)

Activate ViewMode::History when pg_ash_installed = true. On zoom out past ring buffer, trigger re-query.

mod.rs event loop

// On startup, if pg_ash installed:
let history = query_ash_history(&client, window_secs).await?;
for snap in history {
    snapshots.push_back(snap);
}
// Then continue live polling as normal

CI tests required

  • Unit test: query_ash_history() SQL correctness (mock rows)
  • Integration test (ignored by default, needs pg_ash): populate ash.samples, verify ring buffer pre-populated
  • State test: pg_ash_installed = true activates history mode

AI test required

New file: tests/ai/slash-ash-pgash.md

  • Setup: install pg_ash, run pgbench for 5 min to populate history
  • Steps: connect, run /ash, verify left side of timeline shows pre-existing history (not empty)
  • GIF: record 10s showing bars from minute 0, new bars scrolling in live
  • Post GIF to PR as evidence

References

Scope

New PR on top of main (after #756 merges). Do not modify existing /ash live behavior — history is additive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions