PostgreSQL Source Code  git master
pgstat_database.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * pgstat_database.c
4  * Implementation of database statistics.
5  *
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
9  * statistics.
10  *
11  * Copyright (c) 2001-2023, PostgreSQL Global Development Group
12  *
13  * IDENTIFICATION
14  * src/backend/utils/activity/pgstat_database.c
15  * -------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include "utils/pgstat_internal.h"
21 #include "utils/timestamp.h"
22 #include "storage/procsignal.h"
23 
24 
25 static bool pgstat_should_report_connstat(void);
26 
27 
33 
34 
35 static int pgStatXactCommit = 0;
36 static int pgStatXactRollback = 0;
38 
39 
40 /*
41  * Remove entry for the database being dropped.
42  */
43 void
45 {
47 }
48 
49 /*
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.
53  */
54 void
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 }
76 
77 /*
78  * Report a Hot Standby recovery conflict.
79  */
80 void
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_startup_deadlock++;
114  break;
115  }
116 }
117 
118 /*
119  * Report a detected deadlock.
120  */
121 void
123 {
124  PgStat_StatDBEntry *dbent;
125 
126  if (!pgstat_track_counts)
127  return;
128 
130  dbent->deadlocks++;
131 }
132 
133 /*
134  * Report one or more checksum failures.
135  */
136 void
138 {
139  PgStat_EntryRef *entry_ref;
140  PgStatShared_Database *sharedent;
141 
142  if (!pgstat_track_counts)
143  return;
144 
145  /*
146  * Update the shared stats directly - checksum failures should never be
147  * common enough for that to be a problem.
148  */
149  entry_ref =
151 
152  sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
153  sharedent->stats.checksum_failures += failurecount;
155 
156  pgstat_unlock_entry(entry_ref);
157 }
158 
159 /*
160  * Report one checksum failure in the current database.
161  */
162 void
164 {
166 }
167 
168 /*
169  * Report creation of temporary file.
170  */
171 void
172 pgstat_report_tempfile(size_t filesize)
173 {
174  PgStat_StatDBEntry *dbent;
175 
176  if (!pgstat_track_counts)
177  return;
178 
180  dbent->temp_bytes += filesize;
181  dbent->temp_files++;
182 }
183 
184 /*
185  * Notify stats system of a new connection.
186  */
187 void
189 {
190  PgStat_StatDBEntry *dbentry;
191 
193  return;
194 
196 
198  dbentry->sessions++;
199 }
200 
201 /*
202  * Notify the stats system of a disconnect.
203  */
204 void
206 {
207  PgStat_StatDBEntry *dbentry;
208 
210  return;
211 
213 
214  switch (pgStatSessionEndCause)
215  {
216  case DISCONNECT_NOT_YET:
217  case DISCONNECT_NORMAL:
218  /* we don't collect these */
219  break;
221  dbentry->sessions_abandoned++;
222  break;
223  case DISCONNECT_FATAL:
224  dbentry->sessions_fatal++;
225  break;
226  case DISCONNECT_KILLED:
227  dbentry->sessions_killed++;
228  break;
229  }
230 }
231 
232 /*
233  * Support function for the SQL-callable pgstat* functions. Returns
234  * the collected statistics for one database or NULL. NULL doesn't mean
235  * that the database doesn't exist, just that there are no statistics, so the
236  * caller is better off to report ZERO instead.
237  */
240 {
241  return (PgStat_StatDBEntry *)
243 }
244 
245 void
246 AtEOXact_PgStat_Database(bool isCommit, bool parallel)
247 {
248  /* Don't count parallel worker transaction stats */
249  if (!parallel)
250  {
251  /*
252  * Count transaction commit or abort. (We use counters, not just
253  * bools, in case the reporting message isn't sent right away.)
254  */
255  if (isCommit)
257  else
259  }
260 }
261 
262 /*
263  * Subroutine for pgstat_report_stat(): Handle xact commit/rollback and I/O
264  * timings.
265  */
266 void
268 {
269  PgStat_StatDBEntry *dbentry;
270 
272 
273  /*
274  * Accumulate xact commit/rollback and I/O timings to stats entry of the
275  * current database.
276  */
277  dbentry->xact_commit += pgStatXactCommit;
278  dbentry->xact_rollback += pgStatXactRollback;
281 
283  {
284  long secs;
285  int usecs;
286 
287  /*
288  * pgLastSessionReportTime is initialized to MyStartTimestamp by
289  * pgstat_report_connect().
290  */
291  TimestampDifference(pgLastSessionReportTime, ts, &secs, &usecs);
293  dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
294  dbentry->active_time += pgStatActiveTime;
296  }
297 
298  pgStatXactCommit = 0;
299  pgStatXactRollback = 0;
302  pgStatActiveTime = 0;
304 }
305 
306 /*
307  * We report session statistics only for normal backend processes. Parallel
308  * workers run in parallel, so they don't contribute to session times, even
309  * though they use CPU time. Walsender processes could be considered here,
310  * but they have different session characteristics from normal backends (for
311  * example, they are always "active"), so they would skew session statistics.
312  */
313 static bool
315 {
316  return MyBackendType == B_BACKEND;
317 }
318 
319 /*
320  * Find or create a local PgStat_StatDBEntry entry for dboid.
321  */
324 {
325  PgStat_EntryRef *entry_ref;
326 
328  NULL);
329 
330  return entry_ref->pending;
331 }
332 
333 /*
334  * Reset the database's reset timestamp, without resetting the contents of the
335  * database stats.
336  */
337 void
339 {
340  PgStat_EntryRef *dbref;
341  PgStatShared_Database *dbentry;
342 
344  false);
345 
346  dbentry = (PgStatShared_Database *) dbref->shared_stats;
347  dbentry->stats.stat_reset_timestamp = ts;
348 
349  pgstat_unlock_entry(dbref);
350 }
351 
352 /*
353  * Flush out pending stats for the entry
354  *
355  * If nowait is true, this function returns false if lock could not
356  * immediately acquired, otherwise true is returned.
357  */
358 bool
360 {
361  PgStatShared_Database *sharedent;
362  PgStat_StatDBEntry *pendingent;
363 
364  pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
365  sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
366 
367  if (!pgstat_lock_entry(entry_ref, nowait))
368  return false;
369 
370 #define PGSTAT_ACCUM_DBCOUNT(item) \
371  (sharedent)->stats.item += (pendingent)->item
372 
373  PGSTAT_ACCUM_DBCOUNT(xact_commit);
374  PGSTAT_ACCUM_DBCOUNT(xact_rollback);
375  PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
376  PGSTAT_ACCUM_DBCOUNT(blocks_hit);
377 
378  PGSTAT_ACCUM_DBCOUNT(tuples_returned);
379  PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
380  PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
381  PGSTAT_ACCUM_DBCOUNT(tuples_updated);
382  PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
383 
384  /* last_autovac_time is reported immediately */
385  Assert(pendingent->last_autovac_time == 0);
386 
387  PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
388  PGSTAT_ACCUM_DBCOUNT(conflict_lock);
389  PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
390  PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
391  PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
392 
393  PGSTAT_ACCUM_DBCOUNT(temp_bytes);
394  PGSTAT_ACCUM_DBCOUNT(temp_files);
395  PGSTAT_ACCUM_DBCOUNT(deadlocks);
396 
397  /* checksum failures are reported immediately */
398  Assert(pendingent->checksum_failures == 0);
399  Assert(pendingent->last_checksum_failure == 0);
400 
401  PGSTAT_ACCUM_DBCOUNT(blk_read_time);
402  PGSTAT_ACCUM_DBCOUNT(blk_write_time);
403 
404  PGSTAT_ACCUM_DBCOUNT(sessions);
405  PGSTAT_ACCUM_DBCOUNT(session_time);
406  PGSTAT_ACCUM_DBCOUNT(active_time);
407  PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
408  PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
409  PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
410  PGSTAT_ACCUM_DBCOUNT(sessions_killed);
411 #undef PGSTAT_ACCUM_DBCOUNT
412 
413  pgstat_unlock_entry(entry_ref);
414 
415  memset(pendingent, 0, sizeof(*pendingent));
416 
417  return true;
418 }
419 
420 void
422 {
423  ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
424 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1667
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1582
int64 TimestampTz
Definition: timestamp.h:39
bool IsUnderPostmaster
Definition: globals.c:113
TimestampTz MyStartTimestamp
Definition: globals.c:46
Oid MyDatabaseId
Definition: globals.c:89
Assert(fmt[strlen(fmt) - 1] !='\n')
@ B_BACKEND
Definition: miscadmin.h:323
BackendType MyBackendType
Definition: miscinit.c:63
bool pgstat_track_counts
Definition: pgstat.c:187
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:793
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1077
SessionEndType
Definition: pgstat.h:77
@ 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
@ PGSTAT_KIND_DATABASE
Definition: pgstat.h:41
int64 PgStat_Counter
Definition: pgstat.h:89
static bool pgstat_should_report_connstat(void)
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_report_connect(Oid dboid)
PgStat_Counter pgStatBlockWriteTime
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
void pgstat_report_checksum_failure(void)
static PgStat_Counter pgLastSessionReportTime
static int pgStatXactCommit
void AtEOXact_PgStat_Database(bool isCommit, bool parallel)
void pgstat_report_deadlock(void)
void pgstat_report_recovery_conflict(int reason)
void pgstat_update_dbstats(TimestampTz ts)
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
PgStat_Counter pgStatTransactionIdleTime
SessionEndType pgStatSessionEndCause
void pgstat_drop_database(Oid databaseid)
PgStat_StatDBEntry * pgstat_fetch_stat_dbentry(Oid dboid)
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)
void pgstat_report_tempfile(size_t filesize)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:603
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:575
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:612
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:380
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:45
@ PROCSIG_RECOVERY_CONFLICT_LOCK
Definition: procsignal.h:43
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
Definition: procsignal.h:41
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
Definition: procsignal.h:44
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
Definition: procsignal.h:42
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition: procsignal.h:46
PgStat_StatDBEntry stats
PgStatShared_Common * shared_stats
PgStat_Counter blk_write_time
Definition: pgstat.h:342
PgStat_Counter xact_rollback
Definition: pgstat.h:322
PgStat_Counter conflict_startup_deadlock
Definition: pgstat.h:335
PgStat_Counter conflict_lock
Definition: pgstat.h:332
TimestampTz stat_reset_timestamp
Definition: pgstat.h:351
PgStat_Counter conflict_snapshot
Definition: pgstat.h:333
PgStat_Counter sessions_fatal
Definition: pgstat.h:348
TimestampTz last_checksum_failure
Definition: pgstat.h:340
PgStat_Counter blk_read_time
Definition: pgstat.h:341
PgStat_Counter xact_commit
Definition: pgstat.h:321
TimestampTz last_autovac_time
Definition: pgstat.h:330
PgStat_Counter deadlocks
Definition: pgstat.h:338
PgStat_Counter temp_bytes
Definition: pgstat.h:337
PgStat_Counter session_time
Definition: pgstat.h:344
PgStat_Counter temp_files
Definition: pgstat.h:336
PgStat_Counter sessions_abandoned
Definition: pgstat.h:347
PgStat_Counter sessions
Definition: pgstat.h:343
PgStat_Counter active_time
Definition: pgstat.h:345
PgStat_Counter conflict_bufferpin
Definition: pgstat.h:334
PgStat_Counter idle_in_transaction_time
Definition: pgstat.h:346
PgStat_Counter sessions_killed
Definition: pgstat.h:349
PgStat_Counter checksum_failures
Definition: pgstat.h:339
PgStat_Counter conflict_tablespace
Definition: pgstat.h:331