Skip to content

Commit f7b0006

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 0f1345d commit f7b0006

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
@@ -2560,7 +2560,7 @@ pgstat_beshutdown_hook(int code, Datum arg)
25602560
* pgstat_report_activity() -
25612561
*
25622562
* Called from tcop/postgres.c to report what the backend is actually doing
2563-
* (usually "<IDLE>" or the start of the query to be executed).
2563+
* (but note cmd_str can be NULL for certain cases).
25642564
*
25652565
* All updates of the status entry follow the protocol of bumping
25662566
* st_changecount before and after. We use a volatile pointer here to
@@ -2580,36 +2580,40 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
25802580
if (!beentry)
25812581
return;
25822582

2583-
/*
2584-
* To minimize the time spent modifying the entry, fetch all the needed
2585-
* data first.
2586-
*/
2587-
current_timestamp = GetCurrentTimestamp();
2588-
2589-
if (!pgstat_track_activities && beentry->st_state != STATE_DISABLED)
2583+
if (!pgstat_track_activities)
25902584
{
2591-
/*
2592-
* Track activities is disabled, but we have a non-disabled state set.
2593-
* That means the status changed - so as our last update, tell the
2594-
* collector that we disabled it and will no longer update.
2595-
*/
2596-
beentry->st_changecount++;
2597-
beentry->st_state = STATE_DISABLED;
2598-
beentry->st_state_start_timestamp = current_timestamp;
2599-
beentry->st_changecount++;
2600-
Assert((beentry->st_changecount & 1) == 0);
2585+
if (beentry->st_state != STATE_DISABLED)
2586+
{
2587+
/*
2588+
* track_activities is disabled, but we last reported a
2589+
* non-disabled state. As our final update, change the state and
2590+
* clear fields we will not be updating anymore.
2591+
*/
2592+
beentry->st_changecount++;
2593+
beentry->st_state = STATE_DISABLED;
2594+
beentry->st_state_start_timestamp = 0;
2595+
beentry->st_activity[0] = '\0';
2596+
beentry->st_activity_start_timestamp = 0;
2597+
/* st_xact_start_timestamp and st_waiting are also disabled */
2598+
beentry->st_xact_start_timestamp = 0;
2599+
beentry->st_waiting = false;
2600+
beentry->st_changecount++;
2601+
Assert((beentry->st_changecount & 1) == 0);
2602+
}
26012603
return;
26022604
}
26032605

26042606
/*
2605-
* Fetch more data before we start modifying the entry
2607+
* To minimize the time spent modifying the entry, fetch all the needed
2608+
* data first.
26062609
*/
26072610
start_timestamp = GetCurrentStatementStartTimestamp();
26082611
if (cmd_str != NULL)
26092612
{
26102613
len = pg_mbcliplen(cmd_str, strlen(cmd_str),
26112614
pgstat_track_activity_query_size - 1);
26122615
}
2616+
current_timestamp = GetCurrentTimestamp();
26132617

26142618
/*
26152619
* 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
@@ -666,15 +666,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
666666
nulls[4] = true;
667667
break;
668668
}
669-
if (beentry->st_state == STATE_UNDEFINED ||
670-
beentry->st_state == STATE_DISABLED)
671-
{
672-
values[5] = CStringGetTextDatum("");
673-
}
674-
else
675-
{
676-
values[5] = CStringGetTextDatum(beentry->st_activity);
677-
}
669+
670+
values[5] = CStringGetTextDatum(beentry->st_activity);
678671
values[6] = BoolGetDatum(beentry->st_waiting);
679672

680673
if (beentry->st_xact_start_timestamp != 0)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy