Debian Codegraph @

src/backend/backup/basebackup_progress.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * basebackup_progress.c
4 * Basebackup sink implementing progress tracking, including but not
5 * limited to command progress reporting.
6 *
7 * This should be used even if the PROGRESS option to the replication
8 * command BASE_BACKUP is not specified. Without that option, we won't
9 * have tallied up the size of the files that are going to need to be
10 * backed up, but we can still report to the command progress reporting
11 * facility how much data we've processed.
12 *
13 * Moreover, we also use this as a convenient place to update certain
14 * fields of the bbsink_state. That work is accurately described as
15 * keeping track of our progress, but it's not just for introspection.
16 * We need those fields to be updated properly in order for base backups
17 * to work.
18 *
19 * This particular basebackup sink requires extra callbacks that most base
20 * backup sinks don't. Rather than cramming those into the interface, we just
21 * have a few extra functions here that basebackup.c can call. (We could put
22 * the logic directly into that file as it's fairly simple, but it seems
23 * cleaner to have everything related to progress reporting in one place.)
24 *
25 * Portions Copyright (c) 2010-2025, PostgreSQL Global Development Group
26 *
27 * IDENTIFICATION
28 * src/backend/backup/basebackup_progress.c
29 *
30 *-------------------------------------------------------------------------
31 */
32 #include "postgres.h"
33
34 #include "backup/basebackup_sink.h"
35 #include "commands/progress.h"
36 #include "pgstat.h"
37
38 static void bbsink_progress_begin_backup(bbsink *sink);
39 static void bbsink_progress_archive_contents(bbsink *sink, size_t len);
40 static void bbsink_progress_end_archive(bbsink *sink);
41 static void bbsink_progress_cleanup(bbsink *sink);
42
43 static const bbsink_ops bbsink_progress_ops = {
44 .begin_backup = bbsink_progress_begin_backup,
45 .begin_archive = bbsink_forward_begin_archive,
46 .archive_contents = bbsink_progress_archive_contents,
47 .end_archive = bbsink_progress_end_archive,
48 .begin_manifest = bbsink_forward_begin_manifest,
49 .manifest_contents = bbsink_forward_manifest_contents,
50 .end_manifest = bbsink_forward_end_manifest,
51 .end_backup = bbsink_forward_end_backup,
52 .cleanup = bbsink_progress_cleanup
53 };
54
55 /*
56 * Create a new basebackup sink that performs progress tracking functions and
57 * forwards data to a successor sink.
58 */
59 bbsink *
60 bbsink_progress_new(bbsink *next, bool estimate_backup_size)
61 {
62 bbsink *sink;
63
64 Assert(next != NULL);
65
66 sink = palloc0(sizeof(bbsink));
67 *((const bbsink_ops **) &sink->bbs_ops) = &bbsink_progress_ops;
68 sink->bbs_next = next;
69
70 /*
71 * Report that a base backup is in progress, and set the total size of the
72 * backup to -1, which will get translated to NULL. If we're estimating
73 * the backup size, we'll insert the real estimate when we have it.
74 */
75 pgstat_progress_start_command(PROGRESS_COMMAND_BASEBACKUP, InvalidOid);
76 pgstat_progress_update_param(PROGRESS_BASEBACKUP_BACKUP_TOTAL, -1);
77
78 return sink;
79 }
80
81 /*
82 * Progress reporting at start of backup.
83 */
84 static void
85 bbsink_progress_begin_backup(bbsink *sink)
86 {
87 const int index[] = {
88 PROGRESS_BASEBACKUP_PHASE,
89 PROGRESS_BASEBACKUP_BACKUP_TOTAL,
90 PROGRESS_BASEBACKUP_TBLSPC_TOTAL
91 };
92 int64 val[3];
93
94 /*
95 * Report that we are now streaming database files as a base backup. Also
96 * advertise the number of tablespaces, and, if known, the estimated total
97 * backup size.
98 */
99 val[0] = PROGRESS_BASEBACKUP_PHASE_STREAM_BACKUP;
100 if (sink->bbs_state->bytes_total_is_valid)
101 val[1] = sink->bbs_state->bytes_total;
102 else
103 val[1] = -1;
104 val[2] = list_length(sink->bbs_state->tablespaces);
105 pgstat_progress_update_multi_param(3, index, val);
106
107 /* Delegate to next sink. */
108 bbsink_forward_begin_backup(sink);
109 }
110
111 /*
112 * End-of archive progress reporting.
113 */
114 static void
115 bbsink_progress_end_archive(bbsink *sink)
116 {
117 /*
118 * We expect one archive per tablespace, so reaching the end of an archive
119 * also means reaching the end of a tablespace. (Some day we might have a
120 * reason to decouple these concepts.)
121 *
122 * If WAL is included in the backup, we'll mark the last tablespace
123 * complete before the last archive is complete, so we need a guard here
124 * to ensure that the number of tablespaces streamed doesn't exceed the
125 * total.
126 */
127 if (sink->bbs_state->tablespace_num < list_length(sink->bbs_state->tablespaces))
128 pgstat_progress_update_param(PROGRESS_BASEBACKUP_TBLSPC_STREAMED,
129 sink->bbs_state->tablespace_num + 1);
130
131 /* Delegate to next sink. */
132 bbsink_forward_end_archive(sink);
133
134 /*
135 * This is a convenient place to update the bbsink_state's notion of which
136 * is the current tablespace. Note that the bbsink_state object is shared
137 * across all bbsink objects involved, but we're the outermost one and
138 * this is the very last thing we do.
139 */
140 sink->bbs_state->tablespace_num++;
141 }
142
143 /*
144 * Handle progress tracking for new archive contents.
145 *
146 * Increment the counter for the amount of data already streamed
147 * by the given number of bytes, and update the progress report for
148 * pg_stat_progress_basebackup.
149 */
150 static void
151 bbsink_progress_archive_contents(bbsink *sink, size_t len)
152 {
153 bbsink_state *state = sink->bbs_state;
154 const int index[] = {
155 PROGRESS_BASEBACKUP_BACKUP_STREAMED,
156 PROGRESS_BASEBACKUP_BACKUP_TOTAL
157 };
158 int64 val[2];
159 int nparam = 0;
160
161 /* First update bbsink_state with # of bytes done. */
162 state->bytes_done += len;
163
164 /* Now forward to next sink. */
165 bbsink_forward_archive_contents(sink, len);
166
167 /* Prepare to set # of bytes done for command progress reporting. */
168 val[nparam++] = state->bytes_done;
169
170 /*
171 * We may also want to update # of total bytes, to avoid overflowing past
172 * 100% or the full size. This may make the total size number change as we
173 * approach the end of the backup (the estimate will always be wrong if
174 * WAL is included), but that's better than having the done column be
175 * bigger than the total.
176 */
177 if (state->bytes_total_is_valid && state->bytes_done > state->bytes_total)
178 val[nparam++] = state->bytes_done;
179
180 pgstat_progress_update_multi_param(nparam, index, val);
181 }
182
183 /*
184 * Clean up progress reporting.
185 */
186 static void
187 bbsink_progress_cleanup(bbsink *sink)
188 {
189 pgstat_progress_end_command();
190 bbsink_forward_cleanup(sink);
191 }
192
193 /*
194 * Advertise that we are waiting for the start-of-backup checkpoint.
195 */
196 void
197 basebackup_progress_wait_checkpoint(void)
198 {
199 pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
200 PROGRESS_BASEBACKUP_PHASE_WAIT_CHECKPOINT);
201 }
202
203 /*
204 * Advertise that we are estimating the backup size.
205 */
206 void
207 basebackup_progress_estimate_backup_size(void)
208 {
209 pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
210 PROGRESS_BASEBACKUP_PHASE_ESTIMATE_BACKUP_SIZE);
211 }
212
213 /*
214 * Advertise that we are waiting for WAL archiving at end-of-backup.
215 */
216 void
217 basebackup_progress_wait_wal_archive(bbsink_state *state)
218 {
219 const int index[] = {
220 PROGRESS_BASEBACKUP_PHASE,
221 PROGRESS_BASEBACKUP_TBLSPC_STREAMED
222 };
223 int64 val[2];
224
225 /*
226 * We report having finished all tablespaces at this point, even if the
227 * archive for the main tablespace is still open, because what's going to
228 * be added is WAL files, not files that are really from the main
229 * tablespace.
230 */
231 val[0] = PROGRESS_BASEBACKUP_PHASE_WAIT_WAL_ARCHIVE;
232 val[1] = list_length(state->tablespaces);
233 pgstat_progress_update_multi_param(2, index, val);
234 }
235
236 /*
237 * Advertise that we are transferring WAL files into the final archive.
238 */
239 void
240 basebackup_progress_transfer_wal(void)
241 {
242 pgstat_progress_update_param(PROGRESS_BASEBACKUP_PHASE,
243 PROGRESS_BASEBACKUP_PHASE_TRANSFER_WAL);
244 }
245
246 /*
247 * Advertise that we are no longer performing a backup.
248 *
249 * No longer used in core, but kept to preserve ABI compatibility in this
250 * branch.
251 */
252 void
253 basebackup_progress_done(void)
254 {
255 pgstat_progress_end_command();
256 }