PostgreSQL Source Code  git master
clusterdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clusterdb
4  *
5  * Portions Copyright (c) 2002-2024, 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, SimpleStringList *tables,
25  const char *progname, bool verbose, bool echo,
26  bool quiet);
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  ConnParams cparams;
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, "ad:eh:p:qt:U:vwW", long_options, &optindex)) != -1)
73  {
74  switch (c)
75  {
76  case 'a':
77  alldb = true;
78  break;
79  case 'd':
81  break;
82  case 'e':
83  echo = true;
84  break;
85  case 'h':
86  host = pg_strdup(optarg);
87  break;
88  case 'p':
90  break;
91  case 'q':
92  quiet = true;
93  break;
94  case 't':
96  break;
97  case 'U':
99  break;
100  case 'v':
101  verbose = true;
102  break;
103  case 'w':
104  prompt_password = TRI_NO;
105  break;
106  case 'W':
107  prompt_password = TRI_YES;
108  break;
109  case 2:
110  maintenance_db = pg_strdup(optarg);
111  break;
112  default:
113  /* getopt_long already emitted a complaint */
114  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
115  exit(1);
116  }
117  }
118 
119  /*
120  * Non-option argument specifies database name as long as it wasn't
121  * already specified with -d / --dbname
122  */
123  if (optind < argc && dbname == NULL)
124  {
125  dbname = argv[optind];
126  optind++;
127  }
128 
129  if (optind < argc)
130  {
131  pg_log_error("too many command-line arguments (first is \"%s\")",
132  argv[optind]);
133  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
134  exit(1);
135  }
136 
137  /* fill cparams except for dbname, which is set below */
138  cparams.pghost = host;
139  cparams.pgport = port;
140  cparams.pguser = username;
141  cparams.prompt_password = prompt_password;
142  cparams.override_dbname = NULL;
143 
144  setup_cancel_handler(NULL);
145 
146  if (alldb)
147  {
148  if (dbname)
149  pg_fatal("cannot cluster all databases and a specific one at the same time");
150 
151  cparams.dbname = maintenance_db;
152 
153  cluster_all_databases(&cparams, &tables,
154  progname, verbose, echo, quiet);
155  }
156  else
157  {
158  if (dbname == NULL)
159  {
160  if (getenv("PGDATABASE"))
161  dbname = getenv("PGDATABASE");
162  else if (getenv("PGUSER"))
163  dbname = getenv("PGUSER");
164  else
166  }
167 
168  cparams.dbname = dbname;
169 
170  if (tables.head != NULL)
171  {
172  SimpleStringListCell *cell;
173 
174  for (cell = tables.head; cell; cell = cell->next)
175  {
176  cluster_one_database(&cparams, cell->val,
177  progname, verbose, echo);
178  }
179  }
180  else
181  cluster_one_database(&cparams, NULL,
182  progname, verbose, echo);
183  }
184 
185  exit(0);
186 }
187 
188 
189 static void
190 cluster_one_database(const ConnParams *cparams, const char *table,
191  const char *progname, bool verbose, bool echo)
192 {
193  PQExpBufferData sql;
194 
195  PGconn *conn;
196 
197  conn = connectDatabase(cparams, progname, echo, false, true);
198 
199  initPQExpBuffer(&sql);
200 
201  appendPQExpBufferStr(&sql, "CLUSTER");
202  if (verbose)
203  appendPQExpBufferStr(&sql, " VERBOSE");
204  if (table)
205  {
206  appendPQExpBufferChar(&sql, ' ');
207  appendQualifiedRelation(&sql, table, conn, echo);
208  }
209  appendPQExpBufferChar(&sql, ';');
210 
211  if (!executeMaintenanceCommand(conn, sql.data, echo))
212  {
213  if (table)
214  pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
215  table, PQdb(conn), PQerrorMessage(conn));
216  else
217  pg_log_error("clustering of database \"%s\" failed: %s",
219  PQfinish(conn);
220  exit(1);
221  }
222  PQfinish(conn);
223  termPQExpBuffer(&sql);
224 }
225 
226 
227 static void
229  const char *progname, bool verbose, bool echo,
230  bool quiet)
231 {
232  PGconn *conn;
233  PGresult *result;
234  int i;
235 
236  conn = connectMaintenanceDatabase(cparams, progname, echo);
237  result = executeQuery(conn,
238  "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
239  echo);
240  PQfinish(conn);
241 
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  cparams->override_dbname = dbname;
253 
254  if (tables->head != NULL)
255  {
256  SimpleStringListCell *cell;
257 
258  for (cell = tables->head; cell; cell = cell->next)
259  cluster_one_database(cparams, cell->val,
260  progname, verbose, echo);
261  }
262  else
263  cluster_one_database(cparams, NULL,
264  progname, verbose, echo);
265  }
266 
267  PQclear(result);
268 }
269 
270 
271 static void
272 help(const char *progname)
273 {
274  printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
275  printf(_("Usage:\n"));
276  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
277  printf(_("\nOptions:\n"));
278  printf(_(" -a, --all cluster all databases\n"));
279  printf(_(" -d, --dbname=DBNAME database to cluster\n"));
280  printf(_(" -e, --echo show the commands being sent to the server\n"));
281  printf(_(" -q, --quiet don't write any messages\n"));
282  printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
283  printf(_(" -v, --verbose write a lot of output\n"));
284  printf(_(" -V, --version output version information, then exit\n"));
285  printf(_(" -?, --help show this help, then exit\n"));
286  printf(_("\nConnection options:\n"));
287  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
288  printf(_(" -p, --port=PORT database server port\n"));
289  printf(_(" -U, --username=USERNAME user name to connect as\n"));
290  printf(_(" -w, --no-password never prompt for password\n"));
291  printf(_(" -W, --password force password prompt\n"));
292  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
293  printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
294  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
295  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
296 }
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:69
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1205
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183
int main(int argc, char *argv[])
Definition: clusterdb.c:31
static void cluster_all_databases(ConnParams *cparams, SimpleStringList *tables, const char *progname, bool verbose, bool echo, bool quiet)
Definition: clusterdb.c:228
static void help(const char *progname)
Definition: clusterdb.c:272
static void cluster_one_database(const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
Definition: clusterdb.c:190
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
#define _(x)
Definition: elog.c:90
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7048
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7212
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4893
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
int verbose
static char * username
Definition: initdb.c:153
int i
Definition: isn.c:72
static void const char fflush(stdout)
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
const char * progname
Definition: main.c:43
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
#define pg_fatal(...)
static PGconn * connectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1663
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1883
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static int port
Definition: pg_regress.c:115
const char * get_progname(const char *argv0)
Definition: path.c:575
#define printf(...)
Definition: port.h:244
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
char * c
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: query_utils.c:74
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
char * dbname
Definition: streamutil.c:52
PGconn * conn
Definition: streamutil.c:55
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:92
char * pgport
Definition: pg_backup.h:86
char * pghost
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:85
enum trivalue prompt_password
Definition: connect_utils.h:32
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37