1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
17 static void vacuum_one_database(const char *dbname
, bool full
, bool verbose
, bool analyze
,
18 bool freeze
, const char *table
,
19 const char *host
, const char *port
,
20 const char *username
, enum trivalue prompt_password
,
21 const char *progname
, bool echo
);
22 static void vacuum_all_databases(bool full
, bool verbose
, bool analyze
, bool freeze
,
23 const char *host
, const char *port
,
24 const char *username
, enum trivalue prompt_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 {"no-password", no_argument
, NULL
, 'w'},
38 {"password", no_argument
, NULL
, 'W'},
39 {"echo", no_argument
, NULL
, 'e'},
40 {"quiet", no_argument
, NULL
, 'q'},
41 {"dbname", required_argument
, NULL
, 'd'},
42 {"analyze", no_argument
, NULL
, 'z'},
43 {"freeze", no_argument
, NULL
, 'F'},
44 {"all", no_argument
, NULL
, 'a'},
45 {"table", required_argument
, NULL
, 't'},
46 {"full", no_argument
, NULL
, 'f'},
47 {"verbose", no_argument
, NULL
, 'v'},
55 const char *dbname
= NULL
;
58 char *username
= NULL
;
59 enum trivalue prompt_password
= TRI_DEFAULT
;
69 progname
= get_progname(argv
[0]);
70 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
72 handle_help_version_opts(argc
, argv
, "vacuumdb", help
);
74 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWeqd:zaFt:fv", long_options
, &optindex
)) != -1)
88 prompt_password
= TRI_NO
;
91 prompt_password
= TRI_YES
;
121 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
126 switch (argc
- optind
)
131 dbname
= argv
[optind
];
134 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
135 progname
, argv
[optind
+ 1]);
136 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
140 setup_cancel_handler();
146 fprintf(stderr
, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
152 fprintf(stderr
, _("%s: cannot vacuum a specific table in all databases\n"),
157 vacuum_all_databases(full
, verbose
, analyze
, freeze
,
158 host
, port
, username
, prompt_password
,
159 progname
, echo
, quiet
);
165 if (getenv("PGDATABASE"))
166 dbname
= getenv("PGDATABASE");
167 else if (getenv("PGUSER"))
168 dbname
= getenv("PGUSER");
170 dbname
= get_user_name(progname
);
173 vacuum_one_database(dbname
, full
, verbose
, analyze
, freeze
, table
,
174 host
, port
, username
, prompt_password
,
183 vacuum_one_database(const char *dbname
, bool full
, bool verbose
, bool analyze
,
184 bool freeze
, const char *table
,
185 const char *host
, const char *port
,
186 const char *username
, enum trivalue prompt_password
,
187 const char *progname
, bool echo
)
193 initPQExpBuffer(&sql
);
195 appendPQExpBuffer(&sql
, "VACUUM");
197 appendPQExpBuffer(&sql
, " FULL");
199 appendPQExpBuffer(&sql
, " VERBOSE");
201 appendPQExpBuffer(&sql
, " FREEZE");
203 appendPQExpBuffer(&sql
, " ANALYZE");
205 appendPQExpBuffer(&sql
, " %s", table
);
206 appendPQExpBuffer(&sql
, ";\n");
208 conn
= connectDatabase(dbname
, host
, port
, username
, prompt_password
, progname
);
209 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
212 fprintf(stderr
, _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
213 progname
, table
, dbname
, PQerrorMessage(conn
));
215 fprintf(stderr
, _("%s: vacuuming of database \"%s\" failed: %s"),
216 progname
, dbname
, PQerrorMessage(conn
));
221 termPQExpBuffer(&sql
);
226 vacuum_all_databases(bool full
, bool verbose
, bool analyze
, bool freeze
,
227 const char *host
, const char *port
,
228 const char *username
, enum trivalue prompt_password
,
229 const char *progname
, bool echo
, bool quiet
)
235 conn
= connectDatabase("postgres", host
, port
, username
, prompt_password
, progname
);
236 result
= executeQuery(conn
, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname
, echo
);
239 for (i
= 0; i
< PQntuples(result
); i
++)
241 char *dbname
= PQgetvalue(result
, i
, 0);
245 printf(_("%s: vacuuming database \"%s\"\n"), progname
, dbname
);
249 vacuum_one_database(dbname
, full
, verbose
, analyze
, freeze
, NULL
,
250 host
, port
, username
, prompt_password
,
259 help(const char *progname
)
261 printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname
);
262 printf(_("Usage:\n"));
263 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
264 printf(_("\nOptions:\n"));
265 printf(_(" -a, --all vacuum all databases\n"));
266 printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
267 printf(_(" -e, --echo show the commands being sent to the server\n"));
268 printf(_(" -f, --full do full vacuuming\n"));
269 printf(_(" -F, --freeze freeze row transaction information\n"));
270 printf(_(" -q, --quiet don't write any messages\n"));
271 printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table only\n"));
272 printf(_(" -v, --verbose write a lot of output\n"));
273 printf(_(" -z, --analyze update optimizer hints\n"));
274 printf(_(" --help show this help, then exit\n"));
275 printf(_(" --version output version information, then exit\n"));
276 printf(_("\nConnection options:\n"));
277 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
278 printf(_(" -p, --port=PORT database server port\n"));
279 printf(_(" -U, --username=USERNAME user name to connect as\n"));
280 printf(_(" -w, --no-password never prompt for password\n"));
281 printf(_(" -W, --password force password prompt\n"));
282 printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
283 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));