static void vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_only, bool freeze, const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet) { PGconn *conn; PGresult *result; int i; conn = connectMaintenanceDatabase(maintenance_db, host, port, username, prompt_password, progname); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); for (i = 0; i < PQntuples(result); i++) { char *dbname = PQgetvalue(result, i, 0); if (!quiet) { printf(_("%s: vacuuming database \"%s\"\n"), progname, dbname); fflush(stdout); } vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only, freeze, NULL, host, port, username, prompt_password, progname, echo); } PQclear(result); }
static void vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_only, bool analyze_in_stages, bool freeze, const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet) { PGconn *conn; PGresult *result; int stage; conn = connectMaintenanceDatabase(maintenance_db, host, port, username, prompt_password, progname); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); /* If analyzing in stages, then run through all stages. Otherwise just * run once, passing -1 as the stage. */ for (stage = (analyze_in_stages ? 0 : -1); stage < (analyze_in_stages ? 3 : 0); stage++) { int i; for (i = 0; i < PQntuples(result); i++) { char *dbname = PQgetvalue(result, i, 0); if (!quiet) { printf(_("%s: vacuuming database \"%s\"\n"), progname, dbname); fflush(stdout); } vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only, analyze_in_stages, stage, freeze, NULL, host, port, username, prompt_password, progname, echo, quiet); } } PQclear(result); }
int main(int argc, char *argv[]) { static struct option long_options[] = { {"host", required_argument, NULL, 'h'}, {"port", required_argument, NULL, 'p'}, {"username", required_argument, NULL, 'U'}, {"no-password", no_argument, NULL, 'w'}, {"password", no_argument, NULL, 'W'}, {"echo", no_argument, NULL, 'e'}, {"quiet", no_argument, NULL, 'q'}, {"dbname", required_argument, NULL, 'd'}, {"analyze", no_argument, NULL, 'z'}, {"analyze-only", no_argument, NULL, 'Z'}, {"freeze", no_argument, NULL, 'F'}, {"all", no_argument, NULL, 'a'}, {"table", required_argument, NULL, 't'}, {"full", no_argument, NULL, 'f'}, {"verbose", no_argument, NULL, 'v'}, {"jobs", required_argument, NULL, 'j'}, {"maintenance-db", required_argument, NULL, 2}, {"analyze-in-stages", no_argument, NULL, 3}, {NULL, 0, NULL, 0} }; const char *progname; int optindex; int c; const char *dbname = NULL; const char *maintenance_db = NULL; char *host = NULL; char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; bool echo = false; bool quiet = false; vacuumingOptions vacopts; bool analyze_in_stages = false; bool alldb = false; SimpleStringList tables = {NULL, NULL}; int concurrentCons = 1; int tbl_count = 0; /* initialize options to all false */ memset(&vacopts, 0, sizeof(vacopts)); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); handle_help_version_opts(argc, argv, "vacuumdb", help); while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:", long_options, &optindex)) != -1) { switch (c) { case 'h': host = pg_strdup(optarg); break; case 'p': port = pg_strdup(optarg); break; case 'U': username = pg_strdup(optarg); break; case 'w': prompt_password = TRI_NO; break; case 'W': prompt_password = TRI_YES; break; case 'e': echo = true; break; case 'q': quiet = true; break; case 'd': dbname = pg_strdup(optarg); break; case 'z': vacopts.and_analyze = true; break; case 'Z': vacopts.analyze_only = true; break; case 'F': vacopts.freeze = true; break; case 'a': alldb = true; break; case 't': { simple_string_list_append(&tables, optarg); tbl_count++; break; } case 'f': vacopts.full = true; break; case 'v': vacopts.verbose = true; break; case 'j': concurrentCons = atoi(optarg); if (concurrentCons <= 0) { fprintf(stderr, _("%s: number of parallel \"jobs\" must be at least 1\n"), progname); exit(1); } if (concurrentCons > FD_SETSIZE - 1) { fprintf(stderr, _("%s: too many parallel jobs requested (maximum: %d)\n"), progname, FD_SETSIZE - 1); exit(1); } break; case 2: maintenance_db = pg_strdup(optarg); break; case 3: analyze_in_stages = vacopts.analyze_only = true; break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } } /* * Non-option argument specifies database name as long as it wasn't * already specified with -d / --dbname */ if (optind < argc && dbname == NULL) { dbname = argv[optind]; optind++; } if (optind < argc) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (vacopts.analyze_only) { if (vacopts.full) { fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"), progname, "full"); exit(1); } if (vacopts.freeze) { fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"), progname, "freeze"); exit(1); } /* allow 'and_analyze' with 'analyze_only' */ } setup_cancel_handler(); /* Avoid opening extra connections. */ if (tbl_count && (concurrentCons > tbl_count)) concurrentCons = tbl_count; if (alldb) { if (dbname) { fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"), progname); exit(1); } if (tables.head != NULL) { fprintf(stderr, _("%s: cannot vacuum specific table(s) in all databases\n"), progname); exit(1); } vacuum_all_databases(&vacopts, analyze_in_stages, maintenance_db, host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } else { if (dbname == NULL) { if (getenv("PGDATABASE")) dbname = getenv("PGDATABASE"); else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else dbname = get_user_name_or_exit(progname); } if (analyze_in_stages) { int stage; for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++) { vacuum_one_database(dbname, &vacopts, stage, &tables, host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } } else vacuum_one_database(dbname, &vacopts, ANALYZE_NO_STAGE, &tables, host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } exit(0); }
/* * Vacuum/analyze all connectable databases. * * In analyze-in-stages mode, we process all databases in one stage before * moving on to the next stage. That ensure minimal stats are available * quickly everywhere before generating more detailed ones. */ static void vacuum_all_databases(vacuumingOptions *vacopts, bool analyze_in_stages, const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet) { PGconn *conn; PGresult *result; int stage; int i; conn = connectMaintenanceDatabase(maintenance_db, host, port, username, prompt_password, progname); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); if (analyze_in_stages) { /* * When analyzing all databases in stages, we analyze them all in the * fastest stage first, so that initial statistics become available * for all of them as soon as possible. * * This means we establish several times as many connections, but * that's a secondary consideration. */ for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++) { for (i = 0; i < PQntuples(result); i++) { const char *dbname; dbname = PQgetvalue(result, i, 0); vacuum_one_database(dbname, vacopts, stage, NULL, host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } } } else { for (i = 0; i < PQntuples(result); i++) { const char *dbname; dbname = PQgetvalue(result, i, 0); vacuum_one_database(dbname, vacopts, ANALYZE_NO_STAGE, NULL, host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } } PQclear(result); }
int main(int argc, char *argv[]) { static struct option long_options[] = { {"host", required_argument, NULL, 'h'}, {"port", required_argument, NULL, 'p'}, {"username", required_argument, NULL, 'U'}, {"no-password", no_argument, NULL, 'w'}, {"password", no_argument, NULL, 'W'}, {"echo", no_argument, NULL, 'e'}, {"quiet", no_argument, NULL, 'q'}, {"dbname", required_argument, NULL, 'd'}, {"analyze", no_argument, NULL, 'z'}, {"analyze-only", no_argument, NULL, 'Z'}, {"freeze", no_argument, NULL, 'F'}, {"all", no_argument, NULL, 'a'}, {"table", required_argument, NULL, 't'}, {"full", no_argument, NULL, 'f'}, {"verbose", no_argument, NULL, 'v'}, {"maintenance-db", required_argument, NULL, 2}, {NULL, 0, NULL, 0} }; const char *progname; int optindex; int c; const char *dbname = NULL; const char *maintenance_db = NULL; char *host = NULL; char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; bool echo = false; bool quiet = false; bool and_analyze = false; bool analyze_only = false; bool freeze = false; bool alldb = false; char *table = NULL; bool full = false; bool verbose = false; progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); handle_help_version_opts(argc, argv, "vacuumdb", help); while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fv", long_options, &optindex)) != -1) { switch (c) { case 'h': host = pg_strdup(optarg); break; case 'p': port = pg_strdup(optarg); break; case 'U': username = pg_strdup(optarg); break; case 'w': prompt_password = TRI_NO; break; case 'W': prompt_password = TRI_YES; break; case 'e': echo = true; break; case 'q': quiet = true; break; case 'd': dbname = pg_strdup(optarg); break; case 'z': and_analyze = true; break; case 'Z': analyze_only = true; break; case 'F': freeze = true; break; case 'a': alldb = true; break; case 't': table = pg_strdup(optarg); break; case 'f': full = true; break; case 'v': verbose = true; break; case 2: maintenance_db = pg_strdup(optarg); break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } } /* * Non-option argument specifies database name as long as it wasn't * already specified with -d / --dbname */ if (optind < argc && dbname == NULL) { dbname = argv[optind]; optind++; } if (optind < argc) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } if (analyze_only) { if (full) { fprintf(stderr, _("%s: cannot use the \"full\" option when performing only analyze\n"), progname); exit(1); } if (freeze) { fprintf(stderr, _("%s: cannot use the \"freeze\" option when performing only analyze\n"), progname); exit(1); } /* allow 'and_analyze' with 'analyze_only' */ } setup_cancel_handler(); if (alldb) { if (dbname) { fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"), progname); exit(1); } if (table) { fprintf(stderr, _("%s: cannot vacuum a specific table in all databases\n"), progname); exit(1); } vacuum_all_databases(full, verbose, and_analyze, analyze_only, freeze, maintenance_db, host, port, username, prompt_password, progname, echo, quiet); } else { if (dbname == NULL) { if (getenv("PGDATABASE")) dbname = getenv("PGDATABASE"); else if (getenv("PGUSER")) dbname = getenv("PGUSER"); else dbname = get_user_name(progname); } vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only, freeze, table, host, port, username, prompt_password, progname, echo); } exit(0); }