Draft: Add a rake task that prints an active session history report
This MR adds a rake task, bundle exec rake gitlab:db:pg_ash:report. The task prints a text report of database load, built from pg_ash (Active Session History for PostgreSQL), which GitLab vendors. It runs five reader functions that pg_ash provides, in the order upstream documents for investigating an incident, and renders the rows as aligned text tables. This gives an operator a way to check database load from a terminal, with no extra tooling.
The report is upstream's own output, projected to fit a terminal. GitLab adds no analysis of the data.
This MR does not add a web UI or a structured JSON export. The JSON export is blocked, not postponed. Upstream's machine-readable reader, ash.report(), reads only the ash.rollup_1m table, and no GitLab job populates that table yet. The rollup jobs are a follow-up to the sampler merge request.
This MR is stacked on the merge request that adds the pg_ash sampler. It targets that branch, not master. It cannot merge before the sampler merge request merges.
References
- Work item: #608101
- Related epic: &23091
- Sampler merge request, the target branch of this one: !250170 (merged)
- Phase 0 merge request, the installer: !249512 (merged)
What it prints
Captured from a GDK database, with the idle part of the chart cut out. The chart uses ANSI color when the task writes to a terminal, so the bars below are monochrome here.
== Trailing windows ==
period source buckets_with_data avg_aas peak_aas p99_aas
1m raw 0 0.00 0 0
5m raw 2 0.41 1.07 1.07
1h raw 4 0.04 1.07 1.06
1d raw 4 0.00 1.07 1.06
1w raw 4 0.00 1.07 1.06
1mo raw 12 0.00 1.07 1.06
== Summary (last hour) ==
metric value
period_start 2026-09-11 06:31:00+00
period_end 2026-09-11 07:31:00+00
source raw
buckets_with_data 4
avg_aas 0.04
peak_aas 1.07
p99_aas 1.06
backend_seconds 155.00
drill_source raw
drill_period_start 2026-09-11 06:31:00+00
drill_period_end 2026-09-11 07:31:00+00
drill_effective_bucket PT1M
databases_active 4
top_wait_1 CPU* (avg_aas 0.02, 55.48%)
top_wait_2 Timeout:PgSleep (avg_aas 0.01, 33.55%)
top_wait_3 IO:BuffileWrite (avg_aas 0.00, 10.97%)
top_query_1 2056750108613291649 — select count(*) from generate_series($1,$2) (avg_aas 0.03, 61.94%)
top_query_2 5457019535816659310 — select pg_sleep($1) (avg_aas 0.01, 33.55%)
top_query_3 7324011704013533322 — SELECT a.attname, format_type(a.atttypid, a.atttypmod), (avg_aas 0.00, 1.29%)
== AAS chart (last hour) ==
time aas detail chart
█ CPU* ▓ Timeout:PgSleep ░ IO:BuffileWrite · Other
06:30 0.00
06:32 0.00
...
(24 idle buckets)
...
07:22 0.00
07:24 0.00
07:26 0.49 CPU*=0.26 Timeout:PgSleep=0.16 IO:BuffileWrite=0.08 ████████████████████▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░
07:28 0.53 CPU*=0.33 Timeout:PgSleep=0.15 IO:BuffileWrite=0.05 █████████████████████████▓▓▓▓▓▓▓▓▓▓▓░░░░
07:30 0.00
== Top wait events (last hour) ==
key source avg_aas peak_aas p99_aas pct
CPU* raw 0.02 0.67 0.66 55.48
Timeout raw 0.01 0.32 0.32 33.55
IO raw 0.00 0.15 0.15 10.97
== Top queries (last hour) ==
key avg_aas peak_aas pct query_text
2056750108613291649 0.03 0.70 61.94 select count(*) from generate_series($1,$2)
5457019535816659310 0.01 0.32 33.55 select pg_sleep($1)
7324011704013533322 0.00 0.03 1.29 SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2885472602367065956 0.00 0.02 0.65 SELECT "postgres_partitions".* FROM "postgres_partitions" WH
8815024637951609681 0.00 0.02 0.65 SELECT "postgres_partitioned_tables".* FROM "postgres_partitCurrent limits
The task takes no arguments, so the window is fixed at each reader's default, the last hour. There is no JSON export yet. The rollup job that would feed it is a follow-up to the sampler merge request. The top-queries section needs the pg_stat_statements extension. Without it, the query-text column is blank.
Testing guide for reviewers
Before you follow the test steps, note the setup this task needs. pg_ash must be installed, and sampling must be on. An idle database gives an empty report, because active session history only records backends that were active at the sample time. To see rows, hold a query active while the samples run.
1. Install pg_ash and turn on sampling
git checkout 608101-pg-ash-console-report
bundle exec rake gitlab:db:pg_ash:installThen go to Admin area > Settings > Metrics and profiling, expand Active session history, select Turn on session sampling, and save. Sampling starts within about 90 seconds.
2. Make the database busy
psql -h <gdk-root>/postgresql -d gitlabhq_development -c "select pg_sleep(30)"3. Print the report
bundle exec rake gitlab:db:pg_ash:reportExpected: five sections. The chart shows bars at the minutes that held the sleep, in color, one color per wait-event class.
4. Check that a redirect drops the color
bundle exec rake gitlab:db:pg_ash:report > /tmp/ash.txtExpected: no escape sequences in the file.
5. Check the uninstalled path
bundle exec rake gitlab:db:pg_ash:uninstall
bundle exec rake gitlab:db:pg_ash:reportExpected: pg_ash is not installed.