1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2015, 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"
16 #include "dumputils.h"
19 #define ERRCODE_UNDEFINED_TABLE "42P01"
21 /* Parallel vacuuming stuff */
22 typedef struct ParallelSlot
29 /* vacuum options controlled by user flags */
30 typedef struct vacuumingOptions
40 static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
42 SimpleStringList *tables,
43 const char *host, const char *port,
44 const char *username, enum trivalue prompt_password,
46 const char *progname, bool echo, bool quiet);
48 static void vacuum_all_databases(vacuumingOptions *vacopts,
49 bool analyze_in_stages,
50 const char *maintenance_db,
51 const char *host, const char *port,
52 const char *username, enum trivalue prompt_password,
54 const char *progname, bool echo, bool quiet);
56 static void prepare_vacuum_command(PQExpBuffer sql, PGconn *conn,
57 vacuumingOptions *vacopts, const char *table);
59 static void run_vacuum_command(PGconn *conn, const char *sql, bool echo,
60 const char *dbname, const char *table,
61 const char *progname, bool async);
63 static ParallelSlot *GetIdleSlot(ParallelSlot slots[], int numslots,
64 const char *dbname, const char *progname);
66 static bool GetQueryResult(PGconn *conn, const char *dbname,
67 const char *progname);
69 static void DisconnectDatabase(ParallelSlot *slot);
71 static int select_loop(int maxFd, fd_set *workerset, bool *aborting);
73 static void init_slot(ParallelSlot *slot, PGconn *conn);
75 static void help(const char *progname);
77 /* For analyze-in-stages mode */
78 #define ANALYZE_NO_STAGE -1
79 #define ANALYZE_NUM_STAGES 3
83 main(int argc, char *argv[])
85 static struct option long_options[] = {
86 {"host", required_argument, NULL, 'h'},
87 {"port", required_argument, NULL, 'p'},
88 {"username", required_argument, NULL, 'U'},
89 {"no-password", no_argument, NULL, 'w'},
90 {"password", no_argument, NULL, 'W'},
91 {"echo", no_argument, NULL, 'e'},
92 {"quiet", no_argument, NULL, 'q'},
93 {"dbname", required_argument, NULL, 'd'},
94 {"analyze", no_argument, NULL, 'z'},
95 {"analyze-only", no_argument, NULL, 'Z'},
96 {"freeze", no_argument, NULL, 'F'},
97 {"all", no_argument, NULL, 'a'},
98 {"table", required_argument, NULL, 't'},
99 {"full", no_argument, NULL, 'f'},
100 {"verbose", no_argument, NULL, 'v'},
101 {"jobs", required_argument, NULL, 'j'},
102 {"maintenance-db", required_argument, NULL, 2},
103 {"analyze-in-stages", no_argument, NULL, 3},
107 const char *progname;
110 const char *dbname = NULL;
111 const char *maintenance_db = NULL;
114 char *username = NULL;
115 enum trivalue prompt_password = TRI_DEFAULT;
118 vacuumingOptions vacopts;
119 bool analyze_in_stages = false;
121 SimpleStringList tables = {NULL, NULL};
122 int concurrentCons = 1;
125 /* initialize options to all false */
126 memset(&vacopts, 0, sizeof(vacopts));
128 progname = get_progname(argv[0]);
130 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
132 handle_help_version_opts(argc, argv, "vacuumdb", help);
134 while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:", long_options, &optindex)) != -1)
139 host = pg_strdup(optarg);
142 port = pg_strdup(optarg);
145 username = pg_strdup(optarg);
148 prompt_password = TRI_NO;
151 prompt_password = TRI_YES;
160 dbname = pg_strdup(optarg);
163 vacopts.and_analyze = true;
166 vacopts.analyze_only = true;
169 vacopts.freeze = true;
176 simple_string_list_append(&tables, optarg);
184 vacopts.verbose = true;
187 concurrentCons = atoi(optarg);
188 if (concurrentCons <= 0)
190 fprintf(stderr, _("%s: number of parallel \"jobs\" must be at least 1\n"),
194 if (concurrentCons > FD_SETSIZE - 1)
196 fprintf(stderr, _("%s: too many parallel jobs requested (maximum: %d)\n"),
197 progname, FD_SETSIZE - 1);
202 maintenance_db = pg_strdup(optarg);
205 analyze_in_stages = vacopts.analyze_only = true;
208 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
214 * Non-option argument specifies database name as long as it wasn't
215 * already specified with -d / --dbname
217 if (optind < argc && dbname == NULL)
219 dbname = argv[optind];
225 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
226 progname, argv[optind]);
227 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
231 if (vacopts.analyze_only)
235 fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"),
241 fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"),
245 /* allow 'and_analyze' with 'analyze_only' */
248 setup_cancel_handler();
250 /* Avoid opening extra connections. */
251 if (tbl_count && (concurrentCons > tbl_count))
252 concurrentCons = tbl_count;
258 fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
262 if (tables.head != NULL)
264 fprintf(stderr, _("%s: cannot vacuum specific table(s) in all databases\n"),
269 vacuum_all_databases(&vacopts,
272 host, port, username, prompt_password,
274 progname, echo, quiet);
280 if (getenv("PGDATABASE"))
281 dbname = getenv("PGDATABASE");
282 else if (getenv("PGUSER"))
283 dbname = getenv("PGUSER");
285 dbname = get_user_name_or_exit(progname);
288 if (analyze_in_stages)
292 for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
294 vacuum_one_database(dbname, &vacopts,
297 host, port, username, prompt_password,
299 progname, echo, quiet);
303 vacuum_one_database(dbname, &vacopts,
306 host, port, username, prompt_password,
308 progname, echo, quiet);
315 * vacuum_one_database
317 * Process tables in the given database. If the 'tables' list is empty,
318 * process all tables in the database.
320 * Note that this function is only concerned with running exactly one stage
321 * when in analyze-in-stages mode; caller must iterate on us if necessary.
323 * If concurrentCons is > 1, multiple connections are used to vacuum tables
324 * in parallel. In this case and if the table list is empty, we first obtain
325 * a list of tables from the database.
328 vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
330 SimpleStringList *tables,
331 const char *host, const char *port,
332 const char *username, enum trivalue prompt_password,
334 const char *progname, bool echo, bool quiet)
338 SimpleStringListCell *cell;
339 ParallelSlot *slots = NULL;
340 SimpleStringList dbtables = {NULL, NULL};
343 bool parallel = concurrentCons > 1;
344 const char *stage_commands[] = {
345 "SET default_statistics_target=1; SET vacuum_cost_delay=0;",
346 "SET default_statistics_target=10; RESET vacuum_cost_delay;",
347 "RESET default_statistics_target;"
349 const char *stage_messages[] = {
350 gettext_noop("Generating minimal optimizer statistics (1 target)"),
351 gettext_noop("Generating medium optimizer statistics (10 targets)"),
352 gettext_noop("Generating default (full) optimizer statistics")
355 Assert(stage == ANALYZE_NO_STAGE ||
356 (stage >= 0 && stage < ANALYZE_NUM_STAGES));
360 if (stage != ANALYZE_NO_STAGE)
361 printf(_("%s: processing database \"%s\": %s\n"), progname, dbname,
362 stage_messages[stage]);
364 printf(_("%s: vacuuming database \"%s\"\n"), progname, dbname);
368 conn = connectDatabase(dbname, host, port, username, prompt_password,
371 initPQExpBuffer(&sql);
374 * If a table list is not provided and we're using multiple connections,
375 * prepare the list of tables by querying the catalogs.
377 if (parallel && (!tables || !tables->head))
384 initPQExpBuffer(&buf);
386 res = executeQuery(conn,
387 "SELECT c.relname, ns.nspname FROM pg_class c, pg_namespace ns\n"
388 " WHERE relkind IN (\'r\', \'m\') AND c.relnamespace = ns.oid\n"
389 " ORDER BY c.relpages DESC;",
392 ntups = PQntuples(res);
393 for (i = 0; i < ntups; i++)
395 appendPQExpBuffer(&buf, "%s",
396 fmtQualifiedId(PQserverVersion(conn),
397 PQgetvalue(res, i, 1),
398 PQgetvalue(res, i, 0)));
400 simple_string_list_append(&dbtables, buf.data);
401 resetPQExpBuffer(&buf);
404 termPQExpBuffer(&buf);
408 * If there are more connections than vacuumable relations, we don't
409 * need to use them all.
411 if (concurrentCons > ntups)
412 concurrentCons = ntups;
413 if (concurrentCons <= 1)
418 * Setup the database connections. We reuse the connection we already have
419 * for the first slot. If not in parallel mode, the first slot in the
420 * array contains the connection.
422 slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * concurrentCons);
423 init_slot(slots, conn);
426 for (i = 1; i < concurrentCons; i++)
428 conn = connectDatabase(dbname, host, port, username, prompt_password,
430 init_slot(slots + i, conn);
435 * Prepare all the connections to run the appropriate analyze stage, if
436 * caller requested that mode.
438 if (stage != ANALYZE_NO_STAGE)
442 /* We already emitted the message above */
444 for (j = 0; j < concurrentCons; j++)
445 executeCommand((slots + j)->connection,
446 stage_commands[stage], progname, echo);
449 cell = tables ? tables->head : NULL;
452 ParallelSlot *free_slot;
453 const char *tabname = cell ? cell->val : NULL;
455 prepare_vacuum_command(&sql, conn, vacopts, tabname);
464 * Get the connection slot to use. If in parallel mode, here we wait
465 * for one connection to become available if none already is. In
466 * non-parallel mode we simply use the only slot we have, which we
472 * Get a free slot, waiting until one becomes free if none
475 free_slot = GetIdleSlot(slots, concurrentCons, dbname, progname);
482 free_slot->isFree = false;
487 run_vacuum_command(free_slot->connection, sql.data,
488 echo, dbname, tabname, progname, parallel);
492 } while (cell != NULL);
498 for (j = 0; j < concurrentCons; j++)
500 /* wait for all connection to return the results */
501 if (!GetQueryResult((slots + j)->connection, dbname, progname))
504 (slots + j)->isFree = true;
509 for (i = 0; i < concurrentCons; i++)
510 DisconnectDatabase(slots + i);
513 termPQExpBuffer(&sql);
520 * Vacuum/analyze all connectable databases.
522 * In analyze-in-stages mode, we process all databases in one stage before
523 * moving on to the next stage. That ensure minimal stats are available
524 * quickly everywhere before generating more detailed ones.
527 vacuum_all_databases(vacuumingOptions *vacopts,
528 bool analyze_in_stages,
529 const char *maintenance_db, const char *host,
530 const char *port, const char *username,
531 enum trivalue prompt_password,
533 const char *progname, bool echo, bool quiet)
540 conn = connectMaintenanceDatabase(maintenance_db, host, port,
541 username, prompt_password, progname);
542 result = executeQuery(conn,
543 "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
547 if (analyze_in_stages)
550 * When analyzing all databases in stages, we analyze them all in the
551 * fastest stage first, so that initial statistics become available
552 * for all of them as soon as possible.
554 * This means we establish several times as many connections, but
555 * that's a secondary consideration.
557 for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
559 for (i = 0; i < PQntuples(result); i++)
563 dbname = PQgetvalue(result, i, 0);
564 vacuum_one_database(dbname, vacopts,
567 host, port, username, prompt_password,
569 progname, echo, quiet);
575 for (i = 0; i < PQntuples(result); i++)
579 dbname = PQgetvalue(result, i, 0);
580 vacuum_one_database(dbname, vacopts,
583 host, port, username, prompt_password,
585 progname, echo, quiet);
593 * Construct a vacuum/analyze command to run based on the given options, in the
594 * given string buffer, which may contain previous garbage.
596 * An optional table name can be passed; this must be already be properly
597 * quoted. The command is semicolon-terminated.
600 prepare_vacuum_command(PQExpBuffer sql, PGconn *conn, vacuumingOptions *vacopts,
603 resetPQExpBuffer(sql);
605 if (vacopts->analyze_only)
607 appendPQExpBufferStr(sql, "ANALYZE");
608 if (vacopts->verbose)
609 appendPQExpBufferStr(sql, " VERBOSE");
613 appendPQExpBufferStr(sql, "VACUUM");
614 if (PQserverVersion(conn) >= 90000)
616 const char *paren = " (";
617 const char *comma = ", ";
618 const char *sep = paren;
622 appendPQExpBuffer(sql, "%sFULL", sep);
627 appendPQExpBuffer(sql, "%sFREEZE", sep);
630 if (vacopts->verbose)
632 appendPQExpBuffer(sql, "%sVERBOSE", sep);
635 if (vacopts->and_analyze)
637 appendPQExpBuffer(sql, "%sANALYZE", sep);
641 appendPQExpBufferStr(sql, ")");
646 appendPQExpBufferStr(sql, " FULL");
648 appendPQExpBufferStr(sql, " FREEZE");
649 if (vacopts->verbose)
650 appendPQExpBufferStr(sql, " VERBOSE");
651 if (vacopts->and_analyze)
652 appendPQExpBufferStr(sql, " ANALYZE");
657 appendPQExpBuffer(sql, " %s", table);
658 appendPQExpBufferChar(sql, ';');
662 * Execute a vacuum/analyze command to the server.
664 * Result status is checked only if 'async' is false.
667 run_vacuum_command(PGconn *conn, const char *sql, bool echo,
668 const char *dbname, const char *table,
669 const char *progname, bool async)
676 PQsendQuery(conn, sql);
678 else if (!executeMaintenanceCommand(conn, sql, echo))
682 _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
683 progname, table, dbname, PQerrorMessage(conn));
685 fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
686 progname, dbname, PQerrorMessage(conn));
694 * Return a connection slot that is ready to execute a command.
696 * We return the first slot we find that is marked isFree, if one is;
697 * otherwise, we loop on select() until one socket becomes available. When
698 * this happens, we read the whole set and mark as free all sockets that become
701 * Process the slot list, if any free slot is available then return the slotid
702 * else perform the select on all the socket's and wait until at least one slot
705 * If an error occurs, NULL is returned.
707 static ParallelSlot *
708 GetIdleSlot(ParallelSlot slots[], int numslots, const char *dbname,
709 const char *progname)
716 for (i = 0; i < numslots; i++)
717 if ((slots + i)->isFree)
723 for (i = 0; i < numslots; i++)
725 FD_SET((slots + i)->sock, &slotset);
726 if ((slots + i)->sock > maxFd)
727 maxFd = (slots + i)->sock;
731 * No free slot found, so wait until one of the connections has finished
732 * its task and return the available slot.
734 for (firstFree = -1; firstFree < 0;)
738 SetCancelConn(slots->connection);
739 i = select_loop(maxFd, &slotset, &aborting);
745 * We set the cancel-receiving connection to the one in the zeroth
746 * slot above, so fetch the error from there.
748 GetQueryResult(slots->connection, dbname, progname);
753 for (i = 0; i < numslots; i++)
755 if (!FD_ISSET((slots + i)->sock, &slotset))
758 PQconsumeInput((slots + i)->connection);
759 if (PQisBusy((slots + i)->connection))
762 (slots + i)->isFree = true;
764 if (!GetQueryResult((slots + i)->connection, dbname, progname))
772 return slots + firstFree;
778 * Process the query result. Returns true if there's no error, false
779 * otherwise -- but errors about trying to vacuum a missing relation are
780 * reported and subsequently ignored.
783 GetQueryResult(PGconn *conn, const char *dbname, const char *progname)
788 while ((result = PQgetResult(conn)) != NULL)
791 * If errors are found, report them. Errors about a missing table are
792 * harmless so we continue processing; but die for other errors.
794 if (PQresultStatus(result) != PGRES_COMMAND_OK)
796 char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
798 fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
799 progname, dbname, PQerrorMessage(conn));
801 if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
817 * Disconnect the connection associated with the given slot
820 DisconnectDatabase(ParallelSlot *slot)
824 if (!slot->connection)
827 if (PQtransactionStatus(slot->connection) == PQTRANS_ACTIVE)
831 if ((cancel = PQgetCancel(slot->connection)))
833 PQcancel(cancel, errbuf, sizeof(errbuf));
834 PQfreeCancel(cancel);
838 PQfinish(slot->connection);
839 slot->connection = NULL;
843 * Loop on select() until a descriptor from the given set becomes readable.
845 * If we get a cancel request while we're waiting, we forego all further
846 * processing and set the *aborting flag to true. The return value must be
847 * ignored in this case. Otherwise, *aborting is set to false.
850 select_loop(int maxFd, fd_set *workerset, bool *aborting)
853 fd_set saveSet = *workerset;
866 * On Windows, we need to check once in a while for cancel requests;
867 * on other platforms we rely on select() returning when interrupted.
871 struct timeval tv = {0, 1000000};
878 *workerset = saveSet;
879 i = select(maxFd + 1, workerset, NULL, NULL, tvp);
882 if (i == SOCKET_ERROR)
886 if (WSAGetLastError() == WSAEINTR)
891 if (i < 0 && errno == EINTR)
892 continue; /* ignore this */
893 if (i < 0 || CancelRequested)
894 *aborting = true; /* but not this */
896 continue; /* timeout (Win32 only) */
904 init_slot(ParallelSlot *slot, PGconn *conn)
906 slot->connection = conn;
908 slot->sock = PQsocket(conn);
912 help(const char *progname)
914 printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname);
915 printf(_("Usage:\n"));
916 printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
917 printf(_("\nOptions:\n"));
918 printf(_(" -a, --all vacuum all databases\n"));
919 printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
920 printf(_(" -e, --echo show the commands being sent to the server\n"));
921 printf(_(" -f, --full do full vacuuming\n"));
922 printf(_(" -F, --freeze freeze row transaction information\n"));
923 printf(_(" -q, --quiet don't write any messages\n"));
924 printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n"));
925 printf(_(" -v, --verbose write a lot of output\n"));
926 printf(_(" -V, --version output version information, then exit\n"));
927 printf(_(" -z, --analyze update optimizer statistics\n"));
928 printf(_(" -Z, --analyze-only only update optimizer statistics\n"));
929 printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n"));
930 printf(_(" --analyze-in-stages only update optimizer statistics, in multiple\n"
931 " stages for faster results\n"));
932 printf(_(" -?, --help show this help, then exit\n"));
933 printf(_("\nConnection options:\n"));
934 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
935 printf(_(" -p, --port=PORT database server port\n"));
936 printf(_(" -U, --username=USERNAME user name to connect as\n"));
937 printf(_(" -w, --no-password never prompt for password\n"));
938 printf(_(" -W, --password force password prompt\n"));
939 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
940 printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
941 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));