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
, bool 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
, 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 {"freeze", no_argument
, NULL
, 'F'},
43 {"all", no_argument
, NULL
, 'a'},
44 {"table", required_argument
, NULL
, 't'},
45 {"full", no_argument
, NULL
, 'f'},
46 {"verbose", no_argument
, NULL
, 'v'},
54 const char *dbname
= NULL
;
57 char *username
= NULL
;
58 bool password
= false;
68 progname
= get_progname(argv
[0]);
69 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
71 handle_help_version_opts(argc
, argv
, "vacuumdb", help
);
73 while ((c
= getopt_long(argc
, argv
, "h:p:U:Weqd:zaFt:fv", long_options
, &optindex
)) != -1)
117 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
122 switch (argc
- optind
)
127 dbname
= argv
[optind
];
130 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
131 progname
, argv
[optind
+ 1]);
132 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
136 setup_cancel_handler();
142 fprintf(stderr
, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
148 fprintf(stderr
, _("%s: cannot vacuum a specific table in all databases\n"),
153 vacuum_all_databases(full
, verbose
, analyze
, freeze
,
154 host
, port
, username
, password
,
155 progname
, echo
, quiet
);
161 if (getenv("PGDATABASE"))
162 dbname
= getenv("PGDATABASE");
163 else if (getenv("PGUSER"))
164 dbname
= getenv("PGUSER");
166 dbname
= get_user_name(progname
);
169 vacuum_one_database(dbname
, full
, verbose
, analyze
, freeze
, table
,
170 host
, port
, username
, password
,
179 vacuum_one_database(const char *dbname
, bool full
, bool verbose
, bool analyze
,
180 bool freeze
, const char *table
,
181 const char *host
, const char *port
,
182 const char *username
, bool password
,
183 const char *progname
, bool echo
)
189 initPQExpBuffer(&sql
);
191 appendPQExpBuffer(&sql
, "VACUUM");
193 appendPQExpBuffer(&sql
, " FULL");
195 appendPQExpBuffer(&sql
, " VERBOSE");
197 appendPQExpBuffer(&sql
, " ANALYZE");
199 appendPQExpBuffer(&sql
, " FREEZE");
201 appendPQExpBuffer(&sql
, " %s", table
);
202 appendPQExpBuffer(&sql
, ";\n");
204 conn
= connectDatabase(dbname
, host
, port
, username
, password
, progname
);
205 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
208 fprintf(stderr
, _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
209 progname
, table
, dbname
, PQerrorMessage(conn
));
211 fprintf(stderr
, _("%s: vacuuming of database \"%s\" failed: %s"),
212 progname
, dbname
, PQerrorMessage(conn
));
217 termPQExpBuffer(&sql
);
222 vacuum_all_databases(bool full
, bool verbose
, bool analyze
, bool freeze
,
223 const char *host
, const char *port
,
224 const char *username
, bool password
,
225 const char *progname
, bool echo
, bool quiet
)
231 conn
= connectDatabase("postgres", host
, port
, username
, password
, progname
);
232 result
= executeQuery(conn
, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname
, echo
);
235 for (i
= 0; i
< PQntuples(result
); i
++)
237 char *dbname
= PQgetvalue(result
, i
, 0);
241 printf(_("%s: vacuuming database \"%s\"\n"), progname
, dbname
);
245 vacuum_one_database(dbname
, full
, verbose
, analyze
, freeze
, NULL
,
246 host
, port
, username
, password
,
255 help(const char *progname
)
257 printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname
);
258 printf(_("Usage:\n"));
259 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
260 printf(_("\nOptions:\n"));
261 printf(_(" -a, --all vacuum all databases\n"));
262 printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
263 printf(_(" -e, --echo show the commands being sent to the server\n"));
264 printf(_(" -f, --full do full vacuuming\n"));
265 printf(_(" -F, --freeze freeze row transaction information\n"));
266 printf(_(" -q, --quiet don't write any messages\n"));
267 printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table only\n"));
268 printf(_(" -v, --verbose write a lot of output\n"));
269 printf(_(" -z, --analyze update optimizer hints\n"));
270 printf(_(" --help show this help, then exit\n"));
271 printf(_(" --version output version information, then exit\n"));
272 printf(_("\nConnection options:\n"));
273 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
274 printf(_(" -p, --port=PORT database server port\n"));
275 printf(_(" -U, --username=USERNAME user name to connect as\n"));
276 printf(_(" -W, --password force password prompt\n"));
277 printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
278 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));