PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
clusterdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clusterdb
4  *
5  * Portions Copyright (c) 2002-2017, 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 "fe_utils/simple_list.h"
15 #include "fe_utils/string_utils.h"
16 
17 
18 static void cluster_one_database(const char *dbname, bool verbose, const char *table,
19  const char *host, const char *port,
20  const char *username, enum trivalue prompt_password,
21  const char *progname, bool echo);
22 static void cluster_all_databases(bool verbose, const char *maintenance_db,
23  const char *host, const char *port,
24  const char *username, enum trivalue prompt_password,
25  const char *progname, bool echo, bool quiet);
26 
27 static void help(const char *progname);
28 
29 
30 int
31 main(int argc, char *argv[])
32 {
33  static struct option long_options[] = {
34  {"host", required_argument, NULL, 'h'},
35  {"port", required_argument, NULL, 'p'},
36  {"username", required_argument, NULL, 'U'},
37  {"no-password", no_argument, NULL, 'w'},
38  {"password", no_argument, NULL, 'W'},
39  {"echo", no_argument, NULL, 'e'},
40  {"quiet", no_argument, NULL, 'q'},
41  {"dbname", required_argument, NULL, 'd'},
42  {"all", no_argument, NULL, 'a'},
43  {"table", required_argument, NULL, 't'},
44  {"verbose", no_argument, NULL, 'v'},
45  {"maintenance-db", required_argument, NULL, 2},
46  {NULL, 0, NULL, 0}
47  };
48 
49  const char *progname;
50  int optindex;
51  int c;
52 
53  const char *dbname = NULL;
54  const char *maintenance_db = NULL;
55  char *host = NULL;
56  char *port = NULL;
57  char *username = NULL;
58  enum trivalue prompt_password = TRI_DEFAULT;
59  bool echo = false;
60  bool quiet = false;
61  bool alldb = false;
62  bool verbose = false;
63  SimpleStringList tables = {NULL, NULL};
64 
65  progname = get_progname(argv[0]);
66  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
67 
68  handle_help_version_opts(argc, argv, "clusterdb", help);
69 
70  while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
71  {
72  switch (c)
73  {
74  case 'h':
75  host = pg_strdup(optarg);
76  break;
77  case 'p':
78  port = pg_strdup(optarg);
79  break;
80  case 'U':
81  username = pg_strdup(optarg);
82  break;
83  case 'w':
84  prompt_password = TRI_NO;
85  break;
86  case 'W':
87  prompt_password = TRI_YES;
88  break;
89  case 'e':
90  echo = true;
91  break;
92  case 'q':
93  quiet = true;
94  break;
95  case 'd':
96  dbname = pg_strdup(optarg);
97  break;
98  case 'a':
99  alldb = true;
100  break;
101  case 't':
103  break;
104  case 'v':
105  verbose = true;
106  break;
107  case 2:
108  maintenance_db = pg_strdup(optarg);
109  break;
110  default:
111  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
112  exit(1);
113  }
114  }
115 
116  /*
117  * Non-option argument specifies database name as long as it wasn't
118  * already specified with -d / --dbname
119  */
120  if (optind < argc && dbname == NULL)
121  {
122  dbname = argv[optind];
123  optind++;
124  }
125 
126  if (optind < argc)
127  {
128  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
129  progname, argv[optind]);
130  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
131  exit(1);
132  }
133 
135 
136  if (alldb)
137  {
138  if (dbname)
139  {
140  fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"),
141  progname);
142  exit(1);
143  }
144 
145  if (tables.head != NULL)
146  {
147  fprintf(stderr, _("%s: cannot cluster specific table(s) in all databases\n"),
148  progname);
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  initPQExpBuffer(&sql);
199 
200  appendPQExpBufferStr(&sql, "CLUSTER");
201  if (verbose)
202  appendPQExpBufferStr(&sql, " VERBOSE");
203  if (table)
204  appendPQExpBuffer(&sql, " %s", table);
205  appendPQExpBufferChar(&sql, ';');
206 
207  conn = connectDatabase(dbname, host, port, username, prompt_password,
208  progname, false, false);
209  if (!executeMaintenanceCommand(conn, sql.data, echo))
210  {
211  if (table)
212  fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
213  progname, table, PQdb(conn), PQerrorMessage(conn));
214  else
215  fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"),
216  progname, PQdb(conn), PQerrorMessage(conn));
217  PQfinish(conn);
218  exit(1);
219  }
220  PQfinish(conn);
221  termPQExpBuffer(&sql);
222 }
223 
224 
225 static void
226 cluster_all_databases(bool verbose, const char *maintenance_db,
227  const char *host, const char *port,
228  const char *username, enum trivalue prompt_password,
229  const char *progname, bool echo, bool quiet)
230 {
231  PGconn *conn;
232  PGresult *result;
234  int i;
235 
236  conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
237  prompt_password, progname);
238  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
239  PQfinish(conn);
240 
241  initPQExpBuffer(&connstr);
242  for (i = 0; i < PQntuples(result); i++)
243  {
244  char *dbname = PQgetvalue(result, i, 0);
245 
246  if (!quiet)
247  {
248  printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
249  fflush(stdout);
250  }
251 
252  resetPQExpBuffer(&connstr);
253  appendPQExpBuffer(&connstr, "dbname=");
254  appendConnStrVal(&connstr, dbname);
255 
256  cluster_one_database(connstr.data, verbose, NULL,
257  host, port, username, prompt_password,
258  progname, echo);
259  }
260  termPQExpBuffer(&connstr);
261 
262  PQclear(result);
263 }
264 
265 
266 static void
267 help(const char *progname)
268 {
269  printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
270  printf(_("Usage:\n"));
271  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
272  printf(_("\nOptions:\n"));
273  printf(_(" -a, --all cluster all databases\n"));
274  printf(_(" -d, --dbname=DBNAME database to cluster\n"));
275  printf(_(" -e, --echo show the commands being sent to the server\n"));
276  printf(_(" -q, --quiet don't write any messages\n"));
277  printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
278  printf(_(" -v, --verbose write a lot of output\n"));
279  printf(_(" -V, --version output version information, then exit\n"));
280  printf(_(" -?, --help show this help, then exit\n"));
281  printf(_("\nConnection options:\n"));
282  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
283  printf(_(" -p, --port=PORT database server port\n"));
284  printf(_(" -U, --username=USERNAME user name to connect as\n"));
285  printf(_(" -w, --no-password never prompt for password\n"));
286  printf(_(" -W, --password force password prompt\n"));
287  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
288  printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
289  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
290 }
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:226
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * get_progname(const char *argv0)
Definition: path.c:453
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
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:385
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
static void setup_cancel_handler(void)
Definition: parallel.c:622
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
PGconn * conn
Definition: streamutil.c:46
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
char * c
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:235
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:31
static void help(const char *progname)
Definition: clusterdb.c:267
static int port
Definition: pg_regress.c:90
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:35
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname)
Definition: common.c:152
static int verbose
Definition: pg_basebackup.c:85
trivalue
Definition: vacuumlo.c:31
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1018
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:132
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:671
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5965
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:1891
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:42
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
int i
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:64
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74