Coverage Report

Created: 2025-06-15 06:31

/src/postgres/src/backend/utils/activity/pgstat_subscription.c
Line
Count
Source (jump to first uncovered line)
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
0
{
29
0
  PgStat_EntryRef *entry_ref;
30
0
  PgStat_BackendSubEntry *pending;
31
32
0
  entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
33
0
                      InvalidOid, subid, NULL);
34
0
  pending = entry_ref->pending;
35
36
0
  if (is_apply_error)
37
0
    pending->apply_error_count++;
38
0
  else
39
0
    pending->sync_error_count++;
40
0
}
41
42
/*
43
 * Report a subscription conflict.
44
 */
45
void
46
pgstat_report_subscription_conflict(Oid subid, ConflictType type)
47
0
{
48
0
  PgStat_EntryRef *entry_ref;
49
0
  PgStat_BackendSubEntry *pending;
50
51
0
  entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_SUBSCRIPTION,
52
0
                      InvalidOid, subid, NULL);
53
0
  pending = entry_ref->pending;
54
0
  pending->conflict_count[type]++;
55
0
}
56
57
/*
58
 * Report creating the subscription.
59
 */
60
void
61
pgstat_create_subscription(Oid subid)
62
0
{
63
  /* Ensures that stats are dropped if transaction rolls back */
64
0
  pgstat_create_transactional(PGSTAT_KIND_SUBSCRIPTION,
65
0
                InvalidOid, subid);
66
67
  /* Create and initialize the subscription stats entry */
68
0
  pgstat_get_entry_ref(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid,
69
0
             true, NULL);
70
0
  pgstat_reset_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid, 0);
71
0
}
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
0
{
81
0
  pgstat_drop_transactional(PGSTAT_KIND_SUBSCRIPTION,
82
0
                InvalidOid, subid);
83
0
}
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
0
{
92
0
  return (PgStat_StatSubEntry *)
93
0
    pgstat_fetch_entry(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid);
94
0
}
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
0
{
105
0
  PgStat_BackendSubEntry *localent;
106
0
  PgStatShared_Subscription *shsubent;
107
108
0
  localent = (PgStat_BackendSubEntry *) entry_ref->pending;
109
0
  shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
110
111
  /* localent always has non-zero content */
112
113
0
  if (!pgstat_lock_entry(entry_ref, nowait))
114
0
    return false;
115
116
0
#define SUB_ACC(fld) shsubent->stats.fld += localent->fld
117
0
  SUB_ACC(apply_error_count);
118
0
  SUB_ACC(sync_error_count);
119
0
  for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
120
0
    SUB_ACC(conflict_count[i]);
121
0
#undef SUB_ACC
122
123
0
  pgstat_unlock_entry(entry_ref);
124
0
  return true;
125
0
}
126
127
void
128
pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
129
0
{
130
0
  ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
131
0
}