diff options
Diffstat (limited to 'src/bin/scripts/vacuumdb.c')
-rw-r--r-- | src/bin/scripts/vacuumdb.c | 93 |
1 files changed, 74 insertions, 19 deletions
diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 658284da860..281e32db5b3 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -5,7 +5,7 @@ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.29 2010/01/06 02:59:46 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.30 2010/01/06 05:31:14 itagaki Exp $ * *------------------------------------------------------------------------- */ @@ -14,12 +14,12 @@ #include "common.h" -static void vacuum_one_database(const char *dbname, bool full, bool verbose, +static void vacuum_one_database(const char *dbname, bool full, bool inplace, bool verbose, bool and_analyze, bool only_analyze, bool freeze, const char *table, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo); -static void vacuum_all_databases(bool full, bool verbose, bool and_analyze, +static void vacuum_all_databases(bool full, bool inplace, bool verbose, bool and_analyze, bool only_analyze, bool freeze, const char *host, const char *port, const char *username, enum trivalue prompt_password, @@ -47,6 +47,7 @@ main(int argc, char *argv[]) {"table", required_argument, NULL, 't'}, {"full", no_argument, NULL, 'f'}, {"verbose", no_argument, NULL, 'v'}, + {"inplace", no_argument, NULL, 'i'}, {NULL, 0, NULL, 0} }; @@ -68,13 +69,14 @@ main(int argc, char *argv[]) char *table = NULL; bool full = false; bool verbose = false; + bool inplace = 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:zaFt:fv", long_options, &optindex)) != -1) + while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zaFt:fiv", long_options, &optindex)) != -1) { switch (c) { @@ -120,6 +122,9 @@ main(int argc, char *argv[]) case 'f': full = true; break; + case 'i': + inplace = true; + break; case 'v': verbose = true; break; @@ -143,7 +148,12 @@ main(int argc, char *argv[]) exit(1); } - setup_cancel_handler(); + if (inplace && !full) + { + fprintf(stderr, _("%s: cannot use the \"inplace\" option when performing full vacuum\n"), + progname); + exit(1); + } if (only_analyze) { @@ -162,6 +172,8 @@ main(int argc, char *argv[]) /* ignore 'and_analyze' */ } + setup_cancel_handler(); + if (alldb) { if (dbname) @@ -177,7 +189,7 @@ main(int argc, char *argv[]) exit(1); } - vacuum_all_databases(full, verbose, and_analyze, only_analyze, freeze, + vacuum_all_databases(full, inplace, verbose, and_analyze, only_analyze, freeze, host, port, username, prompt_password, progname, echo, quiet); } @@ -193,7 +205,7 @@ main(int argc, char *argv[]) dbname = get_user_name(progname); } - vacuum_one_database(dbname, full, verbose, and_analyze, only_analyze, + vacuum_one_database(dbname, full, inplace, verbose, and_analyze, only_analyze, freeze, table, host, port, username, prompt_password, progname, echo); @@ -204,7 +216,7 @@ main(int argc, char *argv[]) static void -vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyze, +vacuum_one_database(const char *dbname, bool full, bool inplace, bool verbose, bool and_analyze, bool only_analyze, bool freeze, const char *table, const char *host, const char *port, const char *username, enum trivalue prompt_password, @@ -216,25 +228,67 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz initPQExpBuffer(&sql); + conn = connectDatabase(dbname, host, port, username, prompt_password, progname); + if (only_analyze) + { appendPQExpBuffer(&sql, "ANALYZE"); + if (verbose) + appendPQExpBuffer(&sql, " VERBOSE"); + } else { appendPQExpBuffer(&sql, "VACUUM"); - if (full) - appendPQExpBuffer(&sql, " FULL"); - if (freeze) - appendPQExpBuffer(&sql, " FREEZE"); - if (and_analyze) - appendPQExpBuffer(&sql, " ANALYZE"); + if (PQserverVersion(conn) >= 80500) + { + const char *paren = " ("; + const char *comma = ", "; + const char *sep = paren; + + if (full) + { + appendPQExpBuffer(&sql, "%sFULL%s", sep, + inplace ? " INPLACE" : ""); + sep = comma; + } + if (freeze) + { + appendPQExpBuffer(&sql, "%sFREEZE", sep); + sep = comma; + } + if (verbose) + { + appendPQExpBuffer(&sql, "%sVERBOSE", sep); + sep = comma; + } + if (and_analyze) + { + appendPQExpBuffer(&sql, "%sANALYZE", sep); + sep = comma; + } + if (sep != paren) + appendPQExpBuffer(&sql, ")"); + } + else + { + /* + * On older servers, VACUUM FULL is equivalent to VACUUM (FULL + * INPLACE) on newer servers, so we can ignore 'inplace'. + */ + if (full) + appendPQExpBuffer(&sql, " FULL"); + if (freeze) + appendPQExpBuffer(&sql, " FREEZE"); + if (verbose) + appendPQExpBuffer(&sql, " VERBOSE"); + if (and_analyze) + appendPQExpBuffer(&sql, " ANALYZE"); + } } - if (verbose) - appendPQExpBuffer(&sql, " VERBOSE"); if (table) appendPQExpBuffer(&sql, " %s", table); appendPQExpBuffer(&sql, ";\n"); - conn = connectDatabase(dbname, host, port, username, prompt_password, progname); if (!executeMaintenanceCommand(conn, sql.data, echo)) { if (table) @@ -252,7 +306,7 @@ vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyz static void -vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool only_analyze, +vacuum_all_databases(bool full, bool inplace, bool verbose, bool and_analyze, bool only_analyze, bool freeze, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet) @@ -275,7 +329,7 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool only_analyz fflush(stdout); } - vacuum_one_database(dbname, full, verbose, and_analyze, only_analyze, + vacuum_one_database(dbname, full, inplace, verbose, and_analyze, only_analyze, freeze, NULL, host, port, username, prompt_password, progname, echo); } @@ -296,6 +350,7 @@ help(const char *progname) printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -f, --full do full vacuuming\n")); printf(_(" -F, --freeze freeze row transaction information\n")); + printf(_(" -i, --inplace do full inplace vacuuming\n")); printf(_(" -o, --only-analyze only update optimizer hints\n")); printf(_(" -q, --quiet don't write any messages\n")); printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table only\n")); |