Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * clusterdb
4 : : *
5 : : * Portions Copyright (c) 2002-2025, 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
8116 peter_e@gmx.net 30 :CBC 12 : 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 : 12 : const char *dbname = NULL;
5023 rhaas@postgresql.org 53 : 12 : const char *maintenance_db = NULL;
8116 peter_e@gmx.net 54 : 12 : char *host = NULL;
55 : 12 : char *port = NULL;
56 : 12 : char *username = NULL;
6036 57 : 12 : enum trivalue prompt_password = TRI_DEFAULT;
58 : : ConnParams cparams;
8116 59 : 12 : bool echo = false;
60 : 12 : bool quiet = false;
61 : 12 : bool alldb = false;
6130 62 : 12 : bool verbose = false;
4615 magnus@hagander.net 63 : 12 : SimpleStringList tables = {NULL, NULL};
64 : :
2350 peter@eisentraut.org 65 : 12 : pg_logging_init(argv[0]);
8116 peter_e@gmx.net 66 : 12 : progname = get_progname(argv[0]);
6113 67 : 12 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
68 : :
8116 69 : 12 : handle_help_version_opts(argc, argv, "clusterdb", help);
70 : :
999 peter@eisentraut.org 71 [ + + ]: 22 : while ((c = getopt_long(argc, argv, "ad:eh:p:qt:U:vwW", long_options, &optindex)) != -1)
72 : : {
8116 peter_e@gmx.net 73 [ + + + - : 13 : switch (c)
- - + - +
- - - + ]
74 : : {
999 peter@eisentraut.org 75 : 4 : case 'a':
76 : 4 : alldb = true;
77 : 4 : break;
78 : 1 : case 'd':
79 : 1 : dbname = pg_strdup(optarg);
80 : 1 : break;
81 : 2 : case 'e':
82 : 2 : echo = true;
83 : 2 : break;
8116 peter_e@gmx.net 84 :UBC 0 : case 'h':
4712 bruce@momjian.us 85 : 0 : host = pg_strdup(optarg);
8116 peter_e@gmx.net 86 : 0 : break;
87 : 0 : case 'p':
4712 bruce@momjian.us 88 : 0 : port = pg_strdup(optarg);
8116 peter_e@gmx.net 89 : 0 : break;
90 : 0 : case 'q':
91 : 0 : quiet = true;
92 : 0 : break;
8116 peter_e@gmx.net 93 :CBC 3 : case 't':
4615 magnus@hagander.net 94 : 3 : simple_string_list_append(&tables, optarg);
8116 peter_e@gmx.net 95 : 3 : break;
999 peter@eisentraut.org 96 :UBC 0 : case 'U':
97 : 0 : username = pg_strdup(optarg);
98 : 0 : break;
6130 peter_e@gmx.net 99 :CBC 2 : case 'v':
100 : 2 : verbose = true;
101 : 2 : break;
999 peter@eisentraut.org 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;
5023 rhaas@postgresql.org 108 : 0 : case 2:
4712 bruce@momjian.us 109 : 0 : maintenance_db = pg_strdup(optarg);
5023 rhaas@postgresql.org 110 : 0 : break;
8116 peter_e@gmx.net 111 :CBC 1 : default:
112 : : /* getopt_long already emitted a complaint */
1247 tgl@sss.pgh.pa.us 113 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
8116 peter_e@gmx.net 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 : : */
4890 andrew@dunslane.net 122 [ + + + - ]: 9 : if (optind < argc && dbname == NULL)
123 : : {
124 : 1 : dbname = argv[optind];
125 : 1 : optind++;
126 : : }
127 : :
128 [ - + ]: 9 : if (optind < argc)
129 : : {
2350 peter@eisentraut.org 130 :UBC 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
131 : : argv[optind]);
1247 tgl@sss.pgh.pa.us 132 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4890 andrew@dunslane.net 133 : 0 : exit(1);
134 : : }
135 : :
136 : : /* fill cparams except for dbname, which is set below */
1783 tgl@sss.pgh.pa.us 137 :CBC 9 : cparams.pghost = host;
138 : 9 : cparams.pgport = port;
139 : 9 : cparams.pguser = username;
140 : 9 : cparams.prompt_password = prompt_password;
141 : 9 : cparams.override_dbname = NULL;
142 : :
2105 michael@paquier.xyz 143 : 9 : setup_cancel_handler(NULL);
144 : :
8116 peter_e@gmx.net 145 [ + + ]: 9 : if (alldb)
146 : : {
147 [ - + ]: 4 : if (dbname)
1247 tgl@sss.pgh.pa.us 148 :UBC 0 : pg_fatal("cannot cluster all databases and a specific one at the same time");
149 : :
1783 tgl@sss.pgh.pa.us 150 :CBC 4 : cparams.dbname = maintenance_db;
151 : :
544 nathan@postgresql.or 152 : 4 : cluster_all_databases(&cparams, &tables,
153 : : progname, verbose, echo, quiet);
154 : : }
155 : : else
156 : : {
8116 peter_e@gmx.net 157 [ + + ]: 5 : if (dbname == NULL)
158 : : {
159 [ + - ]: 3 : if (getenv("PGDATABASE"))
160 : 3 : dbname = getenv("PGDATABASE");
8116 peter_e@gmx.net 161 [ # # ]:UBC 0 : else if (getenv("PGUSER"))
162 : 0 : dbname = getenv("PGUSER");
163 : : else
4280 bruce@momjian.us 164 : 0 : dbname = get_user_name_or_exit(progname);
165 : : }
166 : :
1783 tgl@sss.pgh.pa.us 167 :CBC 5 : cparams.dbname = dbname;
168 : :
4615 magnus@hagander.net 169 [ + + ]: 5 : if (tables.head != NULL)
170 : : {
171 : : SimpleStringListCell *cell;
172 : :
173 [ + + ]: 3 : for (cell = tables.head; cell; cell = cell->next)
174 : : {
1783 tgl@sss.pgh.pa.us 175 : 2 : cluster_one_database(&cparams, cell->val,
176 : : progname, verbose, echo);
177 : : }
178 : : }
179 : : else
180 : 3 : cluster_one_database(&cparams, NULL,
181 : : progname, verbose, echo);
182 : : }
183 : :
8116 peter_e@gmx.net 184 : 7 : exit(0);
185 : : }
186 : :
187 : :
188 : : static void
1783 tgl@sss.pgh.pa.us 189 : 18 : cluster_one_database(const ConnParams *cparams, const char *table,
190 : : const char *progname, bool verbose, bool echo)
191 : : {
192 : : PQExpBufferData sql;
193 : :
194 : : PGconn *conn;
195 : :
771 nathan@postgresql.or 196 : 18 : conn = connectDatabase(cparams, progname, echo, false, true);
197 : :
8116 peter_e@gmx.net 198 : 17 : initPQExpBuffer(&sql);
199 : :
4310 heikki.linnakangas@i 200 : 17 : appendPQExpBufferStr(&sql, "CLUSTER");
6130 peter_e@gmx.net 201 [ + + ]: 17 : if (verbose)
4310 heikki.linnakangas@i 202 : 8 : appendPQExpBufferStr(&sql, " VERBOSE");
8116 peter_e@gmx.net 203 [ + + ]: 17 : if (table)
204 : : {
2749 noah@leadboat.com 205 : 4 : appendPQExpBufferChar(&sql, ' ');
2241 michael@paquier.xyz 206 : 4 : appendQualifiedRelation(&sql, table, conn, echo);
207 : : }
3719 heikki.linnakangas@i 208 : 16 : appendPQExpBufferChar(&sql, ';');
209 : :
6725 magnus@hagander.net 210 [ - + ]: 16 : if (!executeMaintenanceCommand(conn, sql.data, echo))
211 : : {
8116 peter_e@gmx.net 212 [ # # ]:UBC 0 : if (table)
2350 peter@eisentraut.org 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));
8116 peter_e@gmx.net 218 : 0 : PQfinish(conn);
219 : 0 : exit(1);
220 : : }
8116 peter_e@gmx.net 221 :CBC 16 : PQfinish(conn);
222 : 16 : termPQExpBuffer(&sql);
223 : 16 : }
224 : :
225 : :
226 : : static void
544 nathan@postgresql.or 227 : 4 : cluster_all_databases(ConnParams *cparams, SimpleStringList *tables,
228 : : const char *progname, bool verbose, bool echo,
229 : : bool quiet)
230 : : {
231 : : PGconn *conn;
232 : : PGresult *result;
233 : : int i;
234 : :
1783 tgl@sss.pgh.pa.us 235 : 4 : conn = connectMaintenanceDatabase(cparams, progname, echo);
786 andres@anarazel.de 236 : 4 : result = executeQuery(conn,
237 : : "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
238 : : echo);
8116 peter_e@gmx.net 239 : 4 : PQfinish(conn);
240 : :
241 [ + + ]: 17 : for (i = 0; i < PQntuples(result); i++)
242 : : {
243 : 13 : char *dbname = PQgetvalue(result, i, 0);
244 : :
245 [ + - ]: 13 : if (!quiet)
246 : : {
6669 247 : 13 : printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
248 : 13 : fflush(stdout);
249 : : }
250 : :
1783 tgl@sss.pgh.pa.us 251 : 13 : cparams->override_dbname = dbname;
252 : :
544 nathan@postgresql.or 253 [ + + ]: 13 : if (tables->head != NULL)
254 : : {
255 : : SimpleStringListCell *cell;
256 : :
257 [ + + ]: 4 : for (cell = tables->head; cell; cell = cell->next)
258 : 2 : cluster_one_database(cparams, cell->val,
259 : : progname, verbose, echo);
260 : : }
261 : : else
262 : 11 : cluster_one_database(cparams, NULL,
263 : : progname, verbose, echo);
264 : : }
265 : :
8116 peter_e@gmx.net 266 : 4 : PQclear(result);
267 : 4 : }
268 : :
269 : :
270 : : static void
271 : 1 : help(const char *progname)
272 : : {
8081 273 : 1 : printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
8116 274 : 1 : printf(_("Usage:\n"));
275 : 1 : printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
276 : 1 : printf(_("\nOptions:\n"));
277 : 1 : printf(_(" -a, --all cluster all databases\n"));
278 : 1 : printf(_(" -d, --dbname=DBNAME database to cluster\n"));
279 : 1 : printf(_(" -e, --echo show the commands being sent to the server\n"));
280 : 1 : printf(_(" -q, --quiet don't write any messages\n"));
4615 magnus@hagander.net 281 : 1 : printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
6130 peter_e@gmx.net 282 : 1 : printf(_(" -v, --verbose write a lot of output\n"));
4828 283 : 1 : printf(_(" -V, --version output version information, then exit\n"));
284 : 1 : printf(_(" -?, --help show this help, then exit\n"));
8116 285 : 1 : printf(_("\nConnection options:\n"));
286 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
287 : 1 : printf(_(" -p, --port=PORT database server port\n"));
288 : 1 : printf(_(" -U, --username=USERNAME user name to connect as\n"));
6036 289 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
6479 tgl@sss.pgh.pa.us 290 : 1 : printf(_(" -W, --password force password prompt\n"));
5023 rhaas@postgresql.org 291 : 1 : printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
8116 peter_e@gmx.net 292 : 1 : printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
2017 peter@eisentraut.org 293 : 1 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
294 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
8116 peter_e@gmx.net 295 : 1 : }
|