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