LCOV - differential code coverage report
Current view: top level - src/bin/scripts - clusterdb.c (source / functions) Coverage Total Hit UNC LBC UBC GNC CBC DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 74.5 % 145 108 6 6 25 13 95 12 7
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 4 4 1 3
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * clusterdb
       4                 :  *
       5                 :  * Portions Copyright (c) 2002-2023, PostgreSQL Global Development Group
       6                 :  *
       7                 :  * src/bin/scripts/clusterdb.c
       8                 :  *
       9                 :  *-------------------------------------------------------------------------
      10                 :  */
      11                 : 
      12                 : #include "postgres_fe.h"
      13                 : #include "common.h"
      14                 : #include "common/logging.h"
      15                 : #include "fe_utils/cancel.h"
      16                 : #include "fe_utils/option_utils.h"
      17                 : #include "fe_utils/query_utils.h"
      18                 : #include "fe_utils/simple_list.h"
      19                 : #include "fe_utils/string_utils.h"
      20                 : 
      21                 : 
      22                 : static void cluster_one_database(const ConnParams *cparams, const char *table,
      23                 :                                  const char *progname, bool verbose, bool echo);
      24                 : static void cluster_all_databases(ConnParams *cparams, const char *progname,
      25                 :                                   bool verbose, bool echo, bool quiet);
      26                 : static void help(const char *progname);
      27                 : 
      28                 : 
      29                 : int
      30 CBC           9 : main(int argc, char *argv[])
      31                 : {
      32                 :     static struct option long_options[] = {
      33                 :         {"host", required_argument, NULL, 'h'},
      34                 :         {"port", required_argument, NULL, 'p'},
      35                 :         {"username", required_argument, NULL, 'U'},
      36                 :         {"no-password", no_argument, NULL, 'w'},
      37                 :         {"password", no_argument, NULL, 'W'},
      38                 :         {"echo", no_argument, NULL, 'e'},
      39                 :         {"quiet", no_argument, NULL, 'q'},
      40                 :         {"dbname", required_argument, NULL, 'd'},
      41                 :         {"all", no_argument, NULL, 'a'},
      42                 :         {"table", required_argument, NULL, 't'},
      43                 :         {"verbose", no_argument, NULL, 'v'},
      44                 :         {"maintenance-db", required_argument, NULL, 2},
      45                 :         {NULL, 0, NULL, 0}
      46                 :     };
      47                 : 
      48                 :     const char *progname;
      49                 :     int         optindex;
      50                 :     int         c;
      51                 : 
      52               9 :     const char *dbname = NULL;
      53               9 :     const char *maintenance_db = NULL;
      54               9 :     char       *host = NULL;
      55               9 :     char       *port = NULL;
      56               9 :     char       *username = NULL;
      57               9 :     enum trivalue prompt_password = TRI_DEFAULT;
      58                 :     ConnParams  cparams;
      59               9 :     bool        echo = false;
      60               9 :     bool        quiet = false;
      61               9 :     bool        alldb = false;
      62               9 :     bool        verbose = false;
      63               9 :     SimpleStringList tables = {NULL, NULL};
      64                 : 
      65               9 :     pg_logging_init(argv[0]);
      66               9 :     progname = get_progname(argv[0]);
      67               9 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
      68                 : 
      69               9 :     handle_help_version_opts(argc, argv, "clusterdb", help);
      70                 : 
      71 GNC          15 :     while ((c = getopt_long(argc, argv, "ad:eh:p:qt:U:vwW", long_options, &optindex)) != -1)
      72                 :     {
      73 CBC           9 :         switch (c)
      74                 :         {
      75 GNC           2 :             case 'a':
      76               2 :                 alldb = true;
      77               2 :                 break;
      78 UNC           0 :             case 'd':
      79               0 :                 dbname = pg_strdup(optarg);
      80               0 :                 break;
      81 GNC           2 :             case 'e':
      82               2 :                 echo = true;
      83               2 :                 break;
      84 LBC           0 :             case 'h':
      85               0 :                 host = pg_strdup(optarg);
      86               0 :                 break;
      87 UBC           0 :             case 'p':
      88               0 :                 port = pg_strdup(optarg);
      89               0 :                 break;
      90 UNC           0 :             case 'q':
      91               0 :                 quiet = true;
      92               0 :                 break;
      93 GNC           2 :             case 't':
      94               2 :                 simple_string_list_append(&tables, optarg);
      95               2 :                 break;
      96 LBC           0 :             case 'U':
      97               0 :                 username = pg_strdup(optarg);
      98               0 :                 break;
      99 GNC           2 :             case 'v':
     100               2 :                 verbose = true;
     101               2 :                 break;
     102 UBC           0 :             case 'w':
     103               0 :                 prompt_password = TRI_NO;
     104               0 :                 break;
     105               0 :             case 'W':
     106               0 :                 prompt_password = TRI_YES;
     107               0 :                 break;
     108               0 :             case 2:
     109               0 :                 maintenance_db = pg_strdup(optarg);
     110               0 :                 break;
     111 CBC           1 :             default:
     112                 :                 /* getopt_long already emitted a complaint */
     113               1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     114               1 :                 exit(1);
     115                 :         }
     116                 :     }
     117                 : 
     118                 :     /*
     119                 :      * Non-option argument specifies database name as long as it wasn't
     120                 :      * already specified with -d / --dbname
     121                 :      */
     122               6 :     if (optind < argc && dbname == NULL)
     123                 :     {
     124               1 :         dbname = argv[optind];
     125               1 :         optind++;
     126                 :     }
     127                 : 
     128               6 :     if (optind < argc)
     129                 :     {
     130 UBC           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     131                 :                      argv[optind]);
     132               0 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     133               0 :         exit(1);
     134                 :     }
     135                 : 
     136                 :     /* fill cparams except for dbname, which is set below */
     137 CBC           6 :     cparams.pghost = host;
     138               6 :     cparams.pgport = port;
     139               6 :     cparams.pguser = username;
     140               6 :     cparams.prompt_password = prompt_password;
     141               6 :     cparams.override_dbname = NULL;
     142                 : 
     143               6 :     setup_cancel_handler(NULL);
     144                 : 
     145               6 :     if (alldb)
     146                 :     {
     147               2 :         if (dbname)
     148 UBC           0 :             pg_fatal("cannot cluster all databases and a specific one at the same time");
     149                 : 
     150 CBC           2 :         if (tables.head != NULL)
     151 UBC           0 :             pg_fatal("cannot cluster specific table(s) in all databases");
     152                 : 
     153 CBC           2 :         cparams.dbname = maintenance_db;
     154                 : 
     155               2 :         cluster_all_databases(&cparams, progname, verbose, echo, quiet);
     156                 :     }
     157                 :     else
     158                 :     {
     159               4 :         if (dbname == NULL)
     160                 :         {
     161               3 :             if (getenv("PGDATABASE"))
     162               3 :                 dbname = getenv("PGDATABASE");
     163 UBC           0 :             else if (getenv("PGUSER"))
     164               0 :                 dbname = getenv("PGUSER");
     165                 :             else
     166               0 :                 dbname = get_user_name_or_exit(progname);
     167                 :         }
     168                 : 
     169 CBC           4 :         cparams.dbname = dbname;
     170                 : 
     171               4 :         if (tables.head != NULL)
     172                 :         {
     173                 :             SimpleStringListCell *cell;
     174                 : 
     175               3 :             for (cell = tables.head; cell; cell = cell->next)
     176                 :             {
     177               2 :                 cluster_one_database(&cparams, cell->val,
     178                 :                                      progname, verbose, echo);
     179                 :             }
     180                 :         }
     181                 :         else
     182               2 :             cluster_one_database(&cparams, NULL,
     183                 :                                  progname, verbose, echo);
     184                 :     }
     185                 : 
     186               5 :     exit(0);
     187                 : }
     188                 : 
     189                 : 
     190                 : static void
     191              13 : cluster_one_database(const ConnParams *cparams, const char *table,
     192                 :                      const char *progname, bool verbose, bool echo)
     193                 : {
     194                 :     PQExpBufferData sql;
     195                 : 
     196                 :     PGconn     *conn;
     197                 : 
     198              13 :     conn = connectDatabase(cparams, progname, echo, false, false);
     199                 : 
     200              13 :     initPQExpBuffer(&sql);
     201                 : 
     202              13 :     appendPQExpBufferStr(&sql, "CLUSTER");
     203              13 :     if (verbose)
     204               8 :         appendPQExpBufferStr(&sql, " VERBOSE");
     205              13 :     if (table)
     206                 :     {
     207               2 :         appendPQExpBufferChar(&sql, ' ');
     208               2 :         appendQualifiedRelation(&sql, table, conn, echo);
     209                 :     }
     210              12 :     appendPQExpBufferChar(&sql, ';');
     211                 : 
     212              12 :     if (!executeMaintenanceCommand(conn, sql.data, echo))
     213                 :     {
     214 UBC           0 :         if (table)
     215               0 :             pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s",
     216                 :                          table, PQdb(conn), PQerrorMessage(conn));
     217                 :         else
     218               0 :             pg_log_error("clustering of database \"%s\" failed: %s",
     219                 :                          PQdb(conn), PQerrorMessage(conn));
     220               0 :         PQfinish(conn);
     221               0 :         exit(1);
     222                 :     }
     223 CBC          12 :     PQfinish(conn);
     224              12 :     termPQExpBuffer(&sql);
     225              12 : }
     226                 : 
     227                 : 
     228                 : static void
     229               2 : cluster_all_databases(ConnParams *cparams, const char *progname,
     230                 :                       bool verbose, bool echo, bool quiet)
     231                 : {
     232                 :     PGconn     *conn;
     233                 :     PGresult   *result;
     234                 :     int         i;
     235                 : 
     236               2 :     conn = connectMaintenanceDatabase(cparams, progname, echo);
     237               2 :     result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
     238               2 :     PQfinish(conn);
     239                 : 
     240              11 :     for (i = 0; i < PQntuples(result); i++)
     241                 :     {
     242               9 :         char       *dbname = PQgetvalue(result, i, 0);
     243                 : 
     244               9 :         if (!quiet)
     245                 :         {
     246               9 :             printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
     247               9 :             fflush(stdout);
     248                 :         }
     249                 : 
     250               9 :         cparams->override_dbname = dbname;
     251                 : 
     252               9 :         cluster_one_database(cparams, NULL, progname, verbose, echo);
     253                 :     }
     254                 : 
     255               2 :     PQclear(result);
     256               2 : }
     257                 : 
     258                 : 
     259                 : static void
     260               1 : help(const char *progname)
     261                 : {
     262               1 :     printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
     263               1 :     printf(_("Usage:\n"));
     264               1 :     printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
     265               1 :     printf(_("\nOptions:\n"));
     266               1 :     printf(_("  -a, --all                 cluster all databases\n"));
     267               1 :     printf(_("  -d, --dbname=DBNAME       database to cluster\n"));
     268               1 :     printf(_("  -e, --echo                show the commands being sent to the server\n"));
     269               1 :     printf(_("  -q, --quiet               don't write any messages\n"));
     270               1 :     printf(_("  -t, --table=TABLE         cluster specific table(s) only\n"));
     271               1 :     printf(_("  -v, --verbose             write a lot of output\n"));
     272               1 :     printf(_("  -V, --version             output version information, then exit\n"));
     273               1 :     printf(_("  -?, --help                show this help, then exit\n"));
     274               1 :     printf(_("\nConnection options:\n"));
     275               1 :     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
     276               1 :     printf(_("  -p, --port=PORT           database server port\n"));
     277               1 :     printf(_("  -U, --username=USERNAME   user name to connect as\n"));
     278               1 :     printf(_("  -w, --no-password         never prompt for password\n"));
     279               1 :     printf(_("  -W, --password            force password prompt\n"));
     280               1 :     printf(_("  --maintenance-db=DBNAME   alternate maintenance database\n"));
     281               1 :     printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
     282               1 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     283               1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     284               1 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a