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