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/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 232 of file clusterdb.c.

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

Referenced by main().

234 {
235  PGconn *conn;
236  PGresult *result;
237  int i;
238 
239  conn = connectMaintenanceDatabase(cparams, progname, echo);
240  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
241  PQfinish(conn);
242 
243  for (i = 0; i < PQntuples(result); i++)
244  {
245  char *dbname = PQgetvalue(result, i, 0);
246 
247  if (!quiet)
248  {
249  printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
250  fflush(stdout);
251  }
252 
253  cparams->override_dbname = dbname;
254 
255  cluster_one_database(cparams, NULL, progname, verbose, echo);
256  }
257 
258  PQclear(result);
259 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4174
#define printf(...)
Definition: port.h:221
const char * progname
Definition: pg_standby.c:36
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
PGconn * conn
Definition: streamutil.c:54
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
Definition: common.c:173
char * override_dbname
Definition: pg_backup.h:72
static int verbose
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * dbname
Definition: streamutil.c:51
int i
static void cluster_one_database(const ConnParams *cparams, const char *table, const char *progname, bool verbose, bool echo)
Definition: clusterdb.c:194
#define _(x)
Definition: elog.c:88

◆ 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 194 of file clusterdb.c.

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

Referenced by cluster_all_databases(), and main().

196 {
197  PQExpBufferData sql;
198 
199  PGconn *conn;
200 
201  conn = connectDatabase(cparams, progname, echo, false, false);
202 
203  initPQExpBuffer(&sql);
204 
205  appendPQExpBufferStr(&sql, "CLUSTER");
206  if (verbose)
207  appendPQExpBufferStr(&sql, " VERBOSE");
208  if (table)
209  {
210  appendPQExpBufferChar(&sql, ' ');
211  appendQualifiedRelation(&sql, table, conn, echo);
212  }
213  appendPQExpBufferChar(&sql, ';');
214 
215  if (!executeMaintenanceCommand(conn, sql.data, echo))
216  {
217  if (table)
218  pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
219  table, PQdb(conn), PQerrorMessage(conn));
220  else
221  pg_log_error("clustering of database \"%s\" failed: %s",
222  PQdb(conn), PQerrorMessage(conn));
223  PQfinish(conn);
224  exit(1);
225  }
226  PQfinish(conn);
227  termPQExpBuffer(&sql);
228 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
#define pg_log_error(...)
Definition: logging.h:80
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4174
const char * progname
Definition: pg_standby.c:36
PGconn * conn
Definition: streamutil.c:54
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:273
static int verbose
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6519
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
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:386
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92

◆ help()

static void help ( const char *  progname)
static

Definition at line 263 of file clusterdb.c.

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 28 of file clusterdb.c.

References _, cluster_all_databases(), cluster_one_database(), dbname, _connParams::dbname, fprintf, 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_log_error, 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.

29 {
30  static struct option long_options[] = {
31  {"host", required_argument, NULL, 'h'},
32  {"port", required_argument, NULL, 'p'},
33  {"username", required_argument, NULL, 'U'},
34  {"no-password", no_argument, NULL, 'w'},
35  {"password", no_argument, NULL, 'W'},
36  {"echo", no_argument, NULL, 'e'},
37  {"quiet", no_argument, NULL, 'q'},
38  {"dbname", required_argument, NULL, 'd'},
39  {"all", no_argument, NULL, 'a'},
40  {"table", required_argument, NULL, 't'},
41  {"verbose", no_argument, NULL, 'v'},
42  {"maintenance-db", required_argument, NULL, 2},
43  {NULL, 0, NULL, 0}
44  };
45 
46  const char *progname;
47  int optindex;
48  int c;
49 
50  const char *dbname = NULL;
51  const char *maintenance_db = NULL;
52  char *host = NULL;
53  char *port = NULL;
54  char *username = NULL;
55  enum trivalue prompt_password = TRI_DEFAULT;
56  ConnParams cparams;
57  bool echo = false;
58  bool quiet = false;
59  bool alldb = false;
60  bool verbose = false;
61  SimpleStringList tables = {NULL, NULL};
62 
63  pg_logging_init(argv[0]);
64  progname = get_progname(argv[0]);
65  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
66 
67  handle_help_version_opts(argc, argv, "clusterdb", help);
68 
69  while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
70  {
71  switch (c)
72  {
73  case 'h':
74  host = pg_strdup(optarg);
75  break;
76  case 'p':
77  port = pg_strdup(optarg);
78  break;
79  case 'U':
80  username = pg_strdup(optarg);
81  break;
82  case 'w':
83  prompt_password = TRI_NO;
84  break;
85  case 'W':
86  prompt_password = TRI_YES;
87  break;
88  case 'e':
89  echo = true;
90  break;
91  case 'q':
92  quiet = true;
93  break;
94  case 'd':
95  dbname = pg_strdup(optarg);
96  break;
97  case 'a':
98  alldb = true;
99  break;
100  case 't':
102  break;
103  case 'v':
104  verbose = true;
105  break;
106  case 2:
107  maintenance_db = pg_strdup(optarg);
108  break;
109  default:
110  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
111  exit(1);
112  }
113  }
114 
115  /*
116  * Non-option argument specifies database name as long as it wasn't
117  * already specified with -d / --dbname
118  */
119  if (optind < argc && dbname == NULL)
120  {
121  dbname = argv[optind];
122  optind++;
123  }
124 
125  if (optind < argc)
126  {
127  pg_log_error("too many command-line arguments (first is \"%s\")",
128  argv[optind]);
129  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
130  exit(1);
131  }
132 
133  /* fill cparams except for dbname, which is set below */
134  cparams.pghost = host;
135  cparams.pgport = port;
136  cparams.pguser = username;
137  cparams.prompt_password = prompt_password;
138  cparams.override_dbname = NULL;
139 
140  setup_cancel_handler(NULL);
141 
142  if (alldb)
143  {
144  if (dbname)
145  {
146  pg_log_error("cannot cluster all databases and a specific one at the same time");
147  exit(1);
148  }
149 
150  if (tables.head != NULL)
151  {
152  pg_log_error("cannot cluster specific table(s) in all databases");
153  exit(1);
154  }
155 
156  cparams.dbname = maintenance_db;
157 
158  cluster_all_databases(&cparams, progname, verbose, echo, quiet);
159  }
160  else
161  {
162  if (dbname == NULL)
163  {
164  if (getenv("PGDATABASE"))
165  dbname = getenv("PGDATABASE");
166  else if (getenv("PGUSER"))
167  dbname = getenv("PGUSER");
168  else
169  dbname = get_user_name_or_exit(progname);
170  }
171 
172  cparams.dbname = dbname;
173 
174  if (tables.head != NULL)
175  {
176  SimpleStringListCell *cell;
177 
178  for (cell = tables.head; cell; cell = cell->next)
179  {
180  cluster_one_database(&cparams, cell->val,
181  progname, verbose, echo);
182  }
183  }
184  else
185  cluster_one_database(&cparams, NULL,
186  progname, verbose, echo);
187  }
188 
189  exit(0);
190 }
static void cluster_all_databases(ConnParams *cparams, const char *progname, bool verbose, bool echo, bool quiet)
Definition: clusterdb.c:232
char * pgport
Definition: pg_backup.h:66
const char * get_progname(const char *argv0)
Definition: path.c:453
#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 pg_logging_init(const char *argv0)
Definition: logging.c:81
static void setup_cancel_handler(void)
Definition: parallel.c:613
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:219
char * dbname
Definition: pg_backup.h:65
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
char * c
struct SimpleStringListCell * next
Definition: simple_list.h:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void help(const char *progname)
Definition: clusterdb.c:263
char * override_dbname
Definition: pg_backup.h:72
static int port
Definition: pg_regress.c:92
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:34
char * pghost
Definition: pg_backup.h:67
enum trivalue prompt_password
Definition: common.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:1157
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
static char * username
Definition: initdb.c:134
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:51
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
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:194
#define _(x)
Definition: elog.c:88
const char * pguser
Definition: common.h:31
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74