Header And Logo

PostgreSQL
| The world's most advanced open source database.

clusterdb.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * clusterdb
00004  *
00005  * Portions Copyright (c) 2002-2013, PostgreSQL Global Development Group
00006  *
00007  * src/bin/scripts/clusterdb.c
00008  *
00009  *-------------------------------------------------------------------------
00010  */
00011 
00012 #include "postgres_fe.h"
00013 #include "common.h"
00014 #include "dumputils.h"
00015 
00016 
00017 static void cluster_one_database(const char *dbname, bool verbose, const char *table,
00018                      const char *host, const char *port,
00019                      const char *username, enum trivalue prompt_password,
00020                      const char *progname, bool echo);
00021 static void cluster_all_databases(bool verbose, const char *maintenance_db,
00022                       const char *host, const char *port,
00023                       const char *username, enum trivalue prompt_password,
00024                       const char *progname, bool echo, bool quiet);
00025 
00026 static void help(const char *progname);
00027 
00028 
00029 int
00030 main(int argc, char *argv[])
00031 {
00032     static struct option long_options[] = {
00033         {"host", required_argument, NULL, 'h'},
00034         {"port", required_argument, NULL, 'p'},
00035         {"username", required_argument, NULL, 'U'},
00036         {"no-password", no_argument, NULL, 'w'},
00037         {"password", no_argument, NULL, 'W'},
00038         {"echo", no_argument, NULL, 'e'},
00039         {"quiet", no_argument, NULL, 'q'},
00040         {"dbname", required_argument, NULL, 'd'},
00041         {"all", no_argument, NULL, 'a'},
00042         {"table", required_argument, NULL, 't'},
00043         {"verbose", no_argument, NULL, 'v'},
00044         {"maintenance-db", required_argument, NULL, 2},
00045         {NULL, 0, NULL, 0}
00046     };
00047 
00048     const char *progname;
00049     int         optindex;
00050     int         c;
00051 
00052     const char *dbname = NULL;
00053     const char *maintenance_db = NULL;
00054     char       *host = NULL;
00055     char       *port = NULL;
00056     char       *username = NULL;
00057     enum trivalue prompt_password = TRI_DEFAULT;
00058     bool        echo = false;
00059     bool        quiet = false;
00060     bool        alldb = false;
00061     bool        verbose = false;
00062     SimpleStringList tables = {NULL, NULL};
00063 
00064     progname = get_progname(argv[0]);
00065     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
00066 
00067     handle_help_version_opts(argc, argv, "clusterdb", help);
00068 
00069     while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
00070     {
00071         switch (c)
00072         {
00073             case 'h':
00074                 host = pg_strdup(optarg);
00075                 break;
00076             case 'p':
00077                 port = pg_strdup(optarg);
00078                 break;
00079             case 'U':
00080                 username = pg_strdup(optarg);
00081                 break;
00082             case 'w':
00083                 prompt_password = TRI_NO;
00084                 break;
00085             case 'W':
00086                 prompt_password = TRI_YES;
00087                 break;
00088             case 'e':
00089                 echo = true;
00090                 break;
00091             case 'q':
00092                 quiet = true;
00093                 break;
00094             case 'd':
00095                 dbname = pg_strdup(optarg);
00096                 break;
00097             case 'a':
00098                 alldb = true;
00099                 break;
00100             case 't':
00101                 simple_string_list_append(&tables, optarg);
00102                 break;
00103             case 'v':
00104                 verbose = true;
00105                 break;
00106             case 2:
00107                 maintenance_db = pg_strdup(optarg);
00108                 break;
00109             default:
00110                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00111                 exit(1);
00112         }
00113     }
00114 
00115     /*
00116      * Non-option argument specifies database name as long as it wasn't
00117      * already specified with -d / --dbname
00118      */
00119     if (optind < argc && dbname == NULL)
00120     {
00121         dbname = argv[optind];
00122         optind++;
00123     }
00124 
00125     if (optind < argc)
00126     {
00127         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
00128                 progname, argv[optind]);
00129         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00130         exit(1);
00131     }
00132 
00133     setup_cancel_handler();
00134 
00135     if (alldb)
00136     {
00137         if (dbname)
00138         {
00139             fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"),
00140                     progname);
00141             exit(1);
00142         }
00143 
00144         if (tables.head != NULL)
00145         {
00146             fprintf(stderr, _("%s: cannot cluster specific table(s) in all databases\n"),
00147                     progname);
00148             exit(1);
00149         }
00150 
00151         cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
00152                               progname, echo, quiet);
00153     }
00154     else
00155     {
00156         if (dbname == NULL)
00157         {
00158             if (getenv("PGDATABASE"))
00159                 dbname = getenv("PGDATABASE");
00160             else if (getenv("PGUSER"))
00161                 dbname = getenv("PGUSER");
00162             else
00163                 dbname = get_user_name(progname);
00164         }
00165 
00166         if (tables.head != NULL)
00167         {
00168             SimpleStringListCell *cell;
00169 
00170             for (cell = tables.head; cell; cell = cell->next)
00171             {
00172                 cluster_one_database(dbname, verbose, cell->val,
00173                                      host, port, username, prompt_password,
00174                                      progname, echo);
00175             }
00176         }
00177         else
00178             cluster_one_database(dbname, verbose, NULL,
00179                                  host, port, username, prompt_password,
00180                                  progname, echo);
00181     }
00182 
00183     exit(0);
00184 }
00185 
00186 
00187 static void
00188 cluster_one_database(const char *dbname, bool verbose, const char *table,
00189                      const char *host, const char *port,
00190                      const char *username, enum trivalue prompt_password,
00191                      const char *progname, bool echo)
00192 {
00193     PQExpBufferData sql;
00194 
00195     PGconn     *conn;
00196 
00197     initPQExpBuffer(&sql);
00198 
00199     appendPQExpBuffer(&sql, "CLUSTER");
00200     if (verbose)
00201         appendPQExpBuffer(&sql, " VERBOSE");
00202     if (table)
00203         appendPQExpBuffer(&sql, " %s", table);
00204     appendPQExpBuffer(&sql, ";\n");
00205 
00206     conn = connectDatabase(dbname, host, port, username, prompt_password,
00207                            progname, false);
00208     if (!executeMaintenanceCommand(conn, sql.data, echo))
00209     {
00210         if (table)
00211             fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
00212                     progname, table, dbname, PQerrorMessage(conn));
00213         else
00214             fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"),
00215                     progname, dbname, PQerrorMessage(conn));
00216         PQfinish(conn);
00217         exit(1);
00218     }
00219     PQfinish(conn);
00220     termPQExpBuffer(&sql);
00221 }
00222 
00223 
00224 static void
00225 cluster_all_databases(bool verbose, const char *maintenance_db,
00226                       const char *host, const char *port,
00227                       const char *username, enum trivalue prompt_password,
00228                       const char *progname, bool echo, bool quiet)
00229 {
00230     PGconn     *conn;
00231     PGresult   *result;
00232     int         i;
00233 
00234     conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
00235                                       prompt_password, progname);
00236     result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
00237     PQfinish(conn);
00238 
00239     for (i = 0; i < PQntuples(result); i++)
00240     {
00241         char       *dbname = PQgetvalue(result, i, 0);
00242 
00243         if (!quiet)
00244         {
00245             printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
00246             fflush(stdout);
00247         }
00248 
00249         cluster_one_database(dbname, verbose, NULL,
00250                              host, port, username, prompt_password,
00251                              progname, echo);
00252     }
00253 
00254     PQclear(result);
00255 }
00256 
00257 
00258 static void
00259 help(const char *progname)
00260 {
00261     printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
00262     printf(_("Usage:\n"));
00263     printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
00264     printf(_("\nOptions:\n"));
00265     printf(_("  -a, --all                 cluster all databases\n"));
00266     printf(_("  -d, --dbname=DBNAME       database to cluster\n"));
00267     printf(_("  -e, --echo                show the commands being sent to the server\n"));
00268     printf(_("  -q, --quiet               don't write any messages\n"));
00269     printf(_("  -t, --table=TABLE         cluster specific table(s) only\n"));
00270     printf(_("  -v, --verbose             write a lot of output\n"));
00271     printf(_("  -V, --version             output version information, then exit\n"));
00272     printf(_("  -?, --help                show this help, then exit\n"));
00273     printf(_("\nConnection options:\n"));
00274     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
00275     printf(_("  -p, --port=PORT           database server port\n"));
00276     printf(_("  -U, --username=USERNAME   user name to connect as\n"));
00277     printf(_("  -w, --no-password         never prompt for password\n"));
00278     printf(_("  -W, --password            force password prompt\n"));
00279     printf(_("  --maintenance-db=DBNAME   alternate maintenance database\n"));
00280     printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
00281     printf(_("\nReport bugs to <[email protected]>.\n"));
00282 }