|
| 1 | +/* ------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pgstat_multixact.c |
| 4 | + * Implementation of multixact statistics. |
| 5 | + * |
| 6 | + * This file contains the implementation of multixact 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_multixact.c |
| 15 | + * ------------------------------------------------------------------------- |
| 16 | + */ |
| 17 | + |
| 18 | +#include "postgres.h" |
| 19 | +#include "utils/pgstat_internal.h" |
| 20 | +#include "utils/timestamp.h" |
| 21 | + |
| 22 | +static bool have_multixact_stats = false; |
| 23 | + |
| 24 | +/* |
| 25 | + * pgstat_fetch_stat_multixact() |
| 26 | + * |
| 27 | + * Support function for the SQL-callable pgstat* functions. Returns |
| 28 | + * a pointer to the multixact statistics struct. |
| 29 | + */ |
| 30 | +PgStat_MultiXactStats * |
| 31 | +pgstat_fetch_stat_multixact(void) |
| 32 | +{ |
| 33 | + pgstat_snapshot_fixed(PGSTAT_KIND_MULTIXACT); |
| 34 | + |
| 35 | + return &pgStatLocal.snapshot.multixact; |
| 36 | +} |
| 37 | + |
| 38 | +bool |
| 39 | +pgstat_multixact_have_pending_cb(void) |
| 40 | +{ |
| 41 | + return have_multixact_stats; |
| 42 | +} |
| 43 | + |
| 44 | +void |
| 45 | +pgstat_update_multixact_stats(uint32 nmembers) |
| 46 | +{ |
| 47 | + PgStat_MultiXactStats * local_stats; |
| 48 | + TimestampTz now; |
| 49 | + |
| 50 | + now = GetCurrentTimestamp(); |
| 51 | + local_stats = &pgStatLocal.snapshot.multixact; |
| 52 | + local_stats->num_members_used = nmembers; |
| 53 | + local_stats->stat_update_timestamp = now; |
| 54 | + have_multixact_stats = true; |
| 55 | +} |
| 56 | + |
| 57 | +/* |
| 58 | + * Report multixact statistics. If nowait is true, then this function |
| 59 | + * will return if the lock is currently being held. |
| 60 | + * |
| 61 | + * This function returns true if the lock could not be acquired. Otherwise, false. |
| 62 | + */ |
| 63 | +bool |
| 64 | +pgstat_flush_multixact_cb(bool nowait) |
| 65 | +{ |
| 66 | + PgStatShared_MultiXact *stats_shmem; |
| 67 | + LWLock *shared_lock; |
| 68 | + |
| 69 | + stats_shmem = &pgStatLocal.shmem->multixact; |
| 70 | + shared_lock = &pgStatLocal.shmem->multixact.lock; |
| 71 | + |
| 72 | + // Since this statistic is a gauge, we have to check timestamps |
| 73 | + // of the shared statistic and the local statistic. If ours is |
| 74 | + // larger, then we can overwrite the shared statistic. |
| 75 | + if (!nowait) |
| 76 | + LWLockAcquire(shared_lock, LW_EXCLUSIVE); |
| 77 | + else if (!LWLockConditionalAcquire(shared_lock, LW_EXCLUSIVE)) |
| 78 | + return true; |
| 79 | + |
| 80 | + if (pgStatLocal.snapshot.multixact.stat_update_timestamp <= stats_shmem->stats.stat_update_timestamp) |
| 81 | + { |
| 82 | + // Return if our stats are <= the latest update. |
| 83 | + LWLockRelease(shared_lock); |
| 84 | + have_multixact_stats = false; |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + // Update multixact member usage and latest timestamp. |
| 89 | + stats_shmem->stats.num_members_used = pgStatLocal.snapshot.multixact.num_members_used; |
| 90 | + stats_shmem->stats.stat_update_timestamp = pgStatLocal.snapshot.multixact.stat_update_timestamp; |
| 91 | + have_multixact_stats = false; |
| 92 | + |
| 93 | + LWLockRelease(shared_lock); |
| 94 | + return false; |
| 95 | +} |
| 96 | + |
| 97 | +void |
| 98 | +pgstat_multixact_init_shmem_cb(void *stats) |
| 99 | +{ |
| 100 | + PgStatShared_MultiXact *stats_shmem; |
| 101 | + |
| 102 | + stats_shmem = (PgStatShared_MultiXact *) stats; |
| 103 | + LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA); |
| 104 | +} |
| 105 | + |
| 106 | +void |
| 107 | +pgstat_multixact_snapshot_cb(void) |
| 108 | +{ |
| 109 | + PgStat_MultiXactStats *local_snapshot; |
| 110 | + PgStatShared_MultiXact *stats_shmem; |
| 111 | + |
| 112 | + local_snapshot = &pgStatLocal.snapshot.multixact; |
| 113 | + stats_shmem = &pgStatLocal.shmem->multixact; |
| 114 | + |
| 115 | + LWLockAcquire(&stats_shmem->lock, LW_SHARED); |
| 116 | + if (local_snapshot->stat_update_timestamp < stats_shmem->stats.stat_update_timestamp) |
| 117 | + { |
| 118 | + local_snapshot->stat_update_timestamp = stats_shmem->stats.stat_update_timestamp; |
| 119 | + local_snapshot->num_members_used = stats_shmem->stats.num_members_used; |
| 120 | + } |
| 121 | + LWLockRelease(&stats_shmem->lock); |
| 122 | +} |
| 123 | + |
| 124 | +void |
| 125 | +pgstat_multixact_reset_all_cb(TimestampTz ts) |
| 126 | +{ |
| 127 | + PgStatShared_MultiXact *stats_shmem; |
| 128 | + |
| 129 | + stats_shmem = &pgStatLocal.shmem->multixact; |
| 130 | + LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE); |
| 131 | + memset(&stats_shmem->stats, 0, sizeof(stats_shmem->stats)); |
| 132 | + LWLockRelease(&stats_shmem->lock); |
| 133 | +} |
0 commit comments