]> granicus.if.org Git - postgresql/blob - src/bin/scripts/clusterdb.c
pgindent run.
[postgresql] / src / bin / scripts / clusterdb.c
1 /*-------------------------------------------------------------------------
2  *
3  * clusterdb
4  *
5  * Portions Copyright (c) 2002-2003, PostgreSQL Global Development Group
6  *
7  * $Header: /cvsroot/pgsql/src/bin/scripts/clusterdb.c,v 1.3 2003/08/04 00:43:29 momjian Exp $
8  *
9  *-------------------------------------------------------------------------
10  */
11
12 #include "postgres_fe.h"
13 #include "common.h"
14 #include "dumputils.h"
15
16
17 static
18 void
19 cluster_one_database(const char *dbname, const char *table,
20  const char *host, const char *port, const char *username, bool password,
21                                          const char *progname, bool echo, bool quiet);
22 static
23 void
24 cluster_all_databases(const char *host, const char *port, const char *username, bool password,
25                                           const char *progname, bool echo, bool quiet);
26
27 static void help(const char *progname);
28
29
30 int
31 main(int argc, char *argv[])
32 {
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                 {"all", no_argument, NULL, 'a'},
42                 {"table", required_argument, NULL, 't'},
43                 {NULL, 0, NULL, 0}
44         };
45
46         char       *progname;
47         int                     optindex;
48         int                     c;
49
50         const char *dbname = NULL;
51         char       *host = NULL;
52         char       *port = NULL;
53         char       *username = NULL;
54         bool            password = false;
55         bool            echo = false;
56         bool            quiet = false;
57         bool            alldb = false;
58         char       *table = NULL;
59
60         progname = get_progname(argv[0]);
61         init_nls();
62         handle_help_version_opts(argc, argv, "clusterdb", help);
63
64         while ((c = getopt_long(argc, argv, "h:p:U:Weqd:at:", long_options, &optindex)) != -1)
65         {
66                 switch (c)
67                 {
68                         case 'h':
69                                 host = optarg;
70                                 break;
71                         case 'p':
72                                 port = optarg;
73                                 break;
74                         case 'U':
75                                 username = optarg;
76                                 break;
77                         case 'W':
78                                 password = true;
79                                 break;
80                         case 'e':
81                                 echo = true;
82                                 break;
83                         case 'q':
84                                 quiet = true;
85                                 break;
86                         case 'd':
87                                 dbname = optarg;
88                                 break;
89                         case 'a':
90                                 alldb = true;
91                                 break;
92                         case 't':
93                                 table = optarg;
94                                 break;
95                         default:
96                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
97                                 exit(1);
98                 }
99         }
100
101         switch (argc - optind)
102         {
103                 case 0:
104                         break;
105                 case 1:
106                         dbname = argv[optind];
107                         break;
108                 default:
109                         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
110                                         progname, argv[optind + 1]);
111                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
112                         exit(1);
113         }
114
115         if (alldb)
116         {
117                 if (dbname)
118                 {
119                         fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"),
120                                         progname);
121                         exit(1);
122                 }
123                 if (table)
124                 {
125                         fprintf(stderr, _("%s: cannot cluster a specific table in all databases\n"),
126                                         progname);
127                         exit(1);
128                 }
129
130                 cluster_all_databases(host, port, username, password,
131                                                           progname, echo, quiet);
132         }
133         else
134         {
135                 if (dbname == NULL)
136                 {
137                         if (getenv("PGDATABASE"))
138                                 dbname = getenv("PGDATABASE");
139                         else if (getenv("PGUSER"))
140                                 dbname = getenv("PGUSER");
141                         else
142                                 dbname = get_user_name(progname);
143                 }
144
145                 cluster_one_database(dbname, table,
146                                                          host, port, username, password,
147                                                          progname, echo, quiet);
148         }
149
150         exit(0);
151 }
152
153
154 static
155 void
156 cluster_one_database(const char *dbname, const char *table,
157  const char *host, const char *port, const char *username, bool password,
158                                          const char *progname, bool echo, bool quiet)
159 {
160         PQExpBufferData sql;
161
162         PGconn     *conn;
163         PGresult   *result;
164
165         initPQExpBuffer(&sql);
166
167         appendPQExpBuffer(&sql, "CLUSTER");
168         if (table)
169                 appendPQExpBuffer(&sql, " %s", fmtId(table));
170         appendPQExpBuffer(&sql, ";\n");
171
172         conn = connectDatabase(dbname, host, port, username, password, progname);
173
174         if (echo)
175                 printf("%s", sql.data);
176         result = PQexec(conn, sql.data);
177
178         if (PQresultStatus(result) != PGRES_COMMAND_OK)
179         {
180                 if (table)
181                         fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
182                                         progname, table, dbname, PQerrorMessage(conn));
183                 else
184                         fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"),
185                                         progname, dbname, PQerrorMessage(conn));
186                 PQfinish(conn);
187                 exit(1);
188         }
189
190         PQclear(result);
191         PQfinish(conn);
192         termPQExpBuffer(&sql);
193
194         if (!quiet)
195                 puts("CLUSTER");
196 }
197
198
199 static
200 void
201 cluster_all_databases(const char *host, const char *port, const char *username, bool password,
202                                           const char *progname, bool echo, bool quiet)
203 {
204         PGconn     *conn;
205         PGresult   *result;
206         int                     i;
207
208         conn = connectDatabase("template1", host, port, username, password, progname);
209         result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn;", progname, echo);
210         PQfinish(conn);
211
212         for (i = 0; i < PQntuples(result); i++)
213         {
214                 char       *dbname = PQgetvalue(result, i, 0);
215
216                 if (!quiet)
217                         fprintf(stderr, _("%s: clustering database \"%s\"\n"), progname, dbname);
218
219                 cluster_one_database(dbname, NULL,
220                                                          host, port, username, password,
221                                                          progname, echo, quiet);
222         }
223
224         PQclear(result);
225 }
226
227
228 static void
229 help(const char *progname)
230 {
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(_("  -t, --table=TABLE         cluster specific table only\n"));
238         printf(_("  -e, --echo                show the commands being sent to the server\n"));
239         printf(_("  -q, --quiet               don't write any messages\n"));
240         printf(_("  --help                    show this help, then exit\n"));
241         printf(_("  --version                 output version information, then exit\n"));
242         printf(_("\nConnection options:\n"));
243         printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
244         printf(_("  -p, --port=PORT           database server port\n"));
245         printf(_("  -U, --username=USERNAME   user name to connect as\n"));
246         printf(_("  -W, --password            prompt for password\n"));
247         printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
248         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
249 }