1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 2002-2024, PostgreSQL Global Development Group
7 * src/bin/scripts/clusterdb.c
9 *-------------------------------------------------------------------------
12 #include "postgres_fe.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"
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
,
26 static void help(const char *progname
);
30 main(int argc
, char *argv
[])
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},
52 const char *dbname
= NULL
;
53 const char *maintenance_db
= NULL
;
56 char *username
= NULL
;
57 enum trivalue prompt_password
= TRI_DEFAULT
;
63 SimpleStringList tables
= {NULL
, NULL
};
65 pg_logging_init(argv
[0]);
66 progname
= get_progname(argv
[0]);
67 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
69 handle_help_version_opts(argc
, argv
, "clusterdb", help
);
71 while ((c
= getopt_long(argc
, argv
, "ad:eh:p:qt:U:vwW", long_options
, &optindex
)) != -1)
79 dbname
= pg_strdup(optarg
);
85 host
= pg_strdup(optarg
);
88 port
= pg_strdup(optarg
);
94 simple_string_list_append(&tables
, optarg
);
97 username
= pg_strdup(optarg
);
103 prompt_password
= TRI_NO
;
106 prompt_password
= TRI_YES
;
109 maintenance_db
= pg_strdup(optarg
);
112 /* getopt_long already emitted a complaint */
113 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
119 * Non-option argument specifies database name as long as it wasn't
120 * already specified with -d / --dbname
122 if (optind
< argc
&& dbname
== NULL
)
124 dbname
= argv
[optind
];
130 pg_log_error("too many command-line arguments (first is \"%s\")",
132 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
136 /* fill cparams except for dbname, which is set below */
137 cparams
.pghost
= host
;
138 cparams
.pgport
= port
;
139 cparams
.pguser
= username
;
140 cparams
.prompt_password
= prompt_password
;
141 cparams
.override_dbname
= NULL
;
143 setup_cancel_handler(NULL
);
148 pg_fatal("cannot cluster all databases and a specific one at the same time");
150 cparams
.dbname
= maintenance_db
;
152 cluster_all_databases(&cparams
, &tables
,
153 progname
, verbose
, echo
, quiet
);
159 if (getenv("PGDATABASE"))
160 dbname
= getenv("PGDATABASE");
161 else if (getenv("PGUSER"))
162 dbname
= getenv("PGUSER");
164 dbname
= get_user_name_or_exit(progname
);
167 cparams
.dbname
= dbname
;
169 if (tables
.head
!= NULL
)
171 SimpleStringListCell
*cell
;
173 for (cell
= tables
.head
; cell
; cell
= cell
->next
)
175 cluster_one_database(&cparams
, cell
->val
,
176 progname
, verbose
, echo
);
180 cluster_one_database(&cparams
, NULL
,
181 progname
, verbose
, echo
);
189 cluster_one_database(const ConnParams
*cparams
, const char *table
,
190 const char *progname
, bool verbose
, bool echo
)
196 conn
= connectDatabase(cparams
, progname
, echo
, false, true);
198 initPQExpBuffer(&sql
);
200 appendPQExpBufferStr(&sql
, "CLUSTER");
202 appendPQExpBufferStr(&sql
, " VERBOSE");
205 appendPQExpBufferChar(&sql
, ' ');
206 appendQualifiedRelation(&sql
, table
, conn
, echo
);
208 appendPQExpBufferChar(&sql
, ';');
210 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
213 pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
214 table
, PQdb(conn
), PQerrorMessage(conn
));
216 pg_log_error("clustering of database \"%s\" failed: %s",
217 PQdb(conn
), PQerrorMessage(conn
));
222 termPQExpBuffer(&sql
);
227 cluster_all_databases(ConnParams
*cparams
, SimpleStringList
*tables
,
228 const char *progname
, bool verbose
, bool echo
,
235 conn
= connectMaintenanceDatabase(cparams
, progname
, echo
);
236 result
= executeQuery(conn
,
237 "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
241 for (i
= 0; i
< PQntuples(result
); i
++)
243 char *dbname
= PQgetvalue(result
, i
, 0);
247 printf(_("%s: clustering database \"%s\"\n"), progname
, dbname
);
251 cparams
->override_dbname
= dbname
;
253 if (tables
->head
!= NULL
)
255 SimpleStringListCell
*cell
;
257 for (cell
= tables
->head
; cell
; cell
= cell
->next
)
258 cluster_one_database(cparams
, cell
->val
,
259 progname
, verbose
, echo
);
262 cluster_one_database(cparams
, NULL
,
263 progname
, verbose
, echo
);
271 help(const char *progname
)
273 printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname
);
274 printf(_("Usage:\n"));
275 printf(_(" %s [OPTION]... [DBNAME]\n"), progname
);
276 printf(_("\nOptions:\n"));
277 printf(_(" -a, --all cluster all databases\n"));
278 printf(_(" -d, --dbname=DBNAME database to cluster\n"));
279 printf(_(" -e, --echo show the commands being sent to the server\n"));
280 printf(_(" -q, --quiet don't write any messages\n"));
281 printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
282 printf(_(" -v, --verbose write a lot of output\n"));
283 printf(_(" -V, --version output version information, then exit\n"));
284 printf(_(" -?, --help show this help, then exit\n"));
285 printf(_("\nConnection options:\n"));
286 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
287 printf(_(" -p, --port=PORT database server port\n"));
288 printf(_(" -U, --username=USERNAME user name to connect as\n"));
289 printf(_(" -w, --no-password never prompt for password\n"));
290 printf(_(" -W, --password force password prompt\n"));
291 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
292 printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
293 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
294 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);