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

Go to the source code of this file.

Data Structures

struct  ParallelSlot
 
struct  vacuumingOptions
 

Macros

#define ERRCODE_UNDEFINED_TABLE   "42P01"
 
#define ANALYZE_NO_STAGE   -1
 
#define ANALYZE_NUM_STAGES   3
 

Typedefs

typedef struct ParallelSlot ParallelSlot
 
typedef struct vacuumingOptions vacuumingOptions
 

Functions

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)
 
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)
 
static void prepare_vacuum_command (PQExpBuffer sql, PGconn *conn, vacuumingOptions *vacopts, const char *table)
 
static void run_vacuum_command (PGconn *conn, const char *sql, bool echo, const char *table, const char *progname, bool async)
 
static ParallelSlotGetIdleSlot (ParallelSlot slots[], int numslots, const char *progname)
 
static bool GetQueryResult (PGconn *conn, const char *progname)
 
static void DisconnectDatabase (ParallelSlot *slot)
 
static int select_loop (int maxFd, fd_set *workerset, bool *aborting)
 
static void init_slot (ParallelSlot *slot, PGconn *conn, const char *progname)
 
static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define ANALYZE_NO_STAGE   -1

Definition at line 81 of file vacuumdb.c.

Referenced by main(), vacuum_all_databases(), and vacuum_one_database().

#define ANALYZE_NUM_STAGES   3

Definition at line 82 of file vacuumdb.c.

Referenced by main(), vacuum_all_databases(), and vacuum_one_database().

#define ERRCODE_UNDEFINED_TABLE   "42P01"

Definition at line 24 of file vacuumdb.c.

Referenced by GetQueryResult().

Typedef Documentation

Function Documentation

static void DisconnectDatabase ( ParallelSlot slot)
static

Definition at line 844 of file vacuumdb.c.

References ParallelSlot::connection, NULL, PQcancel(), PQfinish(), PQfreeCancel(), PQgetCancel(), PQTRANS_ACTIVE, and PQtransactionStatus().

Referenced by vacuum_one_database().

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 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3660
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3637
PGconn * connection
Definition: vacuumdb.c:29
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5914
#define NULL
Definition: c.h:226
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3792
static ParallelSlot * GetIdleSlot ( ParallelSlot  slots[],
int  numslots,
const char *  progname 
)
static

Definition at line 732 of file vacuumdb.c.

References Assert, ParallelSlot::connection, GetQueryResult(), i, NULL, PQconsumeInput(), PQisBusy(), ResetCancelConn(), select_loop(), SetCancelConn(), and ParallelSlot::sock.

Referenced by vacuum_one_database().

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  */
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 }
static int select_loop(int maxFd, fd_set *workerset, bool *aborting)
Definition: vacuumdb.c:874
pgsocket sock
Definition: vacuumdb.c:30
const char * progname
Definition: pg_standby.c:37
int pgsocket
Definition: port.h:22
PGconn * connection
Definition: vacuumdb.c:29
void SetCancelConn(void)
Definition: common.c:402
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1631
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static bool GetQueryResult(PGconn *conn, const char *progname)
Definition: vacuumdb.c:807
int PQisBusy(PGconn *conn)
Definition: fe-exec.c:1681
int i
void ResetCancelConn(void)
Definition: common.c:432
static bool GetQueryResult ( PGconn conn,
const char *  progname 
)
static

Definition at line 807 of file vacuumdb.c.

References _, ERRCODE_UNDEFINED_TABLE, NULL, PG_DIAG_SQLSTATE, PGRES_COMMAND_OK, PQclear(), PQdb(), PQerrorMessage(), PQgetResult(), PQresultErrorField(), PQresultStatus(), ResetCancelConn(), and SetCancelConn().

Referenced by GetIdleSlot(), and vacuum_one_database().

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 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
const char * progname
Definition: pg_standby.c:37
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:53
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
void SetCancelConn(void)
Definition: common.c:402
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:2658
#define NULL
Definition: c.h:226
#define ERRCODE_UNDEFINED_TABLE
Definition: vacuumdb.c:24
void ResetCancelConn(void)
Definition: common.c:432
#define _(x)
Definition: elog.c:84
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
static void help ( const char *  progname)
static

Definition at line 943 of file vacuumdb.c.

References _.

Referenced by main().

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 }
const char * progname
Definition: pg_standby.c:37
#define _(x)
Definition: elog.c:84
static void init_slot ( ParallelSlot slot,
PGconn conn,
const char *  progname 
)
static

Definition at line 928 of file vacuumdb.c.

References _, conn, ParallelSlot::connection, ParallelSlot::isFree, PQerrorMessage(), PQsocket(), and ParallelSlot::sock.

Referenced by vacuum_one_database().

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 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
bool isFree
Definition: vacuumdb.c:31
pgsocket sock
Definition: vacuumdb.c:30
const char * progname
Definition: pg_standby.c:37
PGconn * conn
Definition: streamutil.c:42
PGconn * connection
Definition: vacuumdb.c:29
#define _(x)
Definition: elog.c:84
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:5977
int main ( int  argc,
char *  argv[] 
)

Definition at line 86 of file vacuumdb.c.

References _, ANALYZE_NO_STAGE, ANALYZE_NUM_STAGES, vacuumingOptions::analyze_only, vacuumingOptions::and_analyze, dbname, vacuumingOptions::freeze, vacuumingOptions::full, get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), no_argument, NULL, optarg, optind, pg_strdup(), PG_TEXTDOMAIN, port, progname, required_argument, set_pglocale_pgservice(), setup_cancel_handler(), simple_string_list_append(), TRI_DEFAULT, TRI_NO, TRI_YES, username, vacuum_all_databases(), vacuum_one_database(), and vacuumingOptions::verbose.

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 }
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void setup_cancel_handler(void)
Definition: parallel.c:620
#define ANALYZE_NO_STAGE
Definition: vacuumdb.c:81
const char * progname
Definition: pg_standby.c:37
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
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
bool and_analyze
Definition: vacuumdb.c:39
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
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:1012
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 char * username
Definition: initdb.c:129
#define NULL
Definition: c.h:226
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:38
#define ANALYZE_NUM_STAGES
Definition: vacuumdb.c:82
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
#define _(x)
Definition: elog.c:84
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
static void prepare_vacuum_command ( PQExpBuffer  sql,
PGconn conn,
vacuumingOptions vacopts,
const char *  table 
)
static

Definition at line 615 of file vacuumdb.c.

References vacuumingOptions::analyze_only, vacuumingOptions::and_analyze, appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), vacuumingOptions::freeze, vacuumingOptions::full, PQserverVersion(), resetPQExpBuffer(), and vacuumingOptions::verbose.

Referenced by vacuum_one_database().

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 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5949
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
bool and_analyze
Definition: vacuumdb.c:39
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
bool analyze_only
Definition: vacuumdb.c:37
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static void run_vacuum_command ( PGconn conn,
const char *  sql,
bool  echo,
const char *  table,
const char *  progname,
bool  async 
)
static

Definition at line 683 of file vacuumdb.c.

References _, executeMaintenanceCommand(), PQdb(), PQerrorMessage(), PQfinish(), PQsendQuery(), and status().

Referenced by vacuum_one_database().

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 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
const char * progname
Definition: pg_standby.c:37
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1132
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
Definition: common.c:235
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
#define _(x)
Definition: elog.c:84
static int select_loop ( int  maxFd,
fd_set *  workerset,
bool aborting 
)
static

Definition at line 874 of file vacuumdb.c.

References CancelRequested, EINTR, i, NULL, and select.

Referenced by GetIdleSlot().

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 }
#define select(n, r, w, e, timeout)
Definition: win32.h:384
#define EINTR
Definition: win32.h:295
#define NULL
Definition: c.h:226
bool CancelRequested
Definition: common.c:24
int i
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 
)
static

Definition at line 537 of file vacuumdb.c.

References ANALYZE_NO_STAGE, ANALYZE_NUM_STAGES, appendConnStrVal(), appendPQExpBuffer(), conn, connectMaintenanceDatabase(), connstr, PQExpBufferData::data, executeQuery(), i, initPQExpBuffer(), NULL, PQclear(), PQfinish(), PQgetvalue(), PQntuples(), resetPQExpBuffer(), termPQExpBuffer(), and vacuum_one_database().

Referenced by main().

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 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
#define ANALYZE_NO_STAGE
Definition: vacuumdb.c:81
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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
PGconn * conn
Definition: streamutil.c:42
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static int port
Definition: pg_regress.c:87
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname)
Definition: common.c:152
static char * username
Definition: initdb.c:129
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
#define ANALYZE_NUM_STAGES
Definition: vacuumdb.c:82
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:63
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 
)
static

Definition at line 331 of file vacuumdb.c.

References _, ANALYZE_NO_STAGE, ANALYZE_NUM_STAGES, appendPQExpBufferStr(), Assert, buf, CancelRequested, conn, connectDatabase(), ParallelSlot::connection, PQExpBufferData::data, DisconnectDatabase(), executeCommand(), executeQuery(), fmtQualifiedId(), GetIdleSlot(), GetQueryResult(), gettext_noop, SimpleStringList::head, i, init_slot(), initPQExpBuffer(), ParallelSlot::isFree, SimpleStringListCell::next, NULL, pfree(), pg_malloc(), PQclear(), PQdb(), PQgetvalue(), PQntuples(), PQserverVersion(), prepare_vacuum_command(), resetPQExpBuffer(), run_vacuum_command(), simple_string_list_append(), termPQExpBuffer(), and SimpleStringListCell::val.

Referenced by main(), and vacuum_all_databases().

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 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static ParallelSlot * GetIdleSlot(ParallelSlot slots[], int numslots, const char *progname)
Definition: vacuumdb.c:732
#define gettext_noop(x)
Definition: c.h:139
#define ANALYZE_NO_STAGE
Definition: vacuumdb.c:81
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5949
const char * progname
Definition: pg_standby.c:37
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void pfree(void *pointer)
Definition: mcxt.c:992
PGconn * conn
Definition: streamutil.c:42
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2087
static char * buf
Definition: pg_test_fsync.c:65
struct SimpleStringListCell * next
Definition: simple_list.h:34
PGconn * connection
Definition: vacuumdb.c:29
static void init_slot(ParallelSlot *slot, PGconn *conn, const char *progname)
Definition: vacuumdb.c:928
static int port
Definition: pg_regress.c:87
static void DisconnectDatabase(ParallelSlot *slot)
Definition: vacuumdb.c:844
static void run_vacuum_command(PGconn *conn, const char *sql, bool echo, const char *table, const char *progname, bool async)
Definition: vacuumdb.c:683
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
static char * username
Definition: initdb.c:129
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
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:1812
static bool GetQueryResult(PGconn *conn, const char *progname)
Definition: vacuumdb.c:807
SimpleStringListCell * head
Definition: simple_list.h:42
char * dbname
Definition: streamutil.c:38
#define ANALYZE_NUM_STAGES
Definition: vacuumdb.c:82
bool CancelRequested
Definition: common.c:24
int i
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
#define _(x)
Definition: elog.c:84
const char * fmtQualifiedId(int remoteVersion, const char *schema, const char *id)
Definition: string_utils.c:150
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89