Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ayR6f-0004Fb-Nq for pgsql-hackers@arkaria.postgresql.org; Thu, 05 May 2016 21:46:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ayR6f-0001jl-Ad for pgsql-hackers@arkaria.postgresql.org; Thu, 05 May 2016 21:46:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ayR5I-0000FA-GS for pgsql-hackers@postgreSQL.org; Thu, 05 May 2016 21:45:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ayR50-00089X-MW for pgsql-hackers@postgreSQL.org; Thu, 05 May 2016 21:45:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u45Lj3rA015600; Thu, 5 May 2016 17:45:03 -0400 From: Tom Lane To: Fabien COELHO cc: PostgreSQL Developers Subject: Re: Poorly-thought-out handling of double variables in pgbench In-reply-to: References: <9188.1462475559@sss.pgh.pa.us> Comments: In-reply-to Fabien COELHO message dated "Thu, 05 May 2016 22:33:35 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <15587.1462484680.0@sss.pgh.pa.us> Date: Thu, 05 May 2016 17:45:02 -0400 Message-ID: <15599.1462484702@sss.pgh.pa.us> X-Pg-Spam-Score: -4.0 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15587.1462484680.1@sss.pgh.pa.us> Fabien COELHO writes: >> A better answer, perhaps, would be to store double-valued variables in >> double format to begin with, coercing to text only when and if the value >> is interpolated into a string. > Yep, but that was yet more changes for a limited benefit and would have > increase the probability that the patch would have been rejected. >> That's probably a bigger change than we want to be putting in right now, >> though I'm a bit tempted to go try it. > I definitely agree that the text variable solution is pretty ugly, but it > was the minimum change solution, and I do not have much time available. Well, I felt like doing some minor hacking, so I went and adjusted the code to work this way. I'm pretty happy with the result, what do you think? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="pgbench-real-numeric-variables.patch"; charset="us-ascii" Content-ID: <15587.1462484680.2@sss.pgh.pa.us> Content-Description: pgbench-real-numeric-variables.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 2a9063a..a484165 100644 *** a/src/bin/pgbench/pgbench.c --- b/src/bin/pgbench/pgbench.c *************** *** 38,43 **** --- 38,44 ---- #include "portability/instr_time.h" =20=20 #include + #include #include #include #include *************** const char *progname; *** 185,195 **** =20=20 volatile bool timer_exceeded =3D false; /* flag from signal handler */ =20=20 ! /* variable definitions */ typedef struct { ! char *name; /* variable name */ ! char *value; /* its value */ } Variable; =20=20 #define MAX_SCRIPTS 128 /* max number of SQL scripts allowed */ --- 186,205 ---- =20=20 volatile bool timer_exceeded =3D false; /* flag from signal handler */ =20=20 ! /* ! * Variable definitions. If a variable has a string value, "value" is th= at ! * value, is_numeric is false, and num_value is undefined. If the value = is ! * known to be numeric, is_numeric is true and num_value contains the val= ue ! * (in any permitted numeric variant). In this case "value" contains the ! * string equivalent of the number, if we've had occasion to compute that, ! * or NULL if we haven't. ! */ typedef struct { ! char *name; /* variable's name */ ! char *value; /* its value in string form, if known */ ! bool is_numeric; /* is numeric value known? */ ! PgBenchValue num_value; /* variable's value in numeric form */ } Variable; =20=20 #define MAX_SCRIPTS 128 /* max number of SQL scripts allowed */ *************** typedef struct *** 237,243 **** bool throttling; /* whether nap is for throttling */ bool is_throttled; /* whether transaction throttling is done */ Variable *variables; /* array of variable definitions */ ! int nvariables; int64 txn_scheduled; /* scheduled start time of transaction (usec) */ instr_time txn_begin; /* used for measuring schedule lag times */ instr_time stmt_begin; /* used for measuring statement latencies */ --- 247,254 ---- bool throttling; /* whether nap is for throttling */ bool is_throttled; /* whether transaction throttling is done */ Variable *variables; /* array of variable definitions */ ! int nvariables; /* number of variables */ ! bool vars_sorted; /* are variables sorted by name? */ int64 txn_scheduled; /* scheduled start time of transaction (usec) */ instr_time txn_begin; /* used for measuring schedule lag times */ instr_time stmt_begin; /* used for measuring statement latencies */ *************** static const BuiltinScript builtin_scrip *** 363,368 **** --- 374,381 ---- =20=20 =20=20 /* Function prototypes */ + static void setIntValue(PgBenchValue *pv, int64 ival); + static void setDoubleValue(PgBenchValue *pv, double dval); static bool evaluateExpr(TState *, CState *, PgBenchExpr *, PgBenchValue = *); static void doLog(TState *thread, CState *st, instr_time *now, StatsData *agg, bool skipped, double latency, double lag); *************** discard_response(CState *state) *** 836,868 **** } while (res); } =20=20 static int ! compareVariables(const void *v1, const void *v2) { return strcmp(((const Variable *) v1)->name, ((const Variable *) v2)->name); } =20=20 ! static char * ! getVariable(CState *st, char *name) { ! Variable key, ! *var; =20=20 /* On some versions of Solaris, bsearch of zero items dumps core */ if (st->nvariables <=3D 0) return NULL; =20=20 key.name =3D name; ! var =3D (Variable *) bsearch((void *) &key, ! (void *) st->variables, ! st->nvariables, ! sizeof(Variable), ! compareVariables); ! if (var !=3D NULL) ! return var->value; else ! return NULL; } =20=20 /* check whether the name consists of alphabets, numerals and underscores= . */ --- 849,945 ---- } while (res); } =20=20 + /* qsort comparator for Variable array */ static int ! compareVariableNames(const void *v1, const void *v2) { return strcmp(((const Variable *) v1)->name, ((const Variable *) v2)->name); } =20=20 ! /* Locate a variable by name; returns NULL if unknown */ ! static Variable * ! lookupVariable(CState *st, char *name) { ! Variable key; =20=20 /* On some versions of Solaris, bsearch of zero items dumps core */ if (st->nvariables <=3D 0) return NULL; =20=20 + /* Sort if we have to */ + if (!st->vars_sorted) + { + qsort((void *) st->variables, st->nvariables, sizeof(Variable), + compareVariableNames); + st->vars_sorted =3D true; + } +=20 + /* Now we can search */ key.name =3D name; ! return (Variable *) bsearch((void *) &key, ! (void *) st->variables, ! st->nvariables, ! sizeof(Variable), ! compareVariableNames); ! } !=20 ! /* Get the value of a variable, in string form; returns NULL if unknown */ ! static char * ! getVariable(CState *st, char *name) ! { ! Variable *var; ! char stringform[64]; !=20 ! var =3D lookupVariable(st, name); ! if (var =3D=3D NULL) ! return NULL; /* not found */ !=20 ! if (var->value) ! return var->value; /* we have it in string form */ !=20 ! /* We need to produce a string equivalent of the numeric value */ ! Assert(var->is_numeric); ! if (var->num_value.type =3D=3D PGBT_INT) ! snprintf(stringform, sizeof(stringform), ! INT64_FORMAT, var->num_value.u.ival); else ! { ! Assert(var->num_value.type =3D=3D PGBT_DOUBLE); ! snprintf(stringform, sizeof(stringform), ! "%.*g", DBL_DIG, var->num_value.u.dval); ! } ! var->value =3D pg_strdup(stringform); ! return var->value; ! } !=20 ! /* Try to convert variable to numeric form; return false on failure */ ! static bool ! makeVariableNumeric(Variable *var) ! { ! if (var->is_numeric) ! return true; /* no work */ !=20 ! if (is_an_int(var->value)) ! { ! setIntValue(&var->num_value, strtoint64(var->value)); ! var->is_numeric =3D true; ! } ! else /* type should be double */ ! { ! double dv; !=20 ! if (sscanf(var->value, "%lf", &dv) !=3D 1) ! { ! fprintf(stderr, ! "malformed variable \"%s\" value: \"%s\"\n", ! var->name, var->value); ! return false; ! } ! setDoubleValue(&var->num_value, dv); ! var->is_numeric =3D true; ! } ! return true; } =20=20 /* check whether the name consists of alphabets, numerals and underscores= . */ *************** isLegalVariableName(const char *name) *** 877,902 **** return false; } =20=20 ! return true; } =20=20 ! static int ! putVariable(CState *st, const char *context, char *name, char *value) { ! Variable key, ! *var; !=20 ! key.name =3D name; ! /* On some versions of Solaris, bsearch of zero items dumps core */ ! if (st->nvariables > 0) ! var =3D (Variable *) bsearch((void *) &key, ! (void *) st->variables, ! st->nvariables, ! sizeof(Variable), ! compareVariables); ! else ! var =3D NULL; =20=20 if (var =3D=3D NULL) { Variable *newvars; --- 954,973 ---- return false; } =20=20 ! return (i > 0); /* must be non-empty */ } =20=20 ! /* ! * Lookup a variable by name, creating it if need be. ! * Caller is expected to assign a value to the variable. ! * Returns NULL on failure (bad name). ! */ ! static Variable * ! lookupCreateVariable(CState *st, const char *context, char *name) { ! Variable *var; =20=20 + var =3D lookupVariable(st, name); if (var =3D=3D NULL) { Variable *newvars; *************** putVariable(CState *st, const char *cont *** 909,917 **** { fprintf(stderr, "%s: invalid variable name: \"%s\"\n", context, name); ! return false; } =20=20 if (st->variables) newvars =3D (Variable *) pg_realloc(st->variables, (st->nvariables + 1) * sizeof(Variable)); --- 980,989 ---- { fprintf(stderr, "%s: invalid variable name: \"%s\"\n", context, name); ! return NULL; } =20=20 + /* Create variable at the end of the array */ if (st->variables) newvars =3D (Variable *) pg_realloc(st->variables, (st->nvariables + 1) * sizeof(Variable)); *************** putVariable(CState *st, const char *cont *** 923,949 **** var =3D &newvars[st->nvariables]; =20=20 var->name =3D pg_strdup(name); ! var->value =3D pg_strdup(value); =20=20 st->nvariables++; !=20 ! qsort((void *) st->variables, st->nvariables, sizeof(Variable), ! compareVariables); } - else - { - char *val; =20=20 ! /* dup then free, in case value is pointing at this variable */ ! val =3D pg_strdup(value); =20=20 free(var->value); ! var->value =3D val; ! } =20=20 return true; } =20=20 static char * parseVariable(const char *sql, int *eaten) { --- 995,1066 ---- var =3D &newvars[st->nvariables]; =20=20 var->name =3D pg_strdup(name); ! var->value =3D NULL; ! /* caller is expected to initialize remaining fields */ =20=20 st->nvariables++; ! /* we don't re-sort the array till we have to */ ! st->vars_sorted =3D false; } =20=20 ! return var; ! } =20=20 + /* Assign a string value to a variable, creating it if need be */ + /* Returns false on failure (bad name) */ + static bool + putVariable(CState *st, const char *context, char *name, const char *valu= e) + { + Variable *var; + char *val; +=20 + var =3D lookupCreateVariable(st, context, name); + if (!var) + return false; +=20 + /* dup then free, in case value is pointing at this variable */ + val =3D pg_strdup(value); +=20 + if (var->value) free(var->value); ! var->value =3D val; ! var->is_numeric =3D false; !=20 ! return true; ! } !=20 ! /* Assign a numeric value to a variable, creating it if need be */ ! /* Returns false on failure (bad name) */ ! static bool ! putVariableNumber(CState *st, const char *context, char *name, ! const PgBenchValue *value) ! { ! Variable *var; !=20 ! var =3D lookupCreateVariable(st, context, name); ! if (!var) ! return false; !=20 ! if (var->value) ! free(var->value); ! var->value =3D NULL; ! var->is_numeric =3D true; ! var->num_value =3D *value; =20=20 return true; } =20=20 + /* Assign an integer value to a variable, creating it if need be */ + /* Returns false on failure (bad name) */ + static bool + putVariableInt(CState *st, const char *context, char *name, int64 value) + { + PgBenchValue val; +=20 + setIntValue(&val, value); + return putVariableNumber(st, context, name, &val); + } +=20 static char * parseVariable(const char *sql, int *eaten) { *************** evalFunc(TState *thread, CState *st, *** 1260,1266 **** else { Assert(varg->type =3D=3D PGBT_DOUBLE); ! fprintf(stderr, "double %f\n", varg->u.dval); } =20=20 *retval =3D *varg; --- 1377,1383 ---- else { Assert(varg->type =3D=3D PGBT_DOUBLE); ! fprintf(stderr, "double %.*g\n", DBL_DIG, varg->u.dval); } =20=20 *retval =3D *varg; *************** evaluateExpr(TState *thread, CState *st, *** 1454,1485 **** =20=20 case ENODE_VARIABLE: { ! char *var; =20=20 ! if ((var =3D getVariable(st, expr->u.variable.varname)) =3D=3D NULL) { fprintf(stderr, "undefined variable \"%s\"\n", expr->u.variable.varname); return false; } =20=20 ! if (is_an_int(var)) ! { ! setIntValue(retval, strtoint64(var)); ! } ! else /* type should be double */ ! { ! double dv; ! if (sscanf(var, "%lf", &dv) !=3D 1) ! { ! fprintf(stderr, ! "malformed variable \"%s\" value: \"%s\"\n", ! expr->u.variable.varname, var); ! return false; ! } ! setDoubleValue(retval, dv); ! } =20=20 return true; } =20=20 --- 1571,1589 ---- =20=20 case ENODE_VARIABLE: { ! Variable *var; =20=20 ! if ((var =3D lookupVariable(st, expr->u.variable.varname)) =3D=3D NUL= L) { fprintf(stderr, "undefined variable \"%s\"\n", expr->u.variable.varname); return false; } =20=20 ! if (!makeVariableNumeric(var)) ! return false; =20=20 + *retval =3D var->num_value; return true; } =20=20 *************** runShellCommand(CState *st, char *variab *** 1596,1603 **** argv[0], res); return false; } ! snprintf(res, sizeof(res), "%d", retval); ! if (!putVariable(st, "setshell", variable, res)) return false; =20=20 #ifdef DEBUG --- 1700,1706 ---- argv[0], res); return false; } ! if (!putVariableInt(st, "setshell", variable, retval)) return false; =20=20 #ifdef DEBUG *************** top: *** 1973,1979 **** =20=20 if (pg_strcasecmp(argv[0], "set") =3D=3D 0) { - char res[64]; PgBenchExpr *expr =3D commands[st->state]->expr; PgBenchValue result; =20=20 --- 2076,2081 ---- *************** top: *** 1983,1997 **** return true; } =20=20 ! if (result.type =3D=3D PGBT_INT) ! sprintf(res, INT64_FORMAT, result.u.ival); ! else ! { ! Assert(result.type =3D=3D PGBT_DOUBLE); ! sprintf(res, "%.18e", result.u.dval); ! } !=20 ! if (!putVariable(st, argv[0], argv[1], res)) { st->ecnt++; return true; --- 2085,2091 ---- return true; } =20=20 ! if (!putVariableNumber(st, argv[0], argv[1], &result)) { st->ecnt++; return true; *************** main(int argc, char **argv) *** 3325,3332 **** PGresult *res; char *env; =20=20 - char val[64]; -=20 progname =3D get_progname(argv[0]); =20=20 if (argc > 1) --- 3419,3424 ---- *************** main(int argc, char **argv) *** 3767,3774 **** state[i].id =3D i; for (j =3D 0; j < state[0].nvariables; j++) { ! if (!putVariable(&state[i], "startup", state[0].variables[j].name, st= ate[0].variables[j].value)) ! exit(1); } } } --- 3859,3878 ---- state[i].id =3D i; for (j =3D 0; j < state[0].nvariables; j++) { ! Variable *var =3D &state[0].variables[j]; !=20 ! if (var->is_numeric) ! { ! if (!putVariableNumber(&state[i], "startup", ! var->name, &var->num_value)) ! exit(1); ! } ! else ! { ! if (!putVariable(&state[i], "startup", ! var->name, var->value)) ! exit(1); ! } } } } *************** main(int argc, char **argv) *** 3834,3845 **** * :scale variables normally get -s or database scale, but don't override * an explicit -D switch */ ! if (getVariable(&state[0], "scale") =3D=3D NULL) { - snprintf(val, sizeof(val), "%d", scale); for (i =3D 0; i < nclients; i++) { ! if (!putVariable(&state[i], "startup", "scale", val)) exit(1); } } --- 3938,3948 ---- * :scale variables normally get -s or database scale, but don't override * an explicit -D switch */ ! if (lookupVariable(&state[0], "scale") =3D=3D NULL) { for (i =3D 0; i < nclients; i++) { ! if (!putVariableInt(&state[i], "startup", "scale", scale)) exit(1); } } *************** main(int argc, char **argv) *** 3848,3859 **** * Define a :client_id variable that is unique per connection. But don't * override an explicit -D switch. */ ! if (getVariable(&state[0], "client_id") =3D=3D NULL) { for (i =3D 0; i < nclients; i++) { ! snprintf(val, sizeof(val), "%d", i); ! if (!putVariable(&state[i], "startup", "client_id", val)) exit(1); } } --- 3951,3961 ---- * Define a :client_id variable that is unique per connection. But don't * override an explicit -D switch. */ ! if (lookupVariable(&state[0], "client_id") =3D=3D NULL) { for (i =3D 0; i < nclients; i++) { ! if (!putVariableInt(&state[i], "startup", "client_id", i)) exit(1); } } ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--