PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgstat_database.c File Reference
#include "postgres.h"
#include "storage/standby.h"
#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
Include dependency graph for pgstat_database.c:

Go to the source code of this file.

Macros

#define PGSTAT_ACCUM_DBCOUNT(item)    (sharedent)->stats.item += (pendingent)->item
 

Functions

static bool pgstat_should_report_connstat (void)
 
void pgstat_drop_database (Oid databaseid)
 
void pgstat_report_autovac (Oid dboid)
 
void pgstat_report_recovery_conflict (int reason)
 
void pgstat_report_deadlock (void)
 
void pgstat_prepare_report_checksum_failure (Oid dboid)
 
void pgstat_report_checksum_failures_in_db (Oid dboid, int failurecount)
 
void pgstat_report_tempfile (size_t filesize)
 
void pgstat_report_connect (Oid dboid)
 
void pgstat_report_disconnect (Oid dboid)
 
PgStat_StatDBEntrypgstat_fetch_stat_dbentry (Oid dboid)
 
void AtEOXact_PgStat_Database (bool isCommit, bool parallel)
 
void pgstat_update_parallel_workers_stats (PgStat_Counter workers_to_launch, PgStat_Counter workers_launched)
 
void pgstat_update_dbstats (TimestampTz ts)
 
PgStat_StatDBEntrypgstat_prep_database_pending (Oid dboid)
 
void pgstat_reset_database_timestamp (Oid dboid, TimestampTz ts)
 
bool pgstat_database_flush_cb (PgStat_EntryRef *entry_ref, bool nowait)
 
void pgstat_database_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 

Variables

PgStat_Counter pgStatBlockReadTime = 0
 
PgStat_Counter pgStatBlockWriteTime = 0
 
PgStat_Counter pgStatActiveTime = 0
 
PgStat_Counter pgStatTransactionIdleTime = 0
 
SessionEndType pgStatSessionEndCause = DISCONNECT_NORMAL
 
static int pgStatXactCommit = 0
 
static int pgStatXactRollback = 0
 
static PgStat_Counter pgLastSessionReportTime = 0
 

Macro Definition Documentation

◆ PGSTAT_ACCUM_DBCOUNT

#define PGSTAT_ACCUM_DBCOUNT (   item)     (sharedent)->stats.item += (pendingent)->item

Function Documentation

◆ AtEOXact_PgStat_Database()

void AtEOXact_PgStat_Database ( bool  isCommit,
bool  parallel 
)

Definition at line 295 of file pgstat_database.c.

296{
297 /* Don't count parallel worker transaction stats */
298 if (!parallel)
299 {
300 /*
301 * Count transaction commit or abort. (We use counters, not just
302 * bools, in case the reporting message isn't sent right away.)
303 */
304 if (isCommit)
306 else
308 }
309}
static int pgStatXactCommit
static int pgStatXactRollback
static int fb(int x)

References fb(), pgStatXactCommit, and pgStatXactRollback.

Referenced by AtEOXact_PgStat().

◆ pgstat_database_flush_cb()

bool pgstat_database_flush_cb ( PgStat_EntryRef entry_ref,
bool  nowait 
)

Definition at line 438 of file pgstat_database.c.

439{
442
443 pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
445
446 if (!pgstat_lock_entry(entry_ref, nowait))
447 return false;
448
449#define PGSTAT_ACCUM_DBCOUNT(item) \
450 (sharedent)->stats.item += (pendingent)->item
451
452 PGSTAT_ACCUM_DBCOUNT(xact_commit);
453 PGSTAT_ACCUM_DBCOUNT(xact_rollback);
454 PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
455 PGSTAT_ACCUM_DBCOUNT(blocks_hit);
456
457 PGSTAT_ACCUM_DBCOUNT(tuples_returned);
458 PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
459 PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
460 PGSTAT_ACCUM_DBCOUNT(tuples_updated);
461 PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
462
463 /* last_autovac_time is reported immediately */
464 Assert(pendingent->last_autovac_time == 0);
465
466 PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
467 PGSTAT_ACCUM_DBCOUNT(conflict_lock);
468 PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
469 PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
470 PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
471 PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
472
473 PGSTAT_ACCUM_DBCOUNT(temp_bytes);
474 PGSTAT_ACCUM_DBCOUNT(temp_files);
475 PGSTAT_ACCUM_DBCOUNT(deadlocks);
476
477 /* checksum failures are reported immediately */
478 Assert(pendingent->checksum_failures == 0);
479 Assert(pendingent->last_checksum_failure == 0);
480
481 PGSTAT_ACCUM_DBCOUNT(blk_read_time);
482 PGSTAT_ACCUM_DBCOUNT(blk_write_time);
483
484 PGSTAT_ACCUM_DBCOUNT(sessions);
485 PGSTAT_ACCUM_DBCOUNT(session_time);
486 PGSTAT_ACCUM_DBCOUNT(active_time);
487 PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
488 PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
489 PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
490 PGSTAT_ACCUM_DBCOUNT(sessions_killed);
491 PGSTAT_ACCUM_DBCOUNT(parallel_workers_to_launch);
492 PGSTAT_ACCUM_DBCOUNT(parallel_workers_launched);
493#undef PGSTAT_ACCUM_DBCOUNT
494
495 pgstat_unlock_entry(entry_ref);
496
497 memset(pendingent, 0, sizeof(*pendingent));
498
499 return true;
500}
#define Assert(condition)
Definition c.h:885
#define PGSTAT_ACCUM_DBCOUNT(item)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
PgStatShared_Common * shared_stats

References Assert, fb(), PgStat_EntryRef::pending, PGSTAT_ACCUM_DBCOUNT, pgstat_lock_entry(), pgstat_unlock_entry(), and PgStat_EntryRef::shared_stats.

◆ pgstat_database_reset_timestamp_cb()

void pgstat_database_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 503 of file pgstat_database.c.

504{
505 ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
506}

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

Definition at line 44 of file pgstat_database.c.

45{
47}
#define PGSTAT_KIND_DATABASE
Definition pgstat_kind.h:27
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
#define InvalidOid

References fb(), InvalidOid, pgstat_drop_transactional(), and PGSTAT_KIND_DATABASE.

Referenced by dropdb().

◆ pgstat_fetch_stat_dbentry()

PgStat_StatDBEntry * pgstat_fetch_stat_dbentry ( Oid  dboid)

◆ pgstat_prep_database_pending()

PgStat_StatDBEntry * pgstat_prep_database_pending ( Oid  dboid)

Definition at line 396 of file pgstat_database.c.

397{
398 PgStat_EntryRef *entry_ref;
399
400 /*
401 * This should not report stats on database objects before having
402 * connected to a database.
403 */
405
407 NULL);
408
409 return entry_ref->pending;
410}
#define OidIsValid(objectId)
Definition c.h:800
Oid MyDatabaseId
Definition globals.c:94
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition pgstat.c:1275

References Assert, fb(), InvalidOid, MyDatabaseId, OidIsValid, PgStat_EntryRef::pending, PGSTAT_KIND_DATABASE, and pgstat_prep_pending_entry().

Referenced by pgstat_relation_flush_cb(), pgstat_report_connect(), pgstat_report_deadlock(), pgstat_report_disconnect(), pgstat_report_recovery_conflict(), pgstat_report_tempfile(), pgstat_update_dbstats(), and pgstat_update_parallel_workers_stats().

◆ pgstat_prepare_report_checksum_failure()

void pgstat_prepare_report_checksum_failure ( Oid  dboid)

Definition at line 155 of file pgstat_database.c.

156{
158
159 /*
160 * Just need to ensure this backend has an entry ref for the database.
161 * That will allows us to report checksum failures without e.g. needing to
162 * map in DSM segments.
163 */
165 true, NULL);
166}
volatile uint32 CritSectionCount
Definition globals.c:45
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)

References Assert, CritSectionCount, fb(), InvalidOid, pgstat_get_entry_ref(), and PGSTAT_KIND_DATABASE.

Referenced by AsyncReadBuffers(), read_rel_block_ll(), RelationCopyStorage(), and sendFile().

◆ pgstat_report_autovac()

void pgstat_report_autovac ( Oid  dboid)

Definition at line 55 of file pgstat_database.c.

56{
57 PgStat_EntryRef *entry_ref;
59
60 /* can't get here in single user mode */
62
63 /*
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.
67 */
69 dboid, InvalidOid, false);
70
72 dbentry->stats.last_autovac_time = GetCurrentTimestamp();
73
74 pgstat_unlock_entry(entry_ref);
75}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
bool IsUnderPostmaster
Definition globals.c:120
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)

References Assert, fb(), GetCurrentTimestamp(), InvalidOid, IsUnderPostmaster, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, pgstat_unlock_entry(), and PgStat_EntryRef::shared_stats.

Referenced by AutoVacWorkerMain().

◆ pgstat_report_checksum_failures_in_db()

void pgstat_report_checksum_failures_in_db ( Oid  dboid,
int  failurecount 
)

Definition at line 176 of file pgstat_database.c.

177{
178 PgStat_EntryRef *entry_ref;
180
182 return;
183
184 /*
185 * Update the shared stats directly - checksum failures should never be
186 * common enough for that to be a problem. Note that we pass create=false
187 * here, as we want to be sure to not require memory allocations, so this
188 * can be called in critical sections.
189 */
191 false, NULL);
192
193 /*
194 * Should always have been created by
195 * pgstat_prepare_report_checksum_failure().
196 *
197 * When not using assertions, we don't want to crash should something have
198 * gone wrong, so just return.
199 */
200 Assert(entry_ref);
201 if (!entry_ref)
202 {
203 elog(WARNING, "could not report %d checksum failures for database %u",
204 failurecount, dboid);
205 return;
206 }
207
208 (void) pgstat_lock_entry(entry_ref, false);
209
211 sharedent->stats.checksum_failures += failurecount;
212 sharedent->stats.last_checksum_failure = GetCurrentTimestamp();
213
214 pgstat_unlock_entry(entry_ref);
215}
#define WARNING
Definition elog.h:36
#define elog(elevel,...)
Definition elog.h:226
bool pgstat_track_counts
Definition pgstat.c:203

References Assert, elog, fb(), GetCurrentTimestamp(), InvalidOid, pgstat_get_entry_ref(), PGSTAT_KIND_DATABASE, pgstat_lock_entry(), pgstat_track_counts, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, and WARNING.

Referenced by buffer_readv_complete(), RelationCopyStorage(), sendFile(), and shared_buffer_readv_complete_local().

◆ pgstat_report_connect()

void pgstat_report_connect ( Oid  dboid)

Definition at line 237 of file pgstat_database.c.

238{
240
242 return;
243
245
247 dbentry->sessions++;
248}
TimestampTz MyStartTimestamp
Definition globals.c:49
static bool pgstat_should_report_connstat(void)
static PgStat_Counter pgLastSessionReportTime
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)

References fb(), MyDatabaseId, MyStartTimestamp, pgLastSessionReportTime, pgstat_prep_database_pending(), and pgstat_should_report_connstat().

Referenced by PostgresMain().

◆ pgstat_report_deadlock()

void pgstat_report_deadlock ( void  )

Definition at line 135 of file pgstat_database.c.

136{
138
140 return;
141
143 dbent->deadlocks++;
144}

References fb(), MyDatabaseId, pgstat_prep_database_pending(), and pgstat_track_counts.

Referenced by DeadLockReport().

◆ pgstat_report_disconnect()

void pgstat_report_disconnect ( Oid  dboid)

Definition at line 254 of file pgstat_database.c.

255{
257
259 return;
260
262
263 switch (pgStatSessionEndCause)
264 {
267 /* we don't collect these */
268 break;
270 dbentry->sessions_abandoned++;
271 break;
272 case DISCONNECT_FATAL:
273 dbentry->sessions_fatal++;
274 break;
276 dbentry->sessions_killed++;
277 break;
278 }
279}
@ DISCONNECT_NOT_YET
Definition pgstat.h:60
@ DISCONNECT_FATAL
Definition pgstat.h:63
@ DISCONNECT_KILLED
Definition pgstat.h:64
@ DISCONNECT_CLIENT_EOF
Definition pgstat.h:62
@ DISCONNECT_NORMAL
Definition pgstat.h:61
SessionEndType pgStatSessionEndCause

References DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL, DISCONNECT_KILLED, DISCONNECT_NORMAL, DISCONNECT_NOT_YET, fb(), MyDatabaseId, pgstat_prep_database_pending(), pgstat_should_report_connstat(), and pgStatSessionEndCause.

Referenced by pgstat_shutdown_hook().

◆ pgstat_report_recovery_conflict()

void pgstat_report_recovery_conflict ( int  reason)

Definition at line 81 of file pgstat_database.c.

82{
84
87 return;
88
90
91 switch ((RecoveryConflictReason) reason)
92 {
94
95 /*
96 * Since we drop the information about the database as soon as it
97 * replicates, there is no point in counting these conflicts.
98 */
99 break;
101 dbentry->conflict_tablespace++;
102 break;
104 dbentry->conflict_lock++;
105 break;
107 dbentry->conflict_snapshot++;
108 break;
110 dbentry->conflict_bufferpin++;
111 break;
113 dbentry->conflict_logicalslot++;
114 break;
116 dbentry->conflict_startup_deadlock++;
117 break;
119
120 /*
121 * The difference between RECOVERY_CONFLICT_STARTUP_DEADLOCK and
122 * RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK is merely whether a buffer
123 * pin was part of the deadlock. We use the same counter for both
124 * reasons.
125 */
126 dbentry->conflict_startup_deadlock++;
127 break;
128 }
129}
RecoveryConflictReason
Definition standby.h:29
@ RECOVERY_CONFLICT_TABLESPACE
Definition standby.h:34
@ RECOVERY_CONFLICT_SNAPSHOT
Definition standby.h:40
@ RECOVERY_CONFLICT_LOCK
Definition standby.h:37
@ RECOVERY_CONFLICT_DATABASE
Definition standby.h:31
@ RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition standby.h:53
@ RECOVERY_CONFLICT_BUFFERPIN
Definition standby.h:46
@ RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK
Definition standby.h:61
@ RECOVERY_CONFLICT_LOGICALSLOT
Definition standby.h:43

References Assert, fb(), IsUnderPostmaster, MyDatabaseId, pgstat_prep_database_pending(), pgstat_track_counts, RECOVERY_CONFLICT_BUFFERPIN, RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK, RECOVERY_CONFLICT_DATABASE, RECOVERY_CONFLICT_LOCK, RECOVERY_CONFLICT_LOGICALSLOT, RECOVERY_CONFLICT_SNAPSHOT, RECOVERY_CONFLICT_STARTUP_DEADLOCK, and RECOVERY_CONFLICT_TABLESPACE.

Referenced by report_recovery_conflict().

◆ pgstat_report_tempfile()

void pgstat_report_tempfile ( size_t  filesize)

Definition at line 221 of file pgstat_database.c.

222{
224
226 return;
227
229 dbent->temp_bytes += filesize;
230 dbent->temp_files++;
231}

References fb(), MyDatabaseId, pgstat_prep_database_pending(), and pgstat_track_counts.

Referenced by ReportTemporaryFileUsage().

◆ pgstat_reset_database_timestamp()

void pgstat_reset_database_timestamp ( Oid  dboid,
TimestampTz  ts 
)

Definition at line 417 of file pgstat_database.c.

418{
421
423 false);
424
425 dbentry = (PgStatShared_Database *) dbref->shared_stats;
426 dbentry->stats.stat_reset_timestamp = ts;
427
429}

References fb(), InvalidOid, MyDatabaseId, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, and pgstat_unlock_entry().

Referenced by pgstat_reset().

◆ pgstat_should_report_connstat()

static bool pgstat_should_report_connstat ( void  )
static

Definition at line 387 of file pgstat_database.c.

388{
389 return MyBackendType == B_BACKEND;
390}
@ B_BACKEND
Definition miscadmin.h:342
BackendType MyBackendType
Definition miscinit.c:64

References B_BACKEND, and MyBackendType.

Referenced by pgstat_report_connect(), pgstat_report_disconnect(), and pgstat_update_dbstats().

◆ pgstat_update_dbstats()

void pgstat_update_dbstats ( TimestampTz  ts)

Definition at line 333 of file pgstat_database.c.

334{
336
337 /*
338 * If not connected to a database yet, don't attribute time to "shared
339 * state" (InvalidOid is used to track stats for shared relations, etc.).
340 */
342 return;
343
345
346 /*
347 * Accumulate xact commit/rollback and I/O timings to stats entry of the
348 * current database.
349 */
350 dbentry->xact_commit += pgStatXactCommit;
351 dbentry->xact_rollback += pgStatXactRollback;
352 dbentry->blk_read_time += pgStatBlockReadTime;
353 dbentry->blk_write_time += pgStatBlockWriteTime;
354
356 {
357 long secs;
358 int usecs;
359
360 /*
361 * pgLastSessionReportTime is initialized to MyStartTimestamp by
362 * pgstat_report_connect().
363 */
366 dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
367 dbentry->active_time += pgStatActiveTime;
368 dbentry->idle_in_transaction_time += pgStatTransactionIdleTime;
369 }
370
377}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1721
int64 PgStat_Counter
Definition pgstat.h:71
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
PgStat_Counter pgStatTransactionIdleTime

References fb(), MyDatabaseId, OidIsValid, pgLastSessionReportTime, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatActiveTime, pgStatBlockReadTime, pgStatBlockWriteTime, pgStatTransactionIdleTime, pgStatXactCommit, pgStatXactRollback, and TimestampDifference().

Referenced by pgstat_report_stat().

◆ pgstat_update_parallel_workers_stats()

void pgstat_update_parallel_workers_stats ( PgStat_Counter  workers_to_launch,
PgStat_Counter  workers_launched 
)

Definition at line 315 of file pgstat_database.c.

317{
319
321 return;
322
324 dbentry->parallel_workers_to_launch += workers_to_launch;
325 dbentry->parallel_workers_launched += workers_launched;
326}

References fb(), MyDatabaseId, OidIsValid, and pgstat_prep_database_pending().

Referenced by standard_ExecutorEnd().

Variable Documentation

◆ pgLastSessionReportTime

PgStat_Counter pgLastSessionReportTime = 0
static

Definition at line 37 of file pgstat_database.c.

Referenced by pgstat_report_connect(), and pgstat_update_dbstats().

◆ pgStatActiveTime

PgStat_Counter pgStatActiveTime = 0

Definition at line 30 of file pgstat_database.c.

Referenced by pgstat_update_dbstats().

◆ pgStatBlockReadTime

PgStat_Counter pgStatBlockReadTime = 0

Definition at line 28 of file pgstat_database.c.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and pgstat_update_dbstats().

◆ pgStatBlockWriteTime

PgStat_Counter pgStatBlockWriteTime = 0

Definition at line 29 of file pgstat_database.c.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and pgstat_update_dbstats().

◆ pgStatSessionEndCause

SessionEndType pgStatSessionEndCause = DISCONNECT_NORMAL

Definition at line 32 of file pgstat_database.c.

Referenced by die(), errfinish(), pgstat_report_disconnect(), and PostgresMain().

◆ pgStatTransactionIdleTime

PgStat_Counter pgStatTransactionIdleTime = 0

Definition at line 31 of file pgstat_database.c.

Referenced by pgstat_update_dbstats().

◆ pgStatXactCommit

int pgStatXactCommit = 0
static

Definition at line 35 of file pgstat_database.c.

Referenced by AtEOXact_PgStat_Database(), and pgstat_update_dbstats().

◆ pgStatXactRollback

int pgStatXactRollback = 0
static

Definition at line 36 of file pgstat_database.c.

Referenced by AtEOXact_PgStat_Database(), and pgstat_update_dbstats().