Debian Codegraph @

src/backend/utils/activity/pgstat_database.c

raw ◀ ▶
1 Debian patch touch 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-2025, 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 "storage/procsignal.h"
21 #include "utils/pgstat_internal.h"
22 #include "utils/timestamp.h"
23
24
25 static bool pgstat_should_report_connstat(void);
26
27
28 PgStat_Counter pgStatBlockReadTime = 0;
29 PgStat_Counter pgStatBlockWriteTime = 0;
30 PgStat_Counter pgStatActiveTime = 0;
31 PgStat_Counter pgStatTransactionIdleTime = 0;
32 SessionEndType pgStatSessionEndCause = DISCONNECT_NORMAL;
33
34
35 static int pgStatXactCommit = 0;
36 static int pgStatXactRollback = 0;
37 static PgStat_Counter pgLastSessionReportTime = 0;
38
39
40 /*
41 * Remove entry for the database being dropped.
42 */
43 void
44 pgstat_drop_database(Oid databaseid)
45 {
46 pgstat_drop_transactional(PGSTAT_KIND_DATABASE, databaseid, InvalidOid);
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
55 pgstat_report_autovac(Oid dboid)
56 {
57 PgStat_EntryRef *entry_ref;
58 PgStatShared_Database *dbentry;
59
60 /* can't get here in single user mode */
61 Assert(IsUnderPostmaster);
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 */
68 entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE,
69 dboid, InvalidOid, false);
70
71 dbentry = (PgStatShared_Database *) entry_ref->shared_stats;
72 dbentry->stats.last_autovac_time = GetCurrentTimestamp();
73
74 pgstat_unlock_entry(entry_ref);
75 }
76
77 /*
78 * Report a Hot Standby recovery conflict.
79 */
80 void
81 pgstat_report_recovery_conflict(int reason)
82 {
83 PgStat_StatDBEntry *dbentry;
•84 PgStat_EntryRef *entry_ref;
85 PgStatShared_Database *sharedent;
86
87 Assert(IsUnderPostmaster);
88 if (!pgstat_track_counts)
89 return;
90
•91 /*
92 * Update the shared stats directly - recovery conflicts should never be
93 * common enough for that to be a problem.
94 */
95 entry_ref =
96 pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, MyDatabaseId, InvalidOid, false);
97
98 sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
99 dbentry = &sharedent->stats;
100
101 switch (reason)
102 {
103 case PROCSIG_RECOVERY_CONFLICT_DATABASE:
104
105 /*
106 * Since we drop the information about the database as soon as it
107 * replicates, there is no point in counting these conflicts.
108 */
109 break;
110 case PROCSIG_RECOVERY_CONFLICT_TABLESPACE:
111 dbentry->conflict_tablespace++;
112 break;
113 case PROCSIG_RECOVERY_CONFLICT_LOCK:
114 dbentry->conflict_lock++;
115 break;
116 case PROCSIG_RECOVERY_CONFLICT_SNAPSHOT:
117 dbentry->conflict_snapshot++;
118 break;
119 case PROCSIG_RECOVERY_CONFLICT_BUFFERPIN:
120 dbentry->conflict_bufferpin++;
121 break;
122 case PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT:
123 dbentry->conflict_logicalslot++;
124 break;
125 case PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK:
126 dbentry->conflict_startup_deadlock++;
127 break;
128 }
•129
130 pgstat_unlock_entry(entry_ref);
131 }
132
133 /*
134 * Report a detected deadlock.
135 */
136 void
137 pgstat_report_deadlock(void)
138 {
139 PgStat_StatDBEntry *dbent;
140
141 if (!pgstat_track_counts)
142 return;
143
144 dbent = pgstat_prep_database_pending(MyDatabaseId);
145 dbent->deadlocks++;
146 }
147
148 /*
149 * Allow this backend to later report checksum failures for dboid, even if in
150 * a critical section at the time of the report.
151 *
152 * Without this function having been called first, the backend might need to
153 * allocate an EntryRef or might need to map in DSM segments. Neither should
154 * happen in a critical section.
155 */
156 void
157 pgstat_prepare_report_checksum_failure(Oid dboid)
158 {
159 Assert(!CritSectionCount);
160
161 /*
162 * Just need to ensure this backend has an entry ref for the database.
163 * That will allows us to report checksum failures without e.g. needing to
164 * map in DSM segments.
165 */
166 pgstat_get_entry_ref(PGSTAT_KIND_DATABASE, dboid, InvalidOid,
167 true, NULL);
168 }
169
170 /*
171 * Report one or more checksum failures.
172 *
173 * To be allowed to report checksum failures in critical sections, we require
174 * pgstat_prepare_report_checksum_failure() to have been called before this
175 * function is called.
176 */
177 void
178 pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
179 {
180 PgStat_EntryRef *entry_ref;
181 PgStatShared_Database *sharedent;
182
183 if (!pgstat_track_counts)
184 return;
185
186 /*
187 * Update the shared stats directly - checksum failures should never be
188 * common enough for that to be a problem. Note that we pass create=false
189 * here, as we want to be sure to not require memory allocations, so this
190 * can be called in critical sections.
191 */
192 entry_ref = pgstat_get_entry_ref(PGSTAT_KIND_DATABASE, dboid, InvalidOid,
193 false, NULL);
194
195 /*
196 * Should always have been created by
197 * pgstat_prepare_report_checksum_failure().
198 *
199 * When not using assertions, we don't want to crash should something have
200 * gone wrong, so just return.
201 */
202 Assert(entry_ref);
203 if (!entry_ref)
204 {
205 elog(WARNING, "could not report %d checksum failures for database %u",
206 failurecount, dboid);
207 return;
208 }
209
210 (void) pgstat_lock_entry(entry_ref, false);
211
212 sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
213 sharedent->stats.checksum_failures += failurecount;
214 sharedent->stats.last_checksum_failure = GetCurrentTimestamp();
215
216 pgstat_unlock_entry(entry_ref);
217 }
218
219 /*
220 * Report creation of temporary file.
221 */
222 void
223 pgstat_report_tempfile(size_t filesize)
224 {
225 PgStat_StatDBEntry *dbent;
226
227 if (!pgstat_track_counts)
228 return;
229
230 dbent = pgstat_prep_database_pending(MyDatabaseId);
231 dbent->temp_bytes += filesize;
232 dbent->temp_files++;
233 }
234
235 /*
236 * Notify stats system of a new connection.
237 */
238 void
239 pgstat_report_connect(Oid dboid)
240 {
241 PgStat_StatDBEntry *dbentry;
242
243 if (!pgstat_should_report_connstat())
244 return;
245
246 pgLastSessionReportTime = MyStartTimestamp;
247
248 dbentry = pgstat_prep_database_pending(dboid);
249 dbentry->sessions++;
250 }
251
252 /*
253 * Notify the stats system of a disconnect.
254 */
255 void
256 pgstat_report_disconnect(Oid dboid)
257 {
258 PgStat_StatDBEntry *dbentry;
259
260 if (!pgstat_should_report_connstat())
261 return;
262
263 dbentry = pgstat_prep_database_pending(dboid);
264
265 switch (pgStatSessionEndCause)
266 {
267 case DISCONNECT_NOT_YET:
268 case DISCONNECT_NORMAL:
269 /* we don't collect these */
270 break;
271 case DISCONNECT_CLIENT_EOF:
272 dbentry->sessions_abandoned++;
273 break;
274 case DISCONNECT_FATAL:
275 dbentry->sessions_fatal++;
276 break;
277 case DISCONNECT_KILLED:
278 dbentry->sessions_killed++;
279 break;
280 }
281 }
282
283 /*
284 * Support function for the SQL-callable pgstat* functions. Returns
285 * the collected statistics for one database or NULL. NULL doesn't mean
286 * that the database doesn't exist, just that there are no statistics, so the
287 * caller is better off to report ZERO instead.
288 */
289 PgStat_StatDBEntry *
290 pgstat_fetch_stat_dbentry(Oid dboid)
291 {
292 return (PgStat_StatDBEntry *)
293 pgstat_fetch_entry(PGSTAT_KIND_DATABASE, dboid, InvalidOid);
294 }
295
296 void
297 AtEOXact_PgStat_Database(bool isCommit, bool parallel)
298 {
299 /* Don't count parallel worker transaction stats */
300 if (!parallel)
301 {
302 /*
303 * Count transaction commit or abort. (We use counters, not just
304 * bools, in case the reporting message isn't sent right away.)
305 */
306 if (isCommit)
307 pgStatXactCommit++;
308 else
309 pgStatXactRollback++;
310 }
311 }
312
313 /*
314 * Notify the stats system about parallel worker information.
315 */
316 void
317 pgstat_update_parallel_workers_stats(PgStat_Counter workers_to_launch,
318 PgStat_Counter workers_launched)
319 {
320 PgStat_StatDBEntry *dbentry;
321
322 if (!OidIsValid(MyDatabaseId))
323 return;
324
325 dbentry = pgstat_prep_database_pending(MyDatabaseId);
326 dbentry->parallel_workers_to_launch += workers_to_launch;
327 dbentry->parallel_workers_launched += workers_launched;
328 }
329
330 /*
331 * Subroutine for pgstat_report_stat(): Handle xact commit/rollback and I/O
332 * timings.
333 */
334 void
335 pgstat_update_dbstats(TimestampTz ts)
336 {
337 PgStat_StatDBEntry *dbentry;
338
339 /*
340 * If not connected to a database yet, don't attribute time to "shared
341 * state" (InvalidOid is used to track stats for shared relations, etc.).
342 */
343 if (!OidIsValid(MyDatabaseId))
344 return;
345
346 dbentry = pgstat_prep_database_pending(MyDatabaseId);
347
348 /*
349 * Accumulate xact commit/rollback and I/O timings to stats entry of the
350 * current database.
351 */
352 dbentry->xact_commit += pgStatXactCommit;
353 dbentry->xact_rollback += pgStatXactRollback;
354 dbentry->blk_read_time += pgStatBlockReadTime;
355 dbentry->blk_write_time += pgStatBlockWriteTime;
356
357 if (pgstat_should_report_connstat())
358 {
359 long secs;
360 int usecs;
361
362 /*
363 * pgLastSessionReportTime is initialized to MyStartTimestamp by
364 * pgstat_report_connect().
365 */
366 TimestampDifference(pgLastSessionReportTime, ts, &secs, &usecs);
367 pgLastSessionReportTime = ts;
368 dbentry->session_time += (PgStat_Counter) secs * 1000000 + usecs;
369 dbentry->active_time += pgStatActiveTime;
370 dbentry->idle_in_transaction_time += pgStatTransactionIdleTime;
371 }
372
373 pgStatXactCommit = 0;
374 pgStatXactRollback = 0;
375 pgStatBlockReadTime = 0;
376 pgStatBlockWriteTime = 0;
377 pgStatActiveTime = 0;
378 pgStatTransactionIdleTime = 0;
379 }
380
381 /*
382 * We report session statistics only for normal backend processes. Parallel
383 * workers run in parallel, so they don't contribute to session times, even
384 * though they use CPU time. Walsender processes could be considered here,
385 * but they have different session characteristics from normal backends (for
386 * example, they are always "active"), so they would skew session statistics.
387 */
388 static bool
389 pgstat_should_report_connstat(void)
390 {
391 return MyBackendType == B_BACKEND;
392 }
393
394 /*
395 * Find or create a local PgStat_StatDBEntry entry for dboid.
396 */
397 PgStat_StatDBEntry *
398 pgstat_prep_database_pending(Oid dboid)
399 {
400 PgStat_EntryRef *entry_ref;
401
402 /*
403 * This should not report stats on database objects before having
404 * connected to a database.
405 */
406 Assert(!OidIsValid(dboid) || OidIsValid(MyDatabaseId));
407
408 entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_DATABASE, dboid, InvalidOid,
409 NULL);
410
411 return entry_ref->pending;
412 }
413
414 /*
415 * Reset the database's reset timestamp, without resetting the contents of the
416 * database stats.
417 */
418 void
419 pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
420 {
421 PgStat_EntryRef *dbref;
422 PgStatShared_Database *dbentry;
423
424 dbref = pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, dboid, InvalidOid,
425 false);
426
427 dbentry = (PgStatShared_Database *) dbref->shared_stats;
428 dbentry->stats.stat_reset_timestamp = ts;
429
430 pgstat_unlock_entry(dbref);
431 }
432
433 /*
434 * Flush out pending stats for the entry
435 *
436 * If nowait is true and the lock could not be immediately acquired, returns
437 * false without flushing the entry. Otherwise returns true.
438 */
439 bool
440 pgstat_database_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
441 {
442 PgStatShared_Database *sharedent;
443 PgStat_StatDBEntry *pendingent;
444
445 pendingent = (PgStat_StatDBEntry *) entry_ref->pending;
446 sharedent = (PgStatShared_Database *) entry_ref->shared_stats;
447
448 if (!pgstat_lock_entry(entry_ref, nowait))
449 return false;
450
451 #define PGSTAT_ACCUM_DBCOUNT(item) \
452 (sharedent)->stats.item += (pendingent)->item
453
454 PGSTAT_ACCUM_DBCOUNT(xact_commit);
455 PGSTAT_ACCUM_DBCOUNT(xact_rollback);
456 PGSTAT_ACCUM_DBCOUNT(blocks_fetched);
457 PGSTAT_ACCUM_DBCOUNT(blocks_hit);
458
459 PGSTAT_ACCUM_DBCOUNT(tuples_returned);
460 PGSTAT_ACCUM_DBCOUNT(tuples_fetched);
461 PGSTAT_ACCUM_DBCOUNT(tuples_inserted);
462 PGSTAT_ACCUM_DBCOUNT(tuples_updated);
463 PGSTAT_ACCUM_DBCOUNT(tuples_deleted);
464
465 /* last_autovac_time is reported immediately */
466 Assert(pendingent->last_autovac_time == 0);
467
468 PGSTAT_ACCUM_DBCOUNT(conflict_tablespace);
469 PGSTAT_ACCUM_DBCOUNT(conflict_lock);
470 PGSTAT_ACCUM_DBCOUNT(conflict_snapshot);
471 PGSTAT_ACCUM_DBCOUNT(conflict_logicalslot);
472 PGSTAT_ACCUM_DBCOUNT(conflict_bufferpin);
473 PGSTAT_ACCUM_DBCOUNT(conflict_startup_deadlock);
474
475 PGSTAT_ACCUM_DBCOUNT(temp_bytes);
476 PGSTAT_ACCUM_DBCOUNT(temp_files);
477 PGSTAT_ACCUM_DBCOUNT(deadlocks);
478
479 /* checksum failures are reported immediately */
480 Assert(pendingent->checksum_failures == 0);
481 Assert(pendingent->last_checksum_failure == 0);
482
483 PGSTAT_ACCUM_DBCOUNT(blk_read_time);
484 PGSTAT_ACCUM_DBCOUNT(blk_write_time);
485
486 PGSTAT_ACCUM_DBCOUNT(sessions);
487 PGSTAT_ACCUM_DBCOUNT(session_time);
488 PGSTAT_ACCUM_DBCOUNT(active_time);
489 PGSTAT_ACCUM_DBCOUNT(idle_in_transaction_time);
490 PGSTAT_ACCUM_DBCOUNT(sessions_abandoned);
491 PGSTAT_ACCUM_DBCOUNT(sessions_fatal);
492 PGSTAT_ACCUM_DBCOUNT(sessions_killed);
493 PGSTAT_ACCUM_DBCOUNT(parallel_workers_to_launch);
494 PGSTAT_ACCUM_DBCOUNT(parallel_workers_launched);
495 #undef PGSTAT_ACCUM_DBCOUNT
496
497 pgstat_unlock_entry(entry_ref);
498
499 memset(pendingent, 0, sizeof(*pendingent));
500
501 return true;
502 }
503
504 void
505 pgstat_database_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
506 {
507 ((PgStatShared_Database *) header)->stats.stat_reset_timestamp = ts;
508 }