pgbench: Simplify some port, host, user and dbname assignments
authorMichael Paquier
Sat, 6 Mar 2021 12:26:34 +0000 (21:26 +0900)
committerMichael Paquier
Sat, 6 Mar 2021 12:26:34 +0000 (21:26 +0900)
Using pgbench in an environment with both PGPORT and PGUSER set would
have caused the generation of a debug log with an incorrect database
name due to an oversight in 412893b.  Not specifying user, port and/or
database using the option switches, without their respective environment
variables, generated a log entry with empty strings, which was
rather useless.

This commit fixes this set of issues by simplifying the logic grabbing
the connection information, removing a set of getenv() calls that
emulated what libpq already does.  The faulty debug log now directly
uses the information from the libpq connection, and it gets generated
after the connection to the backend is completed, not before it (in the
event of a failure libpq would complain with more information about the
connection attempt so the log is not really useful before anyway).

Author: Kota Miyake
Reviewed-by: Fujii Masao, Michael Paquier
Discussion: https://postgr.es/m/026b3ae6fc339a18394d053c32a4463d@oss.nttdata.com

src/bin/pgbench/pgbench.c

index 31a4df45f579c5af15f3eb0d3dfcaf762370a805..f1d98be2d2debd9be98f74bf19b3ecd7bc112a05 100644 (file)
@@ -60,6 +60,7 @@
 #include "common/int.h"
 #include "common/logging.h"
 #include "common/string.h"
+#include "common/username.h"
 #include "fe_utils/cancel.h"
 #include "fe_utils/conditional.h"
 #include "getopt_long.h"
@@ -240,10 +241,10 @@ bool      is_connect;         /* establish connection for each transaction */
 bool       report_per_command; /* report per-command latencies */
 int            main_pid;           /* main process id used in log filename */
 
-char      *pghost = "";
-char      *pgport = "";
-char      *login = NULL;
-char      *dbName;
+const char *pghost = NULL;
+const char *pgport = NULL;
+const char *username = NULL;
+const char *dbName = NULL;
 char      *logfile_prefix = NULL;
 const char *progname;
 
@@ -1191,7 +1192,7 @@ doConnect(void)
        keywords[1] = "port";
        values[1] = pgport;
        keywords[2] = "user";
-       values[2] = login;
+       values[2] = username;
        keywords[3] = "password";
        values[3] = password;
        keywords[4] = "dbname";
@@ -5483,13 +5484,6 @@ main(int argc, char **argv)
        }
    }
 
-   if ((env = getenv("PGHOST")) != NULL && *env != '\0')
-       pghost = env;
-   if ((env = getenv("PGPORT")) != NULL && *env != '\0')
-       pgport = env;
-   else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
-       login = env;
-
    state = (CState *) pg_malloc0(sizeof(CState));
 
    /* set random seed early, because it may be used while parsing scripts. */
@@ -5610,7 +5604,7 @@ main(int argc, char **argv)
                }
                break;
            case 'U':
-               login = pg_strdup(optarg);
+               username = pg_strdup(optarg);
                break;
            case 'l':
                benchmarking_option_set = true;
@@ -5860,10 +5854,10 @@ main(int argc, char **argv)
    {
        if ((env = getenv("PGDATABASE")) != NULL && *env != '\0')
            dbName = env;
-       else if (login != NULL && *login != '\0')
-           dbName = login;
+       else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
+           dbName = env;
        else
-           dbName = "";
+           dbName = get_user_name_or_exit(progname);
    }
 
    if (optind < argc)
@@ -6026,16 +6020,16 @@ main(int argc, char **argv)
        initRandomState(&state[i].cs_func_rs);
    }
 
-   pg_log_debug("pghost: %s pgport: %s nclients: %d %s: %d dbName: %s",
-                pghost, pgport, nclients,
-                duration <= 0 ? "nxacts" : "duration",
-                duration <= 0 ? nxacts : duration, dbName);
-
    /* opening connection... */
    con = doConnect();
    if (con == NULL)
        exit(1);
 
+   pg_log_debug("pghost: %s pgport: %s nclients: %d %s: %d dbName: %s",
+                PQhost(con), PQport(con), nclients,
+                duration <= 0 ? "nxacts" : "duration",
+                duration <= 0 ? nxacts : duration, PQdb(con));
+
    if (internal_script_used)
        GetTableInfo(con, scale_given);