PostgreSQL Source Code  git master
clusterdb.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "fe_utils/cancel.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/query_utils.h"
#include "fe_utils/simple_list.h"
#include "fe_utils/string_utils.h"
Include dependency graph for clusterdb.c:

Go to the source code of this file.

Functions

static void cluster_one_database (const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
 
static void cluster_all_databases (ConnParams *cparams, const char *progname, bool verbose, bool echo, bool quiet)
 
static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ cluster_all_databases()

static void cluster_all_databases ( ConnParams cparams,
const char *  progname,
bool  verbose,
bool  echo,
bool  quiet 
)
static

Definition at line 229 of file clusterdb.c.

231 {
232  PGconn *conn;
233  PGresult *result;
234  int i;
235 
236  conn = connectMaintenanceDatabase(cparams, progname, echo);
237  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
238  PQfinish(conn);
239 
240  for (i = 0; i < PQntuples(result); i++)
241  {
242  char *dbname = PQgetvalue(result, i, 0);
243 
244  if (!quiet)
245  {
246  printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
247  fflush(stdout);
248  }
249 
250  cparams->override_dbname = dbname;
251 
252  cluster_one_database(cparams, NULL, progname, verbose, echo);
253  }
254 
255  PQclear(result);
256 }
static void cluster_one_database(const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
Definition: clusterdb.c:191
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
#define _(x)
Definition: elog.c:90
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
int i
Definition: isn.c:73
static void const char fflush(stdout)
const char * progname
Definition: main.c:45
static int verbose
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1861
#define printf(...)
Definition: port.h:244
char * dbname
Definition: streamutil.c:51
PGconn * conn
Definition: streamutil.c:54
char * override_dbname
Definition: pg_backup.h:90

References _, cluster_one_database(), conn, connectMaintenanceDatabase(), dbname, executeQuery(), fflush(), i, _connParams::override_dbname, PQclear(), PQfinish(), PQgetvalue(), PQntuples(), printf, progname, generate_unaccent_rules::stdout, and verbose.

Referenced by main().

◆ cluster_one_database()

static void cluster_one_database ( const ConnParams cparams,
const char *  table,
const char *  progname,
bool  verbose,
bool  echo 
)
static

Definition at line 191 of file clusterdb.c.

193 {
194  PQExpBufferData sql;
195 
196  PGconn *conn;
197 
198  conn = connectDatabase(cparams, progname, echo, false, false);
199 
200  initPQExpBuffer(&sql);
201 
202  appendPQExpBufferStr(&sql, "CLUSTER");
203  if (verbose)
204  appendPQExpBufferStr(&sql, " VERBOSE");
205  if (table)
206  {
207  appendPQExpBufferChar(&sql, ' ');
208  appendQualifiedRelation(&sql, table, conn, echo);
209  }
210  appendPQExpBufferChar(&sql, ';');
211 
212  if (!executeMaintenanceCommand(conn, sql.data, echo))
213  {
214  if (table)
215  pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
216  table, PQdb(conn), PQerrorMessage(conn));
217  else
218  pg_log_error("clustering of database \"%s\" failed: %s",
220  PQfinish(conn);
221  exit(1);
222  }
223  PQfinish(conn);
224  termPQExpBuffer(&sql);
225 }
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:69
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6589
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
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:1641
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
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: query_utils.c:74

References appendPQExpBufferChar(), appendPQExpBufferStr(), appendQualifiedRelation(), conn, connectDatabase(), PQExpBufferData::data, executeMaintenanceCommand(), exit(), initPQExpBuffer(), pg_log_error, PQdb(), PQerrorMessage(), PQfinish(), progname, termPQExpBuffer(), and verbose.

Referenced by cluster_all_databases(), and main().

◆ help()

static void help ( const char *  progname)
static

Definition at line 260 of file clusterdb.c.

261 {
262  printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
263  printf(_("Usage:\n"));
264  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
265  printf(_("\nOptions:\n"));
266  printf(_(" -a, --all cluster all databases\n"));
267  printf(_(" -d, --dbname=DBNAME database to cluster\n"));
268  printf(_(" -e, --echo show the commands being sent to the server\n"));
269  printf(_(" -q, --quiet don't write any messages\n"));
270  printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
271  printf(_(" -v, --verbose write a lot of output\n"));
272  printf(_(" -V, --version output version information, then exit\n"));
273  printf(_(" -?, --help show this help, then exit\n"));
274  printf(_("\nConnection options:\n"));
275  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
276  printf(_(" -p, --port=PORT database server port\n"));
277  printf(_(" -U, --username=USERNAME user name to connect as\n"));
278  printf(_(" -w, --no-password never prompt for password\n"));
279  printf(_(" -W, --password force password prompt\n"));
280  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
281  printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
282  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
283  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
284 }

References _, printf, and progname.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 30 of file clusterdb.c.

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':
80  break;
81  case 'U':
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':
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  /* getopt_long already emitted a complaint */
113  pg_log_error_hint("Try \"%s --help\" for more information.", 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  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
133  exit(1);
134  }
135 
136  /* fill cparams except for dbname, which is set below */
137  cparams.pghost = host;
138  cparams.pgport = port;
139  cparams.pguser = username;
140  cparams.prompt_password = prompt_password;
141  cparams.override_dbname = NULL;
142 
143  setup_cancel_handler(NULL);
144 
145  if (alldb)
146  {
147  if (dbname)
148  pg_fatal("cannot cluster all databases and a specific one at the same time");
149 
150  if (tables.head != NULL)
151  pg_fatal("cannot cluster specific table(s) in all databases");
152 
153  cparams.dbname = maintenance_db;
154 
155  cluster_all_databases(&cparams, progname, verbose, echo, quiet);
156  }
157  else
158  {
159  if (dbname == NULL)
160  {
161  if (getenv("PGDATABASE"))
162  dbname = getenv("PGDATABASE");
163  else if (getenv("PGUSER"))
164  dbname = getenv("PGUSER");
165  else
167  }
168 
169  cparams.dbname = dbname;
170 
171  if (tables.head != NULL)
172  {
173  SimpleStringListCell *cell;
174 
175  for (cell = tables.head; cell; cell = cell->next)
176  {
177  cluster_one_database(&cparams, cell->val,
178  progname, verbose, echo);
179  }
180  }
181  else
182  cluster_one_database(&cparams, NULL,
183  progname, verbose, echo);
184  }
185 
186  exit(0);
187 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1153
static void help(const char *progname)
Definition: clusterdb.c:260
static void cluster_all_databases(ConnParams *cparams, const char *progname, bool verbose, bool echo, bool quiet)
Definition: clusterdb.c:229
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:460
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:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error_hint(...)
Definition: logging.h:112
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
#define pg_fatal(...)
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static int port
Definition: pg_regress.c:90
const char * username
Definition: pgbench.c:305
const char * get_progname(const char *argv0)
Definition: path.c:574
char * c
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
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 * pgport
Definition: pg_backup.h:84
char * pghost
Definition: pg_backup.h:85
char * dbname
Definition: pg_backup.h:83
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

References cluster_all_databases(), cluster_one_database(), dbname, _connParams::dbname, exit(), get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), SimpleStringListCell::next, no_argument, optarg, optind, _connParams::override_dbname, pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, _connParams::pghost, _connParams::pgport, _connParams::pguser, port, progname, _connParams::prompt_password, required_argument, set_pglocale_pgservice(), setup_cancel_handler(), simple_string_list_append(), TRI_DEFAULT, TRI_NO, TRI_YES, username, SimpleStringListCell::val, and verbose.