PostgreSQL Source Code  git master
pgstat_database.c File Reference
#include "postgres.h"
#include "storage/procsignal.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_report_checksum_failures_in_db (Oid dboid, int failurecount)
 
void pgstat_report_checksum_failure (void)
 
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_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 249 of file pgstat_database.c.

250 {
251  /* Don't count parallel worker transaction stats */
252  if (!parallel)
253  {
254  /*
255  * Count transaction commit or abort. (We use counters, not just
256  * bools, in case the reporting message isn't sent right away.)
257  */
258  if (isCommit)
260  else
262  }
263 }
static int pgStatXactCommit
static int pgStatXactRollback

References 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 375 of file pgstat_database.c.

376 {
377  PgStatShared_Database *sharedent;
378  PgStat_StatDBEntry *pendingent;
379 
380  pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
381  sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
382 
383  if (!pgstat_lock_entry(entry_ref, nowait))
384  return false;
385 
386 #define PGSTAT_ACCUM_DBCOUNT(item) \
387  (sharedent)->stats.item += (pendingent)->item
388 
389  PGSTAT_ACCUM_DBCOUNT(xact_commit);
390  PGSTAT_ACCUM_DBCOUNT(xact_rollback);
391  PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
392  PGSTAT_ACCUM_DBCOUNT(blocks_hit);
393 
394  PGSTAT_ACCUM_DBCOUNT(tuples_returned);
395  PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
396  PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
397  PGSTAT_ACCUM_DBCOUNT(tuples_updated);
398  PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
399 
400  /* last_autovac_time is reported immediately */
401  Assert(pendingent->last_autovac_time == 0);
402 
403  PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
404  PGSTAT_ACCUM_DBCOUNT(conflict_lock);
405  PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
406  PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
407  PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
408  PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
409 
410  PGSTAT_ACCUM_DBCOUNT(temp_bytes);
411  PGSTAT_ACCUM_DBCOUNT(temp_files);
412  PGSTAT_ACCUM_DBCOUNT(deadlocks);
413 
414  /* checksum failures are reported immediately */
415  Assert(pendingent->checksum_failures == 0);
416  Assert(pendingent->last_checksum_failure == 0);
417 
418  PGSTAT_ACCUM_DBCOUNT(blk_read_time);
419  PGSTAT_ACCUM_DBCOUNT(blk_write_time);
420 
421  PGSTAT_ACCUM_DBCOUNT(sessions);
422  PGSTAT_ACCUM_DBCOUNT(session_time);
423  PGSTAT_ACCUM_DBCOUNT(active_time);
424  PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
425  PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
426  PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
427  PGSTAT_ACCUM_DBCOUNT(sessions_killed);
428 #undef PGSTAT_ACCUM_DBCOUNT
429 
430  pgstat_unlock_entry(entry_ref);
431 
432  memset(pendingent, 0, sizeof(*pendingent));
433 
434  return true;
435 }
#define Assert(condition)
Definition: c.h:858
#define PGSTAT_ACCUM_DBCOUNT(item)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:604
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:576
PgStatShared_Common * shared_stats
TimestampTz last_checksum_failure
Definition: pgstat.h:345
TimestampTz last_autovac_time
Definition: pgstat.h:334
PgStat_Counter checksum_failures
Definition: pgstat.h:344

References Assert, PgStat_StatDBEntry::checksum_failures, PgStat_StatDBEntry::last_autovac_time, PgStat_StatDBEntry::last_checksum_failure, 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 438 of file pgstat_database.c.

439 {
440  ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
441 }

◆ pgstat_drop_database()

void pgstat_drop_database ( Oid  databaseid)

Definition at line 44 of file pgstat_database.c.

45 {
47 }
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:379
#define InvalidOid
Definition: postgres_ext.h:36

References 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 333 of file pgstat_database.c.

334 {
335  PgStat_EntryRef *entry_ref;
336 
337  /*
338  * This should not report stats on database objects before having
339  * connected to a database.
340  */
342 
344  NULL);
345 
346  return entry_ref->pending;
347 }
#define OidIsValid(objectId)
Definition: c.h:775
Oid MyDatabaseId
Definition: globals.c:91
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1105

References Assert, 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(), and pgstat_update_dbstats().

◆ pgstat_report_autovac()

void pgstat_report_autovac ( Oid  dboid)

Definition at line 55 of file pgstat_database.c.

56 {
57  PgStat_EntryRef *entry_ref;
58  PgStatShared_Database *dbentry;
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 
71  dbentry = (PgStatShared_Database *) entry_ref->shared_stats;
73 
74  pgstat_unlock_entry(entry_ref);
75 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
bool IsUnderPostmaster
Definition: globals.c:117
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:613
PgStat_StatDBEntry stats

References Assert, GetCurrentTimestamp(), InvalidOid, IsUnderPostmaster, PgStat_StatDBEntry::last_autovac_time, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, and PgStatShared_Database::stats.

Referenced by AutoVacWorkerMain().

◆ pgstat_report_checksum_failure()

void pgstat_report_checksum_failure ( void  )

Definition at line 166 of file pgstat_database.c.

167 {
169 }
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)

References MyDatabaseId, and pgstat_report_checksum_failures_in_db().

Referenced by PageIsVerifiedExtended().

◆ pgstat_report_checksum_failures_in_db()

void pgstat_report_checksum_failures_in_db ( Oid  dboid,
int  failurecount 
)

Definition at line 140 of file pgstat_database.c.

141 {
142  PgStat_EntryRef *entry_ref;
143  PgStatShared_Database *sharedent;
144 
145  if (!pgstat_track_counts)
146  return;
147 
148  /*
149  * Update the shared stats directly - checksum failures should never be
150  * common enough for that to be a problem.
151  */
152  entry_ref =
154 
155  sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
156  sharedent->stats.checksum_failures += failurecount;
158 
159  pgstat_unlock_entry(entry_ref);
160 }
bool pgstat_track_counts
Definition: pgstat.c:184

References PgStat_StatDBEntry::checksum_failures, GetCurrentTimestamp(), InvalidOid, PgStat_StatDBEntry::last_checksum_failure, pgstat_get_entry_ref_locked(), PGSTAT_KIND_DATABASE, pgstat_track_counts, pgstat_unlock_entry(), PgStat_EntryRef::shared_stats, and PgStatShared_Database::stats.

Referenced by pgstat_report_checksum_failure(), and sendFile().

◆ pgstat_report_connect()

void pgstat_report_connect ( Oid  dboid)

Definition at line 191 of file pgstat_database.c.

192 {
193  PgStat_StatDBEntry *dbentry;
194 
196  return;
197 
199 
201  dbentry->sessions++;
202 }
TimestampTz MyStartTimestamp
Definition: globals.c:47
static bool pgstat_should_report_connstat(void)
static PgStat_Counter pgLastSessionReportTime
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
PgStat_Counter sessions
Definition: pgstat.h:348

References MyDatabaseId, MyStartTimestamp, pgLastSessionReportTime, pgstat_prep_database_pending(), pgstat_should_report_connstat(), and PgStat_StatDBEntry::sessions.

Referenced by PostgresMain().

◆ pgstat_report_deadlock()

void pgstat_report_deadlock ( void  )

Definition at line 125 of file pgstat_database.c.

126 {
127  PgStat_StatDBEntry *dbent;
128 
129  if (!pgstat_track_counts)
130  return;
131 
133  dbent->deadlocks++;
134 }
PgStat_Counter deadlocks
Definition: pgstat.h:343

References PgStat_StatDBEntry::deadlocks, MyDatabaseId, pgstat_prep_database_pending(), and pgstat_track_counts.

Referenced by DeadLockReport().

◆ pgstat_report_disconnect()

void pgstat_report_disconnect ( Oid  dboid)

Definition at line 208 of file pgstat_database.c.

209 {
210  PgStat_StatDBEntry *dbentry;
211 
213  return;
214 
216 
217  switch (pgStatSessionEndCause)
218  {
219  case DISCONNECT_NOT_YET:
220  case DISCONNECT_NORMAL:
221  /* we don't collect these */
222  break;
224  dbentry->sessions_abandoned++;
225  break;
226  case DISCONNECT_FATAL:
227  dbentry->sessions_fatal++;
228  break;
229  case DISCONNECT_KILLED:
230  dbentry->sessions_killed++;
231  break;
232  }
233 }
@ DISCONNECT_NOT_YET
Definition: pgstat.h:78
@ DISCONNECT_FATAL
Definition: pgstat.h:81
@ DISCONNECT_KILLED
Definition: pgstat.h:82
@ DISCONNECT_CLIENT_EOF
Definition: pgstat.h:80
@ DISCONNECT_NORMAL
Definition: pgstat.h:79
SessionEndType pgStatSessionEndCause
PgStat_Counter sessions_fatal
Definition: pgstat.h:353
PgStat_Counter sessions_abandoned
Definition: pgstat.h:352
PgStat_Counter sessions_killed
Definition: pgstat.h:354

References DISCONNECT_CLIENT_EOF, DISCONNECT_FATAL, DISCONNECT_KILLED, DISCONNECT_NORMAL, DISCONNECT_NOT_YET, MyDatabaseId, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatSessionEndCause, PgStat_StatDBEntry::sessions_abandoned, PgStat_StatDBEntry::sessions_fatal, and PgStat_StatDBEntry::sessions_killed.

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 {
83  PgStat_StatDBEntry *dbentry;
84 
87  return;
88 
90 
91  switch (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;
118  }
119 }
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:47
@ PROCSIG_RECOVERY_CONFLICT_LOCK
Definition: procsignal.h:44
@ PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT
Definition: procsignal.h:46
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
Definition: procsignal.h:42
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
Definition: procsignal.h:45
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
Definition: procsignal.h:43
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition: procsignal.h:48
PgStat_Counter conflict_startup_deadlock
Definition: pgstat.h:340
PgStat_Counter conflict_lock
Definition: pgstat.h:336
PgStat_Counter conflict_snapshot
Definition: pgstat.h:337
PgStat_Counter conflict_bufferpin
Definition: pgstat.h:339
PgStat_Counter conflict_logicalslot
Definition: pgstat.h:338
PgStat_Counter conflict_tablespace
Definition: pgstat.h:335

References Assert, PgStat_StatDBEntry::conflict_bufferpin, PgStat_StatDBEntry::conflict_lock, PgStat_StatDBEntry::conflict_logicalslot, PgStat_StatDBEntry::conflict_snapshot, PgStat_StatDBEntry::conflict_startup_deadlock, PgStat_StatDBEntry::conflict_tablespace, IsUnderPostmaster, MyDatabaseId, pgstat_prep_database_pending(), pgstat_track_counts, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, PROCSIG_RECOVERY_CONFLICT_DATABASE, PROCSIG_RECOVERY_CONFLICT_LOCK, PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT, PROCSIG_RECOVERY_CONFLICT_SNAPSHOT, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, and PROCSIG_RECOVERY_CONFLICT_TABLESPACE.

Referenced by ProcessRecoveryConflictInterrupt().

◆ pgstat_report_tempfile()

void pgstat_report_tempfile ( size_t  filesize)

Definition at line 175 of file pgstat_database.c.

176 {
177  PgStat_StatDBEntry *dbent;
178 
179  if (!pgstat_track_counts)
180  return;
181 
183  dbent->temp_bytes += filesize;
184  dbent->temp_files++;
185 }
PgStat_Counter temp_bytes
Definition: pgstat.h:342
PgStat_Counter temp_files
Definition: pgstat.h:341

References MyDatabaseId, pgstat_prep_database_pending(), pgstat_track_counts, PgStat_StatDBEntry::temp_bytes, and PgStat_StatDBEntry::temp_files.

Referenced by ReportTemporaryFileUsage().

◆ pgstat_reset_database_timestamp()

void pgstat_reset_database_timestamp ( Oid  dboid,
TimestampTz  ts 
)

◆ pgstat_should_report_connstat()

static bool pgstat_should_report_connstat ( void  )
static

Definition at line 324 of file pgstat_database.c.

325 {
326  return MyBackendType == B_BACKEND;
327 }
@ B_BACKEND
Definition: miscadmin.h:338
BackendType MyBackendType
Definition: miscinit.c:63

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 270 of file pgstat_database.c.

271 {
272  PgStat_StatDBEntry *dbentry;
273 
274  /*
275  * If not connected to a database yet, don't attribute time to "shared
276  * state" (InvalidOid is used to track stats for shared relations, etc.).
277  */
278  if (!OidIsValid(MyDatabaseId))
279  return;
280 
282 
283  /*
284  * Accumulate xact commit/rollback and I/O timings to stats entry of the
285  * current database.
286  */
287  dbentry->xact_commit += pgStatXactCommit;
288  dbentry->xact_rollback += pgStatXactRollback;
291 
293  {
294  long secs;
295  int usecs;
296 
297  /*
298  * pgLastSessionReportTime is initialized to MyStartTimestamp by
299  * pgstat_report_connect().
300  */
301  TimestampDifference(pgLastSessionReportTime, ts, &secs, &usecs);
303  dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
304  dbentry->active_time += pgStatActiveTime;
306  }
307 
308  pgStatXactCommit = 0;
309  pgStatXactRollback = 0;
312  pgStatActiveTime = 0;
314 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1730
int64 PgStat_Counter
Definition: pgstat.h:89
PgStat_Counter pgStatActiveTime
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
PgStat_Counter pgStatTransactionIdleTime
PgStat_Counter blk_write_time
Definition: pgstat.h:347
PgStat_Counter xact_rollback
Definition: pgstat.h:326
PgStat_Counter blk_read_time
Definition: pgstat.h:346
PgStat_Counter xact_commit
Definition: pgstat.h:325
PgStat_Counter session_time
Definition: pgstat.h:349
PgStat_Counter active_time
Definition: pgstat.h:350
PgStat_Counter idle_in_transaction_time
Definition: pgstat.h:351

References PgStat_StatDBEntry::active_time, PgStat_StatDBEntry::blk_read_time, PgStat_StatDBEntry::blk_write_time, PgStat_StatDBEntry::idle_in_transaction_time, MyDatabaseId, OidIsValid, pgLastSessionReportTime, pgstat_prep_database_pending(), pgstat_should_report_connstat(), pgStatActiveTime, pgStatBlockReadTime, pgStatBlockWriteTime, pgStatTransactionIdleTime, pgStatXactCommit, pgStatXactRollback, PgStat_StatDBEntry::session_time, TimestampDifference(), PgStat_StatDBEntry::xact_commit, and PgStat_StatDBEntry::xact_rollback.

Referenced by pgstat_report_stat().

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().