Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 590f0a8

Browse files
MasaoFujiiCommitfest Bot
authored and
Commitfest Bot
committed
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 d6d8054 commit 590f0a8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,31 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
490490
* Don't expose transaction time for walsenders; it confuses
491491
* monitoring, particularly because we don't keep the time up-to-
492492
* date.
493+
*
494+
* Also, don't show transaction time for backends in the "idle"
495+
* state. There are cases, like during "Describe" message
496+
* handling, removing temporary relations at exit, or processing
497+
* client read interrupts, where the backend remains "idle" but
498+
* still sets transaction time. This can lead to incorrect "idle"
499+
* entries with non-NULL transaction times in pg_stat_activity. To
500+
* prevent these misleading entries, avoid exposing transaction
501+
* time for idle backends.
493502
*/
494503
if (beentry->st_xact_start_timestamp != 0 &&
495-
beentry->st_backendType != B_WAL_SENDER)
504+
beentry->st_backendType != B_WAL_SENDER &&
505+
(beentry->st_state != STATE_IDLE ||
506+
beentry->st_backendType != B_BACKEND))
496507
values[8] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
497508
else
498509
nulls[8] = true;
499510

500-
if (beentry->st_activity_start_timestamp != 0)
511+
/*
512+
* Don't expose query start time for idle backends for the same
513+
* reasons mentioned above regarding transaction time.
514+
*/
515+
if (beentry->st_activity_start_timestamp != 0 &&
516+
(beentry->st_state != STATE_IDLE ||
517+
beentry->st_backendType != B_BACKEND))
501518
values[9] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
502519
else
503520
nulls[9] = true;

0 commit comments

Comments
 (0)