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. The TRUNCATE waits at most 2 seconds for each table's lock, like pg_ash's own rotate(), 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 PreventCrossDatabaseModification in PgAsh.execute, next to the two analyzers it already suppresses. The reset runs inside a transaction, where that analyzer looks up ash.config in the GitLab schema dictionary, which doesn't list the ash tables, and raises UnknownSchemaError in development, or reports it in production, when the detect_cross_database_modification flag 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_dump that includes the ash schema, 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

Screenshots or screen recordings

Help text Confirmation
Help text under the sample interval Confirmation after changing the interval

How to set up and validate locally

  1. Go to Admin > Settings > Metrics and profiling and expand Active session history.
  2. 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.

Edited by Imanpal Singh

Merge request reports

Loading
Loading