Skip to content

Commit d52eed7

Browse files
MasaoFujiihackorum
authored andcommitted
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
1 parent b597835 commit d52eed7

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,31 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
515515
* Don't expose transaction time for walsenders; it confuses
516516
* monitoring, particularly because we don't keep the time up-to-
517517
* date.
518+
*
519+
* Also, don't show transaction time for backends in the "idle"
520+
* state. There are cases, like during "Describe" message
521+
* handling, removing temporary relations at exit, or processing
522+
* client read interrupts, where the backend remains "idle" but
523+
* still sets transaction time. This can lead to incorrect "idle"
524+
* entries with non-NULL transaction times in pg_stat_activity. To
525+
* prevent these misleading entries, avoid exposing transaction
526+
* time for idle backends.
518527
*/
519528
if (beentry->st_xact_start_timestamp != 0 &&
520-
beentry->st_backendType != B_WAL_SENDER)
529+
beentry->st_backendType != B_WAL_SENDER &&
530+
(beentry->st_state != STATE_IDLE ||
531+
beentry->st_backendType != B_BACKEND))
521532
values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
522533
else
523534
nulls[8] = true;
524535

525-
if (beentry->st_activity_start_timestamp != 0)
536+
/*
537+
* Don't expose query start time for idle backends for the same
538+
* reasons mentioned above regarding transaction time.
539+
*/
540+
if (beentry->st_activity_start_timestamp != 0 &&
541+
(beentry->st_state != STATE_IDLE ||
542+
beentry->st_backendType != B_BACKEND))
526543
values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
527544
else
528545
nulls[9] = true;

0 commit comments

Comments
 (0)