From e9040fe4c26378daa45b8dea752a0c0ee8b256a9 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Thu, 17 Oct 2024 11:52:20 +0900 Subject: [PATCH v2] Prevent misleading entries in pg_stat_activity. In some cases, such as handling "Describe" messages, cleaning up temporary relations at exit, or managing client read interrupts, backends can remain in an "idle" state but still set xact_start in pg_stat_activity. This results in misleading entries where "idle" backends appear to have non-NULL transaction times. Additionally, during temp relation cleanup or interrupt handling, xact_start might incorrectly reflect the timestamp of the last executed query, further confusing users. This commit addresses the issue by ensuring pg_stat_activity does not show transaction and query start times for "idle" backends, setting them to NULL instead. This prevents confusing entries for "idle" backends. While it would be possible to correctly track backend states and set timestamps, this approach would introduce performance overhead due to frequent updates of state and timestamp. Moreover, it is not particularly beneficial to treat processes like "Describe" message handling, temp relation cleanup, or client read interrupts as regular transactions. Therefore, the simpler and more efficient solution was adopted. Discussion: https://postgr.es/m/20140424101827.2714.39486@wrigleys.postgresql.org --- src/backend/utils/adt/pgstatfuncs.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index f7b50e0b5a..5967b6afb5 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -460,14 +460,31 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) * Don't expose transaction time for walsenders; it confuses * monitoring, particularly because we don't keep the time up-to- * date. + * + * Also, don't show transaction time for backends in the "idle" + * state. There are cases, like during "Describe" message + * handling, removing temporary relations at exit, or processing + * client read interrupts, where the backend remains "idle" but + * still sets transaction time. This can lead to incorrect "idle" + * entries with non-NULL transaction times in pg_stat_activity. To + * prevent these misleading entries, avoid exposing transaction + * time for idle backends. */ if (beentry->st_xact_start_timestamp != 0 && - beentry->st_backendType != B_WAL_SENDER) + beentry->st_backendType != B_WAL_SENDER && + (beentry->st_state != STATE_IDLE || + beentry->st_backendType != B_BACKEND)) values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp); else nulls[8] = true; - if (beentry->st_activity_start_timestamp != 0) + /* + * Don't expose query start time for idle backends for the same + * reasons mentioned above regarding transaction time. + */ + if (beentry->st_activity_start_timestamp != 0 && + (beentry->st_state != STATE_IDLE || + beentry->st_backendType != B_BACKEND)) values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp); else nulls[9] = true; -- 2.46.2