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 appendPQExpBufferStr(&buf,
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)
419 * Setup the database connections. We reuse the connection we already have
420 * for the first slot. If not in parallel mode, the first slot in the
421 * array contains the connection.
423 slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * concurrentCons);
424 init_slot(slots, conn);
427 for (i = 1; i < concurrentCons; i++)
429 conn = connectDatabase(dbname, host, port, username, prompt_password,
431 init_slot(slots + i, conn);
436 * Prepare all the connections to run the appropriate analyze stage, if
437 * caller requested that mode.
439 if (stage != ANALYZE_NO_STAGE)
443 /* We already emitted the message above */
445 for (j = 0; j < concurrentCons; j++)
446 executeCommand((slots + j)->connection,
447 stage_commands[stage], progname, echo);
450 cell = tables ? tables->head : NULL;
453 ParallelSlot *free_slot;
454 const char *tabname = cell ? cell->val : NULL;
456 prepare_vacuum_command(&sql, conn, vacopts, tabname);
465 * Get the connection slot to use. If in parallel mode, here we wait
466 * for one connection to become available if none already is. In
467 * non-parallel mode we simply use the only slot we have, which we
473 * Get a free slot, waiting until one becomes free if none
476 free_slot = GetIdleSlot(slots, concurrentCons, dbname, progname);
483 free_slot->isFree = false;
489 * Execute the vacuum. If not in parallel mode, this terminates the
490 * program in case of an error. (The parallel case handles query
491 * errors in GetQueryResult through GetIdleSlot.)
493 run_vacuum_command(free_slot->connection, sql.data,
494 echo, dbname, tabname, progname, parallel);
498 } while (cell != NULL);
504 for (j = 0; j < concurrentCons; j++)
506 /* wait for all connection to return the results */
507 if (!GetQueryResult((slots + j)->connection, dbname, progname))
510 (slots + j)->isFree = true;
515 for (i = 0; i < concurrentCons; i++)
516 DisconnectDatabase(slots + i);
519 termPQExpBuffer(&sql);
526 * Vacuum/analyze all connectable databases.
528 * In analyze-in-stages mode, we process all databases in one stage before
529 * moving on to the next stage. That ensure minimal stats are available
530 * quickly everywhere before generating more detailed ones.
533 vacuum_all_databases(vacuumingOptions *vacopts,
534 bool analyze_in_stages,
535 const char *maintenance_db, const char *host,
536 const char *port, const char *username,
537 enum trivalue prompt_password,
539 const char *progname, bool echo, bool quiet)
546 conn = connectMaintenanceDatabase(maintenance_db, host, port,
547 username, prompt_password, progname);
548 result = executeQuery(conn,
549 "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
553 if (analyze_in_stages)
556 * When analyzing all databases in stages, we analyze them all in the
557 * fastest stage first, so that initial statistics become available
558 * for all of them as soon as possible.
560 * This means we establish several times as many connections, but
561 * that's a secondary consideration.
563 for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
565 for (i = 0; i < PQntuples(result); i++)
569 dbname = PQgetvalue(result, i, 0);
570 vacuum_one_database(dbname, vacopts,
573 host, port, username, prompt_password,
575 progname, echo, quiet);
581 for (i = 0; i < PQntuples(result); i++)
585 dbname = PQgetvalue(result, i, 0);
586 vacuum_one_database(dbname, vacopts,
589 host, port, username, prompt_password,
591 progname, echo, quiet);
599 * Construct a vacuum/analyze command to run based on the given options, in the
600 * given string buffer, which may contain previous garbage.
602 * An optional table name can be passed; this must be already be properly
603 * quoted. The command is semicolon-terminated.
606 prepare_vacuum_command(PQExpBuffer sql, PGconn *conn, vacuumingOptions *vacopts,
609 resetPQExpBuffer(sql);
611 if (vacopts->analyze_only)
613 appendPQExpBufferStr(sql, "ANALYZE");
614 if (vacopts->verbose)
615 appendPQExpBufferStr(sql, " VERBOSE");
619 appendPQExpBufferStr(sql, "VACUUM");
620 if (PQserverVersion(conn) >= 90000)
622 const char *paren = " (";
623 const char *comma = ", ";
624 const char *sep = paren;
628 appendPQExpBuffer(sql, "%sFULL", sep);
633 appendPQExpBuffer(sql, "%sFREEZE", sep);
636 if (vacopts->verbose)
638 appendPQExpBuffer(sql, "%sVERBOSE", sep);
641 if (vacopts->and_analyze)
643 appendPQExpBuffer(sql, "%sANALYZE", sep);
647 appendPQExpBufferChar(sql, ')');
652 appendPQExpBufferStr(sql, " FULL");
654 appendPQExpBufferStr(sql, " FREEZE");
655 if (vacopts->verbose)
656 appendPQExpBufferStr(sql, " VERBOSE");
657 if (vacopts->and_analyze)
658 appendPQExpBufferStr(sql, " ANALYZE");
663 appendPQExpBuffer(sql, " %s", table);
664 appendPQExpBufferChar(sql, ';');
668 * Execute a vacuum/analyze command to the server.
670 * Any errors during command execution are reported to stderr. If async is
671 * false, this function exits the program after reporting the error.
674 run_vacuum_command(PGconn *conn, const char *sql, bool echo,
675 const char *dbname, const char *table,
676 const char *progname, bool async)
685 status = PQsendQuery(conn, sql) == 1;
688 status = executeMaintenanceCommand(conn, sql, echo);
694 _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
695 progname, table, dbname, PQerrorMessage(conn));
697 fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
698 progname, dbname, PQerrorMessage(conn));
710 * Return a connection slot that is ready to execute a command.
712 * We return the first slot we find that is marked isFree, if one is;
713 * otherwise, we loop on select() until one socket becomes available. When
714 * this happens, we read the whole set and mark as free all sockets that become
717 * Process the slot list, if any free slot is available then return the slotid
718 * else perform the select on all the socket's and wait until at least one slot
721 * If an error occurs, NULL is returned.
723 static ParallelSlot *
724 GetIdleSlot(ParallelSlot slots[], int numslots, const char *dbname,
725 const char *progname)
732 for (i = 0; i < numslots; i++)
733 if ((slots + i)->isFree)
739 for (i = 0; i < numslots; i++)
741 FD_SET((slots + i)->sock, &slotset);
742 if ((slots + i)->sock > maxFd)
743 maxFd = (slots + i)->sock;
747 * No free slot found, so wait until one of the connections has finished
748 * its task and return the available slot.
750 for (firstFree = -1; firstFree < 0;)
754 SetCancelConn(slots->connection);
755 i = select_loop(maxFd, &slotset, &aborting);
761 * We set the cancel-receiving connection to the one in the zeroth
762 * slot above, so fetch the error from there.
764 GetQueryResult(slots->connection, dbname, progname);
769 for (i = 0; i < numslots; i++)
771 if (!FD_ISSET((slots + i)->sock, &slotset))
774 PQconsumeInput((slots + i)->connection);
775 if (PQisBusy((slots + i)->connection))
778 (slots + i)->isFree = true;
780 if (!GetQueryResult((slots + i)->connection, dbname, progname))
788 return slots + firstFree;
794 * Process the query result. Returns true if there's no error, false
795 * otherwise -- but errors about trying to vacuum a missing relation are
796 * reported and subsequently ignored.
799 GetQueryResult(PGconn *conn, const char *dbname, const char *progname)
804 while ((result = PQgetResult(conn)) != NULL)
807 * If errors are found, report them. Errors about a missing table are
808 * harmless so we continue processing; but die for other errors.
810 if (PQresultStatus(result) != PGRES_COMMAND_OK)
812 char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
814 fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
815 progname, dbname, PQerrorMessage(conn));
817 if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
833 * Disconnect the connection associated with the given slot
836 DisconnectDatabase(ParallelSlot *slot)
840 if (!slot->connection)
843 if (PQtransactionStatus(slot->connection) == PQTRANS_ACTIVE)
847 if ((cancel = PQgetCancel(slot->connection)))
849 PQcancel(cancel, errbuf, sizeof(errbuf));
850 PQfreeCancel(cancel);
854 PQfinish(slot->connection);
855 slot->connection = NULL;
859 * Loop on select() until a descriptor from the given set becomes readable.
861 * If we get a cancel request while we're waiting, we forego all further
862 * processing and set the *aborting flag to true. The return value must be
863 * ignored in this case. Otherwise, *aborting is set to false.
866 select_loop(int maxFd, fd_set *workerset, bool *aborting)
869 fd_set saveSet = *workerset;
882 * On Windows, we need to check once in a while for cancel requests;
883 * on other platforms we rely on select() returning when interrupted.
887 struct timeval tv = {0, 1000000};
894 *workerset = saveSet;
895 i = select(maxFd + 1, workerset, NULL, NULL, tvp);
898 if (i == SOCKET_ERROR)
902 if (WSAGetLastError() == WSAEINTR)
907 if (i < 0 && errno == EINTR)
908 continue; /* ignore this */
909 if (i < 0 || CancelRequested)
910 *aborting = true; /* but not this */
912 continue; /* timeout (Win32 only) */
920 init_slot(ParallelSlot *slot, PGconn *conn)
922 slot->connection = conn;
924 slot->sock = PQsocket(conn);
928 help(const char *progname)
930 printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname);
931 printf(_("Usage:\n"));
932 printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
933 printf(_("\nOptions:\n"));
934 printf(_(" -a, --all vacuum all databases\n"));
935 printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
936 printf(_(" -e, --echo show the commands being sent to the server\n"));
937 printf(_(" -f, --full do full vacuuming\n"));
938 printf(_(" -F, --freeze freeze row transaction information\n"));
939 printf(_(" -q, --quiet don't write any messages\n"));
940 printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n"));
941 printf(_(" -v, --verbose write a lot of output\n"));
942 printf(_(" -V, --version output version information, then exit\n"));
943 printf(_(" -z, --analyze update optimizer statistics\n"));
944 printf(_(" -Z, --analyze-only only update optimizer statistics; no vacuum\n"));
945 printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n"));
946 printf(_(" --analyze-in-stages only update optimizer statistics, in multiple\n"
947 " stages for faster results; no vacuum\n"));
948 printf(_(" -?, --help show this help, then exit\n"));
949 printf(_("\nConnection options:\n"));
950 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
951 printf(_(" -p, --port=PORT database server port\n"));
952 printf(_(" -U, --username=USERNAME user name to connect as\n"));
953 printf(_(" -w, --no-password never prompt for password\n"));
954 printf(_(" -W, --password force password prompt\n"));
955 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
956 printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
957 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));