PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
vacuumdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * vacuumdb
4  *
5  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * src/bin/scripts/vacuumdb.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres_fe.h"
14 
15 #ifdef HAVE_SYS_SELECT_H
16 #include <sys/select.h>
17 #endif
18 
19 #include "common.h"
20 #include "fe_utils/simple_list.h"
21 #include "fe_utils/string_utils.h"
22 
23 
24 #define ERRCODE_UNDEFINED_TABLE "42P01"
25 
26 /* Parallel vacuuming stuff */
27 typedef struct ParallelSlot
28 {
31  bool isFree;
32 } ParallelSlot;
33 
34 /* vacuum options controlled by user flags */
35 typedef struct vacuumingOptions
36 {
38  bool verbose;
40  bool full;
41  bool freeze;
43 
44 
45 static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
46  int stage,
47  SimpleStringList *tables,
48  const char *host, const char *port,
49  const char *username, enum trivalue prompt_password,
50  int concurrentCons,
51  const char *progname, bool echo, bool quiet);
52 
53 static void vacuum_all_databases(vacuumingOptions *vacopts,
54  bool analyze_in_stages,
55  const char *maintenance_db,
56  const char *host, const char *port,
57  const char *username, enum trivalue prompt_password,
58  int concurrentCons,
59  const char *progname, bool echo, bool quiet);
60 
62  vacuumingOptions *vacopts, const char *table);
63 
64 static void run_vacuum_command(PGconn *conn, const char *sql, bool echo,
65  const char *table, const char *progname, bool async);
66 
67 static ParallelSlot *GetIdleSlot(ParallelSlot slots[], int numslots,
68  const char *progname);
69 
70 static bool GetQueryResult(PGconn *conn, const char *progname);
71 
72 static void DisconnectDatabase(ParallelSlot *slot);
73 
74 static int select_loop(int maxFd, fd_set *workerset, bool *aborting);
75 
76 static void init_slot(ParallelSlot *slot, PGconn *conn, const char *progname);
77 
78 static void help(const char *progname);
79 
80 /* For analyze-in-stages mode */
81 #define ANALYZE_NO_STAGE -1
82 #define ANALYZE_NUM_STAGES 3
83 
84 
85 int
86 main(int argc, char *argv[])
87 {
88  static struct option long_options[] = {
89  {"host", required_argument, NULL, 'h'},
90  {"port", required_argument, NULL, 'p'},
91  {"username", required_argument, NULL, 'U'},
92  {"no-password", no_argument, NULL, 'w'},
93  {"password", no_argument, NULL, 'W'},
94  {"echo", no_argument, NULL, 'e'},
95  {"quiet", no_argument, NULL, 'q'},
96  {"dbname", required_argument, NULL, 'd'},
97  {"analyze", no_argument, NULL, 'z'},
98  {"analyze-only", no_argument, NULL, 'Z'},
99  {"freeze", no_argument, NULL, 'F'},
100  {"all", no_argument, NULL, 'a'},
101  {"table", required_argument, NULL, 't'},
102  {"full", no_argument, NULL, 'f'},
103  {"verbose", no_argument, NULL, 'v'},
104  {"jobs", required_argument, NULL, 'j'},
105  {"maintenance-db", required_argument, NULL, 2},
106  {"analyze-in-stages", no_argument, NULL, 3},
107  {NULL, 0, NULL, 0}
108  };
109 
110  const char *progname;
111  int optindex;
112  int c;
113  const char *dbname = NULL;
114  const char *maintenance_db = NULL;
115  char *host = NULL;
116  char *port = NULL;
117  char *username = NULL;
118  enum trivalue prompt_password = TRI_DEFAULT;
119  bool echo = false;
120  bool quiet = false;
121  vacuumingOptions vacopts;
122  bool analyze_in_stages = false;
123  bool alldb = false;
124  SimpleStringList tables = {NULL, NULL};
125  int concurrentCons = 1;
126  int tbl_count = 0;
127 
128  /* initialize options to all false */
129  memset(&vacopts, 0, sizeof(vacopts));
130 
131  progname = get_progname(argv[0]);
132 
133  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
134 
135  handle_help_version_opts(argc, argv, "vacuumdb", help);
136 
137  while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:", long_options, &optindex)) != -1)
138  {
139  switch (c)
140  {
141  case 'h':
142  host = pg_strdup(optarg);
143  break;
144  case 'p':
145  port = pg_strdup(optarg);
146  break;
147  case 'U':
148  username = pg_strdup(optarg);
149  break;
150  case 'w':
151  prompt_password = TRI_NO;
152  break;
153  case 'W':
154  prompt_password = TRI_YES;
155  break;
156  case 'e':
157  echo = true;
158  break;
159  case 'q':
160  quiet = true;
161  break;
162  case 'd':
163  dbname = pg_strdup(optarg);
164  break;
165  case 'z':
166  vacopts.and_analyze = true;
167  break;
168  case 'Z':
169  vacopts.analyze_only = true;
170  break;
171  case 'F':
172  vacopts.freeze = true;
173  break;
174  case 'a':
175  alldb = true;
176  break;
177  case 't':
178  {
180  tbl_count++;
181  break;
182  }
183  case 'f':
184  vacopts.full = true;
185  break;
186  case 'v':
187  vacopts.verbose = true;
188  break;
189  case 'j':
190  concurrentCons = atoi(optarg);
191  if (concurrentCons <= 0)
192  {
193  fprintf(stderr, _("%s: number of parallel jobs must be at least 1\n"),
194  progname);
195  exit(1);
196  }
197  if (concurrentCons > FD_SETSIZE - 1)
198  {
199  fprintf(stderr, _("%s: too many parallel jobs requested (maximum: %d)\n"),
200  progname, FD_SETSIZE - 1);
201  exit(1);
202  }
203  break;
204  case 2:
205  maintenance_db = pg_strdup(optarg);
206  break;
207  case 3:
208  analyze_in_stages = vacopts.analyze_only = true;
209  break;
210  default:
211  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
212  exit(1);
213  }
214  }
215 
216  /*
217  * Non-option argument specifies database name as long as it wasn't
218  * already specified with -d / --dbname
219  */
220  if (optind < argc && dbname == NULL)
221  {
222  dbname = argv[optind];
223  optind++;
224  }
225 
226  if (optind < argc)
227  {
228  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
229  progname, argv[optind]);
230  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
231  exit(1);
232  }
233 
234  if (vacopts.analyze_only)
235  {
236  if (vacopts.full)
237  {
238  fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"),
239  progname, "full");
240  exit(1);
241  }
242  if (vacopts.freeze)
243  {
244  fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"),
245  progname, "freeze");
246  exit(1);
247  }
248  /* allow 'and_analyze' with 'analyze_only' */
249  }
250 
252 
253  /* Avoid opening extra connections. */
254  if (tbl_count && (concurrentCons > tbl_count))
255  concurrentCons = tbl_count;
256 
257  if (alldb)
258  {
259  if (dbname)
260  {
261  fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
262  progname);
263  exit(1);
264  }
265  if (tables.head != NULL)
266  {
267  fprintf(stderr, _("%s: cannot vacuum specific table(s) in all databases\n"),
268  progname);
269  exit(1);
270  }
271 
272  vacuum_all_databases(&vacopts,
273  analyze_in_stages,
274  maintenance_db,
275  host, port, username, prompt_password,
276  concurrentCons,
277  progname, echo, quiet);
278  }
279  else
280  {
281  if (dbname == NULL)
282  {
283  if (getenv("PGDATABASE"))
284  dbname = getenv("PGDATABASE");
285  else if (getenv("PGUSER"))
286  dbname = getenv("PGUSER");
287  else
288  dbname = get_user_name_or_exit(progname);
289  }
290 
291  if (analyze_in_stages)
292  {
293  int stage;
294 
295  for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
296  {
297  vacuum_one_database(dbname, &vacopts,
298  stage,
299  &tables,
300  host, port, username, prompt_password,
301  concurrentCons,
302  progname, echo, quiet);
303  }
304  }
305  else
306  vacuum_one_database(dbname, &vacopts,
308  &tables,
309  host, port, username, prompt_password,
310  concurrentCons,
311  progname, echo, quiet);
312  }
313 
314  exit(0);
315 }
316 
317 /*
318  * vacuum_one_database
319  *
320  * Process tables in the given database. If the 'tables' list is empty,
321  * process all tables in the database.
322  *
323  * Note that this function is only concerned with running exactly one stage
324  * when in analyze-in-stages mode; caller must iterate on us if necessary.
325  *
326  * If concurrentCons is > 1, multiple connections are used to vacuum tables
327  * in parallel. In this case and if the table list is empty, we first obtain
328  * a list of tables from the database.
329  */
330 static void
332  int stage,
333  SimpleStringList *tables,
334  const char *host, const char *port,
335  const char *username, enum trivalue prompt_password,
336  int concurrentCons,
337  const char *progname, bool echo, bool quiet)
338 {
339  PQExpBufferData sql;
340  PGconn *conn;
341  SimpleStringListCell *cell;
342  ParallelSlot *slots = NULL;
343  SimpleStringList dbtables = {NULL, NULL};
344  int i;
345  bool failed = false;
346  bool parallel = concurrentCons > 1;
347  const char *stage_commands[] = {
348  "SET default_statistics_target=1; SET vacuum_cost_delay=0;",
349  "SET default_statistics_target=10; RESET vacuum_cost_delay;",
350  "RESET default_statistics_target;"
351  };
352  const char *stage_messages[] = {
353  gettext_noop("Generating minimal optimizer statistics (1 target)"),
354  gettext_noop("Generating medium optimizer statistics (10 targets)"),
355  gettext_noop("Generating default (full) optimizer statistics")
356  };
357 
358  Assert(stage == ANALYZE_NO_STAGE ||
359  (stage >= 0 && stage < ANALYZE_NUM_STAGES));
360 
361  conn = connectDatabase(dbname, host, port, username, prompt_password,
362  progname, false, true);
363 
364  if (!quiet)
365  {
366  if (stage != ANALYZE_NO_STAGE)
367  printf(_("%s: processing database \"%s\": %s\n"),
368  progname, PQdb(conn), stage_messages[stage]);
369  else
370  printf(_("%s: vacuuming database \"%s\"\n"),
371  progname, PQdb(conn));
372  fflush(stdout);
373  }
374 
375  initPQExpBuffer(&sql);
376 
377  /*
378  * If a table list is not provided and we're using multiple connections,
379  * prepare the list of tables by querying the catalogs.
380  */
381  if (parallel && (!tables || !tables->head))
382  {
384  PGresult *res;
385  int ntups;
386  int i;
387 
388  initPQExpBuffer(&buf);
389 
390  res = executeQuery(conn,
391  "SELECT c.relname, ns.nspname FROM pg_class c, pg_namespace ns\n"
392  " WHERE relkind IN (\'r\', \'m\') AND c.relnamespace = ns.oid\n"
393  " ORDER BY c.relpages DESC;",
394  progname, echo);
395 
396  ntups = PQntuples(res);
397  for (i = 0; i < ntups; i++)
398  {
401  PQgetvalue(res, i, 1),
402  PQgetvalue(res, i, 0)));
403 
404  simple_string_list_append(&dbtables, buf.data);
405  resetPQExpBuffer(&buf);
406  }
407 
408  termPQExpBuffer(&buf);
409  tables = &dbtables;
410 
411  /*
412  * If there are more connections than vacuumable relations, we don't
413  * need to use them all.
414  */
415  if (concurrentCons > ntups)
416  concurrentCons = ntups;
417  if (concurrentCons <= 1)
418  parallel = false;
419  PQclear(res);
420  }
421 
422  /*
423  * Setup the database connections. We reuse the connection we already have
424  * for the first slot. If not in parallel mode, the first slot in the
425  * array contains the connection.
426  */
427  slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * concurrentCons);
428  init_slot(slots, conn, progname);
429  if (parallel)
430  {
431  for (i = 1; i < concurrentCons; i++)
432  {
433  conn = connectDatabase(dbname, host, port, username, prompt_password,
434  progname, false, true);
435  init_slot(slots + i, conn, progname);
436  }
437  }
438 
439  /*
440  * Prepare all the connections to run the appropriate analyze stage, if
441  * caller requested that mode.
442  */
443  if (stage != ANALYZE_NO_STAGE)
444  {
445  int j;
446 
447  /* We already emitted the message above */
448 
449  for (j = 0; j < concurrentCons; j++)
450  executeCommand((slots + j)->connection,
451  stage_commands[stage], progname, echo);
452  }
453 
454  cell = tables ? tables->head : NULL;
455  do
456  {
457  ParallelSlot *free_slot;
458  const char *tabname = cell ? cell->val : NULL;
459 
460  prepare_vacuum_command(&sql, conn, vacopts, tabname);
461 
462  if (CancelRequested)
463  {
464  failed = true;
465  goto finish;
466  }
467 
468  /*
469  * Get the connection slot to use. If in parallel mode, here we wait
470  * for one connection to become available if none already is. In
471  * non-parallel mode we simply use the only slot we have, which we
472  * know to be free.
473  */
474  if (parallel)
475  {
476  /*
477  * Get a free slot, waiting until one becomes free if none
478  * currently is.
479  */
480  free_slot = GetIdleSlot(slots, concurrentCons, progname);
481  if (!free_slot)
482  {
483  failed = true;
484  goto finish;
485  }
486 
487  free_slot->isFree = false;
488  }
489  else
490  free_slot = slots;
491 
492  /*
493  * Execute the vacuum. If not in parallel mode, this terminates the
494  * program in case of an error. (The parallel case handles query
495  * errors in GetQueryResult through GetIdleSlot.)
496  */
497  run_vacuum_command(free_slot->connection, sql.data,
498  echo, tabname, progname, parallel);
499 
500  if (cell)
501  cell = cell->next;
502  } while (cell != NULL);
503 
504  if (parallel)
505  {
506  int j;
507 
508  for (j = 0; j < concurrentCons; j++)
509  {
510  /* wait for all connection to return the results */
511  if (!GetQueryResult((slots + j)->connection, progname))
512  goto finish;
513 
514  (slots + j)->isFree = true;
515  }
516  }
517 
518 finish:
519  for (i = 0; i < concurrentCons; i++)
520  DisconnectDatabase(slots + i);
521  pfree(slots);
522 
523  termPQExpBuffer(&sql);
524 
525  if (failed)
526  exit(1);
527 }
528 
529 /*
530  * Vacuum/analyze all connectable databases.
531  *
532  * In analyze-in-stages mode, we process all databases in one stage before
533  * moving on to the next stage. That ensure minimal stats are available
534  * quickly everywhere before generating more detailed ones.
535  */
536 static void
538  bool analyze_in_stages,
539  const char *maintenance_db, const char *host,
540  const char *port, const char *username,
541  enum trivalue prompt_password,
542  int concurrentCons,
543  const char *progname, bool echo, bool quiet)
544 {
545  PGconn *conn;
546  PGresult *result;
548  int stage;
549  int i;
550 
551  conn = connectMaintenanceDatabase(maintenance_db, host, port,
552  username, prompt_password, progname);
553  result = executeQuery(conn,
554  "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
555  progname, echo);
556  PQfinish(conn);
557 
558  initPQExpBuffer(&connstr);
559  if (analyze_in_stages)
560  {
561  /*
562  * When analyzing all databases in stages, we analyze them all in the
563  * fastest stage first, so that initial statistics become available
564  * for all of them as soon as possible.
565  *
566  * This means we establish several times as many connections, but
567  * that's a secondary consideration.
568  */
569  for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
570  {
571  for (i = 0; i < PQntuples(result); i++)
572  {
573  resetPQExpBuffer(&connstr);
574  appendPQExpBuffer(&connstr, "dbname=");
575  appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
576 
577  vacuum_one_database(connstr.data, vacopts,
578  stage,
579  NULL,
580  host, port, username, prompt_password,
581  concurrentCons,
582  progname, echo, quiet);
583  }
584  }
585  }
586  else
587  {
588  for (i = 0; i < PQntuples(result); i++)
589  {
590  resetPQExpBuffer(&connstr);
591  appendPQExpBuffer(&connstr, "dbname=");
592  appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
593 
594  vacuum_one_database(connstr.data, vacopts,
596  NULL,
597  host, port, username, prompt_password,
598  concurrentCons,
599  progname, echo, quiet);
600  }
601  }
602  termPQExpBuffer(&connstr);
603 
604  PQclear(result);
605 }
606 
607 /*
608  * Construct a vacuum/analyze command to run based on the given options, in the
609  * given string buffer, which may contain previous garbage.
610  *
611  * An optional table name can be passed; this must be already be properly
612  * quoted. The command is semicolon-terminated.
613  */
614 static void
616  const char *table)
617 {
618  resetPQExpBuffer(sql);
619 
620  if (vacopts->analyze_only)
621  {
622  appendPQExpBufferStr(sql, "ANALYZE");
623  if (vacopts->verbose)
624  appendPQExpBufferStr(sql, " VERBOSE");
625  }
626  else
627  {
628  appendPQExpBufferStr(sql, "VACUUM");
629  if (PQserverVersion(conn) >= 90000)
630  {
631  const char *paren = " (";
632  const char *comma = ", ";
633  const char *sep = paren;
634 
635  if (vacopts->full)
636  {
637  appendPQExpBuffer(sql, "%sFULL", sep);
638  sep = comma;
639  }
640  if (vacopts->freeze)
641  {
642  appendPQExpBuffer(sql, "%sFREEZE", sep);
643  sep = comma;
644  }
645  if (vacopts->verbose)
646  {
647  appendPQExpBuffer(sql, "%sVERBOSE", sep);
648  sep = comma;
649  }
650  if (vacopts->and_analyze)
651  {
652  appendPQExpBuffer(sql, "%sANALYZE", sep);
653  sep = comma;
654  }
655  if (sep != paren)
656  appendPQExpBufferChar(sql, ')');
657  }
658  else
659  {
660  if (vacopts->full)
661  appendPQExpBufferStr(sql, " FULL");
662  if (vacopts->freeze)
663  appendPQExpBufferStr(sql, " FREEZE");
664  if (vacopts->verbose)
665  appendPQExpBufferStr(sql, " VERBOSE");
666  if (vacopts->and_analyze)
667  appendPQExpBufferStr(sql, " ANALYZE");
668  }
669  }
670 
671  if (table)
672  appendPQExpBuffer(sql, " %s", table);
673  appendPQExpBufferChar(sql, ';');
674 }
675 
676 /*
677  * Execute a vacuum/analyze command to the server.
678  *
679  * Any errors during command execution are reported to stderr. If async is
680  * false, this function exits the program after reporting the error.
681  */
682 static void
683 run_vacuum_command(PGconn *conn, const char *sql, bool echo,
684  const char *table, const char *progname, bool async)
685 {
686  bool status;
687 
688  if (async)
689  {
690  if (echo)
691  printf("%s\n", sql);
692 
693  status = PQsendQuery(conn, sql) == 1;
694  }
695  else
696  status = executeMaintenanceCommand(conn, sql, echo);
697 
698  if (!status)
699  {
700  if (table)
701  fprintf(stderr,
702  _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
703  progname, table, PQdb(conn), PQerrorMessage(conn));
704  else
705  fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
706  progname, PQdb(conn), PQerrorMessage(conn));
707 
708  if (!async)
709  {
710  PQfinish(conn);
711  exit(1);
712  }
713  }
714 }
715 
716 /*
717  * GetIdleSlot
718  * Return a connection slot that is ready to execute a command.
719  *
720  * We return the first slot we find that is marked isFree, if one is;
721  * otherwise, we loop on select() until one socket becomes available. When
722  * this happens, we read the whole set and mark as free all sockets that become
723  * available.
724  *
725  * Process the slot list, if any free slot is available then return the slotid
726  * else perform the select on all the socket's and wait until at least one slot
727  * becomes available.
728  *
729  * If an error occurs, NULL is returned.
730  */
731 static ParallelSlot *
732 GetIdleSlot(ParallelSlot slots[], int numslots,
733  const char *progname)
734 {
735  int i;
736  int firstFree = -1;
737  fd_set slotset;
738  pgsocket maxFd;
739 
740  for (i = 0; i < numslots; i++)
741  if ((slots + i)->isFree)
742  return slots + i;
743 
744  FD_ZERO(&slotset);
745 
746  maxFd = slots->sock;
747  for (i = 0; i < numslots; i++)
748  {
749  FD_SET((slots + i)->sock, &slotset);
750  if ((slots + i)->sock > maxFd)
751  maxFd = (slots + i)->sock;
752  }
753 
754  /*
755  * No free slot found, so wait until one of the connections has finished
756  * its task and return the available slot.
757  */
758  for (firstFree = -1; firstFree < 0;)
759  {
760  bool aborting;
761 
762  SetCancelConn(slots->connection);
763  i = select_loop(maxFd, &slotset, &aborting);
764  ResetCancelConn();
765 
766  if (aborting)
767  {
768  /*
769  * We set the cancel-receiving connection to the one in the zeroth
770  * slot above, so fetch the error from there.
771  */
772  GetQueryResult(slots->connection, progname);
773  return NULL;
774  }
775  Assert(i != 0);
776 
777  for (i = 0; i < numslots; i++)
778  {
779  if (!FD_ISSET((slots + i)->sock, &slotset))
780  continue;
781 
782  PQconsumeInput((slots + i)->connection);
783  if (PQisBusy((slots + i)->connection))
784  continue;
785 
786  (slots + i)->isFree = true;
787 
788  if (!GetQueryResult((slots + i)->connection, progname))
789  return NULL;
790 
791  if (firstFree < 0)
792  firstFree = i;
793  }
794  }
795 
796  return slots + firstFree;
797 }
798 
799 /*
800  * GetQueryResult
801  *
802  * Process the query result. Returns true if there's no error, false
803  * otherwise -- but errors about trying to vacuum a missing relation are
804  * reported and subsequently ignored.
805  */
806 static bool
808 {
809  PGresult *result;
810 
811  SetCancelConn(conn);
812  while ((result = PQgetResult(conn)) != NULL)
813  {
814  /*
815  * If errors are found, report them. Errors about a missing table are
816  * harmless so we continue processing; but die for other errors.
817  */
818  if (PQresultStatus(result) != PGRES_COMMAND_OK)
819  {
820  char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
821 
822  fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
823  progname, PQdb(conn), PQerrorMessage(conn));
824 
825  if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
826  {
827  PQclear(result);
828  return false;
829  }
830  }
831 
832  PQclear(result);
833  }
834  ResetCancelConn();
835 
836  return true;
837 }
838 
839 /*
840  * DisconnectDatabase
841  * Disconnect the connection associated with the given slot
842  */
843 static void
845 {
846  char errbuf[256];
847 
848  if (!slot->connection)
849  return;
850 
852  {
853  PGcancel *cancel;
854 
855  if ((cancel = PQgetCancel(slot->connection)))
856  {
857  PQcancel(cancel, errbuf, sizeof(errbuf));
858  PQfreeCancel(cancel);
859  }
860  }
861 
862  PQfinish(slot->connection);
863  slot->connection = NULL;
864 }
865 
866 /*
867  * Loop on select() until a descriptor from the given set becomes readable.
868  *
869  * If we get a cancel request while we're waiting, we forego all further
870  * processing and set the *aborting flag to true. The return value must be
871  * ignored in this case. Otherwise, *aborting is set to false.
872  */
873 static int
874 select_loop(int maxFd, fd_set *workerset, bool *aborting)
875 {
876  int i;
877  fd_set saveSet = *workerset;
878 
879  if (CancelRequested)
880  {
881  *aborting = true;
882  return -1;
883  }
884  else
885  *aborting = false;
886 
887  for (;;)
888  {
889  /*
890  * On Windows, we need to check once in a while for cancel requests;
891  * on other platforms we rely on select() returning when interrupted.
892  */
893  struct timeval *tvp;
894 #ifdef WIN32
895  struct timeval tv = {0, 1000000};
896 
897  tvp = &tv;
898 #else
899  tvp = NULL;
900 #endif
901 
902  *workerset = saveSet;
903  i = select(maxFd + 1, workerset, NULL, NULL, tvp);
904 
905 #ifdef WIN32
906  if (i == SOCKET_ERROR)
907  {
908  i = -1;
909 
910  if (WSAGetLastError() == WSAEINTR)
911  errno = EINTR;
912  }
913 #endif
914 
915  if (i < 0 && errno == EINTR)
916  continue; /* ignore this */
917  if (i < 0 || CancelRequested)
918  *aborting = true; /* but not this */
919  if (i == 0)
920  continue; /* timeout (Win32 only) */
921  break;
922  }
923 
924  return i;
925 }
926 
927 static void
929 {
930  slot->connection = conn;
931  slot->isFree = true;
932  slot->sock = PQsocket(conn);
933 
934  if (slot->sock < 0)
935  {
936  fprintf(stderr, _("%s: invalid socket: %s"), progname,
937  PQerrorMessage(conn));
938  exit(1);
939  }
940 }
941 
942 static void
943 help(const char *progname)
944 {
945  printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname);
946  printf(_("Usage:\n"));
947  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
948  printf(_("\nOptions:\n"));
949  printf(_(" -a, --all vacuum all databases\n"));
950  printf(_(" -d, --dbname=DBNAME database to vacuum\n"));
951  printf(_(" -e, --echo show the commands being sent to the server\n"));
952  printf(_(" -f, --full do full vacuuming\n"));
953  printf(_(" -F, --freeze freeze row transaction information\n"));
954  printf(_(" -j, --jobs=NUM use this many concurrent connections to vacuum\n"));
955  printf(_(" -q, --quiet don't write any messages\n"));
956  printf(_(" -t, --table='TABLE[(COLUMNS)]' vacuum specific table(s) only\n"));
957  printf(_(" -v, --verbose write a lot of output\n"));
958  printf(_(" -V, --version output version information, then exit\n"));
959  printf(_(" -z, --analyze update optimizer statistics\n"));
960  printf(_(" -Z, --analyze-only only update optimizer statistics; no vacuum\n"));
961  printf(_(" --analyze-in-stages only update optimizer statistics, in multiple\n"
962  " stages for faster results; no vacuum\n"));
963  printf(_(" -?, --help show this help, then exit\n"));
964  printf(_("\nConnection options:\n"));
965  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
966  printf(_(" -p, --port=PORT database server port\n"));
967  printf(_(" -U, --username=USERNAME user name to connect as\n"));
968  printf(_(" -w, --no-password never prompt for password\n"));
969  printf(_(" -W, --password force password prompt\n"));
970  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
971  printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
972  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
973 }
static int select_loop(int maxFd, fd_set *workerset, bool *aborting)
Definition: vacuumdb.c:874
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2065
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5960
bool isFree
Definition: vacuumdb.c:31
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
const char * get_progname(const char *argv0)
Definition: path.c:453
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3661
static ParallelSlot * GetIdleSlot(ParallelSlot slots[], int numslots, const char *progname)
Definition: vacuumdb.c:732
pgsocket sock
Definition: vacuumdb.c:30
struct vacuumingOptions vacuumingOptions
#define gettext_noop(x)
Definition: c.h:139
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3517
#define select(n, r, w, e, timeout)
Definition: win32.h:384
static void setup_cancel_handler(void)
Definition: parallel.c:622
#define ANALYZE_NO_STAGE
Definition: vacuumdb.c:81
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5950
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:53
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, int stage, SimpleStringList *tables, const char *host, const char *port, const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet)
Definition: vacuumdb.c:331
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1132
#define required_argument
Definition: getopt_long.h:25
void pfree(void *pointer)
Definition: mcxt.c:992
int optind
Definition: getopt.c:51
PGconn * conn
Definition: streamutil.c:45
int main(int argc, char *argv[])
Definition: vacuumdb.c:86
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2091
char * c
static char * buf
Definition: pg_test_fsync.c:65
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:235
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3638
struct SimpleStringListCell * next
Definition: simple_list.h:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
bool and_analyze
Definition: vacuumdb.c:39
struct ParallelSlot ParallelSlot
int pgsocket
Definition: port.h:22
PGconn * connection
Definition: vacuumdb.c:29
void SetCancelConn(void)
Definition: common.c:402
static void init_slot(ParallelSlot *slot, PGconn *conn, const char *progname)
Definition: vacuumdb.c:928
static int port
Definition: pg_regress.c:87
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:35
static void DisconnectDatabase(ParallelSlot *slot)
Definition: vacuumdb.c:844
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5915
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
trivalue
Definition: vacuumlo.c:31
static void help(const char *progname)
Definition: vacuumdb.c:943
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1011
static void vacuum_all_databases(vacuumingOptions *vacopts, bool analyze_in_stages, const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet)
Definition: vacuumdb.c:537
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
static void run_vacuum_command(PGconn *conn, const char *sql, bool echo, const char *table, const char *progname, bool async)
Definition: vacuumdb.c:683
#define EINTR
Definition: win32.h:295
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1631
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:130
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5828
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:2658
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void prepare_vacuum_command(PQExpBuffer sql, PGconn *conn, vacuumingOptions *vacopts, const char *table)
Definition: vacuumdb.c:615
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:1816
static bool GetQueryResult(PGconn *conn, const char *progname)
Definition: vacuumdb.c:807
int PQisBusy(PGconn *conn)
Definition: fe-exec.c:1681
#define ERRCODE_UNDEFINED_TABLE
Definition: vacuumdb.c:24
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:41
#define ANALYZE_NUM_STAGES
Definition: vacuumdb.c:82
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3793
bool CancelRequested
Definition: common.c:24
bool analyze_only
Definition: vacuumdb.c:37
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
int i
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
void ResetCancelConn(void)
Definition: common.c:432
#define _(x)
Definition: elog.c:84
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:5978
const char * fmtQualifiedId(int remoteVersion, const char *schema, const char *id)
Definition: string_utils.c:150
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:67
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:76