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 612 of file reindexdb.c.

614 {
615  PQExpBufferData catalog_query;
617  PGresult *res;
618  SimpleStringList *tables;
619  int ntups,
620  i;
621 
622  initPQExpBuffer(&catalog_query);
623 
624  /*
625  * The queries here are using a safe search_path, so there's no need to
626  * fully qualify everything.
627  */
628  switch (type)
629  {
630  case REINDEX_DATABASE:
631  Assert(user_list == NULL);
632  appendPQExpBufferStr(&catalog_query,
633  "SELECT c.relname, ns.nspname\n"
634  " FROM pg_catalog.pg_class c\n"
635  " JOIN pg_catalog.pg_namespace ns"
636  " ON c.relnamespace = ns.oid\n"
637  " WHERE ns.nspname != 'pg_catalog'\n"
638  " AND c.relkind IN ("
639  CppAsString2(RELKIND_RELATION) ", "
640  CppAsString2(RELKIND_MATVIEW) ")\n"
641  " ORDER BY c.relpages DESC;");
642  break;
643 
644  case REINDEX_SCHEMA:
645  {
646  SimpleStringListCell *cell;
647  bool nsp_listed = false;
648 
649  Assert(user_list != NULL);
650 
651  /*
652  * All the tables from all the listed schemas are grabbed at
653  * once.
654  */
655  appendPQExpBufferStr(&catalog_query,
656  "SELECT c.relname, ns.nspname\n"
657  " FROM pg_catalog.pg_class c\n"
658  " JOIN pg_catalog.pg_namespace ns"
659  " ON c.relnamespace = ns.oid\n"
660  " WHERE c.relkind IN ("
661  CppAsString2(RELKIND_RELATION) ", "
662  CppAsString2(RELKIND_MATVIEW) ")\n"
663  " AND ns.nspname IN (");
664 
665  for (cell = user_list->head; cell; cell = cell->next)
666  {
667  const char *nspname = cell->val;
668 
669  if (nsp_listed)
670  appendPQExpBufferStr(&catalog_query, ", ");
671  else
672  nsp_listed = true;
673 
674  appendStringLiteralConn(&catalog_query, nspname, conn);
675  }
676 
677  appendPQExpBufferStr(&catalog_query, ")\n"
678  " ORDER BY c.relpages DESC;");
679  }
680  break;
681 
682  case REINDEX_SYSTEM:
683  case REINDEX_INDEX:
684  case REINDEX_TABLE:
685  Assert(false);
686  break;
687  }
688 
689  res = executeQuery(conn, catalog_query.data, echo);
690  termPQExpBuffer(&catalog_query);
691 
692  /*
693  * If no rows are returned, there are no matching tables, so we are done.
694  */
695  ntups = PQntuples(res);
696  if (ntups == 0)
697  {
698  PQclear(res);
699  PQfinish(conn);
700  return NULL;
701  }
702 
703  tables = pg_malloc0(sizeof(SimpleStringList));
704 
705  /* Build qualified identifiers for each table */
707  for (i = 0; i < ntups; i++)
708  {
711  PQgetvalue(res, i, 0)));
712 
713  simple_string_list_append(tables, buf.data);
715  }
717  PQclear(res);
718 
719  return tables;
720 }
#define CppAsString2(x)
Definition: c.h:289
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4261
void PQclear(PGresult *res)
Definition: fe-exec.c:718
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3340
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3735
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:1699
static char * buf
Definition: pg_test_fsync.c:67
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
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 757 of file reindexdb.c.

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

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:610
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1212
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
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:92
const char * username
Definition: pgbench.c:309
char * tablespace
Definition: pgbench.c:229
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:723
static void help(const char *progname)
Definition: reindexdb.c:757
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:89
char * pgport
Definition: pg_backup.h:83
char * pghost
Definition: pg_backup.h:84
char * dbname
Definition: pg_backup.h:82
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 723 of file reindexdb.c.

727 {
728  PGconn *conn;
729  PGresult *result;
730  int i;
731 
732  conn = connectMaintenanceDatabase(cparams, progname, echo);
733  result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
734  PQfinish(conn);
735 
736  for (i = 0; i < PQntuples(result); i++)
737  {
738  char *dbname = PQgetvalue(result, i, 0);
739 
740  if (!quiet)
741  {
742  printf(_("%s: reindexing database \"%s\"\n"), progname, dbname);
743  fflush(stdout);
744  }
745 
746  cparams->override_dbname = dbname;
747 
748  reindex_one_database(cparams, REINDEX_DATABASE, NULL,
749  progname, echo, verbose, concurrently,
750  concurrentCons, tablespace);
751  }
752 
753  PQclear(result);
754 }
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  /*
364  * Database-wide parallel reindex requires special processing.
365  * If multiple jobs were asked, we have to reindex system
366  * catalogs first as they cannot be processed in parallel.
367  */
368  if (concurrently)
369  pg_log_warning("cannot reindex system catalogs concurrently, skipping all");
370  else
372  verbose, concurrently, false,
373  tablespace);
374 
375  /* Build a list of relations from the database */
376  process_list = get_parallel_object_list(conn, process_type,
377  user_list, echo);
378  process_type = REINDEX_TABLE;
379 
380  /* Bail out if nothing to process */
381  if (process_list == NULL)
382  return;
383  break;
384 
385  case REINDEX_SCHEMA:
386  Assert(user_list != NULL);
387 
388  /* Build a list of relations from all the schemas */
389  process_list = get_parallel_object_list(conn, process_type,
390  user_list, echo);
391  process_type = REINDEX_TABLE;
392 
393  /* Bail out if nothing to process */
394  if (process_list == NULL)
395  return;
396  break;
397 
398  case REINDEX_SYSTEM:
399  case REINDEX_INDEX:
400  /* not supported */
401  Assert(false);
402  break;
403 
404  case REINDEX_TABLE:
405 
406  /*
407  * Fall through. The list of items for tables is already
408  * created.
409  */
410  break;
411  }
412  }
413 
414  /*
415  * Adjust the number of concurrent connections depending on the items in
416  * the list. We choose the minimum between the number of concurrent
417  * connections and the number of items in the list.
418  */
419  for (cell = process_list->head; cell; cell = cell->next)
420  {
421  items_count++;
422 
423  /* no need to continue if there are more elements than jobs */
424  if (items_count >= concurrentCons)
425  break;
426  }
427  concurrentCons = Min(concurrentCons, items_count);
428  Assert(concurrentCons > 0);
429 
430  Assert(process_list != NULL);
431 
432  sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, NULL);
434 
435  cell = process_list->head;
436  do
437  {
438  const char *objname = cell->val;
439  ParallelSlot *free_slot = NULL;
440 
441  if (CancelRequested)
442  {
443  failed = true;
444  goto finish;
445  }
446 
447  free_slot = ParallelSlotsGetIdle(sa, NULL);
448  if (!free_slot)
449  {
450  failed = true;
451  goto finish;
452  }
453 
455  run_reindex_command(free_slot->connection, process_type, objname,
456  echo, verbose, concurrently, true, tablespace);
457 
458  cell = cell->next;
459  } while (cell != NULL);
460 
462  failed = true;
463 
464 finish:
465  if (process_list != user_list)
466  {
467  simple_string_list_destroy(process_list);
468  pg_free(process_list);
469  }
470 
472  pfree(sa);
473 
474  if (failed)
475  exit(1);
476 }
#define Min(x, y)
Definition: c.h:986
volatile sig_atomic_t CancelRequested
Definition: cancel.c:59
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6898
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6754
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void pfree(void *pointer)
Definition: mcxt.c:1175
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 *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1476
#define pg_log_warning(...)
Definition: pgfnames.c:24
static SimpleStringList * get_parallel_object_list(PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo)
Definition: reindexdb.c:612
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:479
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_log_warning, 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 479 of file reindexdb.c.

482 {
483  const char *paren = "(";
484  const char *comma = ", ";
485  const char *sep = paren;
486  PQExpBufferData sql;
487  bool status;
488 
489  Assert(name);
490 
491  /* build the REINDEX query */
492  initPQExpBuffer(&sql);
493 
494  appendPQExpBufferStr(&sql, "REINDEX ");
495 
496  if (verbose)
497  {
498  appendPQExpBuffer(&sql, "%sVERBOSE", sep);
499  sep = comma;
500  }
501 
502  if (tablespace)
503  {
504  appendPQExpBuffer(&sql, "%sTABLESPACE %s", sep, fmtId(tablespace));
505  sep = comma;
506  }
507 
508  if (sep != paren)
509  appendPQExpBufferStr(&sql, ") ");
510 
511  /* object type */
512  switch (type)
513  {
514  case REINDEX_DATABASE:
515  appendPQExpBufferStr(&sql, "DATABASE ");
516  break;
517  case REINDEX_INDEX:
518  appendPQExpBufferStr(&sql, "INDEX ");
519  break;
520  case REINDEX_SCHEMA:
521  appendPQExpBufferStr(&sql, "SCHEMA ");
522  break;
523  case REINDEX_SYSTEM:
524  appendPQExpBufferStr(&sql, "SYSTEM ");
525  break;
526  case REINDEX_TABLE:
527  appendPQExpBufferStr(&sql, "TABLE ");
528  break;
529  }
530 
531  /*
532  * Parenthesized grammar is only supported for CONCURRENTLY since
533  * PostgreSQL 14. Since 12, CONCURRENTLY can be specified after the
534  * object type.
535  */
536  if (concurrently)
537  appendPQExpBufferStr(&sql, "CONCURRENTLY ");
538 
539  /* object name */
540  switch (type)
541  {
542  case REINDEX_DATABASE:
543  case REINDEX_SYSTEM:
545  break;
546  case REINDEX_INDEX:
547  case REINDEX_TABLE:
548  appendQualifiedRelation(&sql, name, conn, echo);
549  break;
550  case REINDEX_SCHEMA:
551  appendPQExpBufferStr(&sql, name);
552  break;
553  }
554 
555  /* finish the query */
556  appendPQExpBufferChar(&sql, ';');
557 
558  if (async)
559  {
560  if (echo)
561  printf("%s\n", sql.data);
562 
563  status = PQsendQuery(conn, sql.data) == 1;
564  }
565  else
567 
568  if (!status)
569  {
570  switch (type)
571  {
572  case REINDEX_DATABASE:
573  pg_log_error("reindexing of database \"%s\" failed: %s",
575  break;
576  case REINDEX_INDEX:
577  pg_log_error("reindexing of index \"%s\" in database \"%s\" failed: %s",
579  break;
580  case REINDEX_SCHEMA:
581  pg_log_error("reindexing of schema \"%s\" in database \"%s\" failed: %s",
583  break;
584  case REINDEX_SYSTEM:
585  pg_log_error("reindexing of system catalogs in database \"%s\" failed: %s",
587  break;
588  case REINDEX_TABLE:
589  pg_log_error("reindexing of table \"%s\" in database \"%s\" failed: %s",
591  break;
592  }
593  if (!async)
594  {
595  PQfinish(conn);
596  exit(1);
597  }
598  }
599 
600  termPQExpBuffer(&sql);
601 }
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:6908
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1424
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
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().