Age Owner Branch data TLA 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
1914 andres@anarazel.de 28 :CBC 54874 : pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
29 : : {
30 : 54874 : volatile PgBackendStatus *beentry = MyBEEntry;
31 : :
32 [ + - - + ]: 54874 : if (!beentry || !pgstat_track_activities)
1914 andres@anarazel.de 33 :UBC 0 : return;
34 : :
1914 andres@anarazel.de 35 :CBC 54874 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
36 : 54874 : beentry->st_progress_command = cmdtype;
37 : 54874 : beentry->st_progress_command_target = relid;
38 [ + - + - : 1152354 : MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
+ - + - +
+ ]
39 [ - + - + ]: 54874 : 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 : 13504942 : pgstat_progress_update_param(int index, int64 val)
50 : : {
51 : 13504942 : volatile PgBackendStatus *beentry = MyBEEntry;
52 : :
53 [ + - - + ]: 13504942 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
54 : :
55 [ + - - + ]: 13504942 : if (!beentry || !pgstat_track_activities)
1914 andres@anarazel.de 56 :UBC 0 : return;
57 : :
1914 andres@anarazel.de 58 :CBC 13504942 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
59 : 13504942 : beentry->st_progress_param[index] = val;
60 [ - + - + ]: 13504942 : 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
1193 tgl@sss.pgh.pa.us 70 : 2285 : pgstat_progress_incr_param(int index, int64 incr)
71 : : {
72 : 2285 : volatile PgBackendStatus *beentry = MyBEEntry;
73 : :
74 [ + - - + ]: 2285 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
75 : :
76 [ + - - + ]: 2285 : if (!beentry || !pgstat_track_activities)
1193 tgl@sss.pgh.pa.us 77 :UBC 0 : return;
78 : :
1193 tgl@sss.pgh.pa.us 79 :CBC 2285 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
80 : 2285 : beentry->st_progress_param[index] += incr;
81 [ - + - + ]: 2285 : 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
1085 msawada@postgresql.o 92 : 134 : 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 [ + + ]: 134 : if (IsParallelWorker())
100 : : {
101 : : static StringInfoData progress_message;
102 : :
713 nathan@postgresql.or 103 :GBC 5 : pq_beginmessage(&progress_message, PqMsg_Progress);
1085 msawada@postgresql.o 104 : 5 : pq_sendint32(&progress_message, index);
105 : 5 : pq_sendint64(&progress_message, incr);
106 : 5 : pq_endmessage(&progress_message);
107 : : }
108 : : else
1085 msawada@postgresql.o 109 :CBC 129 : pgstat_progress_incr_param(index, incr);
110 : 134 : }
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
1914 andres@anarazel.de 120 : 668456 : pgstat_progress_update_multi_param(int nparam, const int *index,
121 : : const int64 *val)
122 : : {
123 : 668456 : volatile PgBackendStatus *beentry = MyBEEntry;
124 : : int i;
125 : :
126 [ + - + - : 668456 : if (!beentry || !pgstat_track_activities || nparam == 0)
- + ]
1914 andres@anarazel.de 127 :UBC 0 : return;
128 : :
1914 andres@anarazel.de 129 :CBC 668456 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
130 : :
131 [ + + ]: 1735281 : for (i = 0; i < nparam; ++i)
132 : : {
133 [ + - - + ]: 1066825 : Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
134 : :
135 : 1066825 : beentry->st_progress_param[index[i]] = val[i];
136 : : }
137 : :
138 [ - + - + ]: 668456 : 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 : 94092 : pgstat_progress_end_command(void)
150 : : {
151 : 94092 : volatile PgBackendStatus *beentry = MyBEEntry;
152 : :
153 [ + - - + ]: 94092 : if (!beentry || !pgstat_track_activities)
1914 andres@anarazel.de 154 :UBC 0 : return;
155 : :
1914 andres@anarazel.de 156 [ + + ]:CBC 94092 : if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID)
157 : 39935 : return;
158 : :
159 : 54157 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
160 : 54157 : beentry->st_progress_command = PROGRESS_COMMAND_INVALID;
161 : 54157 : beentry->st_progress_command_target = InvalidOid;
162 [ - + - + ]: 54157 : PGSTAT_END_WRITE_ACTIVITY(beentry);
163 : : }
|