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, SimpleStringList *tables, 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,
SimpleStringList tables,
const char *  progname,
bool  verbose,
bool  echo,
bool  quiet 
)
static

Definition at line 228 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,
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 }
static void cluster_one_database(const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
Definition: clusterdb.c:190
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
#define _(x)
Definition: elog.c:90
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4868
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
int verbose
int i
Definition: isn.c:73
static void const char fflush(stdout)
const char * progname
Definition: main.c:44
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1883
#define printf(...)
Definition: port.h:244
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
char * override_dbname
Definition: pg_backup.h:91

References _, cluster_one_database(), conn, connectMaintenanceDatabase(), dbname, executeQuery(), fflush(), SimpleStringList::head, i, SimpleStringListCell::next, _connParams::override_dbname, PQclear(), PQfinish(), PQgetvalue(), PQntuples(), printf, progname, generate_unaccent_rules::stdout, SimpleStringListCell::val, 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 190 of file clusterdb.c.

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 }
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:69
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6993
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7147
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:1663
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 272 of file clusterdb.c.

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 }

References _, printf, and progname.

Referenced by main().

◆ main()

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

Definition at line 31 of file clusterdb.c.

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 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1214
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183
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
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:448
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
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:116
const char * username
Definition: pgbench.c:296
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
const char * pguser
Definition: connect_utils.h:31
char * pgport
Definition: pg_backup.h:85
char * pghost
Definition: pg_backup.h:86
char * dbname
Definition: pg_backup.h:84
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.