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-2022, 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 {
29  PgStat_EntryRef *entry_ref;
30  PgStat_BackendSubEntry *pending;
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 creating the subscription.
44  *
45  * Ensures that stats are dropped if transaction rolls back.
46  */
47 void
49 {
51  InvalidOid, subid);
52 }
53 
54 /*
55  * Report dropping the subscription.
56  *
57  * Ensures that stats are dropped if transaction commits.
58  */
59 void
61 {
63  InvalidOid, subid);
64 }
65 
66 /*
67  * Support function for the SQL-callable pgstat* functions. Returns
68  * the collected statistics for one subscription or NULL.
69  */
72 {
73  return (PgStat_StatSubEntry *)
75 }
76 
77 /*
78  * Flush out pending stats for the entry
79  *
80  * If nowait is true, this function returns false if lock could not
81  * immediately acquired, otherwise true is returned.
82  */
83 bool
85 {
86  PgStat_BackendSubEntry *localent;
87  PgStatShared_Subscription *shsubent;
88 
89  localent = (PgStat_BackendSubEntry *) entry_ref->pending;
90  shsubent = (PgStatShared_Subscription *) entry_ref->shared_stats;
91 
92  /* localent always has non-zero content */
93 
94  if (!pgstat_lock_entry(entry_ref, nowait))
95  return false;
96 
97 #define SUB_ACC(fld) shsubent->stats.fld += localent->fld
98  SUB_ACC(apply_error_count);
99  SUB_ACC(sync_error_count);
100 #undef SUB_ACC
101 
102  pgstat_unlock_entry(entry_ref);
103  return true;
104 }
105 
106 void
108 {
109  ((PgStatShared_Subscription *) header)->stats.stat_reset_timestamp = ts;
110 }
int64 TimestampTz
Definition: timestamp.h:39
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:212
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:779
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1053
@ PGSTAT_KIND_SUBSCRIPTION
Definition: pgstat.h:45
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:583
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:571
void pgstat_drop_subscription(Oid subid)
void pgstat_create_subscription(Oid subid)
PgStat_StatSubEntry * pgstat_fetch_stat_subscription(Oid subid)
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)
void pgstat_subscription_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:366
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:388
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
PgStat_Counter apply_error_count
Definition: pgstat.h:144
PgStat_Counter sync_error_count
Definition: pgstat.h:145
PgStatShared_Common * shared_stats