1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 2002-2021, 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"
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
);
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
, "h:p:U:wWeqd:at:v", long_options
, &optindex
)) != -1)
76 host
= pg_strdup(optarg
);
79 port
= pg_strdup(optarg
);
82 username
= pg_strdup(optarg
);
85 prompt_password
= TRI_NO
;
88 prompt_password
= TRI_YES
;
97 dbname
= pg_strdup(optarg
);
103 simple_string_list_append(&tables
, optarg
);
109 maintenance_db
= pg_strdup(optarg
);
112 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
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
];
129 pg_log_error("too many command-line arguments (first is \"%s\")",
131 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
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
);
148 pg_log_error("cannot cluster all databases and a specific one at the same time");
152 if (tables
.head
!= NULL
)
154 pg_log_error("cannot cluster specific table(s) in all databases");
158 cparams
.dbname
= maintenance_db
;
160 cluster_all_databases(&cparams
, progname
, verbose
, echo
, quiet
);
166 if (getenv("PGDATABASE"))
167 dbname
= getenv("PGDATABASE");
168 else if (getenv("PGUSER"))
169 dbname
= getenv("PGUSER");
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
);
187 cluster_one_database(&cparams
, NULL
,
188 progname
, verbose
, echo
);
196 cluster_one_database(const ConnParams
*cparams
, const char *table
,
197 const char *progname
, bool verbose
, bool echo
)
203 conn
= connectDatabase(cparams
, progname
, echo
, false, false);
205 initPQExpBuffer(&sql
);
207 appendPQExpBufferStr(&sql
, "CLUSTER");
209 appendPQExpBufferStr(&sql
, " VERBOSE");
212 appendPQExpBufferChar(&sql
, ' ');
213 appendQualifiedRelation(&sql
, table
, conn
, echo
);
215 appendPQExpBufferChar(&sql
, ';');
217 if (!executeMaintenanceCommand(conn
, sql
.data
, echo
))
220 pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
221 table
, PQdb(conn
), PQerrorMessage(conn
));
223 pg_log_error("clustering of database \"%s\" failed: %s",
224 PQdb(conn
), PQerrorMessage(conn
));
229 termPQExpBuffer(&sql
);
234 cluster_all_databases(ConnParams
*cparams
, const char *progname
,
235 bool verbose
, bool echo
, bool quiet
)
241 conn
= connectMaintenanceDatabase(cparams
, progname
, echo
);
242 result
= executeQuery(conn
, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo
);
245 for (i
= 0; i
< PQntuples(result
); i
++)
247 char *dbname
= PQgetvalue(result
, i
, 0);
251 printf(_("%s: clustering database \"%s\"\n"), progname
, dbname
);
255 cparams
->override_dbname
= dbname
;
257 cluster_one_database(cparams
, NULL
, progname
, verbose
, echo
);
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
);