1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/bin/scripts/vacuumdb.c
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
17 #include "catalog/pg_class_d.h"
19 #include "common/connect.h"
20 #include "common/logging.h"
21 #include "fe_utils/cancel.h"
22 #include "fe_utils/option_utils.h"
23 #include "fe_utils/parallel_slot.h"
24 #include "fe_utils/query_utils.h"
25 #include "fe_utils/simple_list.h"
26 #include "fe_utils/string_utils.h"
29 /* vacuum options controlled by user flags */
30 typedef struct vacuumingOptions
37 bool disable_page_skipping
;
41 int parallel_workers
; /* >= 0 indicates user specified the
42 * parallel degree, otherwise -1 */
43 bool no_index_cleanup
;
44 bool force_index_cleanup
;
50 static void vacuum_one_database(ConnParams
*cparams
,
51 vacuumingOptions
*vacopts
,
53 SimpleStringList
*tables
,
55 const char *progname
, bool echo
, bool quiet
);
57 static void vacuum_all_databases(ConnParams
*cparams
,
58 vacuumingOptions
*vacopts
,
59 bool analyze_in_stages
,
61 const char *progname
, bool echo
, bool quiet
);
63 static void prepare_vacuum_command(PQExpBuffer sql
, int serverVersion
,
64 vacuumingOptions
*vacopts
, const char *table
);
66 static void run_vacuum_command(PGconn
*conn
, const char *sql
, bool echo
,
69 static void help(const char *progname
);
71 /* For analyze-in-stages mode */
72 #define ANALYZE_NO_STAGE -1
73 #define ANALYZE_NUM_STAGES 3
77 main(int argc
, char *argv
[])
79 static struct option long_options
[] = {
80 {"host", required_argument
, NULL
, 'h'},
81 {"port", required_argument
, NULL
, 'p'},
82 {"username", required_argument
, NULL
, 'U'},
83 {"no-password", no_argument
, NULL
, 'w'},
84 {"password", no_argument
, NULL
, 'W'},
85 {"echo", no_argument
, NULL
, 'e'},
86 {"quiet", no_argument
, NULL
, 'q'},
87 {"dbname", required_argument
, NULL
, 'd'},
88 {"analyze", no_argument
, NULL
, 'z'},
89 {"analyze-only", no_argument
, NULL
, 'Z'},
90 {"freeze", no_argument
, NULL
, 'F'},
91 {"all", no_argument
, NULL
, 'a'},
92 {"table", required_argument
, NULL
, 't'},
93 {"full", no_argument
, NULL
, 'f'},
94 {"verbose", no_argument
, NULL
, 'v'},
95 {"jobs", required_argument
, NULL
, 'j'},
96 {"parallel", required_argument
, NULL
, 'P'},
97 {"maintenance-db", required_argument
, NULL
, 2},
98 {"analyze-in-stages", no_argument
, NULL
, 3},
99 {"disable-page-skipping", no_argument
, NULL
, 4},
100 {"skip-locked", no_argument
, NULL
, 5},
101 {"min-xid-age", required_argument
, NULL
, 6},
102 {"min-mxid-age", required_argument
, NULL
, 7},
103 {"no-index-cleanup", no_argument
, NULL
, 8},
104 {"force-index-cleanup", no_argument
, NULL
, 9},
105 {"no-truncate", no_argument
, NULL
, 10},
106 {"no-process-toast", no_argument
, NULL
, 11},
110 const char *progname
;
113 const char *dbname
= NULL
;
114 const char *maintenance_db
= NULL
;
117 char *username
= NULL
;
118 enum trivalue prompt_password
= TRI_DEFAULT
;
122 vacuumingOptions vacopts
;
123 bool analyze_in_stages
= false;
125 SimpleStringList tables
= {NULL
, NULL
};
126 int concurrentCons
= 1;
129 /* initialize options */
130 memset(&vacopts
, 0, sizeof(vacopts
));
131 vacopts
.parallel_workers
= -1;
132 vacopts
.no_index_cleanup
= false;
133 vacopts
.force_index_cleanup
= false;
134 vacopts
.do_truncate
= true;
135 vacopts
.process_toast
= true;
137 pg_logging_init(argv
[0]);
138 progname
= get_progname(argv
[0]);
139 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
141 handle_help_version_opts(argc
, argv
, "vacuumdb", help
);
143 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWeqd:zZFat:fvj:P:", long_options
, &optindex
)) != -1)
148 host
= pg_strdup(optarg
);
151 port
= pg_strdup(optarg
);
154 username
= pg_strdup(optarg
);
157 prompt_password
= TRI_NO
;
160 prompt_password
= TRI_YES
;
169 dbname
= pg_strdup(optarg
);
172 vacopts
.and_analyze
= true;
175 vacopts
.analyze_only
= true;
178 vacopts
.freeze
= true;
185 simple_string_list_append(&tables
, optarg
);
193 vacopts
.verbose
= true;
196 if (!option_parse_int(optarg
, "-j/--jobs", 1, INT_MAX
,
201 if (!option_parse_int(optarg
, "-P/--parallel", 0, INT_MAX
,
202 &vacopts
.parallel_workers
))
206 maintenance_db
= pg_strdup(optarg
);
209 analyze_in_stages
= vacopts
.analyze_only
= true;
212 vacopts
.disable_page_skipping
= true;
215 vacopts
.skip_locked
= true;
218 if (!option_parse_int(optarg
, "--min-xid-age", 1, INT_MAX
,
219 &vacopts
.min_xid_age
))
223 if (!option_parse_int(optarg
, "--min-mxid-age", 1, INT_MAX
,
224 &vacopts
.min_mxid_age
))
228 vacopts
.no_index_cleanup
= true;
231 vacopts
.force_index_cleanup
= true;
234 vacopts
.do_truncate
= false;
237 vacopts
.process_toast
= false;
240 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
246 * Non-option argument specifies database name as long as it wasn't
247 * already specified with -d / --dbname
249 if (optind
< argc
&& dbname
== NULL
)
251 dbname
= argv
[optind
];
257 pg_log_error("too many command-line arguments (first is \"%s\")",
259 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
263 if (vacopts
.analyze_only
)
267 pg_log_error("cannot use the \"%s\" option when performing only analyze",
273 pg_log_error("cannot use the \"%s\" option when performing only analyze",
277 if (vacopts
.disable_page_skipping
)
279 pg_log_error("cannot use the \"%s\" option when performing only analyze",
280 "disable-page-skipping");
283 if (vacopts
.no_index_cleanup
)
285 pg_log_error("cannot use the \"%s\" option when performing only analyze",
289 if (vacopts
.force_index_cleanup
)
291 pg_log_error("cannot use the \"%s\" option when performing only analyze",
292 "force-index-cleanup");
295 if (!vacopts
.do_truncate
)
297 pg_log_error("cannot use the \"%s\" option when performing only analyze",
301 if (!vacopts
.process_toast
)
303 pg_log_error("cannot use the \"%s\" option when performing only analyze",
307 /* allow 'and_analyze' with 'analyze_only' */
310 /* Prohibit full and analyze_only options with parallel option */
311 if (vacopts
.parallel_workers
>= 0)
313 if (vacopts
.analyze_only
)
315 pg_log_error("cannot use the \"%s\" option when performing only analyze",
321 pg_log_error("cannot use the \"%s\" option when performing full vacuum",
327 /* Prohibit --no-index-cleanup and --force-index-cleanup together */
328 if (vacopts
.no_index_cleanup
&& vacopts
.force_index_cleanup
)
330 pg_log_error("cannot use the \"%s\" option with the \"%s\" option",
331 "no-index-cleanup", "force-index-cleanup");
335 /* fill cparams except for dbname, which is set below */
336 cparams
.pghost
= host
;
337 cparams
.pgport
= port
;
338 cparams
.pguser
= username
;
339 cparams
.prompt_password
= prompt_password
;
340 cparams
.override_dbname
= NULL
;
342 setup_cancel_handler(NULL
);
344 /* Avoid opening extra connections. */
345 if (tbl_count
&& (concurrentCons
> tbl_count
))
346 concurrentCons
= tbl_count
;
352 pg_log_error("cannot vacuum all databases and a specific one at the same time");
355 if (tables
.head
!= NULL
)
357 pg_log_error("cannot vacuum specific table(s) in all databases");
361 cparams
.dbname
= maintenance_db
;
363 vacuum_all_databases(&cparams
, &vacopts
,
366 progname
, echo
, quiet
);
372 if (getenv("PGDATABASE"))
373 dbname
= getenv("PGDATABASE");
374 else if (getenv("PGUSER"))
375 dbname
= getenv("PGUSER");
377 dbname
= get_user_name_or_exit(progname
);
380 cparams
.dbname
= dbname
;
382 if (analyze_in_stages
)
386 for (stage
= 0; stage
< ANALYZE_NUM_STAGES
; stage
++)
388 vacuum_one_database(&cparams
, &vacopts
,
392 progname
, echo
, quiet
);
396 vacuum_one_database(&cparams
, &vacopts
,
400 progname
, echo
, quiet
);
407 * vacuum_one_database
409 * Process tables in the given database. If the 'tables' list is empty,
410 * process all tables in the database.
412 * Note that this function is only concerned with running exactly one stage
413 * when in analyze-in-stages mode; caller must iterate on us if necessary.
415 * If concurrentCons is > 1, multiple connections are used to vacuum tables
416 * in parallel. In this case and if the table list is empty, we first obtain
417 * a list of tables from the database.
420 vacuum_one_database(ConnParams
*cparams
,
421 vacuumingOptions
*vacopts
,
423 SimpleStringList
*tables
,
425 const char *progname
, bool echo
, bool quiet
)
429 PQExpBufferData catalog_query
;
432 SimpleStringListCell
*cell
;
433 ParallelSlotArray
*sa
;
434 SimpleStringList dbtables
= {NULL
, NULL
};
438 bool tables_listed
= false;
439 bool has_where
= false;
441 const char *stage_commands
[] = {
442 "SET default_statistics_target=1; SET vacuum_cost_delay=0;",
443 "SET default_statistics_target=10; RESET vacuum_cost_delay;",
444 "RESET default_statistics_target;"
446 const char *stage_messages
[] = {
447 gettext_noop("Generating minimal optimizer statistics (1 target)"),
448 gettext_noop("Generating medium optimizer statistics (10 targets)"),
449 gettext_noop("Generating default (full) optimizer statistics")
452 Assert(stage
== ANALYZE_NO_STAGE
||
453 (stage
>= 0 && stage
< ANALYZE_NUM_STAGES
));
455 conn
= connectDatabase(cparams
, progname
, echo
, false, true);
457 if (vacopts
->disable_page_skipping
&& PQserverVersion(conn
) < 90600)
460 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
461 "disable-page-skipping", "9.6");
465 if (vacopts
->no_index_cleanup
&& PQserverVersion(conn
) < 120000)
468 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
469 "no-index-cleanup", "12");
473 if (vacopts
->force_index_cleanup
&& PQserverVersion(conn
) < 120000)
476 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
477 "force-index-cleanup", "12");
481 if (!vacopts
->do_truncate
&& PQserverVersion(conn
) < 120000)
484 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
485 "no-truncate", "12");
489 if (!vacopts
->process_toast
&& PQserverVersion(conn
) < 140000)
492 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
493 "no-process-toast", "14");
497 if (vacopts
->skip_locked
&& PQserverVersion(conn
) < 120000)
500 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
501 "skip-locked", "12");
505 if (vacopts
->min_xid_age
!= 0 && PQserverVersion(conn
) < 90600)
507 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
508 "--min-xid-age", "9.6");
512 if (vacopts
->min_mxid_age
!= 0 && PQserverVersion(conn
) < 90600)
514 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
515 "--min-mxid-age", "9.6");
519 if (vacopts
->parallel_workers
>= 0 && PQserverVersion(conn
) < 130000)
521 pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
528 if (stage
!= ANALYZE_NO_STAGE
)
529 printf(_("%s: processing database \"%s\": %s\n"),
530 progname
, PQdb(conn
), _(stage_messages
[stage
]));
532 printf(_("%s: vacuuming database \"%s\"\n"),
533 progname
, PQdb(conn
));
538 * Prepare the list of tables to process by querying the catalogs.
540 * Since we execute the constructed query with the default search_path
541 * (which could be unsafe), everything in this query MUST be fully
544 * First, build a WITH clause for the catalog query if any tables were
545 * specified, with a set of values made of relation names and their
546 * optional set of columns. This is used to match any provided column
547 * lists with the generated qualified identifiers and to filter for the
548 * tables provided via --table. If a listed table does not exist, the
549 * catalog query will fail.
551 initPQExpBuffer(&catalog_query
);
552 for (cell
= tables
? tables
->head
: NULL
; cell
; cell
= cell
->next
)
555 const char *just_columns
;
558 * Split relation and column names given by the user, this is used to
559 * feed the CTE with values on which are performed pre-run validity
560 * checks as well. For now these happen only on the relation name.
562 splitTableColumnsSpec(cell
->val
, PQclientEncoding(conn
),
563 &just_table
, &just_columns
);
567 appendPQExpBufferStr(&catalog_query
,
568 "WITH listed_tables (table_oid, column_list) "
570 tables_listed
= true;
573 appendPQExpBufferStr(&catalog_query
, ",\n (");
575 appendStringLiteralConn(&catalog_query
, just_table
, conn
);
576 appendPQExpBufferStr(&catalog_query
, "::pg_catalog.regclass, ");
578 if (just_columns
&& just_columns
[0] != '\0')
579 appendStringLiteralConn(&catalog_query
, just_columns
, conn
);
581 appendPQExpBufferStr(&catalog_query
, "NULL");
583 appendPQExpBufferStr(&catalog_query
, "::pg_catalog.text)");
588 /* Finish formatting the CTE */
590 appendPQExpBufferStr(&catalog_query
, "\n)\n");
592 appendPQExpBufferStr(&catalog_query
, "SELECT c.relname, ns.nspname");
595 appendPQExpBufferStr(&catalog_query
, ", listed_tables.column_list");
597 appendPQExpBufferStr(&catalog_query
,
598 " FROM pg_catalog.pg_class c\n"
599 " JOIN pg_catalog.pg_namespace ns"
600 " ON c.relnamespace OPERATOR(pg_catalog.=) ns.oid\n"
601 " LEFT JOIN pg_catalog.pg_class t"
602 " ON c.reltoastrelid OPERATOR(pg_catalog.=) t.oid\n");
604 /* Used to match the tables listed by the user */
606 appendPQExpBufferStr(&catalog_query
, " JOIN listed_tables"
607 " ON listed_tables.table_oid OPERATOR(pg_catalog.=) c.oid\n");
610 * If no tables were listed, filter for the relevant relation types. If
611 * tables were given via --table, don't bother filtering by relation type.
612 * Instead, let the server decide whether a given relation can be
613 * processed in which case the user will know about it.
617 appendPQExpBufferStr(&catalog_query
, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array["
618 CppAsString2(RELKIND_RELATION
) ", "
619 CppAsString2(RELKIND_MATVIEW
) "])\n");
624 * For --min-xid-age and --min-mxid-age, the age of the relation is the
625 * greatest of the ages of the main relation and its associated TOAST
626 * table. The commands generated by vacuumdb will also process the TOAST
627 * table for the relation if necessary, so it does not need to be
628 * considered separately.
630 if (vacopts
->min_xid_age
!= 0)
632 appendPQExpBuffer(&catalog_query
,
633 " %s GREATEST(pg_catalog.age(c.relfrozenxid),"
634 " pg_catalog.age(t.relfrozenxid)) "
635 " OPERATOR(pg_catalog.>=) '%d'::pg_catalog.int4\n"
636 " AND c.relfrozenxid OPERATOR(pg_catalog.!=)"
637 " '0'::pg_catalog.xid\n",
638 has_where
? "AND" : "WHERE", vacopts
->min_xid_age
);
642 if (vacopts
->min_mxid_age
!= 0)
644 appendPQExpBuffer(&catalog_query
,
645 " %s GREATEST(pg_catalog.mxid_age(c.relminmxid),"
646 " pg_catalog.mxid_age(t.relminmxid)) OPERATOR(pg_catalog.>=)"
647 " '%d'::pg_catalog.int4\n"
648 " AND c.relminmxid OPERATOR(pg_catalog.!=)"
649 " '0'::pg_catalog.xid\n",
650 has_where
? "AND" : "WHERE", vacopts
->min_mxid_age
);
655 * Execute the catalog query. We use the default search_path for this
656 * query for consistency with table lookups done elsewhere by the user.
658 appendPQExpBufferStr(&catalog_query
, " ORDER BY c.relpages DESC;");
659 executeCommand(conn
, "RESET search_path;", echo
);
660 res
= executeQuery(conn
, catalog_query
.data
, echo
);
661 termPQExpBuffer(&catalog_query
);
662 PQclear(executeQuery(conn
, ALWAYS_SECURE_SEARCH_PATH_SQL
, echo
));
665 * If no rows are returned, there are no matching tables, so we are done.
667 ntups
= PQntuples(res
);
676 * Build qualified identifiers for each table, including the column list
679 initPQExpBuffer(&buf
);
680 for (i
= 0; i
< ntups
; i
++)
682 appendPQExpBufferStr(&buf
,
683 fmtQualifiedId(PQgetvalue(res
, i
, 1),
684 PQgetvalue(res
, i
, 0)));
686 if (tables_listed
&& !PQgetisnull(res
, i
, 2))
687 appendPQExpBufferStr(&buf
, PQgetvalue(res
, i
, 2));
689 simple_string_list_append(&dbtables
, buf
.data
);
690 resetPQExpBuffer(&buf
);
692 termPQExpBuffer(&buf
);
696 * Ensure concurrentCons is sane. If there are more connections than
697 * vacuumable relations, we don't need to use them all.
699 if (concurrentCons
> ntups
)
700 concurrentCons
= ntups
;
701 if (concurrentCons
<= 0)
705 * All slots need to be prepared to run the appropriate analyze stage, if
706 * caller requested that mode. We have to prepare the initial connection
707 * ourselves before setting up the slots.
709 if (stage
== ANALYZE_NO_STAGE
)
713 initcmd
= stage_commands
[stage
];
714 executeCommand(conn
, initcmd
, echo
);
718 * Setup the database connections. We reuse the connection we already have
719 * for the first slot. If not in parallel mode, the first slot in the
720 * array contains the connection.
722 sa
= ParallelSlotsSetup(concurrentCons
, cparams
, progname
, echo
, initcmd
);
723 ParallelSlotsAdoptConn(sa
, conn
);
725 initPQExpBuffer(&sql
);
727 cell
= dbtables
.head
;
730 const char *tabname
= cell
->val
;
731 ParallelSlot
*free_slot
;
739 free_slot
= ParallelSlotsGetIdle(sa
, NULL
);
746 prepare_vacuum_command(&sql
, PQserverVersion(free_slot
->connection
),
750 * Execute the vacuum. All errors are handled in processQueryResult
751 * through ParallelSlotsGetIdle.
753 ParallelSlotSetHandler(free_slot
, TableCommandResultHandler
, NULL
);
754 run_vacuum_command(free_slot
->connection
, sql
.data
,
758 } while (cell
!= NULL
);
760 if (!ParallelSlotsWaitCompletion(sa
))
764 ParallelSlotsTerminate(sa
);
767 termPQExpBuffer(&sql
);
774 * Vacuum/analyze all connectable databases.
776 * In analyze-in-stages mode, we process all databases in one stage before
777 * moving on to the next stage. That ensure minimal stats are available
778 * quickly everywhere before generating more detailed ones.
781 vacuum_all_databases(ConnParams
*cparams
,
782 vacuumingOptions
*vacopts
,
783 bool analyze_in_stages
,
785 const char *progname
, bool echo
, bool quiet
)
792 conn
= connectMaintenanceDatabase(cparams
, progname
, echo
);
793 result
= executeQuery(conn
,
794 "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
798 if (analyze_in_stages
)
801 * When analyzing all databases in stages, we analyze them all in the
802 * fastest stage first, so that initial statistics become available
803 * for all of them as soon as possible.
805 * This means we establish several times as many connections, but
806 * that's a secondary consideration.
808 for (stage
= 0; stage
< ANALYZE_NUM_STAGES
; stage
++)
810 for (i
= 0; i
< PQntuples(result
); i
++)
812 cparams
->override_dbname
= PQgetvalue(result
, i
, 0);
814 vacuum_one_database(cparams
, vacopts
,
818 progname
, echo
, quiet
);
824 for (i
= 0; i
< PQntuples(result
); i
++)
826 cparams
->override_dbname
= PQgetvalue(result
, i
, 0);
828 vacuum_one_database(cparams
, vacopts
,
832 progname
, echo
, quiet
);
840 * Construct a vacuum/analyze command to run based on the given options, in the
841 * given string buffer, which may contain previous garbage.
843 * The table name used must be already properly quoted. The command generated
844 * depends on the server version involved and it is semicolon-terminated.
847 prepare_vacuum_command(PQExpBuffer sql
, int serverVersion
,
848 vacuumingOptions
*vacopts
, const char *table
)
850 const char *paren
= " (";
851 const char *comma
= ", ";
852 const char *sep
= paren
;
854 resetPQExpBuffer(sql
);
856 if (vacopts
->analyze_only
)
858 appendPQExpBufferStr(sql
, "ANALYZE");
860 /* parenthesized grammar of ANALYZE is supported since v11 */
861 if (serverVersion
>= 110000)
863 if (vacopts
->skip_locked
)
865 /* SKIP_LOCKED is supported since v12 */
866 Assert(serverVersion
>= 120000);
867 appendPQExpBuffer(sql
, "%sSKIP_LOCKED", sep
);
870 if (vacopts
->verbose
)
872 appendPQExpBuffer(sql
, "%sVERBOSE", sep
);
876 appendPQExpBufferChar(sql
, ')');
880 if (vacopts
->verbose
)
881 appendPQExpBufferStr(sql
, " VERBOSE");
886 appendPQExpBufferStr(sql
, "VACUUM");
888 /* parenthesized grammar of VACUUM is supported since v9.0 */
889 if (serverVersion
>= 90000)
891 if (vacopts
->disable_page_skipping
)
893 /* DISABLE_PAGE_SKIPPING is supported since v9.6 */
894 Assert(serverVersion
>= 90600);
895 appendPQExpBuffer(sql
, "%sDISABLE_PAGE_SKIPPING", sep
);
898 if (vacopts
->no_index_cleanup
)
900 /* "INDEX_CLEANUP FALSE" has been supported since v12 */
901 Assert(serverVersion
>= 120000);
902 Assert(!vacopts
->force_index_cleanup
);
903 appendPQExpBuffer(sql
, "%sINDEX_CLEANUP FALSE", sep
);
906 if (vacopts
->force_index_cleanup
)
908 /* "INDEX_CLEANUP TRUE" has been supported since v12 */
909 Assert(serverVersion
>= 120000);
910 Assert(!vacopts
->no_index_cleanup
);
911 appendPQExpBuffer(sql
, "%sINDEX_CLEANUP TRUE", sep
);
914 if (!vacopts
->do_truncate
)
916 /* TRUNCATE is supported since v12 */
917 Assert(serverVersion
>= 120000);
918 appendPQExpBuffer(sql
, "%sTRUNCATE FALSE", sep
);
921 if (!vacopts
->process_toast
)
923 /* PROCESS_TOAST is supported since v14 */
924 Assert(serverVersion
>= 140000);
925 appendPQExpBuffer(sql
, "%sPROCESS_TOAST FALSE", sep
);
928 if (vacopts
->skip_locked
)
930 /* SKIP_LOCKED is supported since v12 */
931 Assert(serverVersion
>= 120000);
932 appendPQExpBuffer(sql
, "%sSKIP_LOCKED", sep
);
937 appendPQExpBuffer(sql
, "%sFULL", sep
);
942 appendPQExpBuffer(sql
, "%sFREEZE", sep
);
945 if (vacopts
->verbose
)
947 appendPQExpBuffer(sql
, "%sVERBOSE", sep
);
950 if (vacopts
->and_analyze
)
952 appendPQExpBuffer(sql
, "%sANALYZE", sep
);
955 if (vacopts
->parallel_workers
>= 0)
957 /* PARALLEL is supported since v13 */
958 Assert(serverVersion
>= 130000);
959 appendPQExpBuffer(sql
, "%sPARALLEL %d", sep
,
960 vacopts
->parallel_workers
);
964 appendPQExpBufferChar(sql
, ')');
969 appendPQExpBufferStr(sql
, " FULL");
971 appendPQExpBufferStr(sql
, " FREEZE");
972 if (vacopts
->verbose
)
973 appendPQExpBufferStr(sql
, " VERBOSE");
974 if (vacopts
->and_analyze
)
975 appendPQExpBufferStr(sql
, " ANALYZE");
979 appendPQExpBuffer(sql
, " %s;", table
);
983 * Send a vacuum/analyze command to the server, returning after sending the
986 * Any errors during command execution are reported to stderr.
989 run_vacuum_command(PGconn
*conn
, const char *sql
, bool echo
,
997 status
= PQsendQuery(conn
, sql
) == 1;
1002 pg_log_error("vacuuming of table \"%s\" in database \"%s\" failed: %s",
1003 table
, PQdb(conn
), PQerrorMessage(conn
));
1005 pg_log_error("vacuuming of database \"%s\" failed: %s",
1006 PQdb(conn
), PQerrorMessage(conn
));
1011 help(const char *progname
)
1013 printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname
);
1014 printf(_("Usage:\n"));
1015 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
1016 printf(_("\nOptions:\n"));
1017 printf(_(" -a, --all vacuum all databases\n"));
1018 printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
1019 printf(_(" --disable-page-skipping disable all page-skipping behavior\n"));
1020 printf(_(" -e, --echo show the commands being sent to the server\n"));
1021 printf(_(" -f, --full do full vacuuming\n"));
1022 printf(_(" -F, --freeze freeze row transaction information\n"));
1023 printf(_(" --force-index-cleanup always remove index entries that point to dead tuples\n"));
1024 printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n"));
1025 printf(_(" --min-mxid-age=MXID_AGE minimum multixact ID age of tables to vacuum\n"));
1026 printf(_(" --min-xid-age=XID_AGE minimum transaction ID age of tables to vacuum\n"));
1027 printf(_(" --no-index-cleanup don't remove index entries that point to dead tuples\n"));
1028 printf(_(" --no-process-toast skip the TOAST table associated with the table to vacuum\n"));
1029 printf(_(" --no-truncate don't truncate empty pages at the end of the table\n"));
1030 printf(_(" -P, --parallel=PARALLEL_WORKERS use this many background workers for vacuum, if available\n"));
1031 printf(_(" -q, --quiet don't write any messages\n"));
1032 printf(_(" --skip-locked skip relations that cannot be immediately locked\n"));
1033 printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n"));
1034 printf(_(" -v, --verbose write a lot of output\n"));
1035 printf(_(" -V, --version output version information, then exit\n"));
1036 printf(_(" -z, --analyze update optimizer statistics\n"));
1037 printf(_(" -Z, --analyze-only only update optimizer statistics; no vacuum\n"));
1038 printf(_(" --analyze-in-stages only update optimizer statistics, in multiple\n"
1039 " stages for faster results; no vacuum\n"));
1040 printf(_(" -?, --help show this help, then exit\n"));
1041 printf(_("\nConnection options:\n"));
1042 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1043 printf(_(" -p, --port=PORT database server port\n"));
1044 printf(_(" -U, --username=USERNAME user name to connect as\n"));
1045 printf(_(" -w, --no-password never prompt for password\n"));
1046 printf(_(" -W, --password force password prompt\n"));
1047 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
1048 printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
1049 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
1050 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);