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 164986 : pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
29 : {
30 164986 : volatile PgBackendStatus *beentry = MyBEEntry;
31 :
32 164986 : if (!beentry || !pgstat_track_activities)
33 0 : return;
34 :
35 164986 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
36 164986 : beentry->st_progress_command = cmdtype;
37 164986 : beentry->st_progress_command_target = relid;
38 3464706 : MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
39 164986 : 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 14357507 : pgstat_progress_update_param(int index, int64 val)
50 : {
51 14357507 : volatile PgBackendStatus *beentry = MyBEEntry;
52 :
53 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
54 :
55 14357507 : if (!beentry || !pgstat_track_activities)
56 0 : return;
57 :
58 14357507 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
59 14357507 : beentry->st_progress_param[index] = val;
60 14357507 : 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 2276 : pgstat_progress_incr_param(int index, int64 incr)
71 : {
72 2276 : volatile PgBackendStatus *beentry = MyBEEntry;
73 :
74 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
75 :
76 2276 : if (!beentry || !pgstat_track_activities)
77 0 : return;
78 :
79 2276 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
80 2276 : beentry->st_progress_param[index] += incr;
81 2276 : 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 126 : 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 126 : if (IsParallelWorker())
100 : {
101 : static StringInfoData progress_message;
102 :
103 2 : pq_beginmessage(&progress_message, PqMsg_Progress);
104 2 : pq_sendint32(&progress_message, index);
105 2 : pq_sendint64(&progress_message, incr);
106 2 : pq_endmessage(&progress_message);
107 : }
108 : else
109 124 : pgstat_progress_incr_param(index, incr);
110 126 : }
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 953633 : pgstat_progress_update_multi_param(int nparam, const int *index,
121 : const int64 *val)
122 : {
123 953633 : volatile PgBackendStatus *beentry = MyBEEntry;
124 : int i;
125 :
126 953633 : if (!beentry || !pgstat_track_activities || nparam == 0)
127 0 : return;
128 :
129 953633 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
130 :
131 2726021 : for (i = 0; i < nparam; ++i)
132 : {
133 : Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
134 :
135 1772388 : beentry->st_progress_param[index[i]] = val[i];
136 : }
137 :
138 953633 : 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 204341 : pgstat_progress_end_command(void)
150 : {
151 204341 : volatile PgBackendStatus *beentry = MyBEEntry;
152 :
153 204341 : if (!beentry || !pgstat_track_activities)
154 0 : return;
155 :
156 204341 : if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID)
157 40070 : return;
158 :
159 164271 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
160 164271 : beentry->st_progress_command = PROGRESS_COMMAND_INVALID;
161 164271 : beentry->st_progress_command_target = InvalidOid;
162 164271 : PGSTAT_END_WRITE_ACTIVITY(beentry);
163 : }
|