1/* -------------------------------------------------------------------------
4 * Implementation of database statistics.
6 * This file contains the implementation of database statistics. It is kept
7 * separate from pgstat.c to enforce the line between the statistics access /
8 * storage implementation and the details about individual types of
11 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
14 * src/backend/utils/activity/pgstat_database.c
15 * -------------------------------------------------------------------------
41 * Remove entry for the database being dropped.
50 * Called from autovacuum.c to report startup of an autovacuum process.
51 * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
52 * the db OID must be passed in, instead.
60 /* can't get here in single user mode */
64 * End-of-vacuum is reported instantly. Report the start the same way for
65 * consistency. Vacuum doesn't run frequently and is a long-lasting
66 * operation so it doesn't matter if we get blocked here a little.
78 * Report a Hot Standby recovery conflict.
96 * Since we drop the information about the database as soon as it
97 * replicates, there is no point in counting these conflicts.
122 * Report a detected deadlock.
137 * Allow this backend to later report checksum failures for dboid, even if in
138 * a critical section at the time of the report.
140 * Without this function having been called first, the backend might need to
141 * allocate an EntryRef or might need to map in DSM segments. Neither should
142 * happen in a critical section.
150 * Just need to ensure this backend has an entry ref for the database.
151 * That will allows us to report checksum failures without e.g. needing to
152 * map in DSM segments.
159 * Report one or more checksum failures.
161 * To be allowed to report checksum failures in critical sections, we require
162 * pgstat_prepare_report_checksum_failure() to have been called before this
163 * function is called.
175 * Update the shared stats directly - checksum failures should never be
176 * common enough for that to be a problem. Note that we pass create=false
177 * here, as we want to be sure to not require memory allocations, so this
178 * can be called in critical sections.
184 * Should always have been created by
185 * pgstat_prepare_report_checksum_failure().
187 * When not using assertions, we don't want to crash should something have
188 * gone wrong, so just return.
193 elog(
WARNING,
"could not report %d conflicts for DB %u",
194 failurecount, dboid);
208 * Report creation of temporary file.
224 * Notify stats system of a new connection.
241 * Notify the stats system of a disconnect.
257 /* we don't collect these */
272 * Support function for the SQL-callable pgstat* functions. Returns
273 * the collected statistics for one database or NULL. NULL doesn't mean
274 * that the database doesn't exist, just that there are no statistics, so the
275 * caller is better off to report ZERO instead.
287 /* Don't count parallel worker transaction stats */
291 * Count transaction commit or abort. (We use counters, not just
292 * bools, in case the reporting message isn't sent right away.)
302 * Notify the stats system about parallel worker information.
319 * Subroutine for pgstat_report_stat(): Handle xact commit/rollback and I/O
328 * If not connected to a database yet, don't attribute time to "shared
329 * state" (InvalidOid is used to track stats for shared relations, etc.).
337 * Accumulate xact commit/rollback and I/O timings to stats entry of the
351 * pgLastSessionReportTime is initialized to MyStartTimestamp by
352 * pgstat_report_connect().
370 * We report session statistics only for normal backend processes. Parallel
371 * workers run in parallel, so they don't contribute to session times, even
372 * though they use CPU time. Walsender processes could be considered here,
373 * but they have different session characteristics from normal backends (for
374 * example, they are always "active"), so they would skew session statistics.
383 * Find or create a local PgStat_StatDBEntry entry for dboid.
391 * This should not report stats on database objects before having
392 * connected to a database.
403 * Reset the database's reset timestamp, without resetting the contents of the
422 * Flush out pending stats for the entry
424 * If nowait is true and the lock could not be immediately acquired, returns
425 * false without flushing the entry. Otherwise returns true.
439#define PGSTAT_ACCUM_DBCOUNT(item) \
440 (sharedent)->stats.item += (pendingent)->item
453 /* last_autovac_time is reported immediately */
467 /* checksum failures are reported immediately */
483#undef PGSTAT_ACCUM_DBCOUNT
487 memset(pendingent, 0,
sizeof(*pendingent));
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
TimestampTz GetCurrentTimestamp(void)
#define OidIsValid(objectId)
volatile uint32 CritSectionCount
TimestampTz MyStartTimestamp
Assert(PointerIsAligned(start, uint64))
BackendType MyBackendType
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
static bool pgstat_should_report_connstat(void)
void pgstat_update_parallel_workers_stats(PgStat_Counter workers_to_launch, PgStat_Counter workers_launched)
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
void pgstat_report_autovac(Oid dboid)
void pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_prepare_report_checksum_failure(Oid dboid)
void pgstat_report_connect(Oid dboid)
PgStat_Counter pgStatBlockWriteTime
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
static PgStat_Counter pgLastSessionReportTime
static int pgStatXactCommit
void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
void pgstat_report_deadlock(void)
void pgstat_report_recovery_conflict(int reason)
void pgstat_update_dbstats(TimestampTz ts)
PgStat_Counter pgStatTransactionIdleTime
SessionEndType pgStatSessionEndCause
void pgstat_drop_database(Oid databaseid)
void pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
void pgstat_report_disconnect(Oid dboid)
#define PGSTAT_ACCUM_DBCOUNT(item)
static int pgStatXactRollback
bool pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)
void pgstat_report_tempfile(size_t filesize)
#define PGSTAT_KIND_DATABASE
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
@ PROCSIG_RECOVERY_CONFLICT_LOCK
@ PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
PgStatShared_Common * shared_stats
PgStat_Counter blk_write_time
PgStat_Counter xact_rollback
PgStat_Counter conflict_startup_deadlock
PgStat_Counter conflict_lock
PgStat_Counter parallel_workers_to_launch
TimestampTz stat_reset_timestamp
PgStat_Counter conflict_snapshot
PgStat_Counter sessions_fatal
TimestampTz last_checksum_failure
PgStat_Counter blk_read_time
PgStat_Counter parallel_workers_launched
PgStat_Counter xact_commit
TimestampTz last_autovac_time
PgStat_Counter temp_bytes
PgStat_Counter session_time
PgStat_Counter temp_files
PgStat_Counter sessions_abandoned
PgStat_Counter active_time
PgStat_Counter conflict_bufferpin
PgStat_Counter idle_in_transaction_time
PgStat_Counter conflict_logicalslot
PgStat_Counter sessions_killed
PgStat_Counter checksum_failures
PgStat_Counter conflict_tablespace