diff options
author | Andres Freund <andres@anarazel.de> | 2022-03-19 11:32:18 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2022-03-19 11:42:22 -0700 |
commit | 89c546c2948904a2c811ccb26d92fb54edaf7821 (patch) | |
tree | 3758a0eaee8a6061b87aff450c03aed7ef9befca /src | |
parent | a3a75b982b5bb6fba95ad8b3d48e70439dcd2329 (diff) | |
download | postgresql-89c546c2948904a2c811ccb26d92fb54edaf7821.tar.gz postgresql-89c546c2948904a2c811ccb26d92fb54edaf7821.zip |
pgstat: split relation, database handling out of pgstat_report_stat().
pgstat_report_stat() handles several types of stats, yet relation stats have
so far been handled directly in pgstat_report_stat().
A later commit will move the handling of the different kinds of stats into
separate files. By splitting out relation handling in this commit that later
move will just move code around without other changes.
Author: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/postmaster/pgstat.c | 79 |
1 files changed, 56 insertions, 23 deletions
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 8df60f26814..5c3d286239e 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -223,6 +223,12 @@ static HTAB *pgStatTabHash = NULL; static HTAB *pgStatFunctions = NULL; /* + * Indicates if backend has some relation stats that it hasn't yet + * sent to the collector. + */ +static bool have_relation_stats = false; + +/* * Indicates if backend has some function stats that it hasn't yet * sent to the collector. */ @@ -338,7 +344,9 @@ static bool pgstat_db_requested(Oid databaseid); static PgStat_StatReplSlotEntry *pgstat_get_replslot_entry(NameData name, bool create_it); static void pgstat_reset_replslot(PgStat_StatReplSlotEntry *slotstats, TimestampTz ts); +static void pgstat_send_tabstats(TimestampTz now, bool disconnect); static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now); +static void pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now); static void pgstat_send_funcstats(void); static void pgstat_send_slru(void); static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid); @@ -866,15 +874,9 @@ allow_immediate_pgstat_restart(void) void pgstat_report_stat(bool disconnect) { - /* we assume this inits to all zeroes: */ - static const PgStat_TableCounts all_zeroes; static TimestampTz last_report = 0; TimestampTz now; - PgStat_MsgTabstat regular_msg; - PgStat_MsgTabstat shared_msg; - TabStatusArray *tsa; - int i; pgstat_assert_is_up(); @@ -887,7 +889,7 @@ pgstat_report_stat(bool disconnect) * generates no WAL records can write or sync WAL data when flushing the * data pages. */ - if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) && + if (!have_relation_stats && pgStatXactCommit == 0 && pgStatXactRollback == 0 && pgWalUsage.wal_records == prevWalUsage.wal_records && WalStats.m_wal_write == 0 && WalStats.m_wal_sync == 0 && @@ -908,6 +910,32 @@ pgstat_report_stat(bool disconnect) if (disconnect) pgstat_report_disconnect(MyDatabaseId); + /* First, send relation statistics */ + pgstat_send_tabstats(now, disconnect); + + /* Now, send function statistics */ + pgstat_send_funcstats(); + + /* Send WAL statistics */ + pgstat_send_wal(true); + + /* Finally send SLRU statistics */ + pgstat_send_slru(); +} + +/* + * Subroutine for pgstat_report_stat: Send relation statistics + */ +static void +pgstat_send_tabstats(TimestampTz now, bool disconnect) +{ + /* we assume this inits to all zeroes: */ + static const PgStat_TableCounts all_zeroes; + PgStat_MsgTabstat regular_msg; + PgStat_MsgTabstat shared_msg; + TabStatusArray *tsa; + int i; + /* * Destroy pgStatTabHash before we start invalidating PgStat_TableEntry * entries it points to. (Should we fail partway through the loop below, @@ -980,18 +1008,11 @@ pgstat_report_stat(bool disconnect) if (shared_msg.m_nentries > 0) pgstat_send_tabstat(&shared_msg, now); - /* Now, send function statistics */ - pgstat_send_funcstats(); - - /* Send WAL statistics */ - pgstat_send_wal(true); - - /* Finally send SLRU statistics */ - pgstat_send_slru(); + have_relation_stats = false; } /* - * Subroutine for pgstat_report_stat: finish and send a tabstat message + * Subroutine for pgstat_send_tabstats: finish and send one tabstat message */ static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now) @@ -1007,6 +1028,23 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now) * Report and reset accumulated xact commit/rollback and I/O timings * whenever we send a normal tabstat message */ + pgstat_update_dbstats(tsmsg, now); + + n = tsmsg->m_nentries; + len = offsetof(PgStat_MsgTabstat, m_entry[0]) + + n * sizeof(PgStat_TableEntry); + + pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT); + pgstat_send(tsmsg, len); +} + +/* + * Subroutine for pgstat_send_tabstat: Handle xact commit/rollback and I/O + * timings. + */ +static void +pgstat_update_dbstats(PgStat_MsgTabstat *tsmsg, TimestampTz now) +{ if (OidIsValid(tsmsg->m_databaseid)) { tsmsg->m_xact_commit = pgStatXactCommit; @@ -1052,13 +1090,6 @@ pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg, TimestampTz now) tsmsg->m_active_time = 0; tsmsg->m_idle_in_xact_time = 0; } - - n = tsmsg->m_nentries; - len = offsetof(PgStat_MsgTabstat, m_entry[0]) + - n * sizeof(PgStat_TableEntry); - - pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT); - pgstat_send(tsmsg, len); } /* @@ -2179,6 +2210,8 @@ get_tabstat_entry(Oid rel_id, bool isshared) pgstat_assert_is_up(); + have_relation_stats = true; + /* * Create hash table if we don't have it already. */ |