1
/*-------------------------------------------------------------------------
5
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
6
* Portions Copyright (c) 1994, Regents of the University of California
8
* $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.12 2004-12-31 22:03:17 pgsql Exp $
10
*-------------------------------------------------------------------------
13
#include "postgres_fe.h"
17
static void vacuum_one_database(const char *dbname, bool full, bool verbose, bool analyze,
19
const char *host, const char *port,
20
const char *username, bool password,
21
const char *progname, bool echo, bool quiet);
22
static void vacuum_all_databases(bool full, bool verbose, bool analyze,
23
const char *host, const char *port,
24
const char *username, bool password,
25
const char *progname, bool echo, bool quiet);
27
static void help(const char *progname);
31
main(int argc, char *argv[])
33
static struct option long_options[] = {
34
{"host", required_argument, NULL, 'h'},
35
{"port", required_argument, NULL, 'p'},
36
{"username", required_argument, NULL, 'U'},
37
{"password", no_argument, NULL, 'W'},
38
{"echo", no_argument, NULL, 'e'},
39
{"quiet", no_argument, NULL, 'q'},
40
{"dbname", required_argument, NULL, 'd'},
41
{"analyze", no_argument, NULL, 'z'},
42
{"all", no_argument, NULL, 'a'},
43
{"table", required_argument, NULL, 't'},
44
{"full", no_argument, NULL, 'f'},
45
{"verbose", no_argument, NULL, 'v'},
53
const char *dbname = NULL;
56
char *username = NULL;
57
bool password = false;
66
progname = get_progname(argv[0]);
67
set_pglocale_pgservice(argv[0], "pgscripts");
69
handle_help_version_opts(argc, argv, "vacuumdb", help);
71
while ((c = getopt_long(argc, argv, "h:p:U:Weqd:zat:fv", long_options, &optindex)) != -1)
112
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
117
switch (argc - optind)
122
dbname = argv[optind];
125
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
126
progname, argv[optind + 1]);
127
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
135
fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
141
fprintf(stderr, _("%s: cannot vacuum a specific table in all databases\n"),
146
vacuum_all_databases(full, verbose, analyze,
147
host, port, username, password,
148
progname, echo, quiet);
154
if (getenv("PGDATABASE"))
155
dbname = getenv("PGDATABASE");
156
else if (getenv("PGUSER"))
157
dbname = getenv("PGUSER");
159
dbname = get_user_name(progname);
162
vacuum_one_database(dbname, full, verbose, analyze, table,
163
host, port, username, password,
164
progname, echo, quiet);
172
vacuum_one_database(const char *dbname, bool full, bool verbose, bool analyze,
174
const char *host, const char *port,
175
const char *username, bool password,
176
const char *progname, bool echo, bool quiet)
183
initPQExpBuffer(&sql);
185
appendPQExpBuffer(&sql, "VACUUM");
187
appendPQExpBuffer(&sql, " FULL");
189
appendPQExpBuffer(&sql, " VERBOSE");
191
appendPQExpBuffer(&sql, " ANALYZE");
193
appendPQExpBuffer(&sql, " %s", table);
194
appendPQExpBuffer(&sql, ";\n");
196
conn = connectDatabase(dbname, host, port, username, password, progname);
199
printf("%s", sql.data);
200
result = PQexec(conn, sql.data);
202
if (PQresultStatus(result) != PGRES_COMMAND_OK)
205
fprintf(stderr, _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
206
progname, table, dbname, PQerrorMessage(conn));
208
fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
209
progname, dbname, PQerrorMessage(conn));
216
termPQExpBuffer(&sql);
227
vacuum_all_databases(bool full, bool verbose, bool analyze,
228
const char *host, const char *port,
229
const char *username, bool password,
230
const char *progname, bool echo, bool quiet)
236
conn = connectDatabase("template1", host, port, username, password, progname);
237
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn;", progname, echo);
240
for (i = 0; i < PQntuples(result); i++)
242
char *dbname = PQgetvalue(result, i, 0);
245
fprintf(stderr, _("%s: vacuuming database \"%s\"\n"), progname, dbname);
247
vacuum_one_database(dbname, full, verbose, analyze, NULL,
248
host, port, username, password,
249
progname, echo, quiet);
257
help(const char *progname)
259
printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname);
260
printf(_("Usage:\n"));
261
printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
262
printf(_("\nOptions:\n"));
263
printf(_(" -a, --all vacuum all databases\n"));
264
printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
265
printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table only\n"));
266
printf(_(" -f, --full do full vacuuming\n"));
267
printf(_(" -z, --analyze update optimizer hints\n"));
268
printf(_(" -e, --echo show the commands being sent to the server\n"));
269
printf(_(" -q, --quiet don't write any messages\n"));
270
printf(_(" -v, --verbose write a lot of output\n"));
271
printf(_(" --help show this help, then exit\n"));
272
printf(_(" --version output version information, then exit\n"));
273
printf(_("\nConnection options:\n"));
274
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
275
printf(_(" -p, --port=PORT database server port\n"));
276
printf(_(" -U, --username=USERNAME user name to connect as\n"));
277
printf(_(" -W, --password prompt for password\n"));
278
printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
279
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));