Branch data 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-2026, 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 "replication/worker_internal.h"
21 : : #include "utils/pgstat_internal.h"
22 : :
23 : :
24 : : /*
25 : : * Report a subscription error.
26 : : */
27 : : void
28 : 0 : pgstat_report_subscription_error(Oid subid, LogicalRepWorkerType wtype)
29 : : {
30 : 0 : PgStat_EntryRef *entry_ref;
31 : 0 : PgStat_BackendSubEntry *pending;
32 : :
33 : 0 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
34 : 0 : InvalidOid, subid, NULL);
35 : 0 : pending = entry_ref->pending;
36 : :
37 [ # # # # ]: 0 : switch (wtype)
38 : : {
39 : : case WORKERTYPE_APPLY:
40 : 0 : pending->apply_error_count++;
41 : 0 : break;
42 : :
43 : : case WORKERTYPE_SEQUENCESYNC:
44 : 0 : pending->sync_seq_error_count++;
45 : 0 : break;
46 : :
47 : : case WORKERTYPE_TABLESYNC:
48 : 0 : pending->sync_table_error_count++;
49 : 0 : break;
50 : :
51 : : default:
52 : : /* Should never happen. */
53 : 0 : Assert(0);
54 : 0 : break;
55 : : }
56 : 0 : }
57 : :
58 : : /*
59 : : * Report a subscription conflict.
60 : : */
61 : : void
62 : 0 : pgstat_report_subscription_conflict(Oid subid, ConflictType type)
63 : : {
64 : 0 : PgStat_EntryRef *entry_ref;
65 : 0 : PgStat_BackendSubEntry *pending;
66 : :
67 : 0 : entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
68 : 0 : InvalidOid, subid, NULL);
69 : 0 : pending = entry_ref->pending;
70 : 0 : pending->conflict_count[type]++;
71 : 0 : }
72 : :
73 : : /*
74 : : * Report creating the subscription.
75 : : */
76 : : void
77 : 13 : pgstat_create_subscription(Oid subid)
78 : : {
79 : : /* Ensures that stats are dropped if transaction rolls back */
80 : 13 : pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION,
81 : 13 : InvalidOid, subid);
82 : :
83 : : /* Create and initialize the subscription stats entry */
84 : 13 : pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid,
85 : : true, NULL);
86 : 13 : pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0);
87 : 13 : }
88 : :
89 : : /*
90 : : * Report dropping the subscription.
91 : : *
92 : : * Ensures that stats are dropped if transaction commits.
93 : : */
94 : : void
95 : 13 : pgstat_drop_subscription(Oid subid)
96 : : {
97 : 13 : pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION,
98 : 13 : InvalidOid, subid);
99 : 13 : }
100 : :
101 : : /*
102 : : * Support function for the SQL-callable pgstat* functions. Returns
103 : : * the collected statistics for one subscription or NULL.
104 : : */
105 : : PgStat_StatSubEntry *
106 : 4 : pgstat_fetch_stat_subscription(Oid subid)
107 : : {
108 : 4 : return (PgStat_StatSubEntry *)
109 : 4 : pgstat_fetch_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid);
110 : : }
111 : :
112 : : /*
113 : : * Flush out pending stats for the entry
114 : : *
115 : : * If nowait is true and the lock could not be immediately acquired, returns
116 : : * false without flushing the entry. Otherwise returns true.
117 : : */
118 : : bool
119 : 0 : pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
120 : : {
121 : 0 : PgStat_BackendSubEntry *localent;
122 : 0 : PgStatShared_Subscription *shsubent;
123 : :
124 : 0 : localent = (PgStat_BackendSubEntry *) entry_ref->pending;
125 : 0 : shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
126 : :
127 : : /* localent always has non-zero content */
128 : :
129 [ # # ]: 0 : if (!pgstat_lock_entry(entry_ref, nowait))
130 : 0 : return false;
131 : :
132 : : #define SUB_ACC(fld) shsubent->stats.fld += localent->fld
133 : 0 : SUB_ACC(apply_error_count);
134 : 0 : SUB_ACC(sync_seq_error_count);
135 : 0 : SUB_ACC(sync_table_error_count);
136 [ # # ]: 0 : for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
137 : 0 : SUB_ACC(conflict_count[i]);
138 : : #undef SUB_ACC
139 : :
140 : 0 : pgstat_unlock_entry(entry_ref);
141 : 0 : return true;
142 : 0 : }
143 : :
144 : : void
145 : 15 : pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
146 : : {
147 : 15 : ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
148 : 15 : }
|