Delete Active Session History when the sample interval changes
What does this MR do and why?
pg_ash doesn't store the interval with each sample and weighs every sample by the current one, so changing Sample interval (seconds) rescaled everything collected before it (pg_ash#137). pg_ash v2.0-rc1 refuses the change while any history exists, and its README deletes the history to start a new interval (Starting a new collection cadence).
As suggested in #608101 (comment 4072660038), this MR:
- Deletes the raw samples and both summary tables, and clears the summary watermarks, in the same transaction that writes a changed interval to
ash.config. Nothing is deleted when the interval is unchanged. TheTRUNCATEwaits at most 2 seconds for each table's lock, like pg_ash's ownrotate(), and the sampler retries after its lease expires. - Asks for confirmation when you save a changed interval, and says so in the help text and docs (including the API). It also removes the docs note that a change rescales earlier load, since the history no longer outlives an interval.
- Suppresses
PreventCrossDatabaseModificationinPgAsh.execute, next to the two analyzers it already suppresses. The reset runs inside a transaction, where that analyzer looks upash.configin the GitLab schema dictionary, which doesn't list theashtables, and raisesUnknownSchemaErrorin development, or reports it in production, when thedetect_cross_database_modificationflag is on.
Edge cases:
- Each Sidekiq process caches settings for up to 60 seconds. If the sampling lease moves to another process right after a change, that process can still read the old interval and reset once more, so up to a minute of new history can go.
- A reset that runs while the daily rotation reads the same sample partition can deadlock. PostgreSQL should abort the sampler's transaction, since it waited first, and the sampler retries after its lease.
- The reset waits up to 2 seconds for each table's lock. If another session holds a lock on those tables for longer, for example a
pg_dumpthat includes theashschema, the reset fails and the sampler retries about every 90 seconds. No samples are taken until it succeeds.
SQL
Run by the sampler in one transaction when it writes the settings to ash.config. The middle three only run when the first query returns a row.
SELECT 1 FROM ash.config WHERE singleton AND sample_interval <> 5 * interval '1 second';
SET LOCAL lock_timeout TO '2000ms';
TRUNCATE ash.sample, ash.rollup_1m, ash.rollup_1h;
UPDATE ash.config SET last_rollup_1m_ts = NULL, last_rollup_1h_ts = NULL WHERE singleton;
UPDATE ash.config SET sampling_enabled = true, sample_interval = 5 * interval '1 second' WHERE singleton;Checked on copies of a GDK ash schema (22,911 samples): on the vendored 2.0-beta1 and on v2.0-rc1 these statements leave empty tables, null watermarks and the new interval. Without the reset, rc1 refuses the last UPDATE with cannot change sample_interval while retained history exists.
References
- Epic: gitlab-org#23091
- Work item: #608101
Screenshots or screen recordings
| Help text | Confirmation |
|---|---|
![]() |
![]() |
How to set up and validate locally
- Go to Admin > Settings > Metrics and profiling and expand Active session history.
- Change Sample interval (seconds) and select Save changes.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

