Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : * vacuuming.c
3 : : * Helper routines for vacuumdb
4 : : *
5 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
6 : : * Portions Copyright (c) 1994, Regents of the University of California
7 : : *
8 : : * src/bin/scripts/vacuuming.c
9 : : *
10 : : *-------------------------------------------------------------------------
11 : : */
12 : :
13 : : #include "postgres_fe.h"
14 : :
15 : : #include "catalog/pg_attribute_d.h"
16 : : #include "catalog/pg_class_d.h"
17 : : #include "common/connect.h"
18 : : #include "common/logging.h"
19 : : #include "fe_utils/cancel.h"
20 : : #include "fe_utils/option_utils.h"
21 : : #include "fe_utils/parallel_slot.h"
22 : : #include "fe_utils/query_utils.h"
23 : : #include "fe_utils/string_utils.h"
24 : : #include "vacuuming.h"
25 : :
26 : :
27 : : static int vacuum_one_database(ConnParams *cparams,
28 : : vacuumingOptions *vacopts,
29 : : int stage,
30 : : SimpleStringList *objects,
31 : : SimpleStringList **found_objs,
32 : : int concurrentCons,
33 : : const char *progname);
34 : : static int vacuum_all_databases(ConnParams *cparams,
35 : : vacuumingOptions *vacopts,
36 : : SimpleStringList *objects,
37 : : int concurrentCons,
38 : : const char *progname);
39 : : static SimpleStringList *retrieve_objects(PGconn *conn,
40 : : vacuumingOptions *vacopts,
41 : : SimpleStringList *objects);
42 : : static void free_retrieved_objects(SimpleStringList *list);
43 : : static void prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
44 : : vacuumingOptions *vacopts, const char *table);
45 : : static void run_vacuum_command(ParallelSlot *free_slot,
46 : : vacuumingOptions *vacopts, const char *sql,
47 : : const char *table);
48 : :
49 : : /*
50 : : * Executes vacuum/analyze as indicated. Returns 0 if the plan is carried
51 : : * to completion, or -1 in case of certain errors (which should hopefully
52 : : * been already reported.) Other errors are reported via pg_fatal().
53 : : */
54 : : int
277 alvherre@kurilemu.de 55 :GNC 72 : vacuuming_main(ConnParams *cparams, const char *dbname,
56 : : const char *maintenance_db, vacuumingOptions *vacopts,
57 : : SimpleStringList *objects,
58 : : unsigned int tbl_count, int concurrentCons,
59 : : const char *progname)
60 : : {
61 : 72 : setup_cancel_handler(NULL);
62 : :
63 : : /* Avoid opening extra connections. */
64 [ + + - + ]: 72 : if (tbl_count > 0 && (concurrentCons > tbl_count))
277 alvherre@kurilemu.de 65 :UNC 0 : concurrentCons = tbl_count;
66 : :
277 alvherre@kurilemu.de 67 [ + + ]:GNC 72 : if (vacopts->objfilter & OBJFILTER_ALL_DBS)
68 : : {
69 : 27 : cparams->dbname = maintenance_db;
70 : :
71 : 27 : return vacuum_all_databases(cparams, vacopts,
72 : : objects,
73 : : concurrentCons,
74 : : progname);
75 : : }
76 : : else
77 : : {
78 [ - + ]: 45 : if (dbname == NULL)
79 : : {
277 alvherre@kurilemu.de 80 [ # # ]:UNC 0 : if (getenv("PGDATABASE"))
81 : 0 : dbname = getenv("PGDATABASE");
82 [ # # ]: 0 : else if (getenv("PGUSER"))
83 : 0 : dbname = getenv("PGUSER");
84 : : else
85 : 0 : dbname = get_user_name_or_exit(progname);
86 : : }
87 : :
277 alvherre@kurilemu.de 88 :GNC 45 : cparams->dbname = dbname;
89 : :
90 [ + + ]: 45 : if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
91 : : {
92 : 6 : SimpleStringList *found_objs = NULL;
93 : :
94 [ + + ]: 24 : for (int stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
95 : : {
96 : : int ret;
97 : :
98 : 18 : ret = vacuum_one_database(cparams, vacopts,
99 : : stage,
100 : : objects,
101 [ + + ]: 18 : vacopts->missing_stats_only ? &found_objs : NULL,
102 : : concurrentCons,
103 : : progname);
104 [ - + ]: 18 : if (ret != 0)
105 : : {
251 tgl@sss.pgh.pa.us 106 :UNC 0 : free_retrieved_objects(found_objs);
277 alvherre@kurilemu.de 107 : 0 : return ret;
108 : : }
109 : : }
110 : :
251 tgl@sss.pgh.pa.us 111 :GNC 6 : free_retrieved_objects(found_objs);
277 alvherre@kurilemu.de 112 : 6 : return EXIT_SUCCESS;
113 : : }
114 : : else
115 : 39 : return vacuum_one_database(cparams, vacopts,
116 : : ANALYZE_NO_STAGE,
117 : : objects, NULL,
118 : : concurrentCons,
119 : : progname);
120 : : }
121 : : }
122 : :
123 : : /*
124 : : * vacuum_one_database
125 : : *
126 : : * Process tables in the given database.
127 : : *
128 : : * There are two ways to specify the list of objects to process:
129 : : *
130 : : * 1) The "found_objs" parameter is a double pointer to a fully qualified list
131 : : * of objects to process, as returned by a previous call to
132 : : * vacuum_one_database().
133 : : *
134 : : * a) If both "found_objs" (the double pointer) and "*found_objs" (the
135 : : * once-dereferenced double pointer) are not NULL, this list takes
136 : : * priority, and anything specified in "objects" is ignored.
137 : : *
138 : : * b) If "found_objs" (the double pointer) is not NULL but "*found_objs"
139 : : * (the once-dereferenced double pointer) _is_ NULL, the "objects"
140 : : * parameter takes priority, and the results of the catalog query
141 : : * described in (2) are stored in "found_objs".
142 : : *
143 : : * c) If "found_objs" (the double pointer) is NULL, the "objects"
144 : : * parameter again takes priority, and the results of the catalog query
145 : : * are not saved.
146 : : *
147 : : * 2) The "objects" parameter is a user-specified list of objects to process.
148 : : * When (1b) or (1c) applies, this function performs a catalog query to
149 : : * retrieve a fully qualified list of objects to process, as described
150 : : * below.
151 : : *
152 : : * a) If "objects" is not NULL, the catalog query gathers only the objects
153 : : * listed in "objects".
154 : : *
155 : : * b) If "objects" is NULL, all tables in the database are gathered.
156 : : *
157 : : * Note that this function is only concerned with running exactly one stage
158 : : * when in analyze-in-stages mode; caller must iterate on us if necessary.
159 : : *
160 : : * If concurrentCons is > 1, multiple connections are used to vacuum tables
161 : : * in parallel.
162 : : */
163 : : static int
164 : 119 : vacuum_one_database(ConnParams *cparams,
165 : : vacuumingOptions *vacopts,
166 : : int stage,
167 : : SimpleStringList *objects,
168 : : SimpleStringList **found_objs,
169 : : int concurrentCons,
170 : : const char *progname)
171 : : {
172 : : PQExpBufferData sql;
173 : : PGconn *conn;
174 : : SimpleStringListCell *cell;
175 : : ParallelSlotArray *sa;
176 : 119 : int ntups = 0;
177 : : const char *initcmd;
178 : 119 : SimpleStringList *retobjs = NULL;
251 tgl@sss.pgh.pa.us 179 : 119 : bool free_retobjs = false;
277 alvherre@kurilemu.de 180 : 119 : int ret = EXIT_SUCCESS;
181 : 119 : const char *stage_commands[] = {
182 : : "SET default_statistics_target=1; SET vacuum_cost_delay=0;",
183 : : "SET default_statistics_target=10; RESET vacuum_cost_delay;",
184 : : "RESET default_statistics_target;"
185 : : };
186 : 119 : const char *stage_messages[] = {
187 : : gettext_noop("Generating minimal optimizer statistics (1 target)"),
188 : : gettext_noop("Generating medium optimizer statistics (10 targets)"),
189 : : gettext_noop("Generating default (full) optimizer statistics")
190 : : };
191 : :
192 [ + + + - : 119 : Assert(stage == ANALYZE_NO_STAGE ||
+ - ]
193 : : (stage >= 0 && stage < ANALYZE_NUM_STAGES));
194 : :
203 nathan@postgresql.or 195 : 119 : conn = connectDatabase(cparams, progname, vacopts->echo, false, true);
196 : :
277 alvherre@kurilemu.de 197 [ + + - + ]: 118 : if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600)
198 : : {
277 alvherre@kurilemu.de 199 :UNC 0 : PQfinish(conn);
200 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
201 : : "disable-page-skipping", "9.6");
202 : : }
203 : :
277 alvherre@kurilemu.de 204 [ + + - + ]:GNC 118 : if (vacopts->no_index_cleanup && PQserverVersion(conn) < 120000)
205 : : {
277 alvherre@kurilemu.de 206 :UNC 0 : PQfinish(conn);
207 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
208 : : "no-index-cleanup", "12");
209 : : }
210 : :
277 alvherre@kurilemu.de 211 [ - + - - ]:GNC 118 : if (vacopts->force_index_cleanup && PQserverVersion(conn) < 120000)
212 : : {
277 alvherre@kurilemu.de 213 :UNC 0 : PQfinish(conn);
214 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
215 : : "force-index-cleanup", "12");
216 : : }
217 : :
277 alvherre@kurilemu.de 218 [ + + - + ]:GNC 118 : if (!vacopts->do_truncate && PQserverVersion(conn) < 120000)
219 : : {
277 alvherre@kurilemu.de 220 :UNC 0 : PQfinish(conn);
221 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
222 : : "no-truncate", "12");
223 : : }
224 : :
277 alvherre@kurilemu.de 225 [ + + - + ]:GNC 118 : if (!vacopts->process_main && PQserverVersion(conn) < 160000)
226 : : {
277 alvherre@kurilemu.de 227 :UNC 0 : PQfinish(conn);
228 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
229 : : "no-process-main", "16");
230 : : }
231 : :
277 alvherre@kurilemu.de 232 [ + + - + ]:GNC 118 : if (!vacopts->process_toast && PQserverVersion(conn) < 140000)
233 : : {
277 alvherre@kurilemu.de 234 :UNC 0 : PQfinish(conn);
235 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
236 : : "no-process-toast", "14");
237 : : }
238 : :
277 alvherre@kurilemu.de 239 [ + + - + ]:GNC 118 : if (vacopts->skip_locked && PQserverVersion(conn) < 120000)
240 : : {
277 alvherre@kurilemu.de 241 :UNC 0 : PQfinish(conn);
242 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
243 : : "skip-locked", "12");
244 : : }
245 : :
277 alvherre@kurilemu.de 246 [ + + - + ]:GNC 118 : if (vacopts->min_xid_age != 0 && PQserverVersion(conn) < 90600)
247 : : {
277 alvherre@kurilemu.de 248 :UNC 0 : PQfinish(conn);
249 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
250 : : "--min-xid-age", "9.6");
251 : : }
252 : :
277 alvherre@kurilemu.de 253 [ + + - + ]:GNC 118 : if (vacopts->min_mxid_age != 0 && PQserverVersion(conn) < 90600)
254 : : {
277 alvherre@kurilemu.de 255 :UNC 0 : PQfinish(conn);
256 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
257 : : "--min-mxid-age", "9.6");
258 : : }
259 : :
277 alvherre@kurilemu.de 260 [ + + - + ]:GNC 118 : if (vacopts->parallel_workers >= 0 && PQserverVersion(conn) < 130000)
261 : : {
277 alvherre@kurilemu.de 262 :UNC 0 : PQfinish(conn);
263 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
264 : : "--parallel", "13");
265 : : }
266 : :
277 alvherre@kurilemu.de 267 [ - + - - ]:GNC 118 : if (vacopts->buffer_usage_limit && PQserverVersion(conn) < 160000)
268 : : {
277 alvherre@kurilemu.de 269 :UNC 0 : PQfinish(conn);
270 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
271 : : "--buffer-usage-limit", "16");
272 : : }
273 : :
277 alvherre@kurilemu.de 274 [ + + - + ]:GNC 118 : if (vacopts->missing_stats_only && PQserverVersion(conn) < 150000)
275 : : {
277 alvherre@kurilemu.de 276 :UNC 0 : PQfinish(conn);
277 : 0 : pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
278 : : "--missing-stats-only", "15");
279 : : }
280 : :
281 : : /* skip_database_stats is used automatically if server supports it */
277 alvherre@kurilemu.de 282 :GNC 118 : vacopts->skip_database_stats = (PQserverVersion(conn) >= 160000);
283 : :
203 nathan@postgresql.or 284 [ + - ]: 118 : if (!vacopts->quiet)
285 : : {
277 alvherre@kurilemu.de 286 [ + + ]: 118 : if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
287 : 24 : printf(_("%s: processing database \"%s\": %s\n"),
288 : : progname, PQdb(conn), _(stage_messages[stage]));
289 : : else
290 : 94 : printf(_("%s: vacuuming database \"%s\"\n"),
291 : : progname, PQdb(conn));
292 : 118 : fflush(stdout);
293 : : }
294 : :
295 : : /*
296 : : * If the caller provided the results of a previous catalog query, just
297 : : * use that. Otherwise, run the catalog query ourselves and set the
298 : : * return variable if provided. (If it is, then freeing the string list
299 : : * becomes the caller's responsibility.)
300 : : */
301 [ + + + + ]: 118 : if (found_objs && *found_objs)
302 : 8 : retobjs = *found_objs;
303 : : else
304 : : {
203 nathan@postgresql.or 305 : 110 : retobjs = retrieve_objects(conn, vacopts, objects);
277 alvherre@kurilemu.de 306 [ + + ]: 109 : if (found_objs)
307 : 4 : *found_objs = retobjs;
308 : : else
251 tgl@sss.pgh.pa.us 309 : 105 : free_retobjs = true;
310 : : }
311 : :
312 : : /*
313 : : * Count the number of objects in the catalog query result. If there are
314 : : * none, we are done.
315 : : */
316 [ + + ]: 6193 : for (cell = retobjs->head; cell; cell = cell->next)
277 alvherre@kurilemu.de 317 : 6076 : ntups++;
318 : :
319 [ + + ]: 117 : if (ntups == 0)
320 : : {
321 : 11 : PQfinish(conn);
251 tgl@sss.pgh.pa.us 322 [ + + ]: 11 : if (free_retobjs)
323 : 5 : free_retrieved_objects(retobjs);
277 alvherre@kurilemu.de 324 : 11 : return EXIT_SUCCESS;
325 : : }
326 : :
327 : : /*
328 : : * Ensure concurrentCons is sane. If there are more connections than
329 : : * vacuumable relations, we don't need to use them all.
330 : : */
331 [ - + ]: 106 : if (concurrentCons > ntups)
277 alvherre@kurilemu.de 332 :UNC 0 : concurrentCons = ntups;
277 alvherre@kurilemu.de 333 [ - + ]:GNC 106 : if (concurrentCons <= 0)
277 alvherre@kurilemu.de 334 :UNC 0 : concurrentCons = 1;
335 : :
336 : : /*
337 : : * All slots need to be prepared to run the appropriate analyze stage, if
338 : : * caller requested that mode. We have to prepare the initial connection
339 : : * ourselves before setting up the slots.
340 : : */
277 alvherre@kurilemu.de 341 [ + + ]:GNC 106 : if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
342 : : {
343 : 18 : initcmd = stage_commands[stage];
344 : :
203 nathan@postgresql.or 345 [ - + ]: 18 : if (vacopts->dry_run)
203 nathan@postgresql.or 346 :UNC 0 : printf("%s\n", initcmd);
347 : : else
203 nathan@postgresql.or 348 :GNC 18 : executeCommand(conn, initcmd, vacopts->echo);
349 : : }
350 : : else
277 alvherre@kurilemu.de 351 : 88 : initcmd = NULL;
352 : :
353 : : /*
354 : : * Setup the database connections. We reuse the connection we already have
355 : : * for the first slot. If not in parallel mode, the first slot in the
356 : : * array contains the connection.
357 : : */
203 nathan@postgresql.or 358 : 106 : sa = ParallelSlotsSetup(concurrentCons, cparams, progname,
359 : 106 : vacopts->echo, initcmd);
277 alvherre@kurilemu.de 360 : 106 : ParallelSlotsAdoptConn(sa, conn);
361 : :
362 : 106 : initPQExpBuffer(&sql);
363 : :
364 : 106 : cell = retobjs->head;
365 : : do
366 : : {
367 : 6076 : const char *tabname = cell->val;
368 : : ParallelSlot *free_slot;
369 : :
370 [ - + ]: 6076 : if (CancelRequested)
371 : : {
277 alvherre@kurilemu.de 372 :UNC 0 : ret = EXIT_FAILURE;
373 : 0 : goto finish;
374 : : }
375 : :
277 alvherre@kurilemu.de 376 :GNC 6076 : free_slot = ParallelSlotsGetIdle(sa, NULL);
377 [ - + ]: 6076 : if (!free_slot)
378 : : {
277 alvherre@kurilemu.de 379 :UNC 0 : ret = EXIT_FAILURE;
380 : 0 : goto finish;
381 : : }
382 : :
277 alvherre@kurilemu.de 383 :GNC 6076 : prepare_vacuum_command(free_slot->connection, &sql,
384 : : vacopts, tabname);
385 : :
386 : : /*
387 : : * Execute the vacuum. All errors are handled in processQueryResult
388 : : * through ParallelSlotsGetIdle.
389 : : */
390 : 6076 : ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
203 nathan@postgresql.or 391 : 6076 : run_vacuum_command(free_slot, vacopts, sql.data, tabname);
392 : :
277 alvherre@kurilemu.de 393 : 6076 : cell = cell->next;
394 [ + + ]: 6076 : } while (cell != NULL);
395 : :
396 [ + + ]: 106 : if (!ParallelSlotsWaitCompletion(sa))
397 : : {
398 : 1 : ret = EXIT_FAILURE;
399 : 1 : goto finish;
400 : : }
401 : :
402 : : /* If we used SKIP_DATABASE_STATS, mop up with ONLY_DATABASE_STATS */
403 [ + + - + ]: 105 : if (vacopts->mode == MODE_VACUUM && vacopts->skip_database_stats)
404 : : {
405 : 69 : const char *cmd = "VACUUM (ONLY_DATABASE_STATS);";
406 : 69 : ParallelSlot *free_slot = ParallelSlotsGetIdle(sa, NULL);
407 : :
408 [ - + ]: 69 : if (!free_slot)
409 : : {
277 alvherre@kurilemu.de 410 :UNC 0 : ret = EXIT_FAILURE;
411 : 0 : goto finish;
412 : : }
413 : :
277 alvherre@kurilemu.de 414 :GNC 69 : ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL);
203 nathan@postgresql.or 415 : 69 : run_vacuum_command(free_slot, vacopts, cmd, NULL);
416 : :
277 alvherre@kurilemu.de 417 [ + - ]: 69 : if (!ParallelSlotsWaitCompletion(sa))
277 alvherre@kurilemu.de 418 :UNC 0 : ret = EXIT_FAILURE; /* error already reported by handler */
419 : : }
420 : :
277 alvherre@kurilemu.de 421 :GNC 105 : finish:
422 : 106 : ParallelSlotsTerminate(sa);
423 : 106 : pg_free(sa);
424 : 106 : termPQExpBuffer(&sql);
251 tgl@sss.pgh.pa.us 425 [ + + ]: 106 : if (free_retobjs)
426 : 100 : free_retrieved_objects(retobjs);
427 : :
277 alvherre@kurilemu.de 428 : 106 : return ret;
429 : : }
430 : :
431 : : /*
432 : : * Vacuum/analyze all connectable databases.
433 : : *
434 : : * In analyze-in-stages mode, we process all databases in one stage before
435 : : * moving on to the next stage. That ensure minimal stats are available
436 : : * quickly everywhere before generating more detailed ones.
437 : : */
438 : : static int
439 : 27 : vacuum_all_databases(ConnParams *cparams,
440 : : vacuumingOptions *vacopts,
441 : : SimpleStringList *objects,
442 : : int concurrentCons,
443 : : const char *progname)
444 : : {
251 tgl@sss.pgh.pa.us 445 : 27 : int ret = EXIT_SUCCESS;
446 : : PGconn *conn;
447 : : PGresult *result;
448 : : int numdbs;
449 : :
203 nathan@postgresql.or 450 : 27 : conn = connectMaintenanceDatabase(cparams, progname, vacopts->echo);
277 alvherre@kurilemu.de 451 : 27 : result = executeQuery(conn,
452 : : "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
203 nathan@postgresql.or 453 : 27 : vacopts->echo);
251 tgl@sss.pgh.pa.us 454 : 27 : numdbs = PQntuples(result);
277 alvherre@kurilemu.de 455 : 27 : PQfinish(conn);
456 : :
457 [ + + ]: 27 : if (vacopts->mode == MODE_ANALYZE_IN_STAGES)
458 : : {
459 : 1 : SimpleStringList **found_objs = NULL;
460 : :
461 [ - + ]: 1 : if (vacopts->missing_stats_only)
251 tgl@sss.pgh.pa.us 462 :UNC 0 : found_objs = palloc0(numdbs * sizeof(SimpleStringList *));
463 : :
464 : : /*
465 : : * When analyzing all databases in stages, we analyze them all in the
466 : : * fastest stage first, so that initial statistics become available
467 : : * for all of them as soon as possible.
468 : : *
469 : : * This means we establish several times as many connections, but
470 : : * that's a secondary consideration.
471 : : */
277 alvherre@kurilemu.de 472 [ + + ]:GNC 4 : for (int stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
473 : : {
251 tgl@sss.pgh.pa.us 474 [ + + ]: 9 : for (int i = 0; i < numdbs; i++)
475 : : {
277 alvherre@kurilemu.de 476 : 6 : cparams->override_dbname = PQgetvalue(result, i, 0);
477 : 6 : ret = vacuum_one_database(cparams, vacopts, stage,
478 : : objects,
479 [ - + ]: 6 : vacopts->missing_stats_only ? &found_objs[i] : NULL,
480 : : concurrentCons,
481 : : progname);
482 [ - + ]: 6 : if (ret != EXIT_SUCCESS)
251 tgl@sss.pgh.pa.us 483 :UNC 0 : break;
484 : : }
251 tgl@sss.pgh.pa.us 485 [ - + ]:GNC 3 : if (ret != EXIT_SUCCESS)
251 tgl@sss.pgh.pa.us 486 :UNC 0 : break;
487 : : }
488 : :
251 tgl@sss.pgh.pa.us 489 [ - + ]:GNC 1 : if (vacopts->missing_stats_only)
490 : : {
251 tgl@sss.pgh.pa.us 491 [ # # ]:UNC 0 : for (int i = 0; i < numdbs; i++)
492 : 0 : free_retrieved_objects(found_objs[i]);
493 : 0 : pg_free(found_objs);
494 : : }
495 : : }
496 : : else
497 : : {
251 tgl@sss.pgh.pa.us 498 [ + + ]:GNC 82 : for (int i = 0; i < numdbs; i++)
499 : : {
277 alvherre@kurilemu.de 500 : 56 : cparams->override_dbname = PQgetvalue(result, i, 0);
501 : 56 : ret = vacuum_one_database(cparams, vacopts,
502 : : ANALYZE_NO_STAGE,
503 : : objects,
504 : : NULL,
505 : : concurrentCons,
506 : : progname);
507 [ - + ]: 56 : if (ret != EXIT_SUCCESS)
251 tgl@sss.pgh.pa.us 508 :UNC 0 : break;
509 : : }
510 : : }
511 : :
277 alvherre@kurilemu.de 512 :GNC 27 : PQclear(result);
513 : :
251 tgl@sss.pgh.pa.us 514 : 27 : return ret;
515 : : }
516 : :
517 : : /*
518 : : * Prepare the list of tables to process by querying the catalogs.
519 : : *
520 : : * Since we execute the constructed query with the default search_path (which
521 : : * could be unsafe), everything in this query MUST be fully qualified.
522 : : *
523 : : * First, build a WITH clause for the catalog query if any tables were
524 : : * specified, with a set of values made of relation names and their optional
525 : : * set of columns. This is used to match any provided column lists with the
526 : : * generated qualified identifiers and to filter for the tables provided via
527 : : * --table. If a listed table does not exist, the catalog query will fail.
528 : : */
529 : : static SimpleStringList *
277 alvherre@kurilemu.de 530 : 110 : retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
531 : : SimpleStringList *objects)
532 : : {
533 : : PQExpBufferData buf;
534 : : PQExpBufferData catalog_query;
535 : : PGresult *res;
536 : : SimpleStringListCell *cell;
203 michael@paquier.xyz 537 : 110 : SimpleStringList *found_objs = palloc0_object(SimpleStringList);
277 alvherre@kurilemu.de 538 : 110 : bool objects_listed = false;
539 : :
540 : 110 : initPQExpBuffer(&catalog_query);
541 [ + - + + ]: 143 : for (cell = objects ? objects->head : NULL; cell; cell = cell->next)
542 : : {
543 : 33 : char *just_table = NULL;
544 : 33 : const char *just_columns = NULL;
545 : :
546 [ + + ]: 33 : if (!objects_listed)
547 : : {
548 : 31 : appendPQExpBufferStr(&catalog_query,
549 : : "WITH listed_objects (object_oid, column_list) AS (\n"
550 : : " VALUES (");
551 : 31 : objects_listed = true;
552 : : }
553 : : else
554 : 2 : appendPQExpBufferStr(&catalog_query, ",\n (");
555 : :
556 [ + + ]: 33 : if (vacopts->objfilter & (OBJFILTER_SCHEMA | OBJFILTER_SCHEMA_EXCLUDE))
557 : : {
558 : 11 : appendStringLiteralConn(&catalog_query, cell->val, conn);
559 : 11 : appendPQExpBufferStr(&catalog_query, "::pg_catalog.regnamespace, ");
560 : : }
561 : :
562 [ + + ]: 33 : if (vacopts->objfilter & OBJFILTER_TABLE)
563 : : {
564 : : /*
565 : : * Split relation and column names given by the user, this is used
566 : : * to feed the CTE with values on which are performed pre-run
567 : : * validity checks as well. For now these happen only on the
568 : : * relation name.
569 : : */
570 : 22 : splitTableColumnsSpec(cell->val, PQclientEncoding(conn),
571 : : &just_table, &just_columns);
572 : :
573 : 22 : appendStringLiteralConn(&catalog_query, just_table, conn);
574 : 22 : appendPQExpBufferStr(&catalog_query, "::pg_catalog.regclass, ");
575 : : }
576 : :
577 [ + + + + ]: 33 : if (just_columns && just_columns[0] != '\0')
578 : 5 : appendStringLiteralConn(&catalog_query, just_columns, conn);
579 : : else
580 : 28 : appendPQExpBufferStr(&catalog_query, "NULL");
581 : :
582 : 33 : appendPQExpBufferStr(&catalog_query, "::pg_catalog.text)");
583 : :
584 : 33 : pg_free(just_table);
585 : : }
586 : :
587 : : /* Finish formatting the CTE */
588 [ + + ]: 110 : if (objects_listed)
589 : 31 : appendPQExpBufferStr(&catalog_query, "\n)\n");
590 : :
591 : 110 : appendPQExpBufferStr(&catalog_query, "SELECT c.relname, ns.nspname");
592 : :
593 [ + + ]: 110 : if (objects_listed)
594 : 31 : appendPQExpBufferStr(&catalog_query, ", listed_objects.column_list");
595 : :
596 : 110 : appendPQExpBufferStr(&catalog_query,
597 : : " FROM pg_catalog.pg_class c\n"
598 : : " JOIN pg_catalog.pg_namespace ns"
599 : : " ON c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n"
600 : : " CROSS JOIN LATERAL (SELECT c.relkind IN ("
601 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
602 : : CppAsString2(RELKIND_PARTITIONED_INDEX) ")) as p (inherited)\n"
603 : : " LEFT JOIN pg_catalog.pg_class t"
604 : : " ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid\n");
605 : :
606 : : /*
607 : : * Used to match the tables or schemas listed by the user, completing the
608 : : * JOIN clause.
609 : : */
610 [ + + ]: 110 : if (objects_listed)
611 : : {
612 : 31 : appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects"
613 : : " ON listed_objects.object_oid"
614 : : " OPERATOR(pg_catalog.=) ");
615 : :
616 [ + + ]: 31 : if (vacopts->objfilter & OBJFILTER_TABLE)
617 : 22 : appendPQExpBufferStr(&catalog_query, "c.oid\n");
618 : : else
619 : 9 : appendPQExpBufferStr(&catalog_query, "ns.oid\n");
620 : : }
621 : :
622 : : /*
623 : : * Exclude temporary tables, beginning the WHERE clause.
624 : : */
625 : 110 : appendPQExpBufferStr(&catalog_query,
626 : : " WHERE c.relpersistence OPERATOR(pg_catalog.!=) "
627 : : CppAsString2(RELPERSISTENCE_TEMP) "\n");
628 : :
629 : : /*
630 : : * Used to match the tables or schemas listed by the user, for the WHERE
631 : : * clause.
632 : : */
633 [ + + ]: 110 : if (objects_listed)
634 : : {
635 [ + + ]: 31 : if (vacopts->objfilter & OBJFILTER_SCHEMA_EXCLUDE)
636 : 4 : appendPQExpBufferStr(&catalog_query,
637 : : " AND listed_objects.object_oid IS NULL\n");
638 : : else
639 : 27 : appendPQExpBufferStr(&catalog_query,
640 : : " AND listed_objects.object_oid IS NOT NULL\n");
641 : : }
642 : :
643 : : /*
644 : : * If no tables were listed, filter for the relevant relation types. If
645 : : * tables were given via --table, don't bother filtering by relation type.
646 : : * Instead, let the server decide whether a given relation can be
647 : : * processed in which case the user will know about it.
648 : : */
649 [ + + ]: 110 : if ((vacopts->objfilter & OBJFILTER_TABLE) == 0)
650 : : {
651 : : /*
652 : : * vacuumdb should generally follow the behavior of the underlying
653 : : * VACUUM and ANALYZE commands. In MODE_ANALYZE or
654 : : * MODE_ANALYZE_IN_STAGES modes, process regular tables, materialized
655 : : * views, and partitioned tables, just like ANALYZE (with no specific
656 : : * target tables) does. Otherwise, process only regular tables and
657 : : * materialized views, since VACUUM skips partitioned tables when no
658 : : * target tables are specified.
659 : : */
27 fujii@postgresql.org 660 [ + + ]: 88 : if (vacopts->mode == MODE_ANALYZE ||
661 [ + + ]: 78 : vacopts->mode == MODE_ANALYZE_IN_STAGES)
277 alvherre@kurilemu.de 662 : 22 : appendPQExpBufferStr(&catalog_query,
663 : : " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
664 : : CppAsString2(RELKIND_RELATION) ", "
665 : : CppAsString2(RELKIND_MATVIEW) ", "
666 : : CppAsString2(RELKIND_PARTITIONED_TABLE) "])\n");
667 : : else
668 : 66 : appendPQExpBufferStr(&catalog_query,
669 : : " AND c.relkind OPERATOR(pg_catalog.=) ANY (array["
670 : : CppAsString2(RELKIND_RELATION) ", "
671 : : CppAsString2(RELKIND_MATVIEW) "])\n");
672 : : }
673 : :
674 : : /*
675 : : * For --min-xid-age and --min-mxid-age, the age of the relation is the
676 : : * greatest of the ages of the main relation and its associated TOAST
677 : : * table. The commands generated by vacuumdb will also process the TOAST
678 : : * table for the relation if necessary, so it does not need to be
679 : : * considered separately.
680 : : */
681 [ + + ]: 110 : if (vacopts->min_xid_age != 0)
682 : : {
683 : 1 : appendPQExpBuffer(&catalog_query,
684 : : " AND GREATEST(pg_catalog.age(c.relfrozenxid),"
685 : : " pg_catalog.age(t.relfrozenxid)) "
686 : : " OPERATOR(pg_catalog.>=) '%d'::pg_catalog.int4\n"
687 : : " AND c.relfrozenxid OPERATOR(pg_catalog.!=)"
688 : : " '0'::pg_catalog.xid\n",
689 : : vacopts->min_xid_age);
690 : : }
691 : :
692 [ + + ]: 110 : if (vacopts->min_mxid_age != 0)
693 : : {
694 : 1 : appendPQExpBuffer(&catalog_query,
695 : : " AND GREATEST(pg_catalog.mxid_age(c.relminmxid),"
696 : : " pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=)"
697 : : " '%d'::pg_catalog.int4\n"
698 : : " AND c.relminmxid OPERATOR(pg_catalog.!=)"
699 : : " '0'::pg_catalog.xid\n",
700 : : vacopts->min_mxid_age);
701 : : }
702 : :
703 [ + + ]: 110 : if (vacopts->missing_stats_only)
704 : : {
705 : 11 : appendPQExpBufferStr(&catalog_query, " AND (\n");
706 : :
707 : : /* regular stats */
708 : 11 : appendPQExpBufferStr(&catalog_query,
709 : : " EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n"
710 : : " WHERE a.attrelid OPERATOR(pg_catalog.=) c.oid\n"
711 : : " AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n"
712 : : " AND NOT a.attisdropped\n"
713 : : " AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n"
714 : : " AND a.attgenerated OPERATOR(pg_catalog.<>) "
715 : : CppAsString2(ATTRIBUTE_GENERATED_VIRTUAL) "\n"
716 : : " AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n"
717 : : " WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n"
718 : : " AND s.staattnum OPERATOR(pg_catalog.=) a.attnum\n"
719 : : " AND s.stainherit OPERATOR(pg_catalog.=) p.inherited))\n");
720 : :
721 : : /* extended stats */
722 : 11 : appendPQExpBufferStr(&catalog_query,
723 : : " OR EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext e\n"
724 : : " WHERE e.stxrelid OPERATOR(pg_catalog.=) c.oid\n"
725 : : " AND e.stxstattarget IS DISTINCT FROM 0::pg_catalog.int2\n"
726 : : " AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext_data d\n"
727 : : " WHERE d.stxoid OPERATOR(pg_catalog.=) e.oid\n"
728 : : " AND d.stxdinherit OPERATOR(pg_catalog.=) p.inherited))\n");
729 : :
730 : : /* expression indexes */
731 : 11 : appendPQExpBufferStr(&catalog_query,
732 : : " OR EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n"
733 : : " JOIN pg_catalog.pg_index i"
734 : : " ON i.indexrelid OPERATOR(pg_catalog.=) a.attrelid\n"
735 : : " WHERE i.indrelid OPERATOR(pg_catalog.=) c.oid\n"
736 : : " AND i.indkey[a.attnum OPERATOR(pg_catalog.-) 1::pg_catalog.int2]"
737 : : " OPERATOR(pg_catalog.=) 0::pg_catalog.int2\n"
738 : : " AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n"
739 : : " AND NOT a.attisdropped\n"
740 : : " AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n"
741 : : " AND NOT p.inherited\n"
742 : : " AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n"
743 : : " WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n"
744 : : " AND s.staattnum OPERATOR(pg_catalog.=) a.attnum))\n");
745 : :
746 : : /* inheritance and regular stats */
747 : 11 : appendPQExpBufferStr(&catalog_query,
748 : : " OR EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n"
749 : : " WHERE a.attrelid OPERATOR(pg_catalog.=) c.oid\n"
750 : : " AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n"
751 : : " AND NOT a.attisdropped\n"
752 : : " AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n"
753 : : " AND a.attgenerated OPERATOR(pg_catalog.<>) "
754 : : CppAsString2(ATTRIBUTE_GENERATED_VIRTUAL) "\n"
755 : : " AND c.relhassubclass\n"
756 : : " AND NOT p.inherited\n"
757 : : " AND EXISTS (SELECT NULL FROM pg_catalog.pg_inherits h\n"
758 : : " WHERE h.inhparent OPERATOR(pg_catalog.=) c.oid)\n"
759 : : " AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n"
760 : : " WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n"
761 : : " AND s.staattnum OPERATOR(pg_catalog.=) a.attnum\n"
762 : : " AND s.stainherit))\n");
763 : :
764 : : /* inheritance and extended stats */
765 : 11 : appendPQExpBufferStr(&catalog_query,
766 : : " OR EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext e\n"
767 : : " WHERE e.stxrelid OPERATOR(pg_catalog.=) c.oid\n"
768 : : " AND e.stxstattarget IS DISTINCT FROM 0::pg_catalog.int2\n"
769 : : " AND c.relhassubclass\n"
770 : : " AND NOT p.inherited\n"
771 : : " AND EXISTS (SELECT NULL FROM pg_catalog.pg_inherits h\n"
772 : : " WHERE h.inhparent OPERATOR(pg_catalog.=) c.oid)\n"
773 : : " AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext_data d\n"
774 : : " WHERE d.stxoid OPERATOR(pg_catalog.=) e.oid\n"
775 : : " AND d.stxdinherit))\n");
776 : :
777 : 11 : appendPQExpBufferStr(&catalog_query, " )\n");
778 : : }
779 : :
780 : : /*
781 : : * Execute the catalog query. We use the default search_path for this
782 : : * query for consistency with table lookups done elsewhere by the user.
783 : : */
784 : 110 : appendPQExpBufferStr(&catalog_query, " ORDER BY c.relpages DESC;");
203 nathan@postgresql.or 785 : 110 : executeCommand(conn, "RESET search_path;", vacopts->echo);
786 : 110 : res = executeQuery(conn, catalog_query.data, vacopts->echo);
277 alvherre@kurilemu.de 787 : 109 : termPQExpBuffer(&catalog_query);
203 nathan@postgresql.or 788 : 109 : PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, vacopts->echo));
789 : :
790 : : /*
791 : : * Build qualified identifiers for each table, including the column list
792 : : * if given.
793 : : */
277 alvherre@kurilemu.de 794 : 109 : initPQExpBuffer(&buf);
795 [ + + ]: 6181 : for (int i = 0; i < PQntuples(res); i++)
796 : : {
797 : 12144 : appendPQExpBufferStr(&buf,
798 : 6072 : fmtQualifiedIdEnc(PQgetvalue(res, i, 1),
799 : 6072 : PQgetvalue(res, i, 0),
800 : : PQclientEncoding(conn)));
801 : :
802 [ + + + + ]: 6072 : if (objects_listed && !PQgetisnull(res, i, 2))
803 : 5 : appendPQExpBufferStr(&buf, PQgetvalue(res, i, 2));
804 : :
805 : 6072 : simple_string_list_append(found_objs, buf.data);
806 : 6072 : resetPQExpBuffer(&buf);
807 : : }
808 : 109 : termPQExpBuffer(&buf);
809 : 109 : PQclear(res);
810 : :
811 : 109 : return found_objs;
812 : : }
813 : :
814 : : /*
815 : : * Free the results of retrieve_objects().
816 : : *
817 : : * For caller convenience, we allow the argument to be NULL,
818 : : * although retrieve_objects() will never return that.
819 : : */
820 : : static void
251 tgl@sss.pgh.pa.us 821 : 111 : free_retrieved_objects(SimpleStringList *list)
822 : : {
823 [ + + ]: 111 : if (list)
824 : : {
825 : 109 : simple_string_list_destroy(list);
826 : 109 : pg_free(list);
827 : : }
828 : 111 : }
829 : :
830 : : /*
831 : : * Construct a vacuum/analyze command to run based on the given
832 : : * options, in the given string buffer, which may contain previous garbage.
833 : : *
834 : : * The table name used must be already properly quoted. The command generated
835 : : * depends on the server version involved and it is semicolon-terminated.
836 : : */
837 : : static void
277 alvherre@kurilemu.de 838 : 6076 : prepare_vacuum_command(PGconn *conn, PQExpBuffer sql,
839 : : vacuumingOptions *vacopts, const char *table)
840 : : {
841 : 6076 : int serverVersion = PQserverVersion(conn);
842 : 6076 : const char *paren = " (";
843 : 6076 : const char *comma = ", ";
844 : 6076 : const char *sep = paren;
845 : :
846 : 6076 : resetPQExpBuffer(sql);
847 : :
848 [ + + ]: 6076 : if (vacopts->mode == MODE_ANALYZE ||
849 [ + + ]: 5316 : vacopts->mode == MODE_ANALYZE_IN_STAGES)
850 : : {
851 : 1675 : appendPQExpBufferStr(sql, "ANALYZE");
852 : :
853 : : /* parenthesized grammar of ANALYZE is supported since v11 */
854 [ + - ]: 1675 : if (serverVersion >= 110000)
855 : : {
856 [ + + ]: 1675 : if (vacopts->skip_locked)
857 : : {
858 : : /* SKIP_LOCKED is supported since v12 */
859 [ - + ]: 73 : Assert(serverVersion >= 120000);
860 : 73 : appendPQExpBuffer(sql, "%sSKIP_LOCKED", sep);
861 : 73 : sep = comma;
862 : : }
863 [ - + ]: 1675 : if (vacopts->verbose)
864 : : {
277 alvherre@kurilemu.de 865 :UNC 0 : appendPQExpBuffer(sql, "%sVERBOSE", sep);
866 : 0 : sep = comma;
867 : : }
277 alvherre@kurilemu.de 868 [ - + ]:GNC 1675 : if (vacopts->buffer_usage_limit)
869 : : {
277 alvherre@kurilemu.de 870 [ # # ]:UNC 0 : Assert(serverVersion >= 160000);
871 : 0 : appendPQExpBuffer(sql, "%sBUFFER_USAGE_LIMIT '%s'", sep,
872 : : vacopts->buffer_usage_limit);
873 : 0 : sep = comma;
874 : : }
277 alvherre@kurilemu.de 875 [ + + ]:GNC 1675 : if (sep != paren)
876 : 73 : appendPQExpBufferChar(sql, ')');
877 : : }
878 : : else
879 : : {
277 alvherre@kurilemu.de 880 [ # # ]:UNC 0 : if (vacopts->verbose)
881 : 0 : appendPQExpBufferStr(sql, " VERBOSE");
882 : : }
883 : : }
884 : : else
885 : : {
277 alvherre@kurilemu.de 886 :GNC 4401 : appendPQExpBufferStr(sql, "VACUUM");
887 : :
888 : : /* parenthesized grammar of VACUUM is supported since v9.0 */
889 [ + - ]: 4401 : if (serverVersion >= 90000)
890 : : {
891 [ + + ]: 4401 : if (vacopts->disable_page_skipping)
892 : : {
893 : : /* DISABLE_PAGE_SKIPPING is supported since v9.6 */
894 [ - + ]: 73 : Assert(serverVersion >= 90600);
895 : 73 : appendPQExpBuffer(sql, "%sDISABLE_PAGE_SKIPPING", sep);
896 : 73 : sep = comma;
897 : : }
898 [ + + ]: 4401 : if (vacopts->no_index_cleanup)
899 : : {
900 : : /* "INDEX_CLEANUP FALSE" has been supported since v12 */
901 [ - + ]: 73 : Assert(serverVersion >= 120000);
902 [ - + ]: 73 : Assert(!vacopts->force_index_cleanup);
903 : 73 : appendPQExpBuffer(sql, "%sINDEX_CLEANUP FALSE", sep);
904 : 73 : sep = comma;
905 : : }
906 [ - + ]: 4401 : if (vacopts->force_index_cleanup)
907 : : {
908 : : /* "INDEX_CLEANUP TRUE" has been supported since v12 */
277 alvherre@kurilemu.de 909 [ # # ]:UNC 0 : Assert(serverVersion >= 120000);
910 [ # # ]: 0 : Assert(!vacopts->no_index_cleanup);
911 : 0 : appendPQExpBuffer(sql, "%sINDEX_CLEANUP TRUE", sep);
912 : 0 : sep = comma;
913 : : }
277 alvherre@kurilemu.de 914 [ + + ]:GNC 4401 : if (!vacopts->do_truncate)
915 : : {
916 : : /* TRUNCATE is supported since v12 */
917 [ - + ]: 73 : Assert(serverVersion >= 120000);
918 : 73 : appendPQExpBuffer(sql, "%sTRUNCATE FALSE", sep);
919 : 73 : sep = comma;
920 : : }
921 [ + + ]: 4401 : if (!vacopts->process_main)
922 : : {
923 : : /* PROCESS_MAIN is supported since v16 */
924 [ - + ]: 73 : Assert(serverVersion >= 160000);
925 : 73 : appendPQExpBuffer(sql, "%sPROCESS_MAIN FALSE", sep);
926 : 73 : sep = comma;
927 : : }
928 [ + + ]: 4401 : if (!vacopts->process_toast)
929 : : {
930 : : /* PROCESS_TOAST is supported since v14 */
931 [ - + ]: 73 : Assert(serverVersion >= 140000);
932 : 73 : appendPQExpBuffer(sql, "%sPROCESS_TOAST FALSE", sep);
933 : 73 : sep = comma;
934 : : }
935 [ + - ]: 4401 : if (vacopts->skip_database_stats)
936 : : {
937 : : /* SKIP_DATABASE_STATS is supported since v16 */
938 [ - + ]: 4401 : Assert(serverVersion >= 160000);
939 : 4401 : appendPQExpBuffer(sql, "%sSKIP_DATABASE_STATS", sep);
940 : 4401 : sep = comma;
941 : : }
942 [ + + ]: 4401 : if (vacopts->skip_locked)
943 : : {
944 : : /* SKIP_LOCKED is supported since v12 */
945 [ - + ]: 73 : Assert(serverVersion >= 120000);
946 : 73 : appendPQExpBuffer(sql, "%sSKIP_LOCKED", sep);
947 : 73 : sep = comma;
948 : : }
949 [ + + ]: 4401 : if (vacopts->full)
950 : : {
951 : 73 : appendPQExpBuffer(sql, "%sFULL", sep);
952 : 73 : sep = comma;
953 : : }
954 [ + + ]: 4401 : if (vacopts->freeze)
955 : : {
956 : 1533 : appendPQExpBuffer(sql, "%sFREEZE", sep);
957 : 1533 : sep = comma;
958 : : }
959 [ - + ]: 4401 : if (vacopts->verbose)
960 : : {
277 alvherre@kurilemu.de 961 :UNC 0 : appendPQExpBuffer(sql, "%sVERBOSE", sep);
962 : 0 : sep = comma;
963 : : }
277 alvherre@kurilemu.de 964 [ + + ]:GNC 4401 : if (vacopts->and_analyze)
965 : : {
966 : 1536 : appendPQExpBuffer(sql, "%sANALYZE", sep);
967 : 1536 : sep = comma;
968 : : }
969 [ + + ]: 4401 : if (vacopts->parallel_workers >= 0)
970 : : {
971 : : /* PARALLEL is supported since v13 */
972 [ - + ]: 146 : Assert(serverVersion >= 130000);
973 : 146 : appendPQExpBuffer(sql, "%sPARALLEL %d", sep,
974 : : vacopts->parallel_workers);
975 : 146 : sep = comma;
976 : : }
977 [ - + ]: 4401 : if (vacopts->buffer_usage_limit)
978 : : {
277 alvherre@kurilemu.de 979 [ # # ]:UNC 0 : Assert(serverVersion >= 160000);
980 : 0 : appendPQExpBuffer(sql, "%sBUFFER_USAGE_LIMIT '%s'", sep,
981 : : vacopts->buffer_usage_limit);
982 : 0 : sep = comma;
983 : : }
277 alvherre@kurilemu.de 984 [ + - ]:GNC 4401 : if (sep != paren)
985 : 4401 : appendPQExpBufferChar(sql, ')');
986 : : }
987 : : else
988 : : {
277 alvherre@kurilemu.de 989 [ # # ]:UNC 0 : if (vacopts->full)
990 : 0 : appendPQExpBufferStr(sql, " FULL");
991 [ # # ]: 0 : if (vacopts->freeze)
992 : 0 : appendPQExpBufferStr(sql, " FREEZE");
993 [ # # ]: 0 : if (vacopts->verbose)
994 : 0 : appendPQExpBufferStr(sql, " VERBOSE");
995 [ # # ]: 0 : if (vacopts->and_analyze)
996 : 0 : appendPQExpBufferStr(sql, " ANALYZE");
997 : : }
998 : : }
999 : :
277 alvherre@kurilemu.de 1000 :GNC 6076 : appendPQExpBuffer(sql, " %s;", table);
1001 : 6076 : }
1002 : :
1003 : : /*
1004 : : * Send a vacuum/analyze command to the server, returning after sending the
1005 : : * command. If dry_run is true, the command is printed but not sent to the
1006 : : * server.
1007 : : *
1008 : : * Any errors during command execution are reported to stderr.
1009 : : */
1010 : : static void
203 nathan@postgresql.or 1011 : 6145 : run_vacuum_command(ParallelSlot *free_slot, vacuumingOptions *vacopts,
1012 : : const char *sql, const char *table)
1013 : : {
1014 : 6145 : bool status = true;
1015 : 6145 : PGconn *conn = free_slot->connection;
1016 : :
1017 [ + + + + ]: 6145 : if (vacopts->echo || vacopts->dry_run)
277 alvherre@kurilemu.de 1018 : 441 : printf("%s\n", sql);
1019 : :
203 nathan@postgresql.or 1020 [ + + ]: 6145 : if (vacopts->dry_run)
1021 : 3 : ParallelSlotSetIdle(free_slot);
1022 : : else
1023 : 6142 : status = PQsendQuery(conn, sql) == 1;
1024 : :
277 alvherre@kurilemu.de 1025 [ - + ]: 6145 : if (!status)
1026 : : {
277 alvherre@kurilemu.de 1027 [ # # ]:UNC 0 : if (table)
1028 : : {
1029 : 0 : pg_log_error("vacuuming of table \"%s\" in database \"%s\" failed: %s",
1030 : : table, PQdb(conn), PQerrorMessage(conn));
1031 : : }
1032 : : else
1033 : : {
1034 : 0 : pg_log_error("vacuuming of database \"%s\" failed: %s",
1035 : : PQdb(conn), PQerrorMessage(conn));
1036 : : }
1037 : : }
277 alvherre@kurilemu.de 1038 :GNC 6145 : }
1039 : :
1040 : : /*
1041 : : * Returns a newly malloc'd version of 'src' with escaped single quotes and
1042 : : * backslashes.
1043 : : */
1044 : : char *
277 alvherre@kurilemu.de 1045 :UNC 0 : escape_quotes(const char *src)
1046 : : {
1047 : 0 : char *result = escape_single_quotes_ascii(src);
1048 : :
1049 [ # # ]: 0 : if (!result)
1050 : 0 : pg_fatal("out of memory");
1051 : 0 : return result;
1052 : : }
|