PostgreSQL Source Code  git master
clusterdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clusterdb
4  *
5  * Portions Copyright (c) 2002-2019, 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/simple_list.h"
16 #include "fe_utils/string_utils.h"
17 
18 
19 static void cluster_one_database(const char *dbname, bool verbose, const char *table,
20  const char *host, const char *port,
21  const char *username, enum trivalue prompt_password,
22  const char *progname, bool echo);
23 static void cluster_all_databases(bool verbose, const char *maintenance_db,
24  const char *host, const char *port,
25  const char *username, enum trivalue prompt_password,
26  const char *progname, bool echo, bool quiet);
27 
28 static void help(const char *progname);
29 
30 
31 int
32 main(int argc, char *argv[])
33 {
34  static struct option long_options[] = {
35  {"host", required_argument, NULL, 'h'},
36  {"port", required_argument, NULL, 'p'},
37  {"username", required_argument, NULL, 'U'},
38  {"no-password", no_argument, NULL, 'w'},
39  {"password", no_argument, NULL, 'W'},
40  {"echo", no_argument, NULL, 'e'},
41  {"quiet", no_argument, NULL, 'q'},
42  {"dbname", required_argument, NULL, 'd'},
43  {"all", no_argument, NULL, 'a'},
44  {"table", required_argument, NULL, 't'},
45  {"verbose", no_argument, NULL, 'v'},
46  {"maintenance-db", required_argument, NULL, 2},
47  {NULL, 0, NULL, 0}
48  };
49 
50  const char *progname;
51  int optindex;
52  int c;
53 
54  const char *dbname = NULL;
55  const char *maintenance_db = NULL;
56  char *host = NULL;
57  char *port = NULL;
58  char *username = NULL;
59  enum trivalue prompt_password = TRI_DEFAULT;
60  bool echo = false;
61  bool quiet = false;
62  bool alldb = false;
63  bool verbose = false;
64  SimpleStringList tables = {NULL, NULL};
65 
66  pg_logging_init(argv[0]);
67  progname = get_progname(argv[0]);
68  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
69 
70  handle_help_version_opts(argc, argv, "clusterdb", help);
71 
72  while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
73  {
74  switch (c)
75  {
76  case 'h':
77  host = pg_strdup(optarg);
78  break;
79  case 'p':
80  port = pg_strdup(optarg);
81  break;
82  case 'U':
83  username = pg_strdup(optarg);
84  break;
85  case 'w':
86  prompt_password = TRI_NO;
87  break;
88  case 'W':
89  prompt_password = TRI_YES;
90  break;
91  case 'e':
92  echo = true;
93  break;
94  case 'q':
95  quiet = true;
96  break;
97  case 'd':
98  dbname = pg_strdup(optarg);
99  break;
100  case 'a':
101  alldb = true;
102  break;
103  case 't':
105  break;
106  case 'v':
107  verbose = true;
108  break;
109  case 2:
110  maintenance_db = pg_strdup(optarg);
111  break;
112  default:
113  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
114  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  */
122  if (optind < argc && dbname == NULL)
123  {
124  dbname = argv[optind];
125  optind++;
126  }
127 
128  if (optind < argc)
129  {
130  pg_log_error("too many command-line arguments (first is \"%s\")",
131  argv[optind]);
132  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
133  exit(1);
134  }
135 
137 
138  if (alldb)
139  {
140  if (dbname)
141  {
142  pg_log_error("cannot cluster all databases and a specific one at the same time");
143  exit(1);
144  }
145 
146  if (tables.head != NULL)
147  {
148  pg_log_error("cannot cluster specific table(s) in all databases");
149  exit(1);
150  }
151 
152  cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
153  progname, echo, quiet);
154  }
155  else
156  {
157  if (dbname == NULL)
158  {
159  if (getenv("PGDATABASE"))
160  dbname = getenv("PGDATABASE");
161  else if (getenv("PGUSER"))
162  dbname = getenv("PGUSER");
163  else
164  dbname = get_user_name_or_exit(progname);
165  }
166 
167  if (tables.head != NULL)
168  {
169  SimpleStringListCell *cell;
170 
171  for (cell = tables.head; cell; cell = cell->next)
172  {
173  cluster_one_database(dbname, verbose, cell->val,
174  host, port, username, prompt_password,
175  progname, echo);
176  }
177  }
178  else
179  cluster_one_database(dbname, verbose, NULL,
180  host, port, username, prompt_password,
181  progname, echo);
182  }
183 
184  exit(0);
185 }
186 
187 
188 static void
189 cluster_one_database(const char *dbname, bool verbose, const char *table,
190  const char *host, const char *port,
191  const char *username, enum trivalue prompt_password,
192  const char *progname, bool echo)
193 {
194  PQExpBufferData sql;
195 
196  PGconn *conn;
197 
198  conn = connectDatabase(dbname, host, port, username, prompt_password,
199  progname, echo, false, false);
200 
201  initPQExpBuffer(&sql);
202 
203  appendPQExpBufferStr(&sql, "CLUSTER");
204  if (verbose)
205  appendPQExpBufferStr(&sql, " VERBOSE");
206  if (table)
207  {
208  appendPQExpBufferChar(&sql, ' ');
209  appendQualifiedRelation(&sql, table, conn, echo);
210  }
211  appendPQExpBufferChar(&sql, ';');
212 
213  if (!executeMaintenanceCommand(conn, sql.data, echo))
214  {
215  if (table)
216  pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
217  table, PQdb(conn), PQerrorMessage(conn));
218  else
219  pg_log_error("clustering of database \"%s\" failed: %s",
220  PQdb(conn), PQerrorMessage(conn));
221  PQfinish(conn);
222  exit(1);
223  }
224  PQfinish(conn);
225  termPQExpBuffer(&sql);
226 }
227 
228 
229 static void
230 cluster_all_databases(bool verbose, const char *maintenance_db,
231  const char *host, const char *port,
232  const char *username, enum trivalue prompt_password,
233  const char *progname, bool echo, bool quiet)
234 {
235  PGconn *conn;
236  PGresult *result;
238  int i;
239 
240  conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
241  prompt_password, progname, echo);
242  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
243  PQfinish(conn);
244 
245  initPQExpBuffer(&connstr);
246  for (i = 0; i < PQntuples(result); i++)
247  {
248  char *dbname = PQgetvalue(result, i, 0);
249 
250  if (!quiet)
251  {
252  printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
253  fflush(stdout);
254  }
255 
256  resetPQExpBuffer(&connstr);
257  appendPQExpBufferStr(&connstr, "dbname=");
258  appendConnStrVal(&connstr, dbname);
259 
260  cluster_one_database(connstr.data, verbose, NULL,
261  host, port, username, prompt_password,
262  progname, echo);
263  }
264  termPQExpBuffer(&connstr);
265 
266  PQclear(result);
267 }
268 
269 
270 static void
271 help(const char *progname)
272 {
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 <pgsql-bugs@lists.postgresql.org>.\n"));
294 }
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo)
Definition: common.c:160
static void cluster_all_databases(bool verbose, const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet)
Definition: clusterdb.c:230
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1879
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6596
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
const char * get_progname(const char *argv0)
Definition: path.c:453
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void pg_logging_init(const char *argv0)
Definition: logging.c:39
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:547
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4098
#define printf(...)
Definition: port.h:198
static void setup_cancel_handler(void)
Definition: parallel.c:618
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
#define fprintf
Definition: port.h:196
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
PGconn * conn
Definition: streamutil.c:56
char * c
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:262
struct SimpleStringListCell * next
Definition: simple_list.h:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int main(int argc, char *argv[])
Definition: clusterdb.c:32
static void help(const char *progname)
Definition: clusterdb.c:271
static int port
Definition: pg_regress.c:92
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:40
static int verbose
Definition: pg_basebackup.c:90
trivalue
Definition: vacuumlo.c:34
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1135
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
static char * username
Definition: initdb.c:133
static void cluster_one_database(const char *dbname, bool verbose, const char *table, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo)
Definition: clusterdb.c:189
void PQclear(PGresult *res)
Definition: fe-exec.c:695
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6446
static PGconn * connectDatabase(const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1635
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:52
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:376
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
char * optarg
Definition: getopt.c:52
int i
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
static char * connstr
Definition: pg_dumpall.c:62
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74