1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 2002-2009, PostgreSQL Global Development Group
9 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
14 #include "dumputils.h"
17 static void cluster_one_database(const char *dbname
, bool verbose
, const char *table
,
18 const char *host
, const char *port
,
19 const char *username
, bool password
,
20 const char *progname
, bool echo
);
21 static void cluster_all_databases(bool verbose
, const char *host
, const char *port
,
22 const char *username
, bool password
,
23 const char *progname
, bool echo
, bool quiet
);
25 static void help(const char *progname
);
29 main(int argc
, char *argv
[])
31 static struct option long_options
[] = {
32 {"host", required_argument
, NULL
, 'h'},
33 {"port", required_argument
, NULL
, 'p'},
34 {"username", required_argument
, NULL
, 'U'},
35 {"password", no_argument
, NULL
, 'W'},
36 {"echo", no_argument
, NULL
, 'e'},
37 {"quiet", no_argument
, NULL
, 'q'},
38 {"dbname", required_argument
, NULL
, 'd'},
39 {"all", no_argument
, NULL
, 'a'},
40 {"table", required_argument
, NULL
, 't'},
41 {"verbose", no_argument
, NULL
, 'v'},
49 const char *dbname
= NULL
;
52 char *username
= NULL
;
53 bool password
= false;
60 progname
= get_progname(argv
[0]);
61 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
63 handle_help_version_opts(argc
, argv
, "clusterdb", help
);
65 while ((c
= getopt_long(argc
, argv
, "h:p:U:Weqd:at:v", long_options
, &optindex
)) != -1)
100 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
105 switch (argc
- optind
)
110 dbname
= argv
[optind
];
113 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
114 progname
, argv
[optind
+ 1]);
115 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
119 setup_cancel_handler();
125 fprintf(stderr
, _("%s: cannot cluster all databases and a specific one at the same time\n"),
131 fprintf(stderr
, _("%s: cannot cluster a specific table in all databases\n"),
136 cluster_all_databases(verbose
, host
, port
, username
, password
,
137 progname
, echo
, quiet
);
143 if (getenv("PGDATABASE"))
144 dbname
= getenv("PGDATABASE");
145 else if (getenv("PGUSER"))
146 dbname
= getenv("PGUSER");
148 dbname
= get_user_name(progname
);
151 cluster_one_database(dbname
, verbose
, table
,
152 host
, port
, username
, password
,
161 cluster_one_database(const char *dbname
, bool verbose
, const char *table
,
162 const char *host
, const char *port
,
163 const char *username
, bool password
,
164 const char *progname
, bool echo
)
170 initPQExpBuffer(&sql
);
172 appendPQExpBuffer(&sql
, "CLUSTER");
174 appendPQExpBuffer(&sql
, " VERBOSE");
176 appendPQExpBuffer(&sql
, " %s", fmtId(table
));
177 appendPQExpBuffer(&sql
, ";\n");
179 conn
= connectDatabase(dbname
, host
, port
, username
, password
, progname
);
180 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
183 fprintf(stderr
, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
184 progname
, table
, dbname
, PQerrorMessage(conn
));
186 fprintf(stderr
, _("%s: clustering of database \"%s\" failed: %s"),
187 progname
, dbname
, PQerrorMessage(conn
));
192 termPQExpBuffer(&sql
);
197 cluster_all_databases(bool verbose
, const char *host
, const char *port
,
198 const char *username
, bool password
,
199 const char *progname
, bool echo
, bool quiet
)
205 conn
= connectDatabase("postgres", host
, port
, username
, password
, progname
);
206 result
= executeQuery(conn
, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname
, echo
);
209 for (i
= 0; i
< PQntuples(result
); i
++)
211 char *dbname
= PQgetvalue(result
, i
, 0);
215 printf(_("%s: clustering database \"%s\"\n"), progname
, dbname
);
219 cluster_one_database(dbname
, verbose
, NULL
,
220 host
, port
, username
, password
,
229 help(const char *progname
)
231 printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname
);
232 printf(_("Usage:\n"));
233 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
234 printf(_("\nOptions:\n"));
235 printf(_(" -a, --all cluster all databases\n"));
236 printf(_(" -d, --dbname=DBNAME database to cluster\n"));
237 printf(_(" -e, --echo show the commands being sent to the server\n"));
238 printf(_(" -q, --quiet don't write any messages\n"));
239 printf(_(" -t, --table=TABLE cluster specific table only\n"));
240 printf(_(" -v, --verbose write a lot of output\n"));
241 printf(_(" --help show this help, then exit\n"));
242 printf(_(" --version output version information, then exit\n"));
243 printf(_("\nConnection options:\n"));
244 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
245 printf(_(" -p, --port=PORT database server port\n"));
246 printf(_(" -U, --username=USERNAME user name to connect as\n"));
247 printf(_(" -W, --password force password prompt\n"));
248 printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
249 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));