Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f1516ad

Browse files
committed
pgbench: Simplify some port, host, user and dbname assignments
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
1 parent 0ce4cd0 commit f1516ad

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/bin/pgbench/pgbench.c

+15-21
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "common/int.h"
6161
#include "common/logging.h"
6262
#include "common/string.h"
63+
#include "common/username.h"
6364
#include "fe_utils/cancel.h"
6465
#include "fe_utils/conditional.h"
6566
#include "getopt_long.h"
@@ -240,10 +241,10 @@ bool is_connect; /* establish connection for each transaction */
240241
bool report_per_command; /* report per-command latencies */
241242
int main_pid; /* main process id used in log filename */
242243

243-
char *pghost = "";
244-
char *pgport = "";
245-
char *login = NULL;
246-
char *dbName;
244+
const char *pghost = NULL;
245+
const char *pgport = NULL;
246+
const char *username = NULL;
247+
const char *dbName = NULL;
247248
char *logfile_prefix = NULL;
248249
const char *progname;
249250

@@ -1191,7 +1192,7 @@ doConnect(void)
11911192
keywords[1] = "port";
11921193
values[1] = pgport;
11931194
keywords[2] = "user";
1194-
values[2] = login;
1195+
values[2] = username;
11951196
keywords[3] = "password";
11961197
values[3] = password;
11971198
keywords[4] = "dbname";
@@ -5483,13 +5484,6 @@ main(int argc, char **argv)
54835484
}
54845485
}
54855486

5486-
if ((env = getenv("PGHOST")) != NULL && *env != '\0')
5487-
pghost = env;
5488-
if ((env = getenv("PGPORT")) != NULL && *env != '\0')
5489-
pgport = env;
5490-
else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
5491-
login = env;
5492-
54935487
state = (CState *) pg_malloc0(sizeof(CState));
54945488

54955489
/* set random seed early, because it may be used while parsing scripts. */
@@ -5610,7 +5604,7 @@ main(int argc, char **argv)
56105604
}
56115605
break;
56125606
case 'U':
5613-
login = pg_strdup(optarg);
5607+
username = pg_strdup(optarg);
56145608
break;
56155609
case 'l':
56165610
benchmarking_option_set = true;
@@ -5860,10 +5854,10 @@ main(int argc, char **argv)
58605854
{
58615855
if ((env = getenv("PGDATABASE")) != NULL && *env != '\0')
58625856
dbName = env;
5863-
else if (login != NULL && *login != '\0')
5864-
dbName = login;
5857+
else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
5858+
dbName = env;
58655859
else
5866-
dbName = "";
5860+
dbName = get_user_name_or_exit(progname);
58675861
}
58685862

58695863
if (optind < argc)
@@ -6026,16 +6020,16 @@ main(int argc, char **argv)
60266020
initRandomState(&state[i].cs_func_rs);
60276021
}
60286022

6029-
pg_log_debug("pghost: %s pgport: %s nclients: %d %s: %d dbName: %s",
6030-
pghost, pgport, nclients,
6031-
duration <= 0 ? "nxacts" : "duration",
6032-
duration <= 0 ? nxacts : duration, dbName);
6033-
60346023
/* opening connection... */
60356024
con = doConnect();
60366025
if (con == NULL)
60376026
exit(1);
60386027

6028+
pg_log_debug("pghost: %s pgport: %s nclients: %d %s: %d dbName: %s",
6029+
PQhost(con), PQport(con), nclients,
6030+
duration <= 0 ? "nxacts" : "duration",
6031+
duration <= 0 ? nxacts : duration, PQdb(con));
6032+
60396033
if (internal_script_used)
60406034
GetTableInfo(con, scale_given);
60416035

0 commit comments

Comments
 (0)