Age Owner Branch data TLA 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-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
1265 andres@anarazel.de 27 :CBC 88 : pgstat_report_subscription_error(Oid subid, bool is_apply_error)
28 : : {
29 : : PgStat_EntryRef *entry_ref;
30 : : PgStat_BackendSubEntry *pending;
31 : :
1249 32 : 88 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
33 : : InvalidOid, subid, NULL);
34 : 88 : pending = entry_ref->pending;
35 : :
36 [ + + ]: 88 : if (is_apply_error)
37 : 77 : pending->apply_error_count++;
38 : : else
39 : 11 : pending->sync_error_count++;
1265 40 : 88 : }
41 : :
42 : : /*
43 : : * Report a subscription conflict.
44 : : */
45 : : void
367 akapila@postgresql.o 46 : 51 : pgstat_report_subscription_conflict(Oid subid, ConflictType type)
47 : : {
48 : : PgStat_EntryRef *entry_ref;
49 : : PgStat_BackendSubEntry *pending;
50 : :
51 : 51 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
52 : : InvalidOid, subid, NULL);
53 : 51 : pending = entry_ref->pending;
54 : 51 : pending->conflict_count[type]++;
55 : 51 : }
56 : :
57 : : /*
58 : : * Report creating the subscription.
59 : : */
60 : : void
1249 andres@anarazel.de 61 : 163 : pgstat_create_subscription(Oid subid)
62 : : {
63 : : /* Ensures that stats are dropped if transaction rolls back */
64 : 163 : pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION,
65 : : InvalidOid, subid);
66 : :
67 : : /* Create and initialize the subscription stats entry */
1066 68 : 163 : pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid,
69 : : true, NULL);
70 : 163 : pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0);
1249 71 : 163 : }
72 : :
73 : : /*
74 : : * Report dropping the subscription.
75 : : *
76 : : * Ensures that stats are dropped if transaction commits.
77 : : */
78 : : void
79 : 114 : pgstat_drop_subscription(Oid subid)
80 : : {
81 : 114 : pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION,
82 : : InvalidOid, subid);
1265 83 : 114 : }
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 *
1249 90 : 37 : pgstat_fetch_stat_subscription(Oid subid)
91 : : {
92 : 37 : return (PgStat_StatSubEntry *)
93 : 37 : 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 : 103 : pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
104 : : {
105 : : PgStat_BackendSubEntry *localent;
106 : : PgStatShared_Subscription *shsubent;
107 : :
108 : 103 : localent = (PgStat_BackendSubEntry *) entry_ref->pending;
109 : 103 : shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
110 : :
111 : : /* localent always has non-zero content */
112 : :
113 [ - + ]: 103 : if (!pgstat_lock_entry(entry_ref, nowait))
1249 andres@anarazel.de 114 :UBC 0 : return false;
115 : :
116 : : #define SUB_ACC(fld) shsubent->stats.fld += localent->fld
1249 andres@anarazel.de 117 :CBC 103 : SUB_ACC(apply_error_count);
118 : 103 : SUB_ACC(sync_error_count);
367 akapila@postgresql.o 119 [ + + ]: 927 : for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
120 : 824 : SUB_ACC(conflict_count[i]);
121 : : #undef SUB_ACC
122 : :
1249 andres@anarazel.de 123 : 103 : pgstat_unlock_entry(entry_ref);
124 : 103 : return true;
125 : : }
126 : :
127 : : void
128 : 175 : pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
129 : : {
130 : 175 : ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
131 : 175 : }
|