Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / bin / scripts / clusterdb.c
blobfb801ed11a3cdb6f7ee90742b08e22b4d4577179
1 /*-------------------------------------------------------------------------
3 * clusterdb
5 * Portions Copyright (c) 2002-2009, PostgreSQL Global Development Group
7 * $PostgreSQL$
9 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
13 #include "common.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);
28 int
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'},
43 {NULL, 0, NULL, 0}
46 const char *progname;
47 int optindex;
48 int c;
50 const char *dbname = NULL;
51 char *host = NULL;
52 char *port = NULL;
53 char *username = NULL;
54 enum trivalue prompt_password = TRI_DEFAULT;
55 bool echo = false;
56 bool quiet = false;
57 bool alldb = false;
58 char *table = NULL;
59 bool verbose = false;
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)
68 switch (c)
70 case 'h':
71 host = optarg;
72 break;
73 case 'p':
74 port = optarg;
75 break;
76 case 'U':
77 username = optarg;
78 break;
79 case 'w':
80 prompt_password = TRI_NO;
81 break;
82 case 'W':
83 prompt_password = TRI_YES;
84 break;
85 case 'e':
86 echo = true;
87 break;
88 case 'q':
89 quiet = true;
90 break;
91 case 'd':
92 dbname = optarg;
93 break;
94 case 'a':
95 alldb = true;
96 break;
97 case 't':
98 table = optarg;
99 break;
100 case 'v':
101 verbose = true;
102 break;
103 default:
104 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
105 exit(1);
109 switch (argc - optind)
111 case 0:
112 break;
113 case 1:
114 dbname = argv[optind];
115 break;
116 default:
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);
120 exit(1);
123 setup_cancel_handler();
125 if (alldb)
127 if (dbname)
129 fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"),
130 progname);
131 exit(1);
133 if (table)
135 fprintf(stderr, _("%s: cannot cluster a specific table in all databases\n"),
136 progname);
137 exit(1);
140 cluster_all_databases(verbose, host, port, username, prompt_password,
141 progname, echo, quiet);
143 else
145 if (dbname == NULL)
147 if (getenv("PGDATABASE"))
148 dbname = getenv("PGDATABASE");
149 else if (getenv("PGUSER"))
150 dbname = getenv("PGUSER");
151 else
152 dbname = get_user_name(progname);
155 cluster_one_database(dbname, verbose, table,
156 host, port, username, prompt_password,
157 progname, echo);
160 exit(0);
164 static void
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)
170 PQExpBufferData sql;
172 PGconn *conn;
174 initPQExpBuffer(&sql);
176 appendPQExpBuffer(&sql, "CLUSTER");
177 if (verbose)
178 appendPQExpBuffer(&sql, " VERBOSE");
179 if (table)
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))
186 if (table)
187 fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
188 progname, table, dbname, PQerrorMessage(conn));
189 else
190 fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"),
191 progname, dbname, PQerrorMessage(conn));
192 PQfinish(conn);
193 exit(1);
195 PQfinish(conn);
196 termPQExpBuffer(&sql);
200 static void
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)
205 PGconn *conn;
206 PGresult *result;
207 int i;
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);
211 PQfinish(conn);
213 for (i = 0; i < PQntuples(result); i++)
215 char *dbname = PQgetvalue(result, i, 0);
217 if (!quiet)
219 printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
220 fflush(stdout);
223 cluster_one_database(dbname, verbose, NULL,
224 host, port, username, prompt_password,
225 progname, echo);
228 PQclear(result);
232 static void
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"));