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
, enum trivalue prompt_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
, enum trivalue prompt_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 {"no-password", no_argument
, NULL
, 'w'},
36 {"password", no_argument
, NULL
, 'W'},
37 {"echo", no_argument
, NULL
, 'e'},
38 {"quiet", no_argument
, NULL
, 'q'},
39 {"dbname", required_argument
, NULL
, 'd'},
40 {"all", no_argument
, NULL
, 'a'},
41 {"table", required_argument
, NULL
, 't'},
42 {"verbose", no_argument
, NULL
, 'v'},
50 const char *dbname
= NULL
;
53 char *username
= NULL
;
54 enum trivalue prompt_password
= TRI_DEFAULT
;
61 progname
= get_progname(argv
[0]);
62 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
64 handle_help_version_opts(argc
, argv
, "clusterdb", help
);
66 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWeqd:at:v", long_options
, &optindex
)) != -1)
80 prompt_password
= TRI_NO
;
83 prompt_password
= TRI_YES
;
104 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
109 switch (argc
- optind
)
114 dbname
= argv
[optind
];
117 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
118 progname
, argv
[optind
+ 1]);
119 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
123 setup_cancel_handler();
129 fprintf(stderr
, _("%s: cannot cluster all databases and a specific one at the same time\n"),
135 fprintf(stderr
, _("%s: cannot cluster a specific table in all databases\n"),
140 cluster_all_databases(verbose
, host
, port
, username
, prompt_password
,
141 progname
, echo
, quiet
);
147 if (getenv("PGDATABASE"))
148 dbname
= getenv("PGDATABASE");
149 else if (getenv("PGUSER"))
150 dbname
= getenv("PGUSER");
152 dbname
= get_user_name(progname
);
155 cluster_one_database(dbname
, verbose
, table
,
156 host
, port
, username
, prompt_password
,
165 cluster_one_database(const char *dbname
, bool verbose
, const char *table
,
166 const char *host
, const char *port
,
167 const char *username
, enum trivalue prompt_password
,
168 const char *progname
, bool echo
)
174 initPQExpBuffer(&sql
);
176 appendPQExpBuffer(&sql
, "CLUSTER");
178 appendPQExpBuffer(&sql
, " VERBOSE");
180 appendPQExpBuffer(&sql
, " %s", fmtId(table
));
181 appendPQExpBuffer(&sql
, ";\n");
183 conn
= connectDatabase(dbname
, host
, port
, username
, prompt_password
, progname
);
184 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
187 fprintf(stderr
, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
188 progname
, table
, dbname
, PQerrorMessage(conn
));
190 fprintf(stderr
, _("%s: clustering of database \"%s\" failed: %s"),
191 progname
, dbname
, PQerrorMessage(conn
));
196 termPQExpBuffer(&sql
);
201 cluster_all_databases(bool verbose
, const char *host
, const char *port
,
202 const char *username
, enum trivalue prompt_password
,
203 const char *progname
, bool echo
, bool quiet
)
209 conn
= connectDatabase("postgres", host
, port
, username
, prompt_password
, progname
);
210 result
= executeQuery(conn
, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname
, echo
);
213 for (i
= 0; i
< PQntuples(result
); i
++)
215 char *dbname
= PQgetvalue(result
, i
, 0);
219 printf(_("%s: clustering database \"%s\"\n"), progname
, dbname
);
223 cluster_one_database(dbname
, verbose
, NULL
,
224 host
, port
, username
, prompt_password
,
233 help(const char *progname
)
235 printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname
);
236 printf(_("Usage:\n"));
237 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
238 printf(_("\nOptions:\n"));
239 printf(_(" -a, --all cluster all databases\n"));
240 printf(_(" -d, --dbname=DBNAME database to cluster\n"));
241 printf(_(" -e, --echo show the commands being sent to the server\n"));
242 printf(_(" -q, --quiet don't write any messages\n"));
243 printf(_(" -t, --table=TABLE cluster specific table only\n"));
244 printf(_(" -v, --verbose write a lot of output\n"));
245 printf(_(" --help show this help, then exit\n"));
246 printf(_(" --version output version information, then exit\n"));
247 printf(_("\nConnection options:\n"));
248 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
249 printf(_(" -p, --port=PORT database server port\n"));
250 printf(_(" -U, --username=USERNAME user name to connect as\n"));
251 printf(_(" -w, --no-password never prompt for password\n"));
252 printf(_(" -W, --password force password prompt\n"));
253 printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
254 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));