Skip to content

Commit a0c2492

Browse files
committed
Avoid updating our PgBackendStatus entry when track_activities is off.
The point of turning off track_activities is to avoid this reporting overhead, but a thinko in commit 4f42b54 caused pgstat_report_activity() to perform half of its updates anyway. Fix that, and also make sure that we clear all the now-disabled fields when transitioning to the non-reporting state.
1 parent e084b14 commit a0c2492

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ pgstat_beshutdown_hook(int code, Datum arg)
25202520
* pgstat_report_activity() -
25212521
*
25222522
* Called from tcop/postgres.c to report what the backend is actually doing
2523-
* (usually "<IDLE>" or the start of the query to be executed).
2523+
* (but note cmd_str can be NULL for certain cases).
25242524
*
25252525
* All updates of the status entry follow the protocol of bumping
25262526
* st_changecount before and after. We use a volatile pointer here to
@@ -2540,36 +2540,40 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
25402540
if (!beentry)
25412541
return;
25422542

2543-
/*
2544-
* To minimize the time spent modifying the entry, fetch all the needed
2545-
* data first.
2546-
*/
2547-
current_timestamp = GetCurrentTimestamp();
2548-
2549-
if (!pgstat_track_activities && beentry->st_state != STATE_DISABLED)
2543+
if (!pgstat_track_activities)
25502544
{
2551-
/*
2552-
* Track activities is disabled, but we have a non-disabled state set.
2553-
* That means the status changed - so as our last update, tell the
2554-
* collector that we disabled it and will no longer update.
2555-
*/
2556-
beentry->st_changecount++;
2557-
beentry->st_state = STATE_DISABLED;
2558-
beentry->st_state_start_timestamp = current_timestamp;
2559-
beentry->st_changecount++;
2560-
Assert((beentry->st_changecount & 1) == 0);
2545+
if (beentry->st_state != STATE_DISABLED)
2546+
{
2547+
/*
2548+
* track_activities is disabled, but we last reported a
2549+
* non-disabled state. As our final update, change the state and
2550+
* clear fields we will not be updating anymore.
2551+
*/
2552+
beentry->st_changecount++;
2553+
beentry->st_state = STATE_DISABLED;
2554+
beentry->st_state_start_timestamp = 0;
2555+
beentry->st_activity[0] = '\0';
2556+
beentry->st_activity_start_timestamp = 0;
2557+
/* st_xact_start_timestamp and st_waiting are also disabled */
2558+
beentry->st_xact_start_timestamp = 0;
2559+
beentry->st_waiting = false;
2560+
beentry->st_changecount++;
2561+
Assert((beentry->st_changecount & 1) == 0);
2562+
}
25612563
return;
25622564
}
25632565

25642566
/*
2565-
* Fetch more data before we start modifying the entry
2567+
* To minimize the time spent modifying the entry, fetch all the needed
2568+
* data first.
25662569
*/
25672570
start_timestamp = GetCurrentStatementStartTimestamp();
25682571
if (cmd_str != NULL)
25692572
{
25702573
len = pg_mbcliplen(cmd_str, strlen(cmd_str),
25712574
pgstat_track_activity_query_size - 1);
25722575
}
2576+
current_timestamp = GetCurrentTimestamp();
25732577

25742578
/*
25752579
* Now update the status entry

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,15 +665,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
665665
nulls[4] = true;
666666
break;
667667
}
668-
if (beentry->st_state == STATE_UNDEFINED ||
669-
beentry->st_state == STATE_DISABLED)
670-
{
671-
values[5] = CStringGetTextDatum("");
672-
}
673-
else
674-
{
675-
values[5] = CStringGetTextDatum(beentry->st_activity);
676-
}
668+
669+
values[5] = CStringGetTextDatum(beentry->st_activity);
677670
values[6] = BoolGetDatum(beentry->st_waiting);
678671

679672
if (beentry->st_xact_start_timestamp != 0)

0 commit comments

Comments
 (0)