Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / scripts / clusterdb.c
blobfc771eed77bcc4c11a69eb43ffa93dc75cf16a6c
1 /*-------------------------------------------------------------------------
3 * clusterdb
5 * Portions Copyright (c) 2002-2021, PostgreSQL Global Development Group
7 * src/bin/scripts/clusterdb.c
9 *-------------------------------------------------------------------------
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"
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);
29 int
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},
45 {NULL, 0, NULL, 0}
48 const char *progname;
49 int optindex;
50 int c;
52 const char *dbname = NULL;
53 const char *maintenance_db = NULL;
54 char *host = NULL;
55 char *port = NULL;
56 char *username = NULL;
57 enum trivalue prompt_password = TRI_DEFAULT;
58 ConnParams cparams;
59 bool echo = false;
60 bool quiet = false;
61 bool alldb = false;
62 bool verbose = false;
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, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
73 switch (c)
75 case 'h':
76 host = pg_strdup(optarg);
77 break;
78 case 'p':
79 port = pg_strdup(optarg);
80 break;
81 case 'U':
82 username = pg_strdup(optarg);
83 break;
84 case 'w':
85 prompt_password = TRI_NO;
86 break;
87 case 'W':
88 prompt_password = TRI_YES;
89 break;
90 case 'e':
91 echo = true;
92 break;
93 case 'q':
94 quiet = true;
95 break;
96 case 'd':
97 dbname = pg_strdup(optarg);
98 break;
99 case 'a':
100 alldb = true;
101 break;
102 case 't':
103 simple_string_list_append(&tables, optarg);
104 break;
105 case 'v':
106 verbose = true;
107 break;
108 case 2:
109 maintenance_db = pg_strdup(optarg);
110 break;
111 default:
112 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
113 exit(1);
118 * Non-option argument specifies database name as long as it wasn't
119 * already specified with -d / --dbname
121 if (optind < argc && dbname == NULL)
123 dbname = argv[optind];
124 optind++;
127 if (optind < argc)
129 pg_log_error("too many command-line arguments (first is \"%s\")",
130 argv[optind]);
131 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
132 exit(1);
135 /* fill cparams except for dbname, which is set below */
136 cparams.pghost = host;
137 cparams.pgport = port;
138 cparams.pguser = username;
139 cparams.prompt_password = prompt_password;
140 cparams.override_dbname = NULL;
142 setup_cancel_handler(NULL);
144 if (alldb)
146 if (dbname)
148 pg_log_error("cannot cluster all databases and a specific one at the same time");
149 exit(1);
152 if (tables.head != NULL)
154 pg_log_error("cannot cluster specific table(s) in all databases");
155 exit(1);
158 cparams.dbname = maintenance_db;
160 cluster_all_databases(&cparams, progname, verbose, echo, quiet);
162 else
164 if (dbname == NULL)
166 if (getenv("PGDATABASE"))
167 dbname = getenv("PGDATABASE");
168 else if (getenv("PGUSER"))
169 dbname = getenv("PGUSER");
170 else
171 dbname = get_user_name_or_exit(progname);
174 cparams.dbname = dbname;
176 if (tables.head != NULL)
178 SimpleStringListCell *cell;
180 for (cell = tables.head; cell; cell = cell->next)
182 cluster_one_database(&cparams, cell->val,
183 progname, verbose, echo);
186 else
187 cluster_one_database(&cparams, NULL,
188 progname, verbose, echo);
191 exit(0);
195 static void
196 cluster_one_database(const ConnParams *cparams, const char *table,
197 const char *progname, bool verbose, bool echo)
199 PQExpBufferData sql;
201 PGconn *conn;
203 conn = connectDatabase(cparams, progname, echo, false, false);
205 initPQExpBuffer(&sql);
207 appendPQExpBufferStr(&sql, "CLUSTER");
208 if (verbose)
209 appendPQExpBufferStr(&sql, " VERBOSE");
210 if (table)
212 appendPQExpBufferChar(&sql, ' ');
213 appendQualifiedRelation(&sql, table, conn, echo);
215 appendPQExpBufferChar(&sql, ';');
217 if (!executeMaintenanceCommand(conn, sql.data, echo))
219 if (table)
220 pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
221 table, PQdb(conn), PQerrorMessage(conn));
222 else
223 pg_log_error("clustering of database \"%s\" failed: %s",
224 PQdb(conn), PQerrorMessage(conn));
225 PQfinish(conn);
226 exit(1);
228 PQfinish(conn);
229 termPQExpBuffer(&sql);
233 static void
234 cluster_all_databases(ConnParams *cparams, const char *progname,
235 bool verbose, bool echo, bool quiet)
237 PGconn *conn;
238 PGresult *result;
239 int i;
241 conn = connectMaintenanceDatabase(cparams, progname, echo);
242 result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
243 PQfinish(conn);
245 for (i = 0; i < PQntuples(result); i++)
247 char *dbname = PQgetvalue(result, i, 0);
249 if (!quiet)
251 printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
252 fflush(stdout);
255 cparams->override_dbname = dbname;
257 cluster_one_database(cparams, NULL, progname, verbose, echo);
260 PQclear(result);
264 static void
265 help(const char *progname)
267 printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
268 printf(_("Usage:\n"));
269 printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
270 printf(_("\nOptions:\n"));
271 printf(_(" -a, --all cluster all databases\n"));
272 printf(_(" -d, --dbname=DBNAME database to cluster\n"));
273 printf(_(" -e, --echo show the commands being sent to the server\n"));
274 printf(_(" -q, --quiet don't write any messages\n"));
275 printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
276 printf(_(" -v, --verbose write a lot of output\n"));
277 printf(_(" -V, --version output version information, then exit\n"));
278 printf(_(" -?, --help show this help, then exit\n"));
279 printf(_("\nConnection options:\n"));
280 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
281 printf(_(" -p, --port=PORT database server port\n"));
282 printf(_(" -U, --username=USERNAME user name to connect as\n"));
283 printf(_(" -w, --no-password never prompt for password\n"));
284 printf(_(" -W, --password force password prompt\n"));
285 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
286 printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
287 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
288 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);