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