PostgreSQL Source Code  git master
backend_progress.c
Go to the documentation of this file.
1 /* ----------
2  * backend_progress.c
3  *
4  * Command progress reporting infrastructure.
5  *
6  * Copyright (c) 2001-2024, PostgreSQL Global Development Group
7  *
8  * src/backend/utils/activity/backend_progress.c
9  * ----------
10  */
11 #include "postgres.h"
12 
13 #include "access/parallel.h"
14 #include "libpq/pqformat.h"
15 #include "port/atomics.h" /* for memory barriers */
16 #include "utils/backend_progress.h"
17 #include "utils/backend_status.h"
18 
19 
20 /*-----------
21  * pgstat_progress_start_command() -
22  *
23  * Set st_progress_command (and st_progress_command_target) in own backend
24  * entry. Also, zero-initialize st_progress_param array.
25  *-----------
26  */
27 void
29 {
30  volatile PgBackendStatus *beentry = MyBEEntry;
31 
32  if (!beentry || !pgstat_track_activities)
33  return;
34 
36  beentry->st_progress_command = cmdtype;
37  beentry->st_progress_command_target = relid;
38  MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
40 }
41 
42 /*-----------
43  * pgstat_progress_update_param() -
44  *
45  * Update index'th member in st_progress_param[] of own backend entry.
46  *-----------
47  */
48 void
50 {
51  volatile PgBackendStatus *beentry = MyBEEntry;
52 
54 
55  if (!beentry || !pgstat_track_activities)
56  return;
57 
59  beentry->st_progress_param[index] = val;
61 }
62 
63 /*-----------
64  * pgstat_progress_incr_param() -
65  *
66  * Increment index'th member in st_progress_param[] of own backend entry.
67  *-----------
68  */
69 void
71 {
72  volatile PgBackendStatus *beentry = MyBEEntry;
73 
75 
76  if (!beentry || !pgstat_track_activities)
77  return;
78 
80  beentry->st_progress_param[index] += incr;
82 }
83 
84 /*-----------
85  * pgstat_progress_parallel_incr_param() -
86  *
87  * A variant of pgstat_progress_incr_param to allow a worker to poke at
88  * a leader to do an incremental progress update.
89  *-----------
90  */
91 void
93 {
94  /*
95  * Parallel workers notify a leader through a 'P' protocol message to
96  * update progress, passing the progress index and incremented value.
97  * Leaders can just call pgstat_progress_incr_param directly.
98  */
99  if (IsParallelWorker())
100  {
101  static StringInfoData progress_message;
102 
103  initStringInfo(&progress_message);
104 
105  pq_beginmessage(&progress_message, 'P');
106  pq_sendint32(&progress_message, index);
107  pq_sendint64(&progress_message, incr);
108  pq_endmessage(&progress_message);
109  }
110  else
112 }
113 
114 /*-----------
115  * pgstat_progress_update_multi_param() -
116  *
117  * Update multiple members in st_progress_param[] of own backend entry.
118  * This is atomic; readers won't see intermediate states.
119  *-----------
120  */
121 void
123  const int64 *val)
124 {
125  volatile PgBackendStatus *beentry = MyBEEntry;
126  int i;
127 
128  if (!beentry || !pgstat_track_activities || nparam == 0)
129  return;
130 
132 
133  for (i = 0; i < nparam; ++i)
134  {
136 
137  beentry->st_progress_param[index[i]] = val[i];
138  }
139 
140  PGSTAT_END_WRITE_ACTIVITY(beentry);
141 }
142 
143 /*-----------
144  * pgstat_progress_end_command() -
145  *
146  * Reset st_progress_command (and st_progress_command_target) in own backend
147  * entry. This signals the end of the command.
148  *-----------
149  */
150 void
152 {
153  volatile PgBackendStatus *beentry = MyBEEntry;
154 
155  if (!beentry || !pgstat_track_activities)
156  return;
157 
159  return;
160 
164  PGSTAT_END_WRITE_ACTIVITY(beentry);
165 }
void pgstat_progress_parallel_incr_param(int index, int64 incr)
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_incr_param(int index, int64 incr)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
#define PGSTAT_NUM_PROGRESS_PARAM
ProgressCommandType
@ PROGRESS_COMMAND_INVALID
PgBackendStatus * MyBEEntry
bool pgstat_track_activities
#define PGSTAT_END_WRITE_ACTIVITY(beentry)
#define PGSTAT_BEGIN_WRITE_ACTIVITY(beentry)
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
#define IsParallelWorker()
Definition: parallel.h:60
long val
Definition: informix.c:670
int i
Definition: isn.c:73
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:152
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
ProgressCommandType st_progress_command
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Oid st_progress_command_target
Definition: type.h:95