PostgreSQL Source Code git master
pgstat_subscription.c
Go to the documentation of this file.
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
22
23
24/*
25 * Report a subscription error.
26 */
27void
29{
30 PgStat_EntryRef *entry_ref;
32
34 InvalidOid, subid, NULL);
35 pending = entry_ref->pending;
36
37 switch (wtype)
38 {
40 pending->apply_error_count++;
41 break;
42
44 pending->sync_seq_error_count++;
45 break;
46
48 pending->sync_table_error_count++;
49 break;
50
51 default:
52 /* Should never happen. */
53 Assert(0);
54 break;
55 }
56}
57
58/*
59 * Report a subscription conflict.
60 */
61void
63{
64 PgStat_EntryRef *entry_ref;
66
68 InvalidOid, subid, NULL);
69 pending = entry_ref->pending;
70 pending->conflict_count[type]++;
71}
72
73/*
74 * Report creating the subscription.
75 */
76void
78{
79 /* Ensures that stats are dropped if transaction rolls back */
81 InvalidOid, subid);
82
83 /* Create and initialize the subscription stats entry */
85 true, NULL);
87}
88
89/*
90 * Report dropping the subscription.
91 *
92 * Ensures that stats are dropped if transaction commits.
93 */
94void
96{
98 InvalidOid, subid);
99}
100
101/*
102 * Support function for the SQL-callable pgstat* functions. Returns
103 * the collected statistics for one subscription or NULL.
104 */
107{
108 return (PgStat_StatSubEntry *)
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 */
118bool
120{
121 PgStat_BackendSubEntry *localent;
123
124 localent = (PgStat_BackendSubEntry *) entry_ref->pending;
125 shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
126
127 /* localent always has non-zero content */
128
129 if (!pgstat_lock_entry(entry_ref, nowait))
130 return false;
131
132#define SUB_ACC(fld) shsubent->stats.fld += localent->fld
133 SUB_ACC(apply_error_count);
134 SUB_ACC(sync_seq_error_count);
135 SUB_ACC(sync_table_error_count);
136 for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
137 SUB_ACC(conflict_count[i]);
138#undef SUB_ACC
139
140 pgstat_unlock_entry(entry_ref);
141 return true;
142}
143
144void
146{
147 ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
148}
ConflictType
Definition: conflict.h:32
#define CONFLICT_NUM_TYPES
Definition: conflict.h:64
int64 TimestampTz
Definition: timestamp.h:39
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
Definition: pgstat.c:1265
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:934
#define PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat_kind.h:31
PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry)
Definition: pgstat_shmem.c:469
void pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:720
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:690
void pgstat_drop_subscription(Oid subid)
void pgstat_create_subscription(Oid subid)
void pgstat_report_subscription_conflict(Oid subid, ConflictType type)
void pgstat_report_subscription_error(Oid subid, LogicalRepWorkerType wtype)
#define SUB_ACC(fld)
bool pgstat_subscription_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_xact.c:384
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat_xact.c:361
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
PgStat_Counter sync_table_error_count
Definition: pgstat.h:113
PgStat_Counter apply_error_count
Definition: pgstat.h:111
PgStat_Counter sync_seq_error_count
Definition: pgstat.h:112
PgStat_Counter conflict_count[CONFLICT_NUM_TYPES]
Definition: pgstat.h:114
PgStatShared_Common * shared_stats
const char * type
LogicalRepWorkerType
@ WORKERTYPE_TABLESYNC
@ WORKERTYPE_SEQUENCESYNC
@ WORKERTYPE_APPLY