Line data Source code
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-2024, 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 120 : pgstat_report_subscription_error(Oid subid, bool is_apply_error) 28 : { 29 : PgStat_EntryRef *entry_ref; 30 : PgStat_BackendSubEntry *pending; 31 : 32 120 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION, 33 : InvalidOid, subid, NULL); 34 120 : pending = entry_ref->pending; 35 : 36 120 : if (is_apply_error) 37 104 : pending->apply_error_count++; 38 : else 39 16 : pending->sync_error_count++; 40 120 : } 41 : 42 : /* 43 : * Report a subscription conflict. 44 : */ 45 : void 46 52 : pgstat_report_subscription_conflict(Oid subid, ConflictType type) 47 : { 48 : PgStat_EntryRef *entry_ref; 49 : PgStat_BackendSubEntry *pending; 50 : 51 52 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION, 52 : InvalidOid, subid, NULL); 53 52 : pending = entry_ref->pending; 54 52 : pending->conflict_count[type]++; 55 52 : } 56 : 57 : /* 58 : * Report creating the subscription. 59 : */ 60 : void 61 286 : pgstat_create_subscription(Oid subid) 62 : { 63 : /* Ensures that stats are dropped if transaction rolls back */ 64 286 : pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION, 65 : InvalidOid, subid); 66 : 67 : /* Create and initialize the subscription stats entry */ 68 286 : pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 69 : true, NULL); 70 286 : pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0); 71 286 : } 72 : 73 : /* 74 : * Report dropping the subscription. 75 : * 76 : * Ensures that stats are dropped if transaction commits. 77 : */ 78 : void 79 186 : pgstat_drop_subscription(Oid subid) 80 : { 81 186 : pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION, 82 : InvalidOid, subid); 83 186 : } 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 82 : pgstat_fetch_stat_subscription(Oid subid) 91 : { 92 82 : return (PgStat_StatSubEntry *) 93 82 : 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, this function returns false if lock could not 100 : * immediately acquired, otherwise true is returned. 101 : */ 102 : bool 103 138 : pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait) 104 : { 105 : PgStat_BackendSubEntry *localent; 106 : PgStatShared_Subscription *shsubent; 107 : 108 138 : localent = (PgStat_BackendSubEntry *) entry_ref->pending; 109 138 : shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats; 110 : 111 : /* localent always has non-zero content */ 112 : 113 138 : if (!pgstat_lock_entry(entry_ref, nowait)) 114 0 : return false; 115 : 116 : #define SUB_ACC(fld) shsubent->stats.fld += localent->fld 117 138 : SUB_ACC(apply_error_count); 118 138 : SUB_ACC(sync_error_count); 119 966 : for (int i = 0; i < CONFLICT_NUM_TYPES; i++) 120 828 : SUB_ACC(conflict_count[i]); 121 : #undef SUB_ACC 122 : 123 138 : pgstat_unlock_entry(entry_ref); 124 138 : return true; 125 : } 126 : 127 : void 128 310 : pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts) 129 : { 130 310 : ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts; 131 310 : }