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-2023, 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 108 : pgstat_report_subscription_error(Oid subid, bool is_apply_error) 28 : { 29 : PgStat_EntryRef *entry_ref; 30 : PgStat_BackendSubEntry *pending; 31 : 32 108 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION, 33 : InvalidOid, subid, NULL); 34 108 : pending = entry_ref->pending; 35 : 36 108 : if (is_apply_error) 37 92 : pending->apply_error_count++; 38 : else 39 16 : pending->sync_error_count++; 40 108 : } 41 : 42 : /* 43 : * Report creating the subscription. 44 : */ 45 : void 46 228 : pgstat_create_subscription(Oid subid) 47 : { 48 : /* Ensures that stats are dropped if transaction rolls back */ 49 228 : pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION, 50 : InvalidOid, subid); 51 : 52 : /* Create and initialize the subscription stats entry */ 53 228 : pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 54 : true, NULL); 55 228 : pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0); 56 228 : } 57 : 58 : /* 59 : * Report dropping the subscription. 60 : * 61 : * Ensures that stats are dropped if transaction commits. 62 : */ 63 : void 64 146 : pgstat_drop_subscription(Oid subid) 65 : { 66 146 : pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION, 67 : InvalidOid, subid); 68 146 : } 69 : 70 : /* 71 : * Support function for the SQL-callable pgstat* functions. Returns 72 : * the collected statistics for one subscription or NULL. 73 : */ 74 : PgStat_StatSubEntry * 75 58 : pgstat_fetch_stat_subscription(Oid subid) 76 : { 77 58 : return (PgStat_StatSubEntry *) 78 58 : pgstat_fetch_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid); 79 : } 80 : 81 : /* 82 : * Flush out pending stats for the entry 83 : * 84 : * If nowait is true, this function returns false if lock could not 85 : * immediately acquired, otherwise true is returned. 86 : */ 87 : bool 88 108 : pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait) 89 : { 90 : PgStat_BackendSubEntry *localent; 91 : PgStatShared_Subscription *shsubent; 92 : 93 108 : localent = (PgStat_BackendSubEntry *) entry_ref->pending; 94 108 : shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats; 95 : 96 : /* localent always has non-zero content */ 97 : 98 108 : if (!pgstat_lock_entry(entry_ref, nowait)) 99 0 : return false; 100 : 101 : #define SUB_ACC(fld) shsubent->stats.fld += localent->fld 102 108 : SUB_ACC(apply_error_count); 103 108 : SUB_ACC(sync_error_count); 104 : #undef SUB_ACC 105 : 106 108 : pgstat_unlock_entry(entry_ref); 107 108 : return true; 108 : } 109 : 110 : void 111 252 : pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts) 112 : { 113 252 : ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts; 114 252 : }