Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * clusterdb
4 : *
5 : * Portions Copyright (c) 2002-2021, PostgreSQL Global Development Group
6 : *
7 : * src/bin/scripts/clusterdb.c
8 : *
9 : *-------------------------------------------------------------------------
10 : */
11 :
12 : #include "postgres_fe.h"
13 : #include "common.h"
14 : #include "common/logging.h"
15 : #include "fe_utils/cancel.h"
16 : #include "fe_utils/simple_list.h"
17 : #include "fe_utils/string_utils.h"
18 :
19 :
20 : static void cluster_one_database(const ConnParams *cparams, const char *table,
21 : const char *progname, bool verbose, bool echo);
22 : static void cluster_all_databases(ConnParams *cparams, const char *progname,
23 : bool verbose, bool echo, bool quiet);
24 : static void help(const char *progname);
25 :
26 :
27 : int
28 18 : main(int argc, char *argv[])
29 : {
30 : static struct option long_options[] = {
31 : {"host", required_argument, NULL, 'h'},
32 : {"port", required_argument, NULL, 'p'},
33 : {"username", required_argument, NULL, 'U'},
34 : {"no-password", no_argument, NULL, 'w'},
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'},
42 : {"maintenance-db", required_argument, NULL, 2},
43 : {NULL, 0, NULL, 0}
44 : };
45 :
46 : const char *progname;
47 : int optindex;
48 : int c;
49 :
50 18 : const char *dbname = NULL;
51 18 : const char *maintenance_db = NULL;
52 18 : char *host = NULL;
53 18 : char *port = NULL;
54 18 : char *username = NULL;
55 18 : enum trivalue prompt_password = TRI_DEFAULT;
56 : ConnParams cparams;
57 18 : bool echo = false;
58 18 : bool quiet = false;
59 18 : bool alldb = false;
60 18 : bool verbose = false;
61 18 : SimpleStringList tables = {NULL, NULL};
62 :
63 18 : pg_logging_init(argv[0]);
64 18 : progname = get_progname(argv[0]);
65 18 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
66 :
67 18 : handle_help_version_opts(argc, argv, "clusterdb", help);
68 :
69 30 : while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
70 : {
71 18 : switch (c)
72 : {
73 0 : case 'h':
74 0 : host = pg_strdup(optarg);
75 0 : break;
76 0 : case 'p':
77 0 : port = pg_strdup(optarg);
78 0 : break;
79 0 : case 'U':
80 0 : username = pg_strdup(optarg);
81 0 : break;
82 0 : case 'w':
83 0 : prompt_password = TRI_NO;
84 0 : break;
85 0 : case 'W':
86 0 : prompt_password = TRI_YES;
87 0 : break;
88 4 : case 'e':
89 4 : echo = true;
90 4 : break;
91 0 : case 'q':
92 0 : quiet = true;
93 0 : break;
94 0 : case 'd':
95 0 : dbname = pg_strdup(optarg);
96 0 : break;
97 4 : case 'a':
98 4 : alldb = true;
99 4 : break;
100 4 : case 't':
101 4 : simple_string_list_append(&tables, optarg);
102 4 : break;
103 4 : case 'v':
104 4 : verbose = true;
105 4 : break;
106 0 : case 2:
107 0 : maintenance_db = pg_strdup(optarg);
108 0 : break;
109 2 : default:
110 2 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
111 2 : exit(1);
112 : }
113 : }
114 :
115 : /*
116 : * Non-option argument specifies database name as long as it wasn't
117 : * already specified with -d / --dbname
118 : */
119 12 : if (optind < argc && dbname == NULL)
120 : {
121 2 : dbname = argv[optind];
122 2 : optind++;
123 : }
124 :
125 12 : if (optind < argc)
126 : {
127 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
128 : argv[optind]);
129 0 : fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
130 0 : exit(1);
131 : }
132 :
133 : /* fill cparams except for dbname, which is set below */
134 12 : cparams.pghost = host;
135 12 : cparams.pgport = port;
136 12 : cparams.pguser = username;
137 12 : cparams.prompt_password = prompt_password;
138 12 : cparams.override_dbname = NULL;
139 :
140 12 : setup_cancel_handler(NULL);
141 :
142 12 : if (alldb)
143 : {
144 4 : if (dbname)
145 : {
146 0 : pg_log_error("cannot cluster all databases and a specific one at the same time");
147 0 : exit(1);
148 : }
149 :
150 4 : if (tables.head != NULL)
151 : {
152 0 : pg_log_error("cannot cluster specific table(s) in all databases");
153 0 : exit(1);
154 : }
155 :
156 4 : cparams.dbname = maintenance_db;
157 :
158 4 : cluster_all_databases(&cparams, progname, verbose, echo, quiet);
159 : }
160 : else
161 : {
162 8 : if (dbname == NULL)
163 : {
164 6 : if (getenv("PGDATABASE"))
165 6 : dbname = getenv("PGDATABASE");
166 0 : else if (getenv("PGUSER"))
167 0 : dbname = getenv("PGUSER");
168 : else
169 0 : dbname = get_user_name_or_exit(progname);
170 : }
171 :
172 8 : cparams.dbname = dbname;
173 :
174 8 : if (tables.head != NULL)
175 : {
176 : SimpleStringListCell *cell;
177 :
178 6 : for (cell = tables.head; cell; cell = cell->next)
179 : {
180 4 : cluster_one_database(&cparams, cell->val,
181 : progname, verbose, echo);
182 : }
183 : }
184 : else
185 4 : cluster_one_database(&cparams, NULL,
186 : progname, verbose, echo);
187 : }
188 :
189 10 : exit(0);
190 : }
191 :
192 :
193 : static void
194 26 : cluster_one_database(const ConnParams *cparams, const char *table,
195 : const char *progname, bool verbose, bool echo)
196 : {
197 : PQExpBufferData sql;
198 :
199 : PGconn *conn;
200 :
201 26 : conn = connectDatabase(cparams, progname, echo, false, false);
202 :
203 26 : initPQExpBuffer(&sql);
204 :
205 26 : appendPQExpBufferStr(&sql, "CLUSTER");
206 26 : if (verbose)
207 16 : appendPQExpBufferStr(&sql, " VERBOSE");
208 26 : if (table)
209 : {
210 4 : appendPQExpBufferChar(&sql, ' ');
211 4 : appendQualifiedRelation(&sql, table, conn, echo);
212 : }
213 24 : appendPQExpBufferChar(&sql, ';');
214 :
215 24 : if (!executeMaintenanceCommand(conn, sql.data, echo))
216 : {
217 0 : if (table)
218 0 : pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
219 : table, PQdb(conn), PQerrorMessage(conn));
220 : else
221 0 : pg_log_error("clustering of database \"%s\" failed: %s",
222 : PQdb(conn), PQerrorMessage(conn));
223 0 : PQfinish(conn);
224 0 : exit(1);
225 : }
226 24 : PQfinish(conn);
227 24 : termPQExpBuffer(&sql);
228 24 : }
229 :
230 :
231 : static void
232 4 : cluster_all_databases(ConnParams *cparams, const char *progname,
233 : bool verbose, bool echo, bool quiet)
234 : {
235 : PGconn *conn;
236 : PGresult *result;
237 : int i;
238 :
239 4 : conn = connectMaintenanceDatabase(cparams, progname, echo);
240 4 : result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
241 4 : PQfinish(conn);
242 :
243 22 : for (i = 0; i < PQntuples(result); i++)
244 : {
245 18 : char *dbname = PQgetvalue(result, i, 0);
246 :
247 18 : if (!quiet)
248 : {
249 18 : printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
250 18 : fflush(stdout);
251 : }
252 :
253 18 : cparams->override_dbname = dbname;
254 :
255 18 : cluster_one_database(cparams, NULL, progname, verbose, echo);
256 : }
257 :
258 4 : PQclear(result);
259 4 : }
260 :
261 :
262 : static void
263 2 : help(const char *progname)
264 : {
265 2 : printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
266 2 : printf(_("Usage:\n"));
267 2 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
268 2 : printf(_("\nOptions:\n"));
269 2 : printf(_(" -a, --all cluster all databases\n"));
270 2 : printf(_(" -d, --dbname=DBNAME database to cluster\n"));
271 2 : printf(_(" -e, --echo show the commands being sent to the server\n"));
272 2 : printf(_(" -q, --quiet don't write any messages\n"));
273 2 : printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
274 2 : printf(_(" -v, --verbose write a lot of output\n"));
275 2 : printf(_(" -V, --version output version information, then exit\n"));
276 2 : printf(_(" -?, --help show this help, then exit\n"));
277 2 : printf(_("\nConnection options:\n"));
278 2 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
279 2 : printf(_(" -p, --port=PORT database server port\n"));
280 2 : printf(_(" -U, --username=USERNAME user name to connect as\n"));
281 2 : printf(_(" -w, --no-password never prompt for password\n"));
282 2 : printf(_(" -W, --password force password prompt\n"));
283 2 : printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
284 2 : printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
285 2 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
286 2 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
287 2 : }
|