| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* vacuumdb |
| 4 |
* |
| 5 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 6 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 7 |
* |
| 8 |
* src/bin/scripts/vacuumdb.c |
| 9 |
* |
| 10 |
*------------------------------------------------------------------------- |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "postgres_fe.h" |
| 14 |
|
| 15 |
#include <limits.h> |
| 16 |
|
| 17 |
#include "catalog/pg_attribute_d.h" |
| 18 |
#include "catalog/pg_class_d.h" |
| 19 |
#include "common.h" |
| 20 |
#include "common/connect.h" |
| 21 |
#include "common/logging.h" |
| 22 |
#include "fe_utils/cancel.h" |
| 23 |
#include "fe_utils/option_utils.h" |
| 24 |
#include "fe_utils/parallel_slot.h" |
| 25 |
#include "fe_utils/query_utils.h" |
| 26 |
#include "fe_utils/simple_list.h" |
| 27 |
#include "fe_utils/string_utils.h" |
| 28 |
|
| 29 |
|
| 30 |
/* vacuum options controlled by user flags */ |
| 31 |
typedef struct vacuumingOptions |
| 32 |
{ |
| 33 |
bool analyze_only; |
| 34 |
bool verbose; |
| 35 |
bool and_analyze; |
| 36 |
bool full; |
| 37 |
bool freeze; |
| 38 |
bool disable_page_skipping; |
| 39 |
bool skip_locked; |
| 40 |
int min_xid_age; |
| 41 |
int min_mxid_age; |
| 42 |
int parallel_workers; /* >= 0 indicates user specified the |
| 43 |
* parallel degree, otherwise -1 */ |
| 44 |
bool no_index_cleanup; |
| 45 |
bool force_index_cleanup; |
| 46 |
bool do_truncate; |
| 47 |
bool process_main; |
| 48 |
bool process_toast; |
| 49 |
bool skip_database_stats; |
| 50 |
char *buffer_usage_limit; |
| 51 |
bool missing_stats_only; |
| 52 |
} vacuumingOptions; |
| 53 |
|
| 54 |
/* object filter options */ |
| 55 |
typedef enum |
| 56 |
{ |
| 57 |
OBJFILTER_NONE = 0, /* no filter used */ |
| 58 |
OBJFILTER_ALL_DBS = (1 << 0), /* -a | --all */ |
| 59 |
OBJFILTER_DATABASE = (1 << 1), /* -d | --dbname */ |
| 60 |
OBJFILTER_TABLE = (1 << 2), /* -t | --table */ |
| 61 |
OBJFILTER_SCHEMA = (1 << 3), /* -n | --schema */ |
| 62 |
OBJFILTER_SCHEMA_EXCLUDE = (1 << 4), /* -N | --exclude-schema */ |
| 63 |
} VacObjFilter; |
| 64 |
|
| 65 |
static VacObjFilter objfilter = OBJFILTER_NONE; |
| 66 |
|
| 67 |
static SimpleStringList *retrieve_objects(PGconn *conn, |
| 68 |
vacuumingOptions *vacopts, |
| 69 |
SimpleStringList *objects, |
| 70 |
bool echo); |
| 71 |
|
| 72 |
static void vacuum_one_database(ConnParams *cparams, |
| 73 |
vacuumingOptions *vacopts, |
| 74 |
int stage, |
| 75 |
SimpleStringList *objects, |
| 76 |
SimpleStringList **found_objs, |
| 77 |
int concurrentCons, |
| 78 |
const char *progname, bool echo, bool quiet); |
| 79 |
|
| 80 |
static void vacuum_all_databases(ConnParams *cparams, |
| 81 |
vacuumingOptions *vacopts, |
| 82 |
bool analyze_in_stages, |
| 83 |
SimpleStringList *objects, |
| 84 |
int concurrentCons, |
| 85 |
const char *progname, bool echo, bool quiet); |
| 86 |
|
| 87 |
static void prepare_vacuum_command(PQExpBuffer sql, int serverVersion, |
| 88 |
vacuumingOptions *vacopts, const char *table); |
| 89 |
|
| 90 |
static void run_vacuum_command(PGconn *conn, const char *sql, bool echo, |
| 91 |
const char *table); |
| 92 |
|
| 93 |
static void help(const char *progname); |
| 94 |
|
| 95 |
void check_objfilter(void); |
| 96 |
|
| 97 |
static char *escape_quotes(const char *src); |
| 98 |
|
| 99 |
/* For analyze-in-stages mode */ |
| 100 |
#define ANALYZE_NO_STAGE -1 |
| 101 |
#define ANALYZE_NUM_STAGES 3 |
| 102 |
|
| 103 |
|
| 104 |
int |
| 105 |
main(int argc, char *argv[]) |
| 106 |
{ |
| 107 |
static struct option long_options[] = { |
| 108 |
{"host", required_argument, NULL, 'h'}, |
| 109 |
{"port", required_argument, NULL, 'p'}, |
| 110 |
{"username", required_argument, NULL, 'U'}, |
| 111 |
{"no-password", no_argument, NULL, 'w'}, |
| 112 |
{"password", no_argument, NULL, 'W'}, |
| 113 |
{"echo", no_argument, NULL, 'e'}, |
| 114 |
{"quiet", no_argument, NULL, 'q'}, |
| 115 |
{"dbname", required_argument, NULL, 'd'}, |
| 116 |
{"analyze", no_argument, NULL, 'z'}, |
| 117 |
{"analyze-only", no_argument, NULL, 'Z'}, |
| 118 |
{"freeze", no_argument, NULL, 'F'}, |
| 119 |
{"all", no_argument, NULL, 'a'}, |
| 120 |
{"table", required_argument, NULL, 't'}, |
| 121 |
{"full", no_argument, NULL, 'f'}, |
| 122 |
{"verbose", no_argument, NULL, 'v'}, |
| 123 |
{"jobs", required_argument, NULL, 'j'}, |
| 124 |
{"parallel", required_argument, NULL, 'P'}, |
| 125 |
{"schema", required_argument, NULL, 'n'}, |
| 126 |
{"exclude-schema", required_argument, NULL, 'N'}, |
| 127 |
{"maintenance-db", required_argument, NULL, 2}, |
| 128 |
{"analyze-in-stages", no_argument, NULL, 3}, |
| 129 |
{"disable-page-skipping", no_argument, NULL, 4}, |
| 130 |
{"skip-locked", no_argument, NULL, 5}, |
| 131 |
{"min-xid-age", required_argument, NULL, 6}, |
| 132 |
{"min-mxid-age", required_argument, NULL, 7}, |
| 133 |
{"no-index-cleanup", no_argument, NULL, 8}, |
| 134 |
{"force-index-cleanup", no_argument, NULL, 9}, |
| 135 |
{"no-truncate", no_argument, NULL, 10}, |
| 136 |
{"no-process-toast", no_argument, NULL, 11}, |
| 137 |
{"no-process-main", no_argument, NULL, 12}, |
| 138 |
{"buffer-usage-limit", required_argument, NULL, 13}, |
| 139 |
{"missing-stats-only", no_argument, NULL, 14}, |
| 140 |
{NULL, 0, NULL, 0} |
| 141 |
}; |
| 142 |
|
| 143 |
const char *progname; |
| 144 |
int optindex; |
| 145 |
int c; |
| 146 |
const char *dbname = NULL; |
| 147 |
const char *maintenance_db = NULL; |
| 148 |
char *host = NULL; |
| 149 |
char *port = NULL; |
| 150 |
char *username = NULL; |
| 151 |
enum trivalue prompt_password = TRI_DEFAULT; |
| 152 |
ConnParams cparams; |
| 153 |
bool echo = false; |
| 154 |
bool quiet = false; |
| 155 |
vacuumingOptions vacopts; |
| 156 |
bool analyze_in_stages = false; |
| 157 |
SimpleStringList objects = {NULL, NULL}; |
| 158 |
int concurrentCons = 1; |
| 159 |
int tbl_count = 0; |
| 160 |
|
| 161 |
/* initialize options */ |
| 162 |
memset(&vacopts, 0, sizeof(vacopts)); |
| 163 |
vacopts.parallel_workers = -1; |
| 164 |
vacopts.buffer_usage_limit = NULL; |
| 165 |
vacopts.no_index_cleanup = false; |
| 166 |
vacopts.force_index_cleanup = false; |
| 167 |
vacopts.do_truncate = true; |
| 168 |
vacopts.process_main = true; |
| 169 |
vacopts.process_toast = true; |
| 170 |
|
| 171 |
pg_logging_init(argv[0]); |
| 172 |
progname = get_progname(argv[0]); |
| 173 |
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); |
| 174 |
|
| 175 |
handle_help_version_opts(argc, argv, "vacuumdb", help); |
| 176 |
|
| 177 |
while ((c = getopt_long(argc, argv, "ad:efFh:j:n:N:p:P:qt:U:vwWzZ", long_options, &optindex)) != -1) |
| 178 |
{ |
| 179 |
switch (c) |
| 180 |
{ |
| 181 |
case 'a': |
| 182 |
objfilter |= OBJFILTER_ALL_DBS; |
| 183 |
break; |
| 184 |
case 'd': |
| 185 |
objfilter |= OBJFILTER_DATABASE; |
| 186 |
dbname = pg_strdup(optarg); |
| 187 |
break; |
| 188 |
case 'e': |
| 189 |
echo = true; |
| 190 |
break; |
| 191 |
case 'f': |
| 192 |
vacopts.full = true; |
| 193 |
break; |
| 194 |
case 'F': |
| 195 |
vacopts.freeze = true; |
| 196 |
break; |
| 197 |
case 'h': |
| 198 |
host = pg_strdup(optarg); |
| 199 |
break; |
| 200 |
case 'j': |
| 201 |
if (!option_parse_int(optarg, "-j/--jobs", 1, INT_MAX, |
| 202 |
&concurrentCons)) |
| 203 |
exit(1); |
| 204 |
break; |
| 205 |
case 'n': |
| 206 |
objfilter |= OBJFILTER_SCHEMA; |
| 207 |
simple_string_list_append(&objects, optarg); |
| 208 |
break; |
| 209 |
case 'N': |
| 210 |
objfilter |= OBJFILTER_SCHEMA_EXCLUDE; |
| 211 |
simple_string_list_append(&objects, optarg); |
| 212 |
break; |
| 213 |
case 'p': |
| 214 |
port = pg_strdup(optarg); |
| 215 |
break; |
| 216 |
case 'P': |
| 217 |
if (!option_parse_int(optarg, "-P/--parallel", 0, INT_MAX, |
| 218 |
&vacopts.parallel_workers)) |
| 219 |
exit(1); |
| 220 |
break; |
| 221 |
case 'q': |
| 222 |
quiet = true; |
| 223 |
break; |
| 224 |
case 't': |
| 225 |
objfilter |= OBJFILTER_TABLE; |
| 226 |
simple_string_list_append(&objects, optarg); |
| 227 |
tbl_count++; |
| 228 |
break; |
| 229 |
case 'U': |
| 230 |
username = pg_strdup(optarg); |
| 231 |
break; |
| 232 |
case 'v': |
| 233 |
vacopts.verbose = true; |
| 234 |
break; |
| 235 |
case 'w': |
| 236 |
prompt_password = TRI_NO; |
| 237 |
break; |
| 238 |
case 'W': |
| 239 |
prompt_password = TRI_YES; |
| 240 |
break; |
| 241 |
case 'z': |
| 242 |
vacopts.and_analyze = true; |
| 243 |
break; |
| 244 |
case 'Z': |
| 245 |
vacopts.analyze_only = true; |
| 246 |
break; |
| 247 |
case 2: |
| 248 |
maintenance_db = pg_strdup(optarg); |
| 249 |
break; |
| 250 |
case 3: |
| 251 |
analyze_in_stages = vacopts.analyze_only = true; |
| 252 |
break; |
| 253 |
case 4: |
| 254 |
vacopts.disable_page_skipping = true; |
| 255 |
break; |
| 256 |
case 5: |
| 257 |
vacopts.skip_locked = true; |
| 258 |
break; |
| 259 |
case 6: |
| 260 |
if (!option_parse_int(optarg, "--min-xid-age", 1, INT_MAX, |
| 261 |
&vacopts.min_xid_age)) |
| 262 |
exit(1); |
| 263 |
break; |
| 264 |
case 7: |
| 265 |
if (!option_parse_int(optarg, "--min-mxid-age", 1, INT_MAX, |
| 266 |
&vacopts.min_mxid_age)) |
| 267 |
exit(1); |
| 268 |
break; |
| 269 |
case 8: |
| 270 |
vacopts.no_index_cleanup = true; |
| 271 |
break; |
| 272 |
case 9: |
| 273 |
vacopts.force_index_cleanup = true; |
| 274 |
break; |
| 275 |
case 10: |
| 276 |
vacopts.do_truncate = false; |
| 277 |
break; |
| 278 |
case 11: |
| 279 |
vacopts.process_toast = false; |
| 280 |
break; |
| 281 |
case 12: |
| 282 |
vacopts.process_main = false; |
| 283 |
break; |
| 284 |
case 13: |
| 285 |
vacopts.buffer_usage_limit = escape_quotes(optarg); |
| 286 |
break; |
| 287 |
case 14: |
| 288 |
vacopts.missing_stats_only = true; |
| 289 |
break; |
| 290 |
default: |
| 291 |
/* getopt_long already emitted a complaint */ |
| 292 |
pg_log_error_hint("Try \"%s --help\" for more information.", progname); |
| 293 |
exit(1); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
* Non-option argument specifies database name as long as it wasn't |
| 299 |
* already specified with -d / --dbname |
| 300 |
*/ |
| 301 |
if (optind < argc && dbname == NULL) |
| 302 |
{ |
| 303 |
objfilter |= OBJFILTER_DATABASE; |
| 304 |
dbname = argv[optind]; |
| 305 |
optind++; |
| 306 |
} |
| 307 |
|
| 308 |
if (optind < argc) |
| 309 |
{ |
| 310 |
pg_log_error("too many command-line arguments (first is \"%s\")", |
| 311 |
argv[optind]); |
| 312 |
pg_log_error_hint("Try \"%s --help\" for more information.", progname); |
| 313 |
exit(1); |
| 314 |
} |
| 315 |
|
| 316 |
/* |
| 317 |
* Validate the combination of filters specified in the command-line |
| 318 |
* options. |
| 319 |
*/ |
| 320 |
check_objfilter(); |
| 321 |
|
| 322 |
if (vacopts.analyze_only) |
| 323 |
{ |
| 324 |
if (vacopts.full) |
| 325 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 326 |
"full"); |
| 327 |
if (vacopts.freeze) |
| 328 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 329 |
"freeze"); |
| 330 |
if (vacopts.disable_page_skipping) |
| 331 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 332 |
"disable-page-skipping"); |
| 333 |
if (vacopts.no_index_cleanup) |
| 334 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 335 |
"no-index-cleanup"); |
| 336 |
if (vacopts.force_index_cleanup) |
| 337 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 338 |
"force-index-cleanup"); |
| 339 |
if (!vacopts.do_truncate) |
| 340 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 341 |
"no-truncate"); |
| 342 |
if (!vacopts.process_main) |
| 343 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 344 |
"no-process-main"); |
| 345 |
if (!vacopts.process_toast) |
| 346 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 347 |
"no-process-toast"); |
| 348 |
/* allow 'and_analyze' with 'analyze_only' */ |
| 349 |
} |
| 350 |
|
| 351 |
/* Prohibit full and analyze_only options with parallel option */ |
| 352 |
if (vacopts.parallel_workers >= 0) |
| 353 |
{ |
| 354 |
if (vacopts.analyze_only) |
| 355 |
pg_fatal("cannot use the \"%s\" option when performing only analyze", |
| 356 |
"parallel"); |
| 357 |
if (vacopts.full) |
| 358 |
pg_fatal("cannot use the \"%s\" option when performing full vacuum", |
| 359 |
"parallel"); |
| 360 |
} |
| 361 |
|
| 362 |
/* Prohibit --no-index-cleanup and --force-index-cleanup together */ |
| 363 |
if (vacopts.no_index_cleanup && vacopts.force_index_cleanup) |
| 364 |
pg_fatal("cannot use the \"%s\" option with the \"%s\" option", |
| 365 |
"no-index-cleanup", "force-index-cleanup"); |
| 366 |
|
| 367 |
/* |
| 368 |
* buffer-usage-limit is not allowed with VACUUM FULL unless ANALYZE is |
| 369 |
* included too. |
| 370 |
*/ |
| 371 |
if (vacopts.buffer_usage_limit && vacopts.full && !vacopts.and_analyze) |
| 372 |
pg_fatal("cannot use the \"%s\" option with the \"%s\" option", |
| 373 |
"buffer-usage-limit", "full"); |
| 374 |
|
| 375 |
/* |
| 376 |
* Prohibit --missing-stats-only without --analyze-only or |
| 377 |
* --analyze-in-stages. |
| 378 |
*/ |
| 379 |
if (vacopts.missing_stats_only && !vacopts.analyze_only) |
| 380 |
pg_fatal("cannot use the \"%s\" option without \"%s\" or \"%s\"", |
| 381 |
"missing-stats-only", "analyze-only", "analyze-in-stages"); |
| 382 |
|
| 383 |
/* fill cparams except for dbname, which is set below */ |
| 384 |
cparams.pghost = host; |
| 385 |
cparams.pgport = port; |
| 386 |
cparams.pguser = username; |
| 387 |
cparams.prompt_password = prompt_password; |
| 388 |
cparams.override_dbname = NULL; |
| 389 |
|
| 390 |
setup_cancel_handler(NULL); |
| 391 |
|
| 392 |
/* Avoid opening extra connections. */ |
| 393 |
if (tbl_count && (concurrentCons > tbl_count)) |
| 394 |
concurrentCons = tbl_count; |
| 395 |
|
| 396 |
if (objfilter & OBJFILTER_ALL_DBS) |
| 397 |
{ |
| 398 |
cparams.dbname = maintenance_db; |
| 399 |
|
| 400 |
vacuum_all_databases(&cparams, &vacopts, |
| 401 |
analyze_in_stages, |
| 402 |
&objects, |
| 403 |
concurrentCons, |
| 404 |
progname, echo, quiet); |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
if (dbname == NULL) |
| 409 |
{ |
| 410 |
if (getenv("PGDATABASE")) |
| 411 |
dbname = getenv("PGDATABASE"); |
| 412 |
else if (getenv("PGUSER")) |
| 413 |
dbname = getenv("PGUSER"); |
| 414 |
else |
| 415 |
dbname = get_user_name_or_exit(progname); |
| 416 |
} |
| 417 |
|
| 418 |
cparams.dbname = dbname; |
| 419 |
|
| 420 |
if (analyze_in_stages) |
| 421 |
{ |
| 422 |
int stage; |
| 423 |
SimpleStringList *found_objs = NULL; |
| 424 |
|
| 425 |
for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++) |
| 426 |
{ |
| 427 |
vacuum_one_database(&cparams, &vacopts, |
| 428 |
stage, |
| 429 |
&objects, |
| 430 |
vacopts.missing_stats_only ? &found_objs : NULL, |
| 431 |
concurrentCons, |
| 432 |
progname, echo, quiet); |
| 433 |
} |
| 434 |
} |
| 435 |
else |
| 436 |
vacuum_one_database(&cparams, &vacopts, |
| 437 |
ANALYZE_NO_STAGE, |
| 438 |
&objects, NULL, |
| 439 |
concurrentCons, |
| 440 |
progname, echo, quiet); |
| 441 |
} |
| 442 |
|
| 443 |
exit(0); |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* Verify that the filters used at command line are compatible. |
| 448 |
*/ |
| 449 |
void |
| 450 |
check_objfilter(void) |
| 451 |
{ |
| 452 |
if ((objfilter & OBJFILTER_ALL_DBS) && |
| 453 |
(objfilter & OBJFILTER_DATABASE)) |
| 454 |
pg_fatal("cannot vacuum all databases and a specific one at the same time"); |
| 455 |
|
| 456 |
if ((objfilter & OBJFILTER_TABLE) && |
| 457 |
(objfilter & OBJFILTER_SCHEMA)) |
| 458 |
pg_fatal("cannot vacuum all tables in schema(s) and specific table(s) at the same time"); |
| 459 |
|
| 460 |
if ((objfilter & OBJFILTER_TABLE) && |
| 461 |
(objfilter & OBJFILTER_SCHEMA_EXCLUDE)) |
| 462 |
pg_fatal("cannot vacuum specific table(s) and exclude schema(s) at the same time"); |
| 463 |
|
| 464 |
if ((objfilter & OBJFILTER_SCHEMA) && |
| 465 |
(objfilter & OBJFILTER_SCHEMA_EXCLUDE)) |
| 466 |
pg_fatal("cannot vacuum all tables in schema(s) and exclude schema(s) at the same time"); |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
* Returns a newly malloc'd version of 'src' with escaped single quotes and |
| 471 |
* backslashes. |
| 472 |
*/ |
| 473 |
static char * |
| 474 |
escape_quotes(const char *src) |
| 475 |
{ |
| 476 |
char *result = escape_single_quotes_ascii(src); |
| 477 |
|
| 478 |
if (!result) |
| 479 |
pg_fatal("out of memory"); |
| 480 |
return result; |
| 481 |
} |
| 482 |
|
| 483 |
/* |
| 484 |
* vacuum_one_database |
| 485 |
* |
| 486 |
* Process tables in the given database. |
| 487 |
* |
| 488 |
* There are two ways to specify the list of objects to process: |
| 489 |
* |
| 490 |
* 1) The "found_objs" parameter is a double pointer to a fully qualified list |
| 491 |
* of objects to process, as returned by a previous call to |
| 492 |
* vacuum_one_database(). |
| 493 |
* |
| 494 |
* a) If both "found_objs" (the double pointer) and "*found_objs" (the |
| 495 |
* once-dereferenced double pointer) are not NULL, this list takes |
| 496 |
* priority, and anything specified in "objects" is ignored. |
| 497 |
* |
| 498 |
* b) If "found_objs" (the double pointer) is not NULL but "*found_objs" |
| 499 |
* (the once-dereferenced double pointer) _is_ NULL, the "objects" |
| 500 |
* parameter takes priority, and the results of the catalog query |
| 501 |
* described in (2) are stored in "found_objs". |
| 502 |
* |
| 503 |
* c) If "found_objs" (the double pointer) is NULL, the "objects" |
| 504 |
* parameter again takes priority, and the results of the catalog query |
| 505 |
* are not saved. |
| 506 |
* |
| 507 |
* 2) The "objects" parameter is a user-specified list of objects to process. |
| 508 |
* When (1b) or (1c) applies, this function performs a catalog query to |
| 509 |
* retrieve a fully qualified list of objects to process, as described |
| 510 |
* below. |
| 511 |
* |
| 512 |
* a) If "objects" is not NULL, the catalog query gathers only the objects |
| 513 |
* listed in "objects". |
| 514 |
* |
| 515 |
* b) If "objects" is NULL, all tables in the database are gathered. |
| 516 |
* |
| 517 |
* Note that this function is only concerned with running exactly one stage |
| 518 |
* when in analyze-in-stages mode; caller must iterate on us if necessary. |
| 519 |
* |
| 520 |
* If concurrentCons is > 1, multiple connections are used to vacuum tables |
| 521 |
* in parallel. |
| 522 |
*/ |
| 523 |
static void |
| 524 |
vacuum_one_database(ConnParams *cparams, |
| 525 |
vacuumingOptions *vacopts, |
| 526 |
int stage, |
| 527 |
SimpleStringList *objects, |
| 528 |
SimpleStringList **found_objs, |
| 529 |
int concurrentCons, |
| 530 |
const char *progname, bool echo, bool quiet) |
| 531 |
{ |
| 532 |
PQExpBufferData sql; |
| 533 |
PGconn *conn; |
| 534 |
SimpleStringListCell *cell; |
| 535 |
ParallelSlotArray *sa; |
| 536 |
int ntups = 0; |
| 537 |
bool failed = false; |
| 538 |
const char *initcmd; |
| 539 |
SimpleStringList *ret = NULL; |
| 540 |
const char *stage_commands[] = { |
| 541 |
"SET default_statistics_target=1; SET vacuum_cost_delay=0;", |
| 542 |
"SET default_statistics_target=10; RESET vacuum_cost_delay;", |
| 543 |
"RESET default_statistics_target;" |
| 544 |
}; |
| 545 |
const char *stage_messages[] = { |
| 546 |
gettext_noop("Generating minimal optimizer statistics (1 target)"), |
| 547 |
gettext_noop("Generating medium optimizer statistics (10 targets)"), |
| 548 |
gettext_noop("Generating default (full) optimizer statistics") |
| 549 |
}; |
| 550 |
|
| 551 |
Assert(stage == ANALYZE_NO_STAGE || |
| 552 |
(stage >= 0 && stage < ANALYZE_NUM_STAGES)); |
| 553 |
|
| 554 |
conn = connectDatabase(cparams, progname, echo, false, true); |
| 555 |
|
| 556 |
if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600) |
| 557 |
{ |
| 558 |
PQfinish(conn); |
| 559 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 560 |
"disable-page-skipping", "9.6"); |
| 561 |
} |
| 562 |
|
| 563 |
if (vacopts->no_index_cleanup && PQserverVersion(conn) < 120000) |
| 564 |
{ |
| 565 |
PQfinish(conn); |
| 566 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 567 |
"no-index-cleanup", "12"); |
| 568 |
} |
| 569 |
|
| 570 |
if (vacopts->force_index_cleanup && PQserverVersion(conn) < 120000) |
| 571 |
{ |
| 572 |
PQfinish(conn); |
| 573 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 574 |
"force-index-cleanup", "12"); |
| 575 |
} |
| 576 |
|
| 577 |
if (!vacopts->do_truncate && PQserverVersion(conn) < 120000) |
| 578 |
{ |
| 579 |
PQfinish(conn); |
| 580 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 581 |
"no-truncate", "12"); |
| 582 |
} |
| 583 |
|
| 584 |
if (!vacopts->process_main && PQserverVersion(conn) < 160000) |
| 585 |
{ |
| 586 |
PQfinish(conn); |
| 587 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 588 |
"no-process-main", "16"); |
| 589 |
} |
| 590 |
|
| 591 |
if (!vacopts->process_toast && PQserverVersion(conn) < 140000) |
| 592 |
{ |
| 593 |
PQfinish(conn); |
| 594 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 595 |
"no-process-toast", "14"); |
| 596 |
} |
| 597 |
|
| 598 |
if (vacopts->skip_locked && PQserverVersion(conn) < 120000) |
| 599 |
{ |
| 600 |
PQfinish(conn); |
| 601 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 602 |
"skip-locked", "12"); |
| 603 |
} |
| 604 |
|
| 605 |
if (vacopts->min_xid_age != 0 && PQserverVersion(conn) < 90600) |
| 606 |
{ |
| 607 |
PQfinish(conn); |
| 608 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 609 |
"--min-xid-age", "9.6"); |
| 610 |
} |
| 611 |
|
| 612 |
if (vacopts->min_mxid_age != 0 && PQserverVersion(conn) < 90600) |
| 613 |
{ |
| 614 |
PQfinish(conn); |
| 615 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 616 |
"--min-mxid-age", "9.6"); |
| 617 |
} |
| 618 |
|
| 619 |
if (vacopts->parallel_workers >= 0 && PQserverVersion(conn) < 130000) |
| 620 |
{ |
| 621 |
PQfinish(conn); |
| 622 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 623 |
"--parallel", "13"); |
| 624 |
} |
| 625 |
|
| 626 |
if (vacopts->buffer_usage_limit && PQserverVersion(conn) < 160000) |
| 627 |
{ |
| 628 |
PQfinish(conn); |
| 629 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 630 |
"--buffer-usage-limit", "16"); |
| 631 |
} |
| 632 |
|
| 633 |
if (vacopts->missing_stats_only && PQserverVersion(conn) < 150000) |
| 634 |
{ |
| 635 |
PQfinish(conn); |
| 636 |
pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s", |
| 637 |
"--missing-stats-only", "15"); |
| 638 |
} |
| 639 |
|
| 640 |
/* skip_database_stats is used automatically if server supports it */ |
| 641 |
vacopts->skip_database_stats = (PQserverVersion(conn) >= 160000); |
| 642 |
|
| 643 |
if (!quiet) |
| 644 |
{ |
| 645 |
if (stage != ANALYZE_NO_STAGE) |
| 646 |
printf(_("%s: processing database \"%s\": %s\n"), |
| 647 |
progname, PQdb(conn), _(stage_messages[stage])); |
| 648 |
else |
| 649 |
printf(_("%s: vacuuming database \"%s\"\n"), |
| 650 |
progname, PQdb(conn)); |
| 651 |
fflush(stdout); |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* If the caller provided the results of a previous catalog query, just |
| 656 |
* use that. Otherwise, run the catalog query ourselves and set the |
| 657 |
* return variable if provided. |
| 658 |
*/ |
| 659 |
if (found_objs && *found_objs) |
| 660 |
ret = *found_objs; |
| 661 |
else |
| 662 |
{ |
| 663 |
ret = retrieve_objects(conn, vacopts, objects, echo); |
| 664 |
if (found_objs) |
| 665 |
*found_objs = ret; |
| 666 |
} |
| 667 |
|
| 668 |
/* |
| 669 |
* Count the number of objects in the catalog query result. If there are |
| 670 |
* none, we are done. |
| 671 |
*/ |
| 672 |
for (cell = ret ? ret->head : NULL; cell; cell = cell->next) |
| 673 |
ntups++; |
| 674 |
|
| 675 |
if (ntups == 0) |
| 676 |
{ |
| 677 |
PQfinish(conn); |
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
/* |
| 682 |
* Ensure concurrentCons is sane. If there are more connections than |
| 683 |
* vacuumable relations, we don't need to use them all. |
| 684 |
*/ |
| 685 |
if (concurrentCons > ntups) |
| 686 |
concurrentCons = ntups; |
| 687 |
if (concurrentCons <= 0) |
| 688 |
concurrentCons = 1; |
| 689 |
|
| 690 |
/* |
| 691 |
* All slots need to be prepared to run the appropriate analyze stage, if |
| 692 |
* caller requested that mode. We have to prepare the initial connection |
| 693 |
* ourselves before setting up the slots. |
| 694 |
*/ |
| 695 |
if (stage == ANALYZE_NO_STAGE) |
| 696 |
initcmd = NULL; |
| 697 |
else |
| 698 |
{ |
| 699 |
initcmd = stage_commands[stage]; |
| 700 |
executeCommand(conn, initcmd, echo); |
| 701 |
} |
| 702 |
|
| 703 |
/* |
| 704 |
* Setup the database connections. We reuse the connection we already have |
| 705 |
* for the first slot. If not in parallel mode, the first slot in the |
| 706 |
* array contains the connection. |
| 707 |
*/ |
| 708 |
sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, initcmd); |
| 709 |
ParallelSlotsAdoptConn(sa, conn); |
| 710 |
|
| 711 |
initPQExpBuffer(&sql); |
| 712 |
|
| 713 |
cell = ret->head; |
| 714 |
do |
| 715 |
{ |
| 716 |
const char *tabname = cell->val; |
| 717 |
ParallelSlot *free_slot; |
| 718 |
|
| 719 |
if (CancelRequested) |
| 720 |
{ |
| 721 |
failed = true; |
| 722 |
goto finish; |
| 723 |
} |
| 724 |
|
| 725 |
free_slot = ParallelSlotsGetIdle(sa, NULL); |
| 726 |
if (!free_slot) |
| 727 |
{ |
| 728 |
failed = true; |
| 729 |
goto finish; |
| 730 |
} |
| 731 |
|
| 732 |
prepare_vacuum_command(&sql, PQserverVersion(free_slot->connection), |
| 733 |
vacopts, tabname); |
| 734 |
|
| 735 |
/* |
| 736 |
* Execute the vacuum. All errors are handled in processQueryResult |
| 737 |
* through ParallelSlotsGetIdle. |
| 738 |
*/ |
| 739 |
ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL); |
| 740 |
run_vacuum_command(free_slot->connection, sql.data, |
| 741 |
echo, tabname); |
| 742 |
|
| 743 |
cell = cell->next; |
| 744 |
} while (cell != NULL); |
| 745 |
|
| 746 |
if (!ParallelSlotsWaitCompletion(sa)) |
| 747 |
{ |
| 748 |
failed = true; |
| 749 |
goto finish; |
| 750 |
} |
| 751 |
|
| 752 |
/* If we used SKIP_DATABASE_STATS, mop up with ONLY_DATABASE_STATS */ |
| 753 |
if (vacopts->skip_database_stats && stage == ANALYZE_NO_STAGE) |
| 754 |
{ |
| 755 |
const char *cmd = "VACUUM (ONLY_DATABASE_STATS);"; |
| 756 |
ParallelSlot *free_slot = ParallelSlotsGetIdle(sa, NULL); |
| 757 |
|
| 758 |
if (!free_slot) |
| 759 |
{ |
| 760 |
failed = true; |
| 761 |
goto finish; |
| 762 |
} |
| 763 |
|
| 764 |
ParallelSlotSetHandler(free_slot, TableCommandResultHandler, NULL); |
| 765 |
run_vacuum_command(free_slot->connection, cmd, echo, NULL); |
| 766 |
|
| 767 |
if (!ParallelSlotsWaitCompletion(sa)) |
| 768 |
failed = true; |
| 769 |
} |
| 770 |
|
| 771 |
finish: |
| 772 |
ParallelSlotsTerminate(sa); |
| 773 |
pg_free(sa); |
| 774 |
|
| 775 |
termPQExpBuffer(&sql); |
| 776 |
|
| 777 |
if (failed) |
| 778 |
exit(1); |
| 779 |
} |
| 780 |
|
| 781 |
/* |
| 782 |
* Prepare the list of tables to process by querying the catalogs. |
| 783 |
* |
| 784 |
* Since we execute the constructed query with the default search_path (which |
| 785 |
* could be unsafe), everything in this query MUST be fully qualified. |
| 786 |
* |
| 787 |
* First, build a WITH clause for the catalog query if any tables were |
| 788 |
* specified, with a set of values made of relation names and their optional |
| 789 |
* set of columns. This is used to match any provided column lists with the |
| 790 |
* generated qualified identifiers and to filter for the tables provided via |
| 791 |
* --table. If a listed table does not exist, the catalog query will fail. |
| 792 |
*/ |
| 793 |
static SimpleStringList * |
| 794 |
retrieve_objects(PGconn *conn, vacuumingOptions *vacopts, |
| 795 |
SimpleStringList *objects, bool echo) |
| 796 |
{ |
| 797 |
PQExpBufferData buf; |
| 798 |
PQExpBufferData catalog_query; |
| 799 |
PGresult *res; |
| 800 |
SimpleStringListCell *cell; |
| 801 |
SimpleStringList *found_objs = palloc0(sizeof(SimpleStringList)); |
| 802 |
bool objects_listed = false; |
| 803 |
|
| 804 |
initPQExpBuffer(&catalog_query); |
| 805 |
for (cell = objects ? objects->head : NULL; cell; cell = cell->next) |
| 806 |
{ |
| 807 |
char *just_table = NULL; |
| 808 |
const char *just_columns = NULL; |
| 809 |
|
| 810 |
if (!objects_listed) |
| 811 |
{ |
| 812 |
appendPQExpBufferStr(&catalog_query, |
| 813 |
"WITH listed_objects (object_oid, column_list) " |
| 814 |
"AS (\n VALUES ("); |
| 815 |
objects_listed = true; |
| 816 |
} |
| 817 |
else |
| 818 |
appendPQExpBufferStr(&catalog_query, ",\n ("); |
| 819 |
|
| 820 |
if (objfilter & (OBJFILTER_SCHEMA | OBJFILTER_SCHEMA_EXCLUDE)) |
| 821 |
{ |
| 822 |
appendStringLiteralConn(&catalog_query, cell->val, conn); |
| 823 |
appendPQExpBufferStr(&catalog_query, "::pg_catalog.regnamespace, "); |
| 824 |
} |
| 825 |
|
| 826 |
if (objfilter & OBJFILTER_TABLE) |
| 827 |
{ |
| 828 |
/* |
| 829 |
* Split relation and column names given by the user, this is used |
| 830 |
* to feed the CTE with values on which are performed pre-run |
| 831 |
* validity checks as well. For now these happen only on the |
| 832 |
* relation name. |
| 833 |
*/ |
| 834 |
splitTableColumnsSpec(cell->val, PQclientEncoding(conn), |
| 835 |
&just_table, &just_columns); |
| 836 |
|
| 837 |
appendStringLiteralConn(&catalog_query, just_table, conn); |
| 838 |
appendPQExpBufferStr(&catalog_query, "::pg_catalog.regclass, "); |
| 839 |
} |
| 840 |
|
| 841 |
if (just_columns && just_columns[0] != '\0') |
| 842 |
appendStringLiteralConn(&catalog_query, just_columns, conn); |
| 843 |
else |
| 844 |
appendPQExpBufferStr(&catalog_query, "NULL"); |
| 845 |
|
| 846 |
appendPQExpBufferStr(&catalog_query, "::pg_catalog.text)"); |
| 847 |
|
| 848 |
pg_free(just_table); |
| 849 |
} |
| 850 |
|
| 851 |
/* Finish formatting the CTE */ |
| 852 |
if (objects_listed) |
| 853 |
appendPQExpBufferStr(&catalog_query, "\n)\n"); |
| 854 |
|
| 855 |
appendPQExpBufferStr(&catalog_query, "SELECT c.relname, ns.nspname"); |
| 856 |
|
| 857 |
if (objects_listed) |
| 858 |
appendPQExpBufferStr(&catalog_query, ", listed_objects.column_list"); |
| 859 |
|
| 860 |
appendPQExpBufferStr(&catalog_query, |
| 861 |
" FROM pg_catalog.pg_class c\n" |
| 862 |
" JOIN pg_catalog.pg_namespace ns" |
| 863 |
" ON c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n" |
| 864 |
" CROSS JOIN LATERAL (SELECT c.relkind IN (" |
| 865 |
CppAsString2(RELKIND_PARTITIONED_TABLE) ", " |
| 866 |
CppAsString2(RELKIND_PARTITIONED_INDEX) ")) as p (inherited)\n" |
| 867 |
" LEFT JOIN pg_catalog.pg_class t" |
| 868 |
" ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid\n"); |
| 869 |
|
| 870 |
/* |
| 871 |
* Used to match the tables or schemas listed by the user, completing the |
| 872 |
* JOIN clause. |
| 873 |
*/ |
| 874 |
if (objects_listed) |
| 875 |
{ |
| 876 |
appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects" |
| 877 |
" ON listed_objects.object_oid" |
| 878 |
" OPERATOR(pg_catalog.=) "); |
| 879 |
|
| 880 |
if (objfilter & OBJFILTER_TABLE) |
| 881 |
appendPQExpBufferStr(&catalog_query, "c.oid\n"); |
| 882 |
else |
| 883 |
appendPQExpBufferStr(&catalog_query, "ns.oid\n"); |
| 884 |
} |
| 885 |
|
| 886 |
/* |
| 887 |
* Exclude temporary tables, beginning the WHERE clause. |
| 888 |
*/ |
| 889 |
appendPQExpBufferStr(&catalog_query, |
| 890 |
" WHERE c.relpersistence OPERATOR(pg_catalog.!=) " |
| 891 |
CppAsString2(RELPERSISTENCE_TEMP) "\n"); |
| 892 |
|
| 893 |
/* |
| 894 |
* Used to match the tables or schemas listed by the user, for the WHERE |
| 895 |
* clause. |
| 896 |
*/ |
| 897 |
if (objects_listed) |
| 898 |
{ |
| 899 |
if (objfilter & OBJFILTER_SCHEMA_EXCLUDE) |
| 900 |
appendPQExpBufferStr(&catalog_query, |
| 901 |
" AND listed_objects.object_oid IS NULL\n"); |
| 902 |
else |
| 903 |
appendPQExpBufferStr(&catalog_query, |
| 904 |
" AND listed_objects.object_oid IS NOT NULL\n"); |
| 905 |
} |
| 906 |
|
| 907 |
/* |
| 908 |
* If no tables were listed, filter for the relevant relation types. If |
| 909 |
* tables were given via --table, don't bother filtering by relation type. |
| 910 |
* Instead, let the server decide whether a given relation can be |
| 911 |
* processed in which case the user will know about it. |
| 912 |
*/ |
| 913 |
if ((objfilter & OBJFILTER_TABLE) == 0) |
| 914 |
{ |
| 915 |
appendPQExpBufferStr(&catalog_query, |
| 916 |
" AND c.relkind OPERATOR(pg_catalog.=) ANY (array[" |
| 917 |
CppAsString2(RELKIND_RELATION) ", " |
| 918 |
CppAsString2(RELKIND_MATVIEW) "])\n"); |
| 919 |
} |
| 920 |
|
| 921 |
/* |
| 922 |
* For --min-xid-age and --min-mxid-age, the age of the relation is the |
| 923 |
* greatest of the ages of the main relation and its associated TOAST |
| 924 |
* table. The commands generated by vacuumdb will also process the TOAST |
| 925 |
* table for the relation if necessary, so it does not need to be |
| 926 |
* considered separately. |
| 927 |
*/ |
| 928 |
if (vacopts->min_xid_age != 0) |
| 929 |
{ |
| 930 |
appendPQExpBuffer(&catalog_query, |
| 931 |
" AND GREATEST(pg_catalog.age(c.relfrozenxid)," |
| 932 |
" pg_catalog.age(t.relfrozenxid)) " |
| 933 |
" OPERATOR(pg_catalog.>=) '%d'::pg_catalog.int4\n" |
| 934 |
" AND c.relfrozenxid OPERATOR(pg_catalog.!=)" |
| 935 |
" '0'::pg_catalog.xid\n", |
| 936 |
vacopts->min_xid_age); |
| 937 |
} |
| 938 |
|
| 939 |
if (vacopts->min_mxid_age != 0) |
| 940 |
{ |
| 941 |
appendPQExpBuffer(&catalog_query, |
| 942 |
" AND GREATEST(pg_catalog.mxid_age(c.relminmxid)," |
| 943 |
" pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=)" |
| 944 |
" '%d'::pg_catalog.int4\n" |
| 945 |
" AND c.relminmxid OPERATOR(pg_catalog.!=)" |
| 946 |
" '0'::pg_catalog.xid\n", |
| 947 |
vacopts->min_mxid_age); |
| 948 |
} |
| 949 |
|
| 950 |
if (vacopts->missing_stats_only) |
| 951 |
{ |
| 952 |
appendPQExpBufferStr(&catalog_query, " AND (\n"); |
| 953 |
|
| 954 |
/* regular stats */ |
| 955 |
appendPQExpBufferStr(&catalog_query, |
| 956 |
" EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n" |
| 957 |
" WHERE a.attrelid OPERATOR(pg_catalog.=) c.oid\n" |
| 958 |
" AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n" |
| 959 |
" AND NOT a.attisdropped\n" |
| 960 |
" AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n" |
| 961 |
" AND a.attgenerated OPERATOR(pg_catalog.<>) " |
| 962 |
CppAsString2(ATTRIBUTE_GENERATED_VIRTUAL) "\n" |
| 963 |
" AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n" |
| 964 |
" WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n" |
| 965 |
" AND s.staattnum OPERATOR(pg_catalog.=) a.attnum\n" |
| 966 |
" AND s.stainherit OPERATOR(pg_catalog.=) p.inherited))\n"); |
| 967 |
|
| 968 |
/* extended stats */ |
| 969 |
appendPQExpBufferStr(&catalog_query, |
| 970 |
" OR EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext e\n" |
| 971 |
" WHERE e.stxrelid OPERATOR(pg_catalog.=) c.oid\n" |
| 972 |
" AND e.stxstattarget IS DISTINCT FROM 0::pg_catalog.int2\n" |
| 973 |
" AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext_data d\n" |
| 974 |
" WHERE d.stxoid OPERATOR(pg_catalog.=) e.oid\n" |
| 975 |
" AND d.stxdinherit OPERATOR(pg_catalog.=) p.inherited))\n"); |
| 976 |
|
| 977 |
/* expression indexes */ |
| 978 |
appendPQExpBufferStr(&catalog_query, |
| 979 |
" OR EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n" |
| 980 |
" JOIN pg_catalog.pg_index i" |
| 981 |
" ON i.indexrelid OPERATOR(pg_catalog.=) a.attrelid\n" |
| 982 |
" WHERE i.indrelid OPERATOR(pg_catalog.=) c.oid\n" |
| 983 |
" AND i.indkey[a.attnum OPERATOR(pg_catalog.-) 1::pg_catalog.int2]" |
| 984 |
" OPERATOR(pg_catalog.=) 0::pg_catalog.int2\n" |
| 985 |
" AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n" |
| 986 |
" AND NOT a.attisdropped\n" |
| 987 |
" AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n" |
| 988 |
" AND NOT p.inherited\n" |
| 989 |
" AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n" |
| 990 |
" WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n" |
| 991 |
" AND s.staattnum OPERATOR(pg_catalog.=) a.attnum))\n"); |
| 992 |
|
| 993 |
/* inheritance and regular stats */ |
| 994 |
appendPQExpBufferStr(&catalog_query, |
| 995 |
" OR EXISTS (SELECT NULL FROM pg_catalog.pg_attribute a\n" |
| 996 |
" WHERE a.attrelid OPERATOR(pg_catalog.=) c.oid\n" |
| 997 |
" AND a.attnum OPERATOR(pg_catalog.>) 0::pg_catalog.int2\n" |
| 998 |
" AND NOT a.attisdropped\n" |
| 999 |
" AND a.attstattarget IS DISTINCT FROM 0::pg_catalog.int2\n" |
| 1000 |
" AND a.attgenerated OPERATOR(pg_catalog.<>) " |
| 1001 |
CppAsString2(ATTRIBUTE_GENERATED_VIRTUAL) "\n" |
| 1002 |
" AND c.relhassubclass\n" |
| 1003 |
" AND NOT p.inherited\n" |
| 1004 |
" AND EXISTS (SELECT NULL FROM pg_catalog.pg_inherits h\n" |
| 1005 |
" WHERE h.inhparent OPERATOR(pg_catalog.=) c.oid)\n" |
| 1006 |
" AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic s\n" |
| 1007 |
" WHERE s.starelid OPERATOR(pg_catalog.=) a.attrelid\n" |
| 1008 |
" AND s.staattnum OPERATOR(pg_catalog.=) a.attnum\n" |
| 1009 |
" AND s.stainherit))\n"); |
| 1010 |
|
| 1011 |
/* inheritance and extended stats */ |
| 1012 |
appendPQExpBufferStr(&catalog_query, |
| 1013 |
" OR EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext e\n" |
| 1014 |
" WHERE e.stxrelid OPERATOR(pg_catalog.=) c.oid\n" |
| 1015 |
" AND e.stxstattarget IS DISTINCT FROM 0::pg_catalog.int2\n" |
| 1016 |
" AND c.relhassubclass\n" |
| 1017 |
" AND NOT p.inherited\n" |
| 1018 |
" AND EXISTS (SELECT NULL FROM pg_catalog.pg_inherits h\n" |
| 1019 |
" WHERE h.inhparent OPERATOR(pg_catalog.=) c.oid)\n" |
| 1020 |
" AND NOT EXISTS (SELECT NULL FROM pg_catalog.pg_statistic_ext_data d\n" |
| 1021 |
" WHERE d.stxoid OPERATOR(pg_catalog.=) e.oid\n" |
| 1022 |
" AND d.stxdinherit))\n"); |
| 1023 |
|
| 1024 |
appendPQExpBufferStr(&catalog_query, " )\n"); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* Execute the catalog query. We use the default search_path for this |
| 1029 |
* query for consistency with table lookups done elsewhere by the user. |
| 1030 |
*/ |
| 1031 |
appendPQExpBufferStr(&catalog_query, " ORDER BY c.relpages DESC;"); |
| 1032 |
executeCommand(conn, "RESET search_path;", echo); |
| 1033 |
res = executeQuery(conn, catalog_query.data, echo); |
| 1034 |
termPQExpBuffer(&catalog_query); |
| 1035 |
PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo)); |
| 1036 |
|
| 1037 |
/* |
| 1038 |
* Build qualified identifiers for each table, including the column list |
| 1039 |
* if given. |
| 1040 |
*/ |
| 1041 |
initPQExpBuffer(&buf); |
| 1042 |
for (int i = 0; i < PQntuples(res); i++) |
| 1043 |
{ |
| 1044 |
appendPQExpBufferStr(&buf, |
| 1045 |
fmtQualifiedIdEnc(PQgetvalue(res, i, 1), |
| 1046 |
PQgetvalue(res, i, 0), |
| 1047 |
PQclientEncoding(conn))); |
| 1048 |
|
| 1049 |
if (objects_listed && !PQgetisnull(res, i, 2)) |
| 1050 |
appendPQExpBufferStr(&buf, PQgetvalue(res, i, 2)); |
| 1051 |
|
| 1052 |
simple_string_list_append(found_objs, buf.data); |
| 1053 |
resetPQExpBuffer(&buf); |
| 1054 |
} |
| 1055 |
termPQExpBuffer(&buf); |
| 1056 |
PQclear(res); |
| 1057 |
|
| 1058 |
return found_objs; |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* Vacuum/analyze all connectable databases. |
| 1063 |
* |
| 1064 |
* In analyze-in-stages mode, we process all databases in one stage before |
| 1065 |
* moving on to the next stage. That ensure minimal stats are available |
| 1066 |
* quickly everywhere before generating more detailed ones. |
| 1067 |
*/ |
| 1068 |
static void |
| 1069 |
vacuum_all_databases(ConnParams *cparams, |
| 1070 |
vacuumingOptions *vacopts, |
| 1071 |
bool analyze_in_stages, |
| 1072 |
SimpleStringList *objects, |
| 1073 |
int concurrentCons, |
| 1074 |
const char *progname, bool echo, bool quiet) |
| 1075 |
{ |
| 1076 |
PGconn *conn; |
| 1077 |
PGresult *result; |
| 1078 |
int stage; |
| 1079 |
int i; |
| 1080 |
|
| 1081 |
conn = connectMaintenanceDatabase(cparams, progname, echo); |
| 1082 |
result = executeQuery(conn, |
| 1083 |
"SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;", |
| 1084 |
echo); |
| 1085 |
PQfinish(conn); |
| 1086 |
|
| 1087 |
if (analyze_in_stages) |
| 1088 |
{ |
| 1089 |
SimpleStringList **found_objs = NULL; |
| 1090 |
|
| 1091 |
if (vacopts->missing_stats_only) |
| 1092 |
found_objs = palloc0(PQntuples(result) * sizeof(SimpleStringList *)); |
| 1093 |
|
| 1094 |
/* |
| 1095 |
* When analyzing all databases in stages, we analyze them all in the |
| 1096 |
* fastest stage first, so that initial statistics become available |
| 1097 |
* for all of them as soon as possible. |
| 1098 |
* |
| 1099 |
* This means we establish several times as many connections, but |
| 1100 |
* that's a secondary consideration. |
| 1101 |
*/ |
| 1102 |
for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++) |
| 1103 |
{ |
| 1104 |
for (i = 0; i < PQntuples(result); i++) |
| 1105 |
{ |
| 1106 |
cparams->override_dbname = PQgetvalue(result, i, 0); |
| 1107 |
|
| 1108 |
vacuum_one_database(cparams, vacopts, |
| 1109 |
stage, |
| 1110 |
objects, |
| 1111 |
vacopts->missing_stats_only ? &found_objs[i] : NULL, |
| 1112 |
concurrentCons, |
| 1113 |
progname, echo, quiet); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
else |
| 1118 |
{ |
| 1119 |
for (i = 0; i < PQntuples(result); i++) |
| 1120 |
{ |
| 1121 |
cparams->override_dbname = PQgetvalue(result, i, 0); |
| 1122 |
|
| 1123 |
vacuum_one_database(cparams, vacopts, |
| 1124 |
ANALYZE_NO_STAGE, |
| 1125 |
objects, NULL, |
| 1126 |
concurrentCons, |
| 1127 |
progname, echo, quiet); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
PQclear(result); |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* |
| 1135 |
* Construct a vacuum/analyze command to run based on the given options, in the |
| 1136 |
* given string buffer, which may contain previous garbage. |
| 1137 |
* |
| 1138 |
* The table name used must be already properly quoted. The command generated |
| 1139 |
* depends on the server version involved and it is semicolon-terminated. |
| 1140 |
*/ |
| 1141 |
static void |
| 1142 |
prepare_vacuum_command(PQExpBuffer sql, int serverVersion, |
| 1143 |
vacuumingOptions *vacopts, const char *table) |
| 1144 |
{ |
| 1145 |
const char *paren = " ("; |
| 1146 |
const char *comma = ", "; |
| 1147 |
const char *sep = paren; |
| 1148 |
|
| 1149 |
resetPQExpBuffer(sql); |
| 1150 |
|
| 1151 |
if (vacopts->analyze_only) |
| 1152 |
{ |
| 1153 |
appendPQExpBufferStr(sql, "ANALYZE"); |
| 1154 |
|
| 1155 |
/* parenthesized grammar of ANALYZE is supported since v11 */ |
| 1156 |
if (serverVersion >= 110000) |
| 1157 |
{ |
| 1158 |
if (vacopts->skip_locked) |
| 1159 |
{ |
| 1160 |
/* SKIP_LOCKED is supported since v12 */ |
| 1161 |
Assert(serverVersion >= 120000); |
| 1162 |
appendPQExpBuffer(sql, "%sSKIP_LOCKED", sep); |
| 1163 |
sep = comma; |
| 1164 |
} |
| 1165 |
if (vacopts->verbose) |
| 1166 |
{ |
| 1167 |
appendPQExpBuffer(sql, "%sVERBOSE", sep); |
| 1168 |
sep = comma; |
| 1169 |
} |
| 1170 |
if (vacopts->buffer_usage_limit) |
| 1171 |
{ |
| 1172 |
Assert(serverVersion >= 160000); |
| 1173 |
appendPQExpBuffer(sql, "%sBUFFER_USAGE_LIMIT '%s'", sep, |
| 1174 |
vacopts->buffer_usage_limit); |
| 1175 |
sep = comma; |
| 1176 |
} |
| 1177 |
if (sep != paren) |
| 1178 |
appendPQExpBufferChar(sql, ')'); |
| 1179 |
} |
| 1180 |
else |
| 1181 |
{ |
| 1182 |
if (vacopts->verbose) |
| 1183 |
appendPQExpBufferStr(sql, " VERBOSE"); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
else |
| 1187 |
{ |
| 1188 |
appendPQExpBufferStr(sql, "VACUUM"); |
| 1189 |
|
| 1190 |
/* parenthesized grammar of VACUUM is supported since v9.0 */ |
| 1191 |
if (serverVersion >= 90000) |
| 1192 |
{ |
| 1193 |
if (vacopts->disable_page_skipping) |
| 1194 |
{ |
| 1195 |
/* DISABLE_PAGE_SKIPPING is supported since v9.6 */ |
| 1196 |
Assert(serverVersion >= 90600); |
| 1197 |
appendPQExpBuffer(sql, "%sDISABLE_PAGE_SKIPPING", sep); |
| 1198 |
sep = comma; |
| 1199 |
} |
| 1200 |
if (vacopts->no_index_cleanup) |
| 1201 |
{ |
| 1202 |
/* "INDEX_CLEANUP FALSE" has been supported since v12 */ |
| 1203 |
Assert(serverVersion >= 120000); |
| 1204 |
Assert(!vacopts->force_index_cleanup); |
| 1205 |
appendPQExpBuffer(sql, "%sINDEX_CLEANUP FALSE", sep); |
| 1206 |
sep = comma; |
| 1207 |
} |
| 1208 |
if (vacopts->force_index_cleanup) |
| 1209 |
{ |
| 1210 |
/* "INDEX_CLEANUP TRUE" has been supported since v12 */ |
| 1211 |
Assert(serverVersion >= 120000); |
| 1212 |
Assert(!vacopts->no_index_cleanup); |
| 1213 |
appendPQExpBuffer(sql, "%sINDEX_CLEANUP TRUE", sep); |
| 1214 |
sep = comma; |
| 1215 |
} |
| 1216 |
if (!vacopts->do_truncate) |
| 1217 |
{ |
| 1218 |
/* TRUNCATE is supported since v12 */ |
| 1219 |
Assert(serverVersion >= 120000); |
| 1220 |
appendPQExpBuffer(sql, "%sTRUNCATE FALSE", sep); |
| 1221 |
sep = comma; |
| 1222 |
} |
| 1223 |
if (!vacopts->process_main) |
| 1224 |
{ |
| 1225 |
/* PROCESS_MAIN is supported since v16 */ |
| 1226 |
Assert(serverVersion >= 160000); |
| 1227 |
appendPQExpBuffer(sql, "%sPROCESS_MAIN FALSE", sep); |
| 1228 |
sep = comma; |
| 1229 |
} |
| 1230 |
if (!vacopts->process_toast) |
| 1231 |
{ |
| 1232 |
/* PROCESS_TOAST is supported since v14 */ |
| 1233 |
Assert(serverVersion >= 140000); |
| 1234 |
appendPQExpBuffer(sql, "%sPROCESS_TOAST FALSE", sep); |
| 1235 |
sep = comma; |
| 1236 |
} |
| 1237 |
if (vacopts->skip_database_stats) |
| 1238 |
{ |
| 1239 |
/* SKIP_DATABASE_STATS is supported since v16 */ |
| 1240 |
Assert(serverVersion >= 160000); |
| 1241 |
appendPQExpBuffer(sql, "%sSKIP_DATABASE_STATS", sep); |
| 1242 |
sep = comma; |
| 1243 |
} |
| 1244 |
if (vacopts->skip_locked) |
| 1245 |
{ |
| 1246 |
/* SKIP_LOCKED is supported since v12 */ |
| 1247 |
Assert(serverVersion >= 120000); |
| 1248 |
appendPQExpBuffer(sql, "%sSKIP_LOCKED", sep); |
| 1249 |
sep = comma; |
| 1250 |
} |
| 1251 |
if (vacopts->full) |
| 1252 |
{ |
| 1253 |
appendPQExpBuffer(sql, "%sFULL", sep); |
| 1254 |
sep = comma; |
| 1255 |
} |
| 1256 |
if (vacopts->freeze) |
| 1257 |
{ |
| 1258 |
appendPQExpBuffer(sql, "%sFREEZE", sep); |
| 1259 |
sep = comma; |
| 1260 |
} |
| 1261 |
if (vacopts->verbose) |
| 1262 |
{ |
| 1263 |
appendPQExpBuffer(sql, "%sVERBOSE", sep); |
| 1264 |
sep = comma; |
| 1265 |
} |
| 1266 |
if (vacopts->and_analyze) |
| 1267 |
{ |
| 1268 |
appendPQExpBuffer(sql, "%sANALYZE", sep); |
| 1269 |
sep = comma; |
| 1270 |
} |
| 1271 |
if (vacopts->parallel_workers >= 0) |
| 1272 |
{ |
| 1273 |
/* PARALLEL is supported since v13 */ |
| 1274 |
Assert(serverVersion >= 130000); |
| 1275 |
appendPQExpBuffer(sql, "%sPARALLEL %d", sep, |
| 1276 |
vacopts->parallel_workers); |
| 1277 |
sep = comma; |
| 1278 |
} |
| 1279 |
if (vacopts->buffer_usage_limit) |
| 1280 |
{ |
| 1281 |
Assert(serverVersion >= 160000); |
| 1282 |
appendPQExpBuffer(sql, "%sBUFFER_USAGE_LIMIT '%s'", sep, |
| 1283 |
vacopts->buffer_usage_limit); |
| 1284 |
sep = comma; |
| 1285 |
} |
| 1286 |
if (sep != paren) |
| 1287 |
appendPQExpBufferChar(sql, ')'); |
| 1288 |
} |
| 1289 |
else |
| 1290 |
{ |
| 1291 |
if (vacopts->full) |
| 1292 |
appendPQExpBufferStr(sql, " FULL"); |
| 1293 |
if (vacopts->freeze) |
| 1294 |
appendPQExpBufferStr(sql, " FREEZE"); |
| 1295 |
if (vacopts->verbose) |
| 1296 |
appendPQExpBufferStr(sql, " VERBOSE"); |
| 1297 |
if (vacopts->and_analyze) |
| 1298 |
appendPQExpBufferStr(sql, " ANALYZE"); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
appendPQExpBuffer(sql, " %s;", table); |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* |
| 1306 |
* Send a vacuum/analyze command to the server, returning after sending the |
| 1307 |
* command. |
| 1308 |
* |
| 1309 |
* Any errors during command execution are reported to stderr. |
| 1310 |
*/ |
| 1311 |
static void |
| 1312 |
run_vacuum_command(PGconn *conn, const char *sql, bool echo, |
| 1313 |
const char *table) |
| 1314 |
{ |
| 1315 |
bool status; |
| 1316 |
|
| 1317 |
if (echo) |
| 1318 |
printf("%s\n", sql); |
| 1319 |
|
| 1320 |
status = PQsendQuery(conn, sql) == 1; |
| 1321 |
|
| 1322 |
if (!status) |
| 1323 |
{ |
| 1324 |
if (table) |
| 1325 |
pg_log_error("vacuuming of table \"%s\" in database \"%s\" failed: %s", |
| 1326 |
table, PQdb(conn), PQerrorMessage(conn)); |
| 1327 |
else |
| 1328 |
pg_log_error("vacuuming of database \"%s\" failed: %s", |
| 1329 |
PQdb(conn), PQerrorMessage(conn)); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
static void |
| 1334 |
help(const char *progname) |
| 1335 |
{ |
| 1336 |
printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname); |
| 1337 |
printf(_("Usage:\n")); |
| 1338 |
printf(_(" %s [OPTION]... [DBNAME]\n"), progname); |
| 1339 |
printf(_("\nOptions:\n")); |
| 1340 |
printf(_(" -a, --all vacuum all databases\n")); |
| 1341 |
printf(_(" --buffer-usage-limit=SIZE size of ring buffer used for vacuum\n")); |
| 1342 |
printf(_(" -d, --dbname=DBNAME database to vacuum\n")); |
| 1343 |
printf(_(" --disable-page-skipping disable all page-skipping behavior\n")); |
| 1344 |
printf(_(" -e, --echo show the commands being sent to the server\n")); |
| 1345 |
printf(_(" -f, --full do full vacuuming\n")); |
| 1346 |
printf(_(" -F, --freeze freeze row transaction information\n")); |
| 1347 |
printf(_(" --force-index-cleanup always remove index entries that point to dead tuples\n")); |
| 1348 |
printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n")); |
| 1349 |
printf(_(" --min-mxid-age=MXID_AGE minimum multixact ID age of tables to vacuum\n")); |
| 1350 |
printf(_(" --min-xid-age=XID_AGE minimum transaction ID age of tables to vacuum\n")); |
| 1351 |
printf(_(" --missing-stats-only only analyze relations with missing statistics\n")); |
| 1352 |
printf(_(" --no-index-cleanup don't remove index entries that point to dead tuples\n")); |
| 1353 |
printf(_(" --no-process-main skip the main relation\n")); |
| 1354 |
printf(_(" --no-process-toast skip the TOAST table associated with the table to vacuum\n")); |
| 1355 |
printf(_(" --no-truncate don't truncate empty pages at the end of the table\n")); |
| 1356 |
printf(_(" -n, --schema=SCHEMA vacuum tables in the specified schema(s) only\n")); |
| 1357 |
printf(_(" -N, --exclude-schema=SCHEMA do not vacuum tables in the specified schema(s)\n")); |
| 1358 |
printf(_(" -P, --parallel=PARALLEL_WORKERS use this many background workers for vacuum, if available\n")); |
| 1359 |
printf(_(" -q, --quiet don't write any messages\n")); |
| 1360 |
printf(_(" --skip-locked skip relations that cannot be immediately locked\n")); |
| 1361 |
printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n")); |
| 1362 |
printf(_(" -v, --verbose write a lot of output\n")); |
| 1363 |
printf(_(" -V, --version output version information, then exit\n")); |
| 1364 |
printf(_(" -z, --analyze update optimizer statistics\n")); |
| 1365 |
printf(_(" -Z, --analyze-only only update optimizer statistics; no vacuum\n")); |
| 1366 |
printf(_(" --analyze-in-stages only update optimizer statistics, in multiple\n" |
| 1367 |
" stages for faster results; no vacuum\n")); |
| 1368 |
printf(_(" -?, --help show this help, then exit\n")); |
| 1369 |
printf(_("\nConnection options:\n")); |
| 1370 |
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); |
| 1371 |
printf(_(" -p, --port=PORT database server port\n")); |
| 1372 |
printf(_(" -U, --username=USERNAME user name to connect as\n")); |
| 1373 |
printf(_(" -w, --no-password never prompt for password\n")); |
| 1374 |
printf(_(" -W, --password force password prompt\n")); |
| 1375 |
printf(_(" --maintenance-db=DBNAME alternate maintenance database\n")); |
| 1376 |
printf(_("\nRead the description of the SQL command VACUUM for details.\n")); |
| 1377 |
printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); |
| 1378 |
printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); |
| 1379 |
} |