PostgreSQL Source Code  git master
reindexdb.c File Reference
#include "postgres_fe.h"
#include <limits.h>
#include "catalog/pg_class_d.h"
#include "common.h"
#include "common/connect.h"
#include "common/logging.h"
#include "fe_utils/cancel.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/parallel_slot.h"
#include "fe_utils/query_utils.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.

Typedefs

typedef enum ReindexType ReindexType
 

Enumerations

enum  ReindexType {
  REINDEX_DATABASE , REINDEX_INDEX , REINDEX_SCHEMA , REINDEX_SYSTEM ,
  REINDEX_TABLE
}
 

Functions

static SimpleStringListget_parallel_object_list (PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo)
 
static void reindex_one_database (ConnParams *cparams, ReindexType type, SimpleStringList *user_list, const char *progname, bool echo, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
 
static void reindex_all_databases (ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
 
static void run_reindex_command (PGconn *conn, ReindexType type, const char *name, bool echo, bool verbose, bool concurrently, bool async, const char *tablespace)
 
static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Typedef Documentation

◆ ReindexType

typedef enum ReindexType ReindexType

Enumeration Type Documentation

◆ ReindexType

Enumerator
REINDEX_DATABASE 
REINDEX_INDEX 
REINDEX_SCHEMA 
REINDEX_SYSTEM 
REINDEX_TABLE 

Definition at line 27 of file reindexdb.c.

28 {
34 } ReindexType;
ReindexType
Definition: reindexdb.c:28
@ REINDEX_SYSTEM
Definition: reindexdb.c:32
@ REINDEX_DATABASE
Definition: reindexdb.c:29
@ REINDEX_INDEX
Definition: reindexdb.c:30
@ REINDEX_SCHEMA
Definition: reindexdb.c:31
@ REINDEX_TABLE
Definition: reindexdb.c:33

Function Documentation

◆ get_parallel_object_list()

static SimpleStringList * get_parallel_object_list ( PGconn conn,
ReindexType  type,
SimpleStringList user_list,
bool  echo 
)
static

Definition at line 600 of file reindexdb.c.

602 {
603  PQExpBufferData catalog_query;
605  PGresult *res;
606  SimpleStringList *tables;
607  int ntups,
608  i;
609 
610  initPQExpBuffer(&catalog_query);
611 
612  /*
613  * The queries here are using a safe search_path, so there's no need to
614  * fully qualify everything.
615  */
616  switch (type)
617  {
618  case REINDEX_DATABASE:
619  Assert(user_list == NULL);
620  appendPQExpBufferStr(&catalog_query,
621  "SELECT c.relname, ns.nspname\n"
622  " FROM pg_catalog.pg_class c\n"
623  " JOIN pg_catalog.pg_namespace ns"
624  " ON c.relnamespace = ns.oid\n"
625  " WHERE ns.nspname != 'pg_catalog'\n"
626  " AND c.relkind IN ("
627  CppAsString2(RELKIND_RELATION) ", "
628  CppAsString2(RELKIND_MATVIEW) ")\n"
629  " ORDER BY c.relpages DESC;");
630  break;
631 
632  case REINDEX_SCHEMA:
633  {
634  SimpleStringListCell *cell;
635  bool nsp_listed = false;
636 
637  Assert(user_list != NULL);
638 
639  /*
640  * All the tables from all the listed schemas are grabbed at
641  * once.
642  */
643  appendPQExpBufferStr(&catalog_query,
644  "SELECT c.relname, ns.nspname\n"
645  " FROM pg_catalog.pg_class c\n"
646  " JOIN pg_catalog.pg_namespace ns"
647  " ON c.relnamespace = ns.oid\n"
648  " WHERE c.relkind IN ("
649  CppAsString2(RELKIND_RELATION) ", "
650  CppAsString2(RELKIND_MATVIEW) ")\n"
651  " AND ns.nspname IN (");
652 
653  for (cell = user_list->head; cell; cell = cell->next)
654  {
655  const char *nspname = cell->val;
656 
657  if (nsp_listed)
658  appendPQExpBufferStr(&catalog_query, ", ");
659  else
660  nsp_listed = true;
661 
662  appendStringLiteralConn(&catalog_query, nspname, conn);
663  }
664 
665  appendPQExpBufferStr(&catalog_query, ")\n"
666  " ORDER BY c.relpages DESC;");
667  }
668  break;
669 
670  case REINDEX_SYSTEM:
671  case REINDEX_INDEX:
672  case REINDEX_TABLE:
673  Assert(false);
674  break;
675  }
676 
677  res = executeQuery(conn, catalog_query.data, echo);
678  termPQExpBuffer(&catalog_query);
679 
680  /*
681  * If no rows are returned, there are no matching tables, so we are done.
682  */
683  ntups = PQntuples(res);
684  if (ntups == 0)
685  {
686  PQclear(res);
687  PQfinish(conn);
688  return NULL;
689  }
690 
691  tables = pg_malloc0(sizeof(SimpleStringList));
692 
693  /* Build qualified identifiers for each table */
695  for (i = 0; i < ntups; i++)
696  {
699  PQgetvalue(res, i, 0)));
700 
701  simple_string_list_append(tables, buf.data);
703  }
705  PQclear(res);
706 
707  return tables;
708 }
#define CppAsString2(x)
Definition: c.h:311
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
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1861
static char * buf
Definition: pg_test_fsync.c:67
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
PGconn * conn
Definition: streamutil.c:54
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * fmtQualifiedId(const char *schema, const char *id)
Definition: string_utils.c:145
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

References appendPQExpBufferStr(), appendStringLiteralConn(), Assert(), buf, conn, CppAsString2, PQExpBufferData::data, executeQuery(), fmtQualifiedId(), SimpleStringList::head, i, initPQExpBuffer(), SimpleStringListCell::next, pg_malloc0(), PQclear(), PQfinish(), PQgetvalue(), PQntuples(), REINDEX_DATABASE, REINDEX_INDEX, REINDEX_SCHEMA, REINDEX_SYSTEM, REINDEX_TABLE, res, resetPQExpBuffer(), simple_string_list_append(), termPQExpBuffer(), generate_unaccent_rules::type, and SimpleStringListCell::val.

Referenced by reindex_one_database().

◆ help()

static void help ( const char *  progname)
static

Definition at line 745 of file reindexdb.c.

746 {
747  printf(_("%s reindexes a PostgreSQL database.\n\n"), progname);
748  printf(_("Usage:\n"));
749  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
750  printf(_("\nOptions:\n"));
751  printf(_(" -a, --all reindex all databases\n"));
752  printf(_(" --concurrently reindex concurrently\n"));
753  printf(_(" -d, --dbname=DBNAME database to reindex\n"));
754  printf(_(" -e, --echo show the commands being sent to the server\n"));
755  printf(_(" -i, --index=INDEX recreate specific index(es) only\n"));
756  printf(_(" -j, --jobs=NUM use this many concurrent connections to reindex\n"));
757  printf(_(" -q, --quiet don't write any messages\n"));
758  printf(_(" -s, --system reindex system catalogs only\n"));
759  printf(_(" -S, --schema=SCHEMA reindex specific schema(s) only\n"));
760  printf(_(" -t, --table=TABLE reindex specific table(s) only\n"));
761  printf(_(" --tablespace=TABLESPACE tablespace where indexes are rebuilt\n"));
762  printf(_(" -v, --verbose write a lot of output\n"));
763  printf(_(" -V, --version output version information, then exit\n"));
764  printf(_(" -?, --help show this help, then exit\n"));
765  printf(_("\nConnection options:\n"));
766  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
767  printf(_(" -p, --port=PORT database server port\n"));
768  printf(_(" -U, --username=USERNAME user name to connect as\n"));
769  printf(_(" -w, --no-password never prompt for password\n"));
770  printf(_(" -W, --password force password prompt\n"));
771  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
772  printf(_("\nRead the description of the SQL command REINDEX for details.\n"));
773  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
774  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
775 }
#define _(x)
Definition: elog.c:90
const char * progname
Definition: main.c:45
#define printf(...)
Definition: port.h:244

References _, printf, and progname.

Referenced by main().

◆ main()

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

Definition at line 58 of file reindexdb.c.

59 {
60  static struct option long_options[] = {
61  {"host", required_argument, NULL, 'h'},
62  {"port", required_argument, NULL, 'p'},
63  {"username", required_argument, NULL, 'U'},
64  {"no-password", no_argument, NULL, 'w'},
65  {"password", no_argument, NULL, 'W'},
66  {"echo", no_argument, NULL, 'e'},
67  {"quiet", no_argument, NULL, 'q'},
68  {"schema", required_argument, NULL, 'S'},
69  {"dbname", required_argument, NULL, 'd'},
70  {"all", no_argument, NULL, 'a'},
71  {"system", no_argument, NULL, 's'},
72  {"table", required_argument, NULL, 't'},
73  {"index", required_argument, NULL, 'i'},
74  {"jobs", required_argument, NULL, 'j'},
75  {"verbose", no_argument, NULL, 'v'},
76  {"concurrently", no_argument, NULL, 1},
77  {"maintenance-db", required_argument, NULL, 2},
78  {"tablespace", required_argument, NULL, 3},
79  {NULL, 0, NULL, 0}
80  };
81 
82  const char *progname;
83  int optindex;
84  int c;
85 
86  const char *dbname = NULL;
87  const char *maintenance_db = NULL;
88  const char *host = NULL;
89  const char *port = NULL;
90  const char *username = NULL;
91  const char *tablespace = NULL;
92  enum trivalue prompt_password = TRI_DEFAULT;
93  ConnParams cparams;
94  bool syscatalog = false;
95  bool alldb = false;
96  bool echo = false;
97  bool quiet = false;
98  bool verbose = false;
99  bool concurrently = false;
100  SimpleStringList indexes = {NULL, NULL};
101  SimpleStringList tables = {NULL, NULL};
102  SimpleStringList schemas = {NULL, NULL};
103  int concurrentCons = 1;
104 
105  pg_logging_init(argv[0]);
106  progname = get_progname(argv[0]);
107  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
108 
109  handle_help_version_opts(argc, argv, "reindexdb", help);
110 
111  /* process command-line options */
112  while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1)
113  {
114  switch (c)
115  {
116  case 'h':
117  host = pg_strdup(optarg);
118  break;
119  case 'p':
120  port = pg_strdup(optarg);
121  break;
122  case 'U':
124  break;
125  case 'w':
126  prompt_password = TRI_NO;
127  break;
128  case 'W':
129  prompt_password = TRI_YES;
130  break;
131  case 'e':
132  echo = true;
133  break;
134  case 'q':
135  quiet = true;
136  break;
137  case 'S':
139  break;
140  case 'd':
142  break;
143  case 'a':
144  alldb = true;
145  break;
146  case 's':
147  syscatalog = true;
148  break;
149  case 't':
151  break;
152  case 'i':
154  break;
155  case 'j':
156  if (!option_parse_int(optarg, "-j/--jobs", 1, INT_MAX,
157  &concurrentCons))
158  exit(1);
159  break;
160  case 'v':
161  verbose = true;
162  break;
163  case 1:
164  concurrently = true;
165  break;
166  case 2:
167  maintenance_db = pg_strdup(optarg);
168  break;
169  case 3:
171  break;
172  default:
173  /* getopt_long already emitted a complaint */
174  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
175  exit(1);
176  }
177  }
178 
179  /*
180  * Non-option argument specifies database name as long as it wasn't
181  * already specified with -d / --dbname
182  */
183  if (optind < argc && dbname == NULL)
184  {
185  dbname = argv[optind];
186  optind++;
187  }
188 
189  if (optind < argc)
190  {
191  pg_log_error("too many command-line arguments (first is \"%s\")",
192  argv[optind]);
193  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
194  exit(1);
195  }
196 
197  /* fill cparams except for dbname, which is set below */
198  cparams.pghost = host;
199  cparams.pgport = port;
200  cparams.pguser = username;
201  cparams.prompt_password = prompt_password;
202  cparams.override_dbname = NULL;
203 
204  setup_cancel_handler(NULL);
205 
206  if (alldb)
207  {
208  if (dbname)
209  pg_fatal("cannot reindex all databases and a specific one at the same time");
210  if (syscatalog)
211  pg_fatal("cannot reindex all databases and system catalogs at the same time");
212  if (schemas.head != NULL)
213  pg_fatal("cannot reindex specific schema(s) in all databases");
214  if (tables.head != NULL)
215  pg_fatal("cannot reindex specific table(s) in all databases");
216  if (indexes.head != NULL)
217  pg_fatal("cannot reindex specific index(es) in all databases");
218 
219  cparams.dbname = maintenance_db;
220 
221  reindex_all_databases(&cparams, progname, echo, quiet, verbose,
222  concurrently, concurrentCons, tablespace);
223  }
224  else if (syscatalog)
225  {
226  if (schemas.head != NULL)
227  pg_fatal("cannot reindex specific schema(s) and system catalogs at the same time");
228  if (tables.head != NULL)
229  pg_fatal("cannot reindex specific table(s) and system catalogs at the same time");
230  if (indexes.head != NULL)
231  pg_fatal("cannot reindex specific index(es) and system catalogs at the same time");
232 
233  if (concurrentCons > 1)
234  pg_fatal("cannot use multiple jobs to reindex system catalogs");
235 
236  if (dbname == NULL)
237  {
238  if (getenv("PGDATABASE"))
239  dbname = getenv("PGDATABASE");
240  else if (getenv("PGUSER"))
241  dbname = getenv("PGUSER");
242  else
244  }
245 
246  cparams.dbname = dbname;
247 
248  reindex_one_database(&cparams, REINDEX_SYSTEM, NULL,
249  progname, echo, verbose,
250  concurrently, 1, tablespace);
251  }
252  else
253  {
254  /*
255  * Index-level REINDEX is not supported with multiple jobs as we
256  * cannot control the concurrent processing of multiple indexes
257  * depending on the same relation.
258  */
259  if (concurrentCons > 1 && indexes.head != NULL)
260  pg_fatal("cannot use multiple jobs to reindex indexes");
261 
262  if (dbname == NULL)
263  {
264  if (getenv("PGDATABASE"))
265  dbname = getenv("PGDATABASE");
266  else if (getenv("PGUSER"))
267  dbname = getenv("PGUSER");
268  else
270  }
271 
272  cparams.dbname = dbname;
273 
274  if (schemas.head != NULL)
275  reindex_one_database(&cparams, REINDEX_SCHEMA, &schemas,
276  progname, echo, verbose,
277  concurrently, concurrentCons, tablespace);
278 
279  if (indexes.head != NULL)
280  reindex_one_database(&cparams, REINDEX_INDEX, &indexes,
281  progname, echo, verbose,
282  concurrently, 1, tablespace);
283 
284  if (tables.head != NULL)
285  reindex_one_database(&cparams, REINDEX_TABLE, &tables,
286  progname, echo, verbose,
287  concurrently, concurrentCons, tablespace);
288 
289  /*
290  * reindex database only if neither index nor table nor schema is
291  * specified
292  */
293  if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
294  reindex_one_database(&cparams, REINDEX_DATABASE, NULL,
295  progname, echo, verbose,
296  concurrently, concurrentCons, tablespace);
297  }
298 
299  exit(0);
300 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1153
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
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
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
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 int verbose
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
char * tablespace
Definition: pgbench.c:225
const char * get_progname(const char *argv0)
Definition: path.c:574
char * c
static void reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
Definition: reindexdb.c:711
static void help(const char *progname)
Definition: reindexdb.c:745
static void reindex_one_database(ConnParams *cparams, ReindexType type, SimpleStringList *user_list, const char *progname, bool echo, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
Definition: reindexdb.c:303
char * dbname
Definition: streamutil.c:51
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:90
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 dbname, _connParams::dbname, exit(), get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), no_argument, optarg, optind, option_parse_int(), _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, reindex_all_databases(), REINDEX_DATABASE, REINDEX_INDEX, reindex_one_database(), REINDEX_SCHEMA, REINDEX_SYSTEM, REINDEX_TABLE, required_argument, set_pglocale_pgservice(), setup_cancel_handler(), simple_string_list_append(), tablespace, TRI_DEFAULT, TRI_NO, TRI_YES, username, and verbose.

◆ reindex_all_databases()

static void reindex_all_databases ( ConnParams cparams,
const char *  progname,
bool  echo,
bool  quiet,
bool  verbose,
bool  concurrently,
int  concurrentCons,
const char *  tablespace 
)
static

Definition at line 711 of file reindexdb.c.

715 {
716  PGconn *conn;
717  PGresult *result;
718  int i;
719 
720  conn = connectMaintenanceDatabase(cparams, progname, echo);
721  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
722  PQfinish(conn);
723 
724  for (i = 0; i < PQntuples(result); i++)
725  {
726  char *dbname = PQgetvalue(result, i, 0);
727 
728  if (!quiet)
729  {
730  printf(_("%s: reindexing database \"%s\"\n"), progname, dbname);
731  fflush(stdout);
732  }
733 
734  cparams->override_dbname = dbname;
735 
736  reindex_one_database(cparams, REINDEX_DATABASE, NULL,
737  progname, echo, verbose, concurrently,
738  concurrentCons, tablespace);
739  }
740 
741  PQclear(result);
742 }
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void const char fflush(stdout)

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

Referenced by main().

◆ reindex_one_database()

static void reindex_one_database ( ConnParams cparams,
ReindexType  type,
SimpleStringList user_list,
const char *  progname,
bool  echo,
bool  verbose,
bool  concurrently,
int  concurrentCons,
const char *  tablespace 
)
static

Definition at line 303 of file reindexdb.c.

308 {
309  PGconn *conn;
310  SimpleStringListCell *cell;
311  bool parallel = concurrentCons > 1;
312  SimpleStringList *process_list = user_list;
313  ReindexType process_type = type;
315  bool failed = false;
316  int items_count = 0;
317 
318  conn = connectDatabase(cparams, progname, echo, false, false);
319 
320  if (concurrently && PQserverVersion(conn) < 120000)
321  {
322  PQfinish(conn);
323  pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
324  "concurrently", "12");
325  }
326 
327  if (tablespace && PQserverVersion(conn) < 140000)
328  {
329  PQfinish(conn);
330  pg_fatal("cannot use the \"%s\" option on server versions older than PostgreSQL %s",
331  "tablespace", "14");
332  }
333 
334  if (!parallel)
335  {
336  switch (process_type)
337  {
338  case REINDEX_DATABASE:
339  case REINDEX_SYSTEM:
340 
341  /*
342  * Database and system reindexes only need to work on the
343  * database itself, so build a list with a single entry.
344  */
345  Assert(user_list == NULL);
346  process_list = pg_malloc0(sizeof(SimpleStringList));
347  simple_string_list_append(process_list, PQdb(conn));
348  break;
349 
350  case REINDEX_INDEX:
351  case REINDEX_SCHEMA:
352  case REINDEX_TABLE:
353  Assert(user_list != NULL);
354  break;
355  }
356  }
357  else
358  {
359  switch (process_type)
360  {
361  case REINDEX_DATABASE:
362 
363  /* Build a list of relations from the database */
364  process_list = get_parallel_object_list(conn, process_type,
365  user_list, echo);
366  process_type = REINDEX_TABLE;
367 
368  /* Bail out if nothing to process */
369  if (process_list == NULL)
370  return;
371  break;
372 
373  case REINDEX_SCHEMA:
374  Assert(user_list != NULL);
375 
376  /* Build a list of relations from all the schemas */
377  process_list = get_parallel_object_list(conn, process_type,
378  user_list, echo);
379  process_type = REINDEX_TABLE;
380 
381  /* Bail out if nothing to process */
382  if (process_list == NULL)
383  return;
384  break;
385 
386  case REINDEX_SYSTEM:
387  case REINDEX_INDEX:
388  /* not supported */
389  Assert(false);
390  break;
391 
392  case REINDEX_TABLE:
393 
394  /*
395  * Fall through. The list of items for tables is already
396  * created.
397  */
398  break;
399  }
400  }
401 
402  /*
403  * Adjust the number of concurrent connections depending on the items in
404  * the list. We choose the minimum between the number of concurrent
405  * connections and the number of items in the list.
406  */
407  for (cell = process_list->head; cell; cell = cell->next)
408  {
409  items_count++;
410 
411  /* no need to continue if there are more elements than jobs */
412  if (items_count >= concurrentCons)
413  break;
414  }
415  concurrentCons = Min(concurrentCons, items_count);
416  Assert(concurrentCons > 0);
417 
418  Assert(process_list != NULL);
419 
420  sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, NULL);
422 
423  cell = process_list->head;
424  do
425  {
426  const char *objname = cell->val;
427  ParallelSlot *free_slot = NULL;
428 
429  if (CancelRequested)
430  {
431  failed = true;
432  goto finish;
433  }
434 
435  free_slot = ParallelSlotsGetIdle(sa, NULL);
436  if (!free_slot)
437  {
438  failed = true;
439  goto finish;
440  }
441 
443  run_reindex_command(free_slot->connection, process_type, objname,
444  echo, verbose, concurrently, true, tablespace);
445 
446  cell = cell->next;
447  } while (cell != NULL);
448 
450  failed = true;
451 
452 finish:
453  if (process_list != user_list)
454  {
455  simple_string_list_destroy(process_list);
456  pg_free(process_list);
457  }
458 
460  pfree(sa);
461 
462  if (failed)
463  exit(1);
464 }
#define Min(x, y)
Definition: c.h:937
volatile sig_atomic_t CancelRequested
Definition: cancel.c:59
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6733
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6589
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void pfree(void *pointer)
Definition: mcxt.c:1306
ParallelSlot * ParallelSlotsGetIdle(ParallelSlotArray *sa, const char *dbname)
bool ParallelSlotsWaitCompletion(ParallelSlotArray *sa)
ParallelSlotArray * ParallelSlotsSetup(int numslots, ConnParams *cparams, const char *progname, bool echo, const char *initcmd)
bool TableCommandResultHandler(PGresult *res, PGconn *conn, void *context)
void ParallelSlotsTerminate(ParallelSlotArray *sa)
void ParallelSlotsAdoptConn(ParallelSlotArray *sa, PGconn *conn)
static void ParallelSlotSetHandler(ParallelSlot *slot, ParallelSlotResultHandler handler, void *context)
Definition: parallel_slot.h:47
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
static SimpleStringList * get_parallel_object_list(PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo)
Definition: reindexdb.c:600
static void run_reindex_command(PGconn *conn, ReindexType type, const char *name, bool echo, bool verbose, bool concurrently, bool async, const char *tablespace)
Definition: reindexdb.c:467
void simple_string_list_destroy(SimpleStringList *list)
Definition: simple_list.c:125
PGconn * connection
Definition: parallel_slot.h:23

References Assert(), CancelRequested, conn, connectDatabase(), ParallelSlot::connection, exit(), get_parallel_object_list(), SimpleStringList::head, Min, SimpleStringListCell::next, ParallelSlotsAdoptConn(), ParallelSlotSetHandler(), ParallelSlotsGetIdle(), ParallelSlotsSetup(), ParallelSlotsTerminate(), ParallelSlotsWaitCompletion(), pfree(), pg_fatal, pg_free(), pg_malloc0(), PQdb(), PQfinish(), PQserverVersion(), progname, REINDEX_DATABASE, REINDEX_INDEX, REINDEX_SCHEMA, REINDEX_SYSTEM, REINDEX_TABLE, run_reindex_command(), simple_string_list_append(), simple_string_list_destroy(), TableCommandResultHandler(), tablespace, generate_unaccent_rules::type, SimpleStringListCell::val, and verbose.

Referenced by main(), and reindex_all_databases().

◆ run_reindex_command()

static void run_reindex_command ( PGconn conn,
ReindexType  type,
const char *  name,
bool  echo,
bool  verbose,
bool  concurrently,
bool  async,
const char *  tablespace 
)
static

Definition at line 467 of file reindexdb.c.

470 {
471  const char *paren = "(";
472  const char *comma = ", ";
473  const char *sep = paren;
474  PQExpBufferData sql;
475  bool status;
476 
477  Assert(name);
478 
479  /* build the REINDEX query */
480  initPQExpBuffer(&sql);
481 
482  appendPQExpBufferStr(&sql, "REINDEX ");
483 
484  if (verbose)
485  {
486  appendPQExpBuffer(&sql, "%sVERBOSE", sep);
487  sep = comma;
488  }
489 
490  if (tablespace)
491  {
492  appendPQExpBuffer(&sql, "%sTABLESPACE %s", sep, fmtId(tablespace));
493  sep = comma;
494  }
495 
496  if (sep != paren)
497  appendPQExpBufferStr(&sql, ") ");
498 
499  /* object type */
500  switch (type)
501  {
502  case REINDEX_DATABASE:
503  appendPQExpBufferStr(&sql, "DATABASE ");
504  break;
505  case REINDEX_INDEX:
506  appendPQExpBufferStr(&sql, "INDEX ");
507  break;
508  case REINDEX_SCHEMA:
509  appendPQExpBufferStr(&sql, "SCHEMA ");
510  break;
511  case REINDEX_SYSTEM:
512  appendPQExpBufferStr(&sql, "SYSTEM ");
513  break;
514  case REINDEX_TABLE:
515  appendPQExpBufferStr(&sql, "TABLE ");
516  break;
517  }
518 
519  /*
520  * Parenthesized grammar is only supported for CONCURRENTLY since
521  * PostgreSQL 14. Since 12, CONCURRENTLY can be specified after the
522  * object type.
523  */
524  if (concurrently)
525  appendPQExpBufferStr(&sql, "CONCURRENTLY ");
526 
527  /* object name */
528  switch (type)
529  {
530  case REINDEX_DATABASE:
531  case REINDEX_SYSTEM:
533  break;
534  case REINDEX_INDEX:
535  case REINDEX_TABLE:
536  appendQualifiedRelation(&sql, name, conn, echo);
537  break;
538  case REINDEX_SCHEMA:
539  appendPQExpBufferStr(&sql, name);
540  break;
541  }
542 
543  /* finish the query */
544  appendPQExpBufferChar(&sql, ';');
545 
546  if (async)
547  {
548  if (echo)
549  printf("%s\n", sql.data);
550 
551  status = PQsendQuery(conn, sql.data) == 1;
552  }
553  else
555 
556  if (!status)
557  {
558  switch (type)
559  {
560  case REINDEX_DATABASE:
561  pg_log_error("reindexing of database \"%s\" failed: %s",
563  break;
564  case REINDEX_INDEX:
565  pg_log_error("reindexing of index \"%s\" in database \"%s\" failed: %s",
567  break;
568  case REINDEX_SCHEMA:
569  pg_log_error("reindexing of schema \"%s\" in database \"%s\" failed: %s",
571  break;
572  case REINDEX_SYSTEM:
573  pg_log_error("reindexing of system catalogs in database \"%s\" failed: %s",
575  break;
576  case REINDEX_TABLE:
577  pg_log_error("reindexing of table \"%s\" in database \"%s\" failed: %s",
579  break;
580  }
581  if (!async)
582  {
583  PQfinish(conn);
584  exit(1);
585  }
586  }
587 
588  termPQExpBuffer(&sql);
589 }
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
Definition: common.c:69
const char * name
Definition: encode.c:561
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1418
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: query_utils.c:74
const char * fmtId(const char *rawid)
Definition: string_utils.c:64

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendQualifiedRelation(), Assert(), conn, PQExpBufferData::data, executeMaintenanceCommand(), exit(), fmtId(), initPQExpBuffer(), name, pg_log_error, PQdb(), PQerrorMessage(), PQfinish(), PQsendQuery(), printf, REINDEX_DATABASE, REINDEX_INDEX, REINDEX_SCHEMA, REINDEX_SYSTEM, REINDEX_TABLE, status(), tablespace, termPQExpBuffer(), generate_unaccent_rules::type, and verbose.

Referenced by reindex_one_database().