PostgreSQL Source Code  git master
clusterdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clusterdb
4  *
5  * Portions Copyright (c) 2002-2021, 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 #include "fe_utils/string_utils.h"
20 
21 
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);
27 
28 
29 int
30 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  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};
64 
65  pg_logging_init(argv[0]);
66  progname = get_progname(argv[0]);
67  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
68 
69  handle_help_version_opts(argc, argv, "clusterdb", help);
70 
71  while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
72  {
73  switch (c)
74  {
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':
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);
114  }
115  }
116 
117  /*
118  * Non-option argument specifies database name as long as it wasn't
119  * already specified with -d / --dbname
120  */
121  if (optind < argc && dbname == NULL)
122  {
123  dbname = argv[optind];
124  optind++;
125  }
126 
127  if (optind < argc)
128  {
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);
133  }
134 
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;
141 
142  setup_cancel_handler(NULL);
143 
144  if (alldb)
145  {
146  if (dbname)
147  {
148  pg_log_error("cannot cluster all databases and a specific one at the same time");
149  exit(1);
150  }
151 
152  if (tables.head != NULL)
153  {
154  pg_log_error("cannot cluster specific table(s) in all databases");
155  exit(1);
156  }
157 
158  cparams.dbname = maintenance_db;
159 
160  cluster_all_databases(&cparams, progname, verbose, echo, quiet);
161  }
162  else
163  {
164  if (dbname == NULL)
165  {
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);
172  }
173 
174  cparams.dbname = dbname;
175 
176  if (tables.head != NULL)
177  {
178  SimpleStringListCell *cell;
179 
180  for (cell = tables.head; cell; cell = cell->next)
181  {
182  cluster_one_database(&cparams, cell->val,
183  progname, verbose, echo);
184  }
185  }
186  else
187  cluster_one_database(&cparams, NULL,
188  progname, verbose, echo);
189  }
190 
191  exit(0);
192 }
193 
194 
195 static void
196 cluster_one_database(const ConnParams *cparams, const char *table,
197  const char *progname, bool verbose, bool echo)
198 {
199  PQExpBufferData sql;
200 
201  PGconn *conn;
202 
203  conn = connectDatabase(cparams, progname, echo, false, false);
204 
205  initPQExpBuffer(&sql);
206 
207  appendPQExpBufferStr(&sql, "CLUSTER");
208  if (verbose)
209  appendPQExpBufferStr(&sql, " VERBOSE");
210  if (table)
211  {
212  appendPQExpBufferChar(&sql, ' ');
213  appendQualifiedRelation(&sql, table, conn, echo);
214  }
215  appendPQExpBufferChar(&sql, ';');
216 
217  if (!executeMaintenanceCommand(conn, sql.data, echo))
218  {
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);
227  }
228  PQfinish(conn);
229  termPQExpBuffer(&sql);
230 }
231 
232 
233 static void
235  bool verbose, bool echo, bool quiet)
236 {
237  PGconn *conn;
238  PGresult *result;
239  int i;
240 
241  conn = connectMaintenanceDatabase(cparams, progname, echo);
242  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
243  PQfinish(conn);
244 
245  for (i = 0; i < PQntuples(result); i++)
246  {
247  char *dbname = PQgetvalue(result, i, 0);
248 
249  if (!quiet)
250  {
251  printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
252  fflush(stdout);
253  }
254 
255  cparams->override_dbname = dbname;
256 
257  cluster_one_database(cparams, NULL, progname, verbose, echo);
258  }
259 
260  PQclear(result);
261 }
262 
263 
264 static void
265 help(const char *progname)
266 {
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);
289 }
static void cluster_all_databases(ConnParams *cparams, const char *progname, bool verbose, bool echo, bool quiet)
Definition: clusterdb.c:234
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1874
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6705
const char * progname
Definition: main.c:46
char * pgport
Definition: pg_backup.h:66
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3026
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:80
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:81
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4203
#define printf(...)
Definition: port.h:222
static void setup_cancel_handler(void)
Definition: parallel.c:613
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2632
#define fprintf
Definition: port.h:220
char * dbname
Definition: pg_backup.h:65
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:22
PGconn * conn
Definition: streamutil.c:54
char * c
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:30
const char * username
Definition: pgbench.c:246
static void help(const char *progname)
Definition: clusterdb.c:265
char * override_dbname
Definition: pg_backup.h:72
static int port
Definition: pg_regress.c:92
char * pghost
Definition: pg_backup.h:67
enum trivalue prompt_password
Definition: connect_utils.h:32
static int verbose
trivalue
Definition: vacuumlo.c:34
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1215
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void PQclear(PGresult *res)
Definition: fe-exec.c:676
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6555
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:1636
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:51
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:69
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: query_utils.c:74
char * optarg
Definition: getopt.c:52
int i
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
static void cluster_one_database(const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
Definition: clusterdb.c:196
#define _(x)
Definition: elog.c:89
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
const char * pguser
Definition: connect_utils.h:31
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74