PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
reindexdb.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "fe_utils/simple_list.h"
#include "fe_utils/string_utils.h"
Include dependency graph for reindexdb.c:

Go to the source code of this file.

Functions

static void reindex_one_database (const char *name, const char *dbname, const char *type, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool verbose)
 
static void reindex_all_databases (const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet, bool verbose)
 
static void reindex_system_catalogs (const char *dbname, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool verbose)
 
static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

static void help ( const char *  progname)
static

Definition at line 398 of file reindexdb.c.

References _.

Referenced by main().

399 {
400  printf(_("%s reindexes a PostgreSQL database.\n\n"), progname);
401  printf(_("Usage:\n"));
402  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
403  printf(_("\nOptions:\n"));
404  printf(_(" -a, --all reindex all databases\n"));
405  printf(_(" -d, --dbname=DBNAME database to reindex\n"));
406  printf(_(" -e, --echo show the commands being sent to the server\n"));
407  printf(_(" -i, --index=INDEX recreate specific index(es) only\n"));
408  printf(_(" -q, --quiet don't write any messages\n"));
409  printf(_(" -s, --system reindex system catalogs\n"));
410  printf(_(" -S, --schema=SCHEMA reindex specific schema(s) only\n"));
411  printf(_(" -t, --table=TABLE reindex specific table(s) only\n"));
412  printf(_(" -v, --verbose write a lot of output\n"));
413  printf(_(" -V, --version output version information, then exit\n"));
414  printf(_(" -?, --help show this help, then exit\n"));
415  printf(_("\nConnection options:\n"));
416  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
417  printf(_(" -p, --port=PORT database server port\n"));
418  printf(_(" -U, --username=USERNAME user name to connect as\n"));
419  printf(_(" -w, --no-password never prompt for password\n"));
420  printf(_(" -W, --password force password prompt\n"));
421  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
422  printf(_("\nRead the description of the SQL command REINDEX for details.\n"));
423  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
424 }
const char * progname
Definition: pg_standby.c:37
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char *  argv[] 
)

Definition at line 35 of file reindexdb.c.

References _, dbname, get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), SimpleStringListCell::next, no_argument, NULL, optarg, optind, pg_strdup(), PG_TEXTDOMAIN, port, progname, reindex_all_databases(), reindex_one_database(), reindex_system_catalogs(), required_argument, set_pglocale_pgservice(), setup_cancel_handler(), simple_string_list_append(), TRI_DEFAULT, TRI_NO, TRI_YES, username, SimpleStringListCell::val, and verbose.

36 {
37  static struct option long_options[] = {
38  {"host", required_argument, NULL, 'h'},
39  {"port", required_argument, NULL, 'p'},
40  {"username", required_argument, NULL, 'U'},
41  {"no-password", no_argument, NULL, 'w'},
42  {"password", no_argument, NULL, 'W'},
43  {"echo", no_argument, NULL, 'e'},
44  {"quiet", no_argument, NULL, 'q'},
45  {"schema", required_argument, NULL, 'S'},
46  {"dbname", required_argument, NULL, 'd'},
47  {"all", no_argument, NULL, 'a'},
48  {"system", no_argument, NULL, 's'},
49  {"table", required_argument, NULL, 't'},
50  {"index", required_argument, NULL, 'i'},
51  {"verbose", no_argument, NULL, 'v'},
52  {"maintenance-db", required_argument, NULL, 2},
53  {NULL, 0, NULL, 0}
54  };
55 
56  const char *progname;
57  int optindex;
58  int c;
59 
60  const char *dbname = NULL;
61  const char *maintenance_db = NULL;
62  const char *host = NULL;
63  const char *port = NULL;
64  const char *username = NULL;
65  enum trivalue prompt_password = TRI_DEFAULT;
66  bool syscatalog = false;
67  bool alldb = false;
68  bool echo = false;
69  bool quiet = false;
70  bool verbose = false;
71  SimpleStringList indexes = {NULL, NULL};
72  SimpleStringList tables = {NULL, NULL};
73  SimpleStringList schemas = {NULL, NULL};
74 
75  progname = get_progname(argv[0]);
76  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
77 
78  handle_help_version_opts(argc, argv, "reindexdb", help);
79 
80  /* process command-line options */
81  while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:v", long_options, &optindex)) != -1)
82  {
83  switch (c)
84  {
85  case 'h':
86  host = pg_strdup(optarg);
87  break;
88  case 'p':
89  port = pg_strdup(optarg);
90  break;
91  case 'U':
92  username = pg_strdup(optarg);
93  break;
94  case 'w':
95  prompt_password = TRI_NO;
96  break;
97  case 'W':
98  prompt_password = TRI_YES;
99  break;
100  case 'e':
101  echo = true;
102  break;
103  case 'q':
104  quiet = true;
105  break;
106  case 'S':
108  break;
109  case 'd':
110  dbname = pg_strdup(optarg);
111  break;
112  case 'a':
113  alldb = true;
114  break;
115  case 's':
116  syscatalog = true;
117  break;
118  case 't':
120  break;
121  case 'i':
123  break;
124  case 'v':
125  verbose = true;
126  break;
127  case 2:
128  maintenance_db = pg_strdup(optarg);
129  break;
130  default:
131  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
132  exit(1);
133  }
134  }
135 
136  /*
137  * Non-option argument specifies database name as long as it wasn't
138  * already specified with -d / --dbname
139  */
140  if (optind < argc && dbname == NULL)
141  {
142  dbname = argv[optind];
143  optind++;
144  }
145 
146  if (optind < argc)
147  {
148  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
149  progname, argv[optind]);
150  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
151  exit(1);
152  }
153 
155 
156  if (alldb)
157  {
158  if (dbname)
159  {
160  fprintf(stderr, _("%s: cannot reindex all databases and a specific one at the same time\n"), progname);
161  exit(1);
162  }
163  if (syscatalog)
164  {
165  fprintf(stderr, _("%s: cannot reindex all databases and system catalogs at the same time\n"), progname);
166  exit(1);
167  }
168  if (schemas.head != NULL)
169  {
170  fprintf(stderr, _("%s: cannot reindex specific schema(s) in all databases\n"), progname);
171  exit(1);
172  }
173  if (tables.head != NULL)
174  {
175  fprintf(stderr, _("%s: cannot reindex specific table(s) in all databases\n"), progname);
176  exit(1);
177  }
178  if (indexes.head != NULL)
179  {
180  fprintf(stderr, _("%s: cannot reindex specific index(es) in all databases\n"), progname);
181  exit(1);
182  }
183 
184  reindex_all_databases(maintenance_db, host, port, username,
185  prompt_password, progname, echo, quiet, verbose);
186  }
187  else if (syscatalog)
188  {
189  if (schemas.head != NULL)
190  {
191  fprintf(stderr, _("%s: cannot reindex specific schema(s) and system catalogs at the same time\n"), progname);
192  exit(1);
193  }
194  if (tables.head != NULL)
195  {
196  fprintf(stderr, _("%s: cannot reindex specific table(s) and system catalogs at the same time\n"), progname);
197  exit(1);
198  }
199  if (indexes.head != NULL)
200  {
201  fprintf(stderr, _("%s: cannot reindex specific index(es) and system catalogs at the same time\n"), progname);
202  exit(1);
203  }
204 
205  if (dbname == NULL)
206  {
207  if (getenv("PGDATABASE"))
208  dbname = getenv("PGDATABASE");
209  else if (getenv("PGUSER"))
210  dbname = getenv("PGUSER");
211  else
212  dbname = get_user_name_or_exit(progname);
213  }
214 
215  reindex_system_catalogs(dbname, host, port, username, prompt_password,
216  progname, echo, verbose);
217  }
218  else
219  {
220  if (dbname == NULL)
221  {
222  if (getenv("PGDATABASE"))
223  dbname = getenv("PGDATABASE");
224  else if (getenv("PGUSER"))
225  dbname = getenv("PGUSER");
226  else
227  dbname = get_user_name_or_exit(progname);
228  }
229 
230  if (schemas.head != NULL)
231  {
232  SimpleStringListCell *cell;
233 
234  for (cell = schemas.head; cell; cell = cell->next)
235  {
236  reindex_one_database(cell->val, dbname, "SCHEMA", host, port,
237  username, prompt_password, progname, echo, verbose);
238  }
239  }
240 
241  if (indexes.head != NULL)
242  {
243  SimpleStringListCell *cell;
244 
245  for (cell = indexes.head; cell; cell = cell->next)
246  {
247  reindex_one_database(cell->val, dbname, "INDEX", host, port,
248  username, prompt_password, progname, echo, verbose);
249  }
250  }
251  if (tables.head != NULL)
252  {
253  SimpleStringListCell *cell;
254 
255  for (cell = tables.head; cell; cell = cell->next)
256  {
257  reindex_one_database(cell->val, dbname, "TABLE", host, port,
258  username, prompt_password, progname, echo, verbose);
259  }
260  }
261 
262  /*
263  * reindex database only if neither index nor table nor schema is
264  * specified
265  */
266  if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
267  reindex_one_database(NULL, dbname, "DATABASE", host, port,
268  username, prompt_password, progname, echo, verbose);
269  }
270 
271  exit(0);
272 }
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void setup_cancel_handler(void)
Definition: parallel.c:620
const char * progname
Definition: pg_standby.c:37
static void help(const char *progname)
Definition: reindexdb.c:398
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
char * c
static void reindex_all_databases(const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool quiet, bool verbose)
Definition: reindexdb.c:327
struct SimpleStringListCell * next
Definition: simple_list.h:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int port
Definition: pg_regress.c:89
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:35
static int verbose
Definition: pg_basebackup.c:84
trivalue
Definition: vacuumlo.c:31
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1013
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
static char * username
Definition: initdb.c:131
#define NULL
Definition: c.h:229
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:39
static void reindex_system_catalogs(const char *dbname, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool verbose)
Definition: reindexdb.c:367
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
static void reindex_one_database(const char *name, const char *dbname, const char *type, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool verbose)
Definition: reindexdb.c:275
#define _(x)
Definition: elog.c:84
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
static void reindex_all_databases ( const char *  maintenance_db,
const char *  host,
const char *  port,
const char *  username,
enum trivalue  prompt_password,
const char *  progname,
bool  echo,
bool  quiet,
bool  verbose 
)
static

Definition at line 327 of file reindexdb.c.

References _, appendConnStrVal(), appendPQExpBuffer(), conn, connectMaintenanceDatabase(), connstr, PQExpBufferData::data, dbname, executeQuery(), i, initPQExpBuffer(), NULL, PQclear(), PQfinish(), PQgetvalue(), PQntuples(), reindex_one_database(), resetPQExpBuffer(), result, and termPQExpBuffer().

Referenced by main().

331 {
332  PGconn *conn;
333  PGresult *result;
335  int i;
336 
337  conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
338  prompt_password, progname);
339  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
340  PQfinish(conn);
341 
342  initPQExpBuffer(&connstr);
343  for (i = 0; i < PQntuples(result); i++)
344  {
345  char *dbname = PQgetvalue(result, i, 0);
346 
347  if (!quiet)
348  {
349  printf(_("%s: reindexing database \"%s\"\n"), progname, dbname);
350  fflush(stdout);
351  }
352 
353  resetPQExpBuffer(&connstr);
354  appendPQExpBuffer(&connstr, "dbname=");
355  appendConnStrVal(&connstr, dbname);
356 
357  reindex_one_database(NULL, connstr.data, "DATABASE", host,
358  port, username, prompt_password,
359  progname, echo, verbose);
360  }
361  termPQExpBuffer(&connstr);
362 
363  PQclear(result);
364 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
return result
Definition: formatting.c:1633
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
PGconn * conn
Definition: streamutil.c:43
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static int port
Definition: pg_regress.c:89
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:84
static char * username
Definition: initdb.c:131
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
char * dbname
Definition: streamutil.c:39
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static void reindex_one_database(const char *name, const char *dbname, const char *type, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, bool verbose)
Definition: reindexdb.c:275
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:64
static void reindex_one_database ( const char *  name,
const char *  dbname,
const char *  type,
const char *  host,
const char *  port,
const char *  username,
enum trivalue  prompt_password,
const char *  progname,
bool  echo,
bool  verbose 
)
static

Definition at line 275 of file reindexdb.c.

References _, appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), conn, connectDatabase(), PQExpBufferData::data, executeMaintenanceCommand(), fmtId(), initPQExpBuffer(), PQdb(), PQerrorMessage(), PQfinish(), and termPQExpBuffer().

Referenced by main(), and reindex_all_databases().

279 {
280  PQExpBufferData sql;
281 
282  PGconn *conn;
283 
284  conn = connectDatabase(dbname, host, port, username, prompt_password,
285  progname, false, false);
286 
287  initPQExpBuffer(&sql);
288 
289  appendPQExpBufferStr(&sql, "REINDEX");
290 
291  if (verbose)
292  appendPQExpBufferStr(&sql, " (VERBOSE)");
293 
294  if (strcmp(type, "TABLE") == 0)
295  appendPQExpBuffer(&sql, " TABLE %s", name);
296  else if (strcmp(type, "INDEX") == 0)
297  appendPQExpBuffer(&sql, " INDEX %s", name);
298  else if (strcmp(type, "SCHEMA") == 0)
299  appendPQExpBuffer(&sql, " SCHEMA %s", name);
300  else if (strcmp(type, "DATABASE") == 0)
301  appendPQExpBuffer(&sql, " DATABASE %s", fmtId(PQdb(conn)));
302  appendPQExpBufferChar(&sql, ';');
303 
304  if (!executeMaintenanceCommand(conn, sql.data, echo))
305  {
306  if (strcmp(type, "TABLE") == 0)
307  fprintf(stderr, _("%s: reindexing of table \"%s\" in database \"%s\" failed: %s"),
308  progname, name, PQdb(conn), PQerrorMessage(conn));
309  if (strcmp(type, "INDEX") == 0)
310  fprintf(stderr, _("%s: reindexing of index \"%s\" in database \"%s\" failed: %s"),
311  progname, name, PQdb(conn), PQerrorMessage(conn));
312  if (strcmp(type, "SCHEMA") == 0)
313  fprintf(stderr, _("%s: reindexing of schema \"%s\" in database \"%s\" failed: %s"),
314  progname, name, PQdb(conn), PQerrorMessage(conn));
315  else
316  fprintf(stderr, _("%s: reindexing of database \"%s\" failed: %s"),
317  progname, PQdb(conn), PQerrorMessage(conn));
318  PQfinish(conn);
319  exit(1);
320  }
321 
322  PQfinish(conn);
323  termPQExpBuffer(&sql);
324 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
const char * progname
Definition: pg_standby.c:37
PGconn * conn
Definition: streamutil.c:43
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:235
static int port
Definition: pg_regress.c:89
static int verbose
Definition: pg_basebackup.c:84
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:131
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:1869
char * dbname
Definition: streamutil.c:39
const char * name
Definition: encode.c:521
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static void reindex_system_catalogs ( const char *  dbname,
const char *  host,
const char *  port,
const char *  username,
enum trivalue  prompt_password,
const char *  progname,
bool  echo,
bool  verbose 
)
static

Definition at line 367 of file reindexdb.c.

References _, appendPQExpBuffer(), conn, connectDatabase(), PQExpBufferData::data, executeMaintenanceCommand(), fmtId(), initPQExpBuffer(), PQdb(), PQerrorMessage(), PQfinish(), and termPQExpBuffer().

Referenced by main().

370 {
371  PGconn *conn;
372  PQExpBufferData sql;
373 
374  conn = connectDatabase(dbname, host, port, username, prompt_password,
375  progname, false, false);
376 
377  initPQExpBuffer(&sql);
378 
379  appendPQExpBuffer(&sql, "REINDEX");
380 
381  if (verbose)
382  appendPQExpBuffer(&sql, " (VERBOSE)");
383 
384  appendPQExpBuffer(&sql, " SYSTEM %s;", fmtId(PQdb(conn)));
385 
386  if (!executeMaintenanceCommand(conn, sql.data, echo))
387  {
388  fprintf(stderr, _("%s: reindexing of system catalogs failed: %s"),
389  progname, PQerrorMessage(conn));
390  PQfinish(conn);
391  exit(1);
392  }
393  PQfinish(conn);
394  termPQExpBuffer(&sql);
395 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
const char * progname
Definition: pg_standby.c:37
PGconn * conn
Definition: streamutil.c:43
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:235
static int port
Definition: pg_regress.c:89
static int verbose
Definition: pg_basebackup.c:84
static char * username
Definition: initdb.c:131
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:1869
char * dbname
Definition: streamutil.c:39
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89