PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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
21
22
23/*
24 * Report a subscription error.
25 */
26void
27pgstat_report_subscription_error(Oid subid, bool is_apply_error)
28{
29 PgStat_EntryRef *entry_ref;
31
33 InvalidOid, subid, NULL);
34 pending = entry_ref->pending;
35
36 if (is_apply_error)
37 pending->apply_error_count++;
38 else
39 pending->sync_error_count++;
40}
41
42/*
43 * Report a subscription conflict.
44 */
45void
47{
48 PgStat_EntryRef *entry_ref;
50
52 InvalidOid, subid, NULL);
53 pending = entry_ref->pending;
54 pending->conflict_count[type]++;
55}
56
57/*
58 * Report creating the subscription.
59 */
60void
62{
63 /* Ensures that stats are dropped if transaction rolls back */
65 InvalidOid, subid);
66
67 /* Create and initialize the subscription stats entry */
69 true, NULL);
71}
72
73/*
74 * Report dropping the subscription.
75 *
76 * Ensures that stats are dropped if transaction commits.
77 */
78void
80{
82 InvalidOid, subid);
83}
84
85/*
86 * Support function for the SQL-callable pgstat* functions. Returns
87 * the collected statistics for one subscription or NULL.
88 */
91{
92 return (PgStat_StatSubEntry *)
94}
95
96/*
97 * Flush out pending stats for the entry
98 *
99 * If nowait is true, this function returns false if lock could not
100 * immediately acquired, otherwise true is returned.
101 */
102bool
104{
105 PgStat_BackendSubEntry *localent;
107
108 localent = (PgStat_BackendSubEntry *) entry_ref->pending;
109 shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
110
111 /* localent always has non-zero content */
112
113 if (!pgstat_lock_entry(entry_ref, nowait))
114 return false;
115
116#define SUB_ACC(fld) shsubent->stats.fld += localent->fld
117 SUB_ACC(apply_error_count);
118 SUB_ACC(sync_error_count);
119 for (int i = 0; i < CONFLICT_NUM_TYPES; i++)
120 SUB_ACC(conflict_count[i]);
121#undef SUB_ACC
122
123 pgstat_unlock_entry(entry_ref);
124 return true;
125}
126
127void
129{
130 ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
131}
ConflictType
Definition: conflict.h:25
#define CONFLICT_NUM_TYPES
Definition: conflict.h:54
int64 TimestampTz
Definition: timestamp.h:39
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:1280
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:946
#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:444
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:684
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:654
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, bool is_apply_error)
#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:35
unsigned int Oid
Definition: postgres_ext.h:30
PgStat_Counter apply_error_count
Definition: pgstat.h:109
PgStat_Counter sync_error_count
Definition: pgstat.h:110
PgStat_Counter conflict_count[CONFLICT_NUM_TYPES]
Definition: pgstat.h:111
PgStatShared_Common * shared_stats
const char * type