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 "utils/backend_progress.h"
16 #include "utils/backend_status.h"
17 
18 
19 /*-----------
20  * pgstat_progress_start_command() -
21  *
22  * Set st_progress_command (and st_progress_command_target) in own backend
23  * entry. Also, zero-initialize st_progress_param array.
24  *-----------
25  */
26 void
28 {
29  volatile PgBackendStatus *beentry = MyBEEntry;
30 
31  if (!beentry || !pgstat_track_activities)
32  return;
33 
35  beentry->st_progress_command = cmdtype;
36  beentry->st_progress_command_target = relid;
37  MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
39 }
40 
41 /*-----------
42  * pgstat_progress_update_param() -
43  *
44  * Update index'th member in st_progress_param[] of own backend entry.
45  *-----------
46  */
47 void
49 {
50  volatile PgBackendStatus *beentry = MyBEEntry;
51 
53 
54  if (!beentry || !pgstat_track_activities)
55  return;
56 
58  beentry->st_progress_param[index] = val;
60 }
61 
62 /*-----------
63  * pgstat_progress_incr_param() -
64  *
65  * Increment index'th member in st_progress_param[] of own backend entry.
66  *-----------
67  */
68 void
70 {
71  volatile PgBackendStatus *beentry = MyBEEntry;
72 
74 
75  if (!beentry || !pgstat_track_activities)
76  return;
77 
79  beentry->st_progress_param[index] += incr;
81 }
82 
83 /*-----------
84  * pgstat_progress_parallel_incr_param() -
85  *
86  * A variant of pgstat_progress_incr_param to allow a worker to poke at
87  * a leader to do an incremental progress update.
88  *-----------
89  */
90 void
92 {
93  /*
94  * Parallel workers notify a leader through a PqMsg_Progress message to
95  * update progress, passing the progress index and incremented value.
96  * Leaders can just call pgstat_progress_incr_param directly.
97  */
98  if (IsParallelWorker())
99  {
100  static StringInfoData progress_message;
101 
102  initStringInfo(&progress_message);
103 
104  pq_beginmessage(&progress_message, PqMsg_Progress);
105  pq_sendint32(&progress_message, index);
106  pq_sendint64(&progress_message, incr);
107  pq_endmessage(&progress_message);
108  }
109  else
111 }
112 
113 /*-----------
114  * pgstat_progress_update_multi_param() -
115  *
116  * Update multiple members in st_progress_param[] of own backend entry.
117  * This is atomic; readers won't see intermediate states.
118  *-----------
119  */
120 void
122  const int64 *val)
123 {
124  volatile PgBackendStatus *beentry = MyBEEntry;
125  int i;
126 
127  if (!beentry || !pgstat_track_activities || nparam == 0)
128  return;
129 
131 
132  for (i = 0; i < nparam; ++i)
133  {
135 
136  beentry->st_progress_param[index[i]] = val[i];
137  }
138 
139  PGSTAT_END_WRITE_ACTIVITY(beentry);
140 }
141 
142 /*-----------
143  * pgstat_progress_end_command() -
144  *
145  * Reset st_progress_command (and st_progress_command_target) in own backend
146  * entry. This signals the end of the command.
147  *-----------
148  */
149 void
151 {
152  volatile PgBackendStatus *beentry = MyBEEntry;
153 
154  if (!beentry || !pgstat_track_activities)
155  return;
156 
158  return;
159 
163  PGSTAT_END_WRITE_ACTIVITY(beentry);
164 }
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:812
int64_t int64
Definition: c.h:482
#define MemSet(start, val, len)
Definition: c.h:974
#define IsParallelWorker()
Definition: parallel.h:60
long val
Definition: informix.c:689
int i
Definition: isn.c:72
#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
#define PqMsg_Progress
Definition: protocol.h:69
void initStringInfo(StringInfo str)
Definition: stringinfo.c:56
ProgressCommandType st_progress_command
int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM]
Oid st_progress_command_target
Definition: type.h:96