| 1 |
/* ------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pgstat_subscription.c |
| 4 |
* Implementation of subscription statistics. |
| 5 |
* |
| 6 |
* This file contains the implementation of subscription 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_subscription.c |
| 15 |
* ------------------------------------------------------------------------- |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "postgres.h" |
| 19 |
|
| 20 |
#include "utils/pgstat_internal.h" |
| 21 |
|
| 22 |
|
| 23 |
/* |
| 24 |
* Report a subscription error. |
| 25 |
*/ |
| 26 |
void |
| 27 |
pgstat_report_subscription_error(Oid subid, bool is_apply_error) |
| 28 |
{ |
| 29 |
PgStat_EntryRef *entry_ref; |
| 30 |
PgStat_BackendSubEntry *pending; |
| 31 |
|
| 32 |
entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION, |
| 33 |
InvalidOid, subid, NULL); |
| 34 |
pending = entry_ref->pending; |
| 35 |
|
| 36 |
if (is_apply_error) |
| 37 |
pending->apply_error_count++; |
| 38 |
else |
| 39 |
pending->sync_error_count++; |
| 40 |
} |
| 41 |
|
| 42 |
/* |
| 43 |
* Report a subscription conflict. |
| 44 |
*/ |
| 45 |
void |
| 46 |
pgstat_report_subscription_conflict(Oid subid, ConflictType type) |
| 47 |
{ |
| 48 |
PgStat_EntryRef *entry_ref; |
| 49 |
PgStat_BackendSubEntry *pending; |
| 50 |
|
| 51 |
entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION, |
| 52 |
InvalidOid, subid, NULL); |
| 53 |
pending = entry_ref->pending; |
| 54 |
pending->conflict_count[type]++; |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* Report creating the subscription. |
| 59 |
*/ |
| 60 |
void |
| 61 |
pgstat_create_subscription(Oid subid) |
| 62 |
{ |
| 63 |
/* Ensures that stats are dropped if transaction rolls back */ |
| 64 |
pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION, |
| 65 |
InvalidOid, subid); |
| 66 |
|
| 67 |
/* Create and initialize the subscription stats entry */ |
| 68 |
pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, |
| 69 |
true, NULL); |
| 70 |
pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Report dropping the subscription. |
| 75 |
* |
| 76 |
* Ensures that stats are dropped if transaction commits. |
| 77 |
*/ |
| 78 |
void |
| 79 |
pgstat_drop_subscription(Oid subid) |
| 80 |
{ |
| 81 |
pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION, |
| 82 |
InvalidOid, subid); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Support function for the SQL-callable pgstat* functions. Returns |
| 87 |
* the collected statistics for one subscription or NULL. |
| 88 |
*/ |
| 89 |
PgStat_StatSubEntry * |
| 90 |
pgstat_fetch_stat_subscription(Oid subid) |
| 91 |
{ |
| 92 |
return (PgStat_StatSubEntry *) |
| 93 |
pgstat_fetch_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid); |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Flush out pending stats for the entry |
| 98 |
* |
| 99 |
* If nowait is true and the lock could not be immediately acquired, returns |
| 100 |
* false without flushing the entry. Otherwise returns true. |
| 101 |
*/ |
| 102 |
bool |
| 103 |
pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait) |
| 104 |
{ |
| 105 |
PgStat_BackendSubEntry *localent; |
| 106 |
PgStatShared_Subscription *shsubent; |
| 107 |
|
| 108 |
localent = (PgStat_BackendSubEntry *) entry_ref->pending; |
| 109 |
shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats; |
| 110 |
|
| 111 |
/* localent always has non-zero content */ |
| 112 |
|
| 113 |
if (!pgstat_lock_entry(entry_ref, nowait)) |
| 114 |
return false; |
| 115 |
|
| 116 |
#define SUB_ACC(fld) shsubent->stats.fld += localent->fld |
| 117 |
SUB_ACC(apply_error_count); |
| 118 |
SUB_ACC(sync_error_count); |
| 119 |
for (int i = 0; i < CONFLICT_NUM_TYPES; i++) |
| 120 |
SUB_ACC(conflict_count[i]); |
| 121 |
#undef SUB_ACC |
| 122 |
|
| 123 |
pgstat_unlock_entry(entry_ref); |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts) |
| 129 |
{ |
| 130 |
((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts; |
| 131 |
} |