diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 0646f53098..27419c1851 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -249,8 +249,8 @@ static int pgStatXactRollback = 0; PgStat_Counter pgStatBlockReadTime = 0; PgStat_Counter pgStatBlockWriteTime = 0; static PgStat_Counter pgLastSessionReportTime = 0; -PgStat_Counter pgStatActiveTime = 0; -PgStat_Counter pgStatTransactionIdleTime = 0; +PgStat_Counter pgStatLastActiveTime = 0; +PgStat_Counter pgStatLastTransactionIdleTime = 0; SessionEndType pgStatSessionEndCause = DISCONNECT_NORMAL; /* Record that's written to 2PC state file when pgstat state is persisted */ @@ -1026,8 +1026,13 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now) TimestampDifference(pgLastSessionReportTime, now, &secs, &usecs); pgLastSessionReportTime = now; tsmsg->m_session_time = (PgStat_Counter) secs * 1000000 + usecs; - tsmsg->m_active_time = pgStatActiveTime; - tsmsg->m_idle_in_xact_time = pgStatTransactionIdleTime; + + /* send the difference since the last report */ + tsmsg->m_active_time = + pgstat_get_my_active_time() - pgStatLastActiveTime; + tsmsg->m_idle_in_xact_time = + pgstat_get_my_transaction_idle_time() - + pgStatLastTransactionIdleTime; } else { @@ -1039,8 +1044,8 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now) pgStatXactRollback = 0; pgStatBlockReadTime = 0; pgStatBlockWriteTime = 0; - pgStatActiveTime = 0; - pgStatTransactionIdleTime = 0; + pgStatLastActiveTime = pgstat_get_my_active_time(); + pgStatLastTransactionIdleTime = pgstat_get_my_transaction_idle_time(); } else { diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c index 5f15dcdc05..8b6836a662 100644 --- a/src/backend/utils/activity/backend_status.c +++ b/src/backend/utils/activity/backend_status.c @@ -613,15 +613,9 @@ pgstat_report_activity(BackendState state, const char *cmd_str) */ if (beentry->st_state == STATE_RUNNING || beentry->st_state == STATE_FASTPATH) - { - pgstat_count_conn_active_time((PgStat_Counter) usecs_diff); active_time_diff = usecs_diff; - } else - { - pgstat_count_conn_txn_idle_time((PgStat_Counter) usecs_diff); transaction_idle_time_diff = usecs_diff; - } } /* @@ -1078,6 +1072,48 @@ pgstat_get_my_query_id(void) } +/* ---------- + * pgstat_get_my_active_time() - + * + * Return current backend's accumulated active time. + */ +uint64 +pgstat_get_my_active_time(void) +{ + if (!MyBEEntry) + return 0; + + /* + * There's no need for a lock around pgstat_begin_read_activity / + * pgstat_end_read_activity here as it's only called from + * pg_stat_get_activity which is already protected, or from the same + * backend which means that there won't be concurrent writes. + */ + return MyBEEntry->st_total_active_time; +} + + +/* ---------- + * pgstat_get_my_transaction_idle_time() - + * + * Return current backend's accumulated in-transaction idel time. + */ +uint64 +pgstat_get_my_transaction_idle_time(void) +{ + if (!MyBEEntry) + return 0; + + /* + * There's no need for a lock around pgstat_begin_read_activity / + * pgstat_end_read_activity here as it's only called from + * pg_stat_get_activity which is already protected, or from the same + * backend which means that there won't be concurrent writes. + */ + return MyBEEntry->st_total_transaction_idle_time; +} + + /* ---------- * pgstat_fetch_stat_beentry() - * diff --git a/src/include/pgstat.h b/src/include/pgstat.h index e10d20222a..382d7202c1 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -1185,10 +1185,6 @@ extern void pgstat_initstats(Relation rel); (pgStatBlockReadTime += (n)) #define pgstat_count_buffer_write_time(n) \ (pgStatBlockWriteTime += (n)) -#define pgstat_count_conn_active_time(n) \ - (pgStatActiveTime += (n)) -#define pgstat_count_conn_txn_idle_time(n) \ - (pgStatTransactionIdleTime += (n)) extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n); extern void pgstat_count_heap_update(Relation rel, bool hot); diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h index 96d432ce49..1791dd6842 100644 --- a/src/include/utils/backend_status.h +++ b/src/include/utils/backend_status.h @@ -309,6 +309,8 @@ extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser); extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen); extern uint64 pgstat_get_my_query_id(void); +extern uint64 pgstat_get_my_active_time(void); +extern uint64 pgstat_get_my_transaction_idle_time(void); /* ----------