Branch data Line data Source code
1 : : /* ----------
2 : : * backend_progress.c
3 : : *
4 : : * Command progress reporting infrastructure.
5 : : *
6 : : * Copyright (c) 2001-2026, 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 "storage/proc.h"
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
28 : 164353 : pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
29 : : {
30 : 164353 : volatile PgBackendStatus *beentry = MyBEEntry;
31 : :
32 [ + - - + ]: 164353 : if (!beentry || !pgstat_track_activities)
33 : 0 : return;
34 : :
35 : 164353 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
36 : 164353 : beentry->st_progress_command = cmdtype;
37 : 164353 : beentry->st_progress_command_target = relid;
38 [ + - + - : 3451413 : MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
+ - + - +
+ ]
39 : 164353 : PGSTAT_END_WRITE_ACTIVITY(beentry);
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
49 : 14397992 : pgstat_progress_update_param(int index, int64 val)
50 : : {
51 : 14397992 : volatile PgBackendStatus *beentry = MyBEEntry;
52 : :
53 : : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
54 : :
55 [ + - - + ]: 14397992 : if (!beentry || !pgstat_track_activities)
56 : 0 : return;
57 : :
58 : 14397992 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
59 : 14397992 : beentry->st_progress_param[index] = val;
60 : 14397992 : PGSTAT_END_WRITE_ACTIVITY(beentry);
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
70 : 2291 : pgstat_progress_incr_param(int index, int64 incr)
71 : : {
72 : 2291 : volatile PgBackendStatus *beentry = MyBEEntry;
73 : :
74 : : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
75 : :
76 [ + - - + ]: 2291 : if (!beentry || !pgstat_track_activities)
77 : 0 : return;
78 : :
79 : 2291 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
80 : 2291 : beentry->st_progress_param[index] += incr;
81 : 2291 : PGSTAT_END_WRITE_ACTIVITY(beentry);
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
92 : 140 : pgstat_progress_parallel_incr_param(int index, int64 incr)
93 : : {
94 : : /*
95 : : * Parallel workers notify a leader through a PqMsg_Progress message to
96 : : * update progress, passing the progress index and incremented value.
97 : : * Leaders can just call pgstat_progress_incr_param directly.
98 : : */
99 [ + + ]: 140 : if (IsParallelWorker())
100 : : {
101 : : static StringInfoData progress_message;
102 : :
103 : 4 : pq_beginmessage(&progress_message, PqMsg_Progress);
104 : 4 : pq_sendint32(&progress_message, index);
105 : 4 : pq_sendint64(&progress_message, incr);
106 : 4 : pq_endmessage(&progress_message);
107 : : }
108 : : else
109 : 136 : pgstat_progress_incr_param(index, incr);
110 : 140 : }
111 : :
112 : : /*-----------
113 : : * pgstat_progress_update_multi_param() -
114 : : *
115 : : * Update multiple members in st_progress_param[] of own backend entry.
116 : : * This is atomic; readers won't see intermediate states.
117 : : *-----------
118 : : */
119 : : void
120 : 966242 : pgstat_progress_update_multi_param(int nparam, const int *index,
121 : : const int64 *val)
122 : : {
123 : 966242 : volatile PgBackendStatus *beentry = MyBEEntry;
124 : : int i;
125 : :
126 [ + - + - : 966242 : if (!beentry || !pgstat_track_activities || nparam == 0)
- + ]
127 : 0 : return;
128 : :
129 : 966242 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
130 : :
131 [ + + ]: 2761194 : for (i = 0; i < nparam; ++i)
132 : : {
133 : : Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
134 : :
135 : 1794952 : beentry->st_progress_param[index[i]] = val[i];
136 : : }
137 : :
138 : 966242 : PGSTAT_END_WRITE_ACTIVITY(beentry);
139 : : }
140 : :
141 : : /*-----------
142 : : * pgstat_progress_end_command() -
143 : : *
144 : : * Reset st_progress_command (and st_progress_command_target) in own backend
145 : : * entry. This signals the end of the command.
146 : : *-----------
147 : : */
148 : : void
149 : 203804 : pgstat_progress_end_command(void)
150 : : {
151 : 203804 : volatile PgBackendStatus *beentry = MyBEEntry;
152 : :
153 [ + - - + ]: 203804 : if (!beentry || !pgstat_track_activities)
154 : 0 : return;
155 : :
156 [ + + ]: 203804 : if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID)
157 : 40167 : return;
158 : :
159 : 163637 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
160 : 163637 : beentry->st_progress_command = PROGRESS_COMMAND_INVALID;
161 : 163637 : beentry->st_progress_command_target = InvalidOid;
162 : 163637 : PGSTAT_END_WRITE_ACTIVITY(beentry);
163 : : }
|