fix(weather): analyze_all skip_recent check reads both analyze timestamps

pg_stat_user_tables tracks two timestamps — `last_autoanalyze`
is only bumped by the autovacuum daemon, while `last_analyze` is
bumped by manual ANALYZE statements (including the ones our Task
issues). The previous query only filtered on `last_autoanalyze`,
so on a re-run within the skip_recent window every manually-
analyzed table came back for another ANALYZE pass.

Uses `GREATEST(last_analyze, last_autoanalyze)` so fresh manual
analyses count as recent.
This commit is contained in:
Graham McIntire 2026-04-24 16:39:57 -05:00
parent 36049808a4
commit e192ca3300
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -434,12 +434,18 @@ defmodule Microwaveprop.Weather do
def analyze_all(opts \\ []) do
skip_recent = Keyword.get(opts, :skip_recent_seconds, 6 * 3600)
# `last_autoanalyze` is only set by the autovacuum daemon;
# `last_analyze` is set by manual ANALYZE (including our Task
# below). Take the newer of the two — otherwise a re-run within
# `skip_recent` would re-analyze everything we just did.
q = """
SELECT relname
FROM pg_stat_user_tables
WHERE schemaname = 'public'
AND (last_autoanalyze IS NULL
OR last_autoanalyze < now() - ($1 || ' seconds')::interval)
AND COALESCE(
GREATEST(last_analyze, last_autoanalyze),
'epoch'::timestamptz
) < now() - ($1 || ' seconds')::interval
ORDER BY n_live_tup ASC
"""