PostgreSQL Source Code  git master
pgbench.c
Go to the documentation of this file.
1 /*
2  * pgbench.c
3  *
4  * A simple benchmark program for PostgreSQL
5  * Originally written by Tatsuo Ishii and enhanced by many contributors.
6  *
7  * src/bin/pgbench/pgbench.c
8  * Copyright (c) 2000-2019, PostgreSQL Global Development Group
9  * ALL RIGHTS RESERVED;
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose, without fee, and without a written agreement
13  * is hereby granted, provided that the above copyright notice and this
14  * paragraph and the following two paragraphs appear in all copies.
15  *
16  * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
17  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
18  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
19  * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
20  * POSSIBILITY OF SUCH DAMAGE.
21  *
22  * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
25  * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
26  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27  *
28  */
29 
30 #ifdef WIN32
31 #define FD_SETSIZE 1024 /* must set before winsock2.h is included */
32 #endif
33 
34 #include "postgres_fe.h"
35 
36 #include <ctype.h>
37 #include <float.h>
38 #include <limits.h>
39 #include <math.h>
40 #include <signal.h>
41 #include <time.h>
42 #include <sys/time.h>
43 #ifdef HAVE_SYS_RESOURCE_H
44 #include <sys/resource.h> /* for getrlimit */
45 #endif
46 
47 /* For testing, PGBENCH_USE_SELECT can be defined to force use of that code */
48 #if defined(HAVE_PPOLL) && !defined(PGBENCH_USE_SELECT)
49 #define POLL_USING_PPOLL
50 #ifdef HAVE_POLL_H
51 #include <poll.h>
52 #endif
53 #else /* no ppoll(), so use select() */
54 #define POLL_USING_SELECT
55 #ifdef HAVE_SYS_SELECT_H
56 #include <sys/select.h>
57 #endif
58 #endif
59 
60 #include "common/int.h"
61 #include "common/logging.h"
62 #include "fe_utils/conditional.h"
63 #include "getopt_long.h"
64 #include "libpq-fe.h"
65 #include "pgbench.h"
66 #include "portability/instr_time.h"
67 
68 #ifndef M_PI
69 #define M_PI 3.14159265358979323846
70 #endif
71 
72 
73 #define ERRCODE_UNDEFINED_TABLE "42P01"
74 
75 /*
76  * Hashing constants
77  */
78 #define FNV_PRIME UINT64CONST(0x100000001b3)
79 #define FNV_OFFSET_BASIS UINT64CONST(0xcbf29ce484222325)
80 #define MM2_MUL UINT64CONST(0xc6a4a7935bd1e995)
81 #define MM2_MUL_TIMES_8 UINT64CONST(0x35253c9ade8f4ca8)
82 #define MM2_ROT 47
83 
84 /*
85  * Multi-platform socket set implementations
86  */
87 
88 #ifdef POLL_USING_PPOLL
89 #define SOCKET_WAIT_METHOD "ppoll"
90 
91 typedef struct socket_set
92 {
93  int maxfds; /* allocated length of pollfds[] array */
94  int curfds; /* number currently in use */
95  struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER];
96 } socket_set;
97 
98 #endif /* POLL_USING_PPOLL */
99 
100 #ifdef POLL_USING_SELECT
101 #define SOCKET_WAIT_METHOD "select"
102 
103 typedef struct socket_set
104 {
105  int maxfd; /* largest FD currently set in fds */
106  fd_set fds;
107 } socket_set;
108 
109 #endif /* POLL_USING_SELECT */
110 
111 /*
112  * Multi-platform pthread implementations
113  */
114 
115 #ifdef WIN32
116 /* Use native win32 threads on Windows */
117 typedef struct win32_pthread *pthread_t;
118 typedef int pthread_attr_t;
119 
120 static int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
121 static int pthread_join(pthread_t th, void **thread_return);
122 #elif defined(ENABLE_THREAD_SAFETY)
123 /* Use platform-dependent pthread capability */
124 #include <pthread.h>
125 #else
126 /* No threads implementation, use none (-j 1) */
127 #define pthread_t void *
128 #endif
129 
130 
131 /********************************************************************
132  * some configurable parameters */
133 
134 #define DEFAULT_INIT_STEPS "dtgvp" /* default -I setting */
135 #define ALL_INIT_STEPS "dtgGvpf" /* all possible steps */
136 
137 #define LOG_STEP_SECONDS 5 /* seconds between log messages */
138 #define DEFAULT_NXACTS 10 /* default nxacts */
139 
140 #define MIN_GAUSSIAN_PARAM 2.0 /* minimum parameter for gauss */
141 
142 #define MIN_ZIPFIAN_PARAM 1.001 /* minimum parameter for zipfian */
143 #define MAX_ZIPFIAN_PARAM 1000.0 /* maximum parameter for zipfian */
144 
145 int nxacts = 0; /* number of transactions per client */
146 int duration = 0; /* duration in seconds */
147 int64 end_time = 0; /* when to stop in micro seconds, under -T */
148 
149 /*
150  * scaling factor. for example, scale = 10 will make 1000000 tuples in
151  * pgbench_accounts table.
152  */
153 int scale = 1;
154 
155 /*
156  * fillfactor. for example, fillfactor = 90 will use only 90 percent
157  * space during inserts and leave 10 percent free.
158  */
159 int fillfactor = 100;
160 
161 /*
162  * use unlogged tables?
163  */
164 bool unlogged_tables = false;
165 
166 /*
167  * log sampling rate (1.0 = log everything, 0.0 = option not given)
168  */
169 double sample_rate = 0.0;
170 
171 /*
172  * When threads are throttled to a given rate limit, this is the target delay
173  * to reach that rate in usec. 0 is the default and means no throttling.
174  */
175 double throttle_delay = 0;
176 
177 /*
178  * Transactions which take longer than this limit (in usec) are counted as
179  * late, and reported as such, although they are completed anyway. When
180  * throttling is enabled, execution time slots that are more than this late
181  * are skipped altogether, and counted separately.
182  */
183 int64 latency_limit = 0;
184 
185 /*
186  * tablespace selection
187  */
188 char *tablespace = NULL;
189 char *index_tablespace = NULL;
190 
191 /*
192  * Number of "pgbench_accounts" partitions. 0 is the default and means no
193  * partitioning.
194  */
195 static int partitions = 0;
196 
197 /* partitioning strategy for "pgbench_accounts" */
198 typedef enum
199 {
200  PART_NONE, /* no partitioning */
201  PART_RANGE, /* range partitioning */
202  PART_HASH /* hash partitioning */
204 
205 static partition_method_t partition_method = PART_NONE;
206 static const char *PARTITION_METHOD[] = {"none", "range", "hash"};
207 
208 /* random seed used to initialize base_random_sequence */
209 int64 random_seed = -1;
210 
211 /*
212  * end of configurable parameters
213  *********************************************************************/
214 
215 #define nbranches 1 /* Makes little sense to change this. Change
216  * -s instead */
217 #define ntellers 10
218 #define naccounts 100000
219 
220 /*
221  * The scale factor at/beyond which 32bit integers are incapable of storing
222  * 64bit values.
223  *
224  * Although the actual threshold is 21474, we use 20000 because it is easier to
225  * document and remember, and isn't that far away from the real threshold.
226  */
227 #define SCALE_32BIT_THRESHOLD 20000
228 
229 bool use_log; /* log transaction latencies to a file */
230 bool use_quiet; /* quiet logging onto stderr */
231 int agg_interval; /* log aggregates instead of individual
232  * transactions */
233 bool per_script_stats = false; /* whether to collect stats per script */
234 int progress = 0; /* thread progress report every this seconds */
235 bool progress_timestamp = false; /* progress report with Unix time */
236 int nclients = 1; /* number of clients */
237 int nthreads = 1; /* number of threads */
238 bool is_connect; /* establish connection for each transaction */
239 bool report_per_command; /* report per-command latencies */
240 int main_pid; /* main process id used in log filename */
241 
242 char *pghost = "";
243 char *pgport = "";
244 char *login = NULL;
245 char *dbName;
246 char *logfile_prefix = NULL;
247 const char *progname;
248 
249 #define WSEP '@' /* weight separator */
250 
251 volatile bool timer_exceeded = false; /* flag from signal handler */
252 
253 /*
254  * Variable definitions.
255  *
256  * If a variable only has a string value, "svalue" is that value, and value is
257  * "not set". If the value is known, "value" contains the value (in any
258  * variant).
259  *
260  * In this case "svalue" contains the string equivalent of the value, if we've
261  * had occasion to compute that, or NULL if we haven't.
262  */
263 typedef struct
264 {
265  char *name; /* variable's name */
266  char *svalue; /* its value in string form, if known */
267  PgBenchValue value; /* actual variable's value */
268 } Variable;
269 
270 #define MAX_SCRIPTS 128 /* max number of SQL scripts allowed */
271 #define SHELL_COMMAND_SIZE 256 /* maximum size allowed for shell command */
272 
273 /*
274  * Simple data structure to keep stats about something.
275  *
276  * XXX probably the first value should be kept and used as an offset for
277  * better numerical stability...
278  */
279 typedef struct SimpleStats
280 {
281  int64 count; /* how many values were encountered */
282  double min; /* the minimum seen */
283  double max; /* the maximum seen */
284  double sum; /* sum of values */
285  double sum2; /* sum of squared values */
286 } SimpleStats;
287 
288 /*
289  * Data structure to hold various statistics: per-thread and per-script stats
290  * are maintained and merged together.
291  */
292 typedef struct StatsData
293 {
294  time_t start_time; /* interval start time, for aggregates */
295  int64 cnt; /* number of transactions, including skipped */
296  int64 skipped; /* number of transactions skipped under --rate
297  * and --latency-limit */
300 } StatsData;
301 
302 /*
303  * Struct to keep random state.
304  */
305 typedef struct RandomState
306 {
307  unsigned short xseed[3];
308 } RandomState;
309 
310 /* Various random sequences are initialized from this one. */
312 
313 /*
314  * Connection state machine states.
315  */
316 typedef enum
317 {
318  /*
319  * The client must first choose a script to execute. Once chosen, it can
320  * either be throttled (state CSTATE_PREPARE_THROTTLE under --rate), start
321  * right away (state CSTATE_START_TX) or not start at all if the timer was
322  * exceeded (state CSTATE_FINISHED).
323  */
325 
326  /*
327  * CSTATE_START_TX performs start-of-transaction processing. Establishes
328  * a new connection for the transaction in --connect mode, records the
329  * transaction start time, and proceed to the first command.
330  *
331  * Note: once a script is started, it will either error or run till its
332  * end, where it may be interrupted. It is not interrupted while running,
333  * so pgbench --time is to be understood as tx are allowed to start in
334  * that time, and will finish when their work is completed.
335  */
337 
338  /*
339  * In CSTATE_PREPARE_THROTTLE state, we calculate when to begin the next
340  * transaction, and advance to CSTATE_THROTTLE. CSTATE_THROTTLE state
341  * sleeps until that moment, then advances to CSTATE_START_TX, or
342  * CSTATE_FINISHED if the next transaction would start beyond the end of
343  * the run.
344  */
347 
348  /*
349  * We loop through these states, to process each command in the script:
350  *
351  * CSTATE_START_COMMAND starts the execution of a command. On a SQL
352  * command, the command is sent to the server, and we move to
353  * CSTATE_WAIT_RESULT state. On a \sleep meta-command, the timer is set,
354  * and we enter the CSTATE_SLEEP state to wait for it to expire. Other
355  * meta-commands are executed immediately. If the command about to start
356  * is actually beyond the end of the script, advance to CSTATE_END_TX.
357  *
358  * CSTATE_WAIT_RESULT waits until we get a result set back from the server
359  * for the current command.
360  *
361  * CSTATE_SLEEP waits until the end of \sleep.
362  *
363  * CSTATE_END_COMMAND records the end-of-command timestamp, increments the
364  * command counter, and loops back to CSTATE_START_COMMAND state.
365  *
366  * CSTATE_SKIP_COMMAND is used by conditional branches which are not
367  * executed. It quickly skip commands that do not need any evaluation.
368  * This state can move forward several commands, till there is something
369  * to do or the end of the script.
370  */
376 
377  /*
378  * CSTATE_END_TX performs end-of-transaction processing. It calculates
379  * latency, and logs the transaction. In --connect mode, it closes the
380  * current connection.
381  *
382  * Then either starts over in CSTATE_CHOOSE_SCRIPT, or enters
383  * CSTATE_FINISHED if we have no more work to do.
384  */
386 
387  /*
388  * Final states. CSTATE_ABORTED means that the script execution was
389  * aborted because a command failed, CSTATE_FINISHED means success.
390  */
394 
395 /*
396  * Connection state.
397  */
398 typedef struct
399 {
400  PGconn *con; /* connection handle to DB */
401  int id; /* client No. */
402  ConnectionStateEnum state; /* state machine's current state. */
403  ConditionalStack cstack; /* enclosing conditionals state */
404 
405  /*
406  * Separate randomness for each client. This is used for random functions
407  * PGBENCH_RANDOM_* during the execution of the script.
408  */
410 
411  int use_file; /* index in sql_script for this client */
412  int command; /* command number in script */
413 
414  /* client variables */
415  Variable *variables; /* array of variable definitions */
416  int nvariables; /* number of variables */
417  bool vars_sorted; /* are variables sorted by name? */
418 
419  /* various times about current transaction */
420  int64 txn_scheduled; /* scheduled start time of transaction (usec) */
421  int64 sleep_until; /* scheduled start time of next cmd (usec) */
422  instr_time txn_begin; /* used for measuring schedule lag times */
423  instr_time stmt_begin; /* used for measuring statement latencies */
424 
425  bool prepared[MAX_SCRIPTS]; /* whether client prepared the script */
426 
427  /* per client collected stats */
428  int64 cnt; /* client transaction count, for -t */
429  int ecnt; /* error count */
430 } CState;
431 
432 /*
433  * Thread state
434  */
435 typedef struct
436 {
437  int tid; /* thread id */
438  pthread_t thread; /* thread handle */
439  CState *state; /* array of CState */
440  int nstate; /* length of state[] */
441 
442  /*
443  * Separate randomness for each thread. Each thread option uses its own
444  * random state to make all of them independent of each other and
445  * therefore deterministic at the thread level.
446  */
447  RandomState ts_choose_rs; /* random state for selecting a script */
448  RandomState ts_throttle_rs; /* random state for transaction throttling */
449  RandomState ts_sample_rs; /* random state for log sampling */
450 
451  int64 throttle_trigger; /* previous/next throttling (us) */
452  FILE *logfile; /* where to log, or NULL */
453 
454  /* per thread collected stats */
455  instr_time start_time; /* thread start time */
458  int64 latency_late; /* executed but late transactions */
459 } TState;
460 
461 #define INVALID_THREAD ((pthread_t) 0)
462 
463 /*
464  * queries read from files
465  */
466 #define SQL_COMMAND 1
467 #define META_COMMAND 2
468 
469 /*
470  * max number of backslash command arguments or SQL variables,
471  * including the command or SQL statement itself
472  */
473 #define MAX_ARGS 256
474 
475 typedef enum MetaCommand
476 {
477  META_NONE, /* not a known meta-command */
478  META_SET, /* \set */
479  META_SETSHELL, /* \setshell */
480  META_SHELL, /* \shell */
481  META_SLEEP, /* \sleep */
482  META_GSET, /* \gset */
483  META_IF, /* \if */
484  META_ELIF, /* \elif */
485  META_ELSE, /* \else */
486  META_ENDIF /* \endif */
487 } MetaCommand;
488 
489 typedef enum QueryMode
490 {
491  QUERY_SIMPLE, /* simple query */
492  QUERY_EXTENDED, /* extended query */
493  QUERY_PREPARED, /* extended query with prepared statements */
495 } QueryMode;
496 
497 static QueryMode querymode = QUERY_SIMPLE;
498 static const char *QUERYMODE[] = {"simple", "extended", "prepared"};
499 
500 /*
501  * struct Command represents one command in a script.
502  *
503  * lines The raw, possibly multi-line command text. Variable substitution
504  * not applied.
505  * first_line A short, single-line extract of 'lines', for error reporting.
506  * type SQL_COMMAND or META_COMMAND
507  * meta The type of meta-command, or META_NONE if command is SQL
508  * argc Number of arguments of the command, 0 if not yet processed.
509  * argv Command arguments, the first of which is the command or SQL
510  * string itself. For SQL commands, after post-processing
511  * argv[0] is the same as 'lines' with variables substituted.
512  * varprefix SQL commands terminated with \gset have this set
513  * to a non NULL value. If nonempty, it's used to prefix the
514  * variable name that receives the value.
515  * expr Parsed expression, if needed.
516  * stats Time spent in this command.
517  */
518 typedef struct Command
519 {
521  char *first_line;
522  int type;
523  MetaCommand meta;
524  int argc;
525  char *argv[MAX_ARGS];
526  char *varprefix;
529 } Command;
530 
531 typedef struct ParsedScript
532 {
533  const char *desc; /* script descriptor (eg, file name) */
534  int weight; /* selection weight */
535  Command **commands; /* NULL-terminated array of Commands */
536  StatsData stats; /* total time spent in script */
537 } ParsedScript;
538 
539 static ParsedScript sql_script[MAX_SCRIPTS]; /* SQL script files */
540 static int num_scripts; /* number of scripts in sql_script[] */
541 static int64 total_weight = 0;
542 
543 static int debug = 0; /* debug flag */
544 
545 /* Builtin test scripts */
546 typedef struct BuiltinScript
547 {
548  const char *name; /* very short name for -b ... */
549  const char *desc; /* short description */
550  const char *script; /* actual pgbench script */
551 } BuiltinScript;
552 
554 {
555  {
556  "tpcb-like",
557  "<builtin: TPC-B (sort of)>",
558  "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
559  "\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
560  "\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
561  "\\set delta random(-5000, 5000)\n"
562  "BEGIN;\n"
563  "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
564  "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
565  "UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;\n"
566  "UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;\n"
567  "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
568  "END;\n"
569  },
570  {
571  "simple-update",
572  "<builtin: simple update>",
573  "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
574  "\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
575  "\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
576  "\\set delta random(-5000, 5000)\n"
577  "BEGIN;\n"
578  "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
579  "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
580  "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
581  "END;\n"
582  },
583  {
584  "select-only",
585  "<builtin: select only>",
586  "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
587  "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
588  }
589 };
590 
591 
592 /* Function prototypes */
593 static void setNullValue(PgBenchValue *pv);
594 static void setBoolValue(PgBenchValue *pv, bool bval);
595 static void setIntValue(PgBenchValue *pv, int64 ival);
596 static void setDoubleValue(PgBenchValue *pv, double dval);
597 static bool evaluateExpr(CState *st, PgBenchExpr *expr,
598  PgBenchValue *retval);
599 static ConnectionStateEnum executeMetaCommand(CState *st, instr_time *now);
600 static void doLog(TState *thread, CState *st,
601  StatsData *agg, bool skipped, double latency, double lag);
602 static void processXactStats(TState *thread, CState *st, instr_time *now,
603  bool skipped, StatsData *agg);
604 static void append_fillfactor(char *opts, int len);
605 static void addScript(ParsedScript script);
606 static void *threadRun(void *arg);
607 static void finishCon(CState *st);
608 static void setalarm(int seconds);
609 static socket_set *alloc_socket_set(int count);
610 static void free_socket_set(socket_set *sa);
611 static void clear_socket_set(socket_set *sa);
612 static void add_socket_to_set(socket_set *sa, int fd, int idx);
613 static int wait_on_socket_set(socket_set *sa, int64 usecs);
614 static bool socket_has_input(socket_set *sa, int fd, int idx);
615 
616 
617 /* callback functions for our flex lexer */
619  NULL, /* don't need get_variable functionality */
620 };
621 
622 
623 static void
624 usage(void)
625 {
626  printf("%s is a benchmarking tool for PostgreSQL.\n\n"
627  "Usage:\n"
628  " %s [OPTION]... [DBNAME]\n"
629  "\nInitialization options:\n"
630  " -i, --initialize invokes initialization mode\n"
631  " -I, --init-steps=[" ALL_INIT_STEPS "]+ (default \"" DEFAULT_INIT_STEPS "\")\n"
632  " run selected initialization steps\n"
633  " -F, --fillfactor=NUM set fill factor\n"
634  " -n, --no-vacuum do not run VACUUM during initialization\n"
635  " -q, --quiet quiet logging (one message each 5 seconds)\n"
636  " -s, --scale=NUM scaling factor\n"
637  " --foreign-keys create foreign key constraints between tables\n"
638  " --index-tablespace=TABLESPACE\n"
639  " create indexes in the specified tablespace\n"
640  " --partitions=NUM partition pgbench_accounts in NUM parts (default: 0)\n"
641  " --partition-method=(range|hash)\n"
642  " partition pgbench_accounts with this method (default: range)\n"
643  " --tablespace=TABLESPACE create tables in the specified tablespace\n"
644  " --unlogged-tables create tables as unlogged tables\n"
645  "\nOptions to select what to run:\n"
646  " -b, --builtin=NAME[@W] add builtin script NAME weighted at W (default: 1)\n"
647  " (use \"-b list\" to list available scripts)\n"
648  " -f, --file=FILENAME[@W] add script FILENAME weighted at W (default: 1)\n"
649  " -N, --skip-some-updates skip updates of pgbench_tellers and pgbench_branches\n"
650  " (same as \"-b simple-update\")\n"
651  " -S, --select-only perform SELECT-only transactions\n"
652  " (same as \"-b select-only\")\n"
653  "\nBenchmarking options:\n"
654  " -c, --client=NUM number of concurrent database clients (default: 1)\n"
655  " -C, --connect establish new connection for each transaction\n"
656  " -D, --define=VARNAME=VALUE\n"
657  " define variable for use by custom script\n"
658  " -j, --jobs=NUM number of threads (default: 1)\n"
659  " -l, --log write transaction times to log file\n"
660  " -L, --latency-limit=NUM count transactions lasting more than NUM ms as late\n"
661  " -M, --protocol=simple|extended|prepared\n"
662  " protocol for submitting queries (default: simple)\n"
663  " -n, --no-vacuum do not run VACUUM before tests\n"
664  " -P, --progress=NUM show thread progress report every NUM seconds\n"
665  " -r, --report-latencies report average latency per command\n"
666  " -R, --rate=NUM target rate in transactions per second\n"
667  " -s, --scale=NUM report this scale factor in output\n"
668  " -t, --transactions=NUM number of transactions each client runs (default: 10)\n"
669  " -T, --time=NUM duration of benchmark test in seconds\n"
670  " -v, --vacuum-all vacuum all four standard tables before tests\n"
671  " --aggregate-interval=NUM aggregate data over NUM seconds\n"
672  " --log-prefix=PREFIX prefix for transaction time log file\n"
673  " (default: \"pgbench_log\")\n"
674  " --progress-timestamp use Unix epoch timestamps for progress\n"
675  " --random-seed=SEED set random seed (\"time\", \"rand\", integer)\n"
676  " --sampling-rate=NUM fraction of transactions to log (e.g., 0.01 for 1%%)\n"
677  " --show-script=NAME show builtin script code, then exit\n"
678  "\nCommon options:\n"
679  " -d, --debug print debugging output\n"
680  " -h, --host=HOSTNAME database server host or socket directory\n"
681  " -p, --port=PORT database server port number\n"
682  " -U, --username=USERNAME connect as specified database user\n"
683  " -V, --version output version information, then exit\n"
684  " -?, --help show this help, then exit\n"
685  "\n"
686  "Report bugs to <pgsql-bugs@lists.postgresql.org>.\n",
687  progname, progname);
688 }
689 
690 /* return whether str matches "^\s*[-+]?[0-9]+$" */
691 static bool
692 is_an_int(const char *str)
693 {
694  const char *ptr = str;
695 
696  /* skip leading spaces; cast is consistent with strtoint64 */
697  while (*ptr && isspace((unsigned char) *ptr))
698  ptr++;
699 
700  /* skip sign */
701  if (*ptr == '+' || *ptr == '-')
702  ptr++;
703 
704  /* at least one digit */
705  if (*ptr && !isdigit((unsigned char) *ptr))
706  return false;
707 
708  /* eat all digits */
709  while (*ptr && isdigit((unsigned char) *ptr))
710  ptr++;
711 
712  /* must have reached end of string */
713  return *ptr == '\0';
714 }
715 
716 
717 /*
718  * strtoint64 -- convert a string to 64-bit integer
719  *
720  * This function is a slightly modified version of scanint8() from
721  * src/backend/utils/adt/int8.c.
722  *
723  * The function returns whether the conversion worked, and if so
724  * "*result" is set to the result.
725  *
726  * If not errorOK, an error message is also printed out on errors.
727  */
728 bool
729 strtoint64(const char *str, bool errorOK, int64 *result)
730 {
731  const char *ptr = str;
732  int64 tmp = 0;
733  bool neg = false;
734 
735  /*
736  * Do our own scan, rather than relying on sscanf which might be broken
737  * for long long.
738  *
739  * As INT64_MIN can't be stored as a positive 64 bit integer, accumulate
740  * value as a negative number.
741  */
742 
743  /* skip leading spaces */
744  while (*ptr && isspace((unsigned char) *ptr))
745  ptr++;
746 
747  /* handle sign */
748  if (*ptr == '-')
749  {
750  ptr++;
751  neg = true;
752  }
753  else if (*ptr == '+')
754  ptr++;
755 
756  /* require at least one digit */
757  if (unlikely(!isdigit((unsigned char) *ptr)))
758  goto invalid_syntax;
759 
760  /* process digits */
761  while (*ptr && isdigit((unsigned char) *ptr))
762  {
763  int8 digit = (*ptr++ - '0');
764 
765  if (unlikely(pg_mul_s64_overflow(tmp, 10, &tmp)) ||
766  unlikely(pg_sub_s64_overflow(tmp, digit, &tmp)))
767  goto out_of_range;
768  }
769 
770  /* allow trailing whitespace, but not other trailing chars */
771  while (*ptr != '\0' && isspace((unsigned char) *ptr))
772  ptr++;
773 
774  if (unlikely(*ptr != '\0'))
775  goto invalid_syntax;
776 
777  if (!neg)
778  {
779  if (unlikely(tmp == PG_INT64_MIN))
780  goto out_of_range;
781  tmp = -tmp;
782  }
783 
784  *result = tmp;
785  return true;
786 
787 out_of_range:
788  if (!errorOK)
789  fprintf(stderr,
790  "value \"%s\" is out of range for type bigint\n", str);
791  return false;
792 
793 invalid_syntax:
794  if (!errorOK)
795  fprintf(stderr,
796  "invalid input syntax for type bigint: \"%s\"\n", str);
797  return false;
798 }
799 
800 /* convert string to double, detecting overflows/underflows */
801 bool
802 strtodouble(const char *str, bool errorOK, double *dv)
803 {
804  char *end;
805 
806  errno = 0;
807  *dv = strtod(str, &end);
808 
809  if (unlikely(errno != 0))
810  {
811  if (!errorOK)
812  fprintf(stderr,
813  "value \"%s\" is out of range for type double\n", str);
814  return false;
815  }
816 
817  if (unlikely(end == str || *end != '\0'))
818  {
819  if (!errorOK)
820  fprintf(stderr,
821  "invalid input syntax for type double: \"%s\"\n", str);
822  return false;
823  }
824  return true;
825 }
826 
827 /*
828  * Initialize a random state struct.
829  *
830  * We derive the seed from base_random_sequence, which must be set up already.
831  */
832 static void
834 {
835  random_state->xseed[0] = (unsigned short)
836  (pg_jrand48(base_random_sequence.xseed) & 0xFFFF);
837  random_state->xseed[1] = (unsigned short)
838  (pg_jrand48(base_random_sequence.xseed) & 0xFFFF);
839  random_state->xseed[2] = (unsigned short)
840  (pg_jrand48(base_random_sequence.xseed) & 0xFFFF);
841 }
842 
843 /*
844  * Random number generator: uniform distribution from min to max inclusive.
845  *
846  * Although the limits are expressed as int64, you can't generate the full
847  * int64 range in one call, because the difference of the limits mustn't
848  * overflow int64. In practice it's unwise to ask for more than an int32
849  * range, because of the limited precision of pg_erand48().
850  */
851 static int64
852 getrand(RandomState *random_state, int64 min, int64 max)
853 {
854  /*
855  * Odd coding is so that min and max have approximately the same chance of
856  * being selected as do numbers between them.
857  *
858  * pg_erand48() is thread-safe and concurrent, which is why we use it
859  * rather than random(), which in glibc is non-reentrant, and therefore
860  * protected by a mutex, and therefore a bottleneck on machines with many
861  * CPUs.
862  */
863  return min + (int64) ((max - min + 1) * pg_erand48(random_state->xseed));
864 }
865 
866 /*
867  * random number generator: exponential distribution from min to max inclusive.
868  * the parameter is so that the density of probability for the last cut-off max
869  * value is exp(-parameter).
870  */
871 static int64
872 getExponentialRand(RandomState *random_state, int64 min, int64 max,
873  double parameter)
874 {
875  double cut,
876  uniform,
877  rand;
878 
879  /* abort if wrong parameter, but must really be checked beforehand */
880  Assert(parameter > 0.0);
881  cut = exp(-parameter);
882  /* erand in [0, 1), uniform in (0, 1] */
883  uniform = 1.0 - pg_erand48(random_state->xseed);
884 
885  /*
886  * inner expression in (cut, 1] (if parameter > 0), rand in [0, 1)
887  */
888  Assert((1.0 - cut) != 0.0);
889  rand = -log(cut + (1.0 - cut) * uniform) / parameter;
890  /* return int64 random number within between min and max */
891  return min + (int64) ((max - min + 1) * rand);
892 }
893 
894 /* random number generator: gaussian distribution from min to max inclusive */
895 static int64
896 getGaussianRand(RandomState *random_state, int64 min, int64 max,
897  double parameter)
898 {
899  double stdev;
900  double rand;
901 
902  /* abort if parameter is too low, but must really be checked beforehand */
903  Assert(parameter >= MIN_GAUSSIAN_PARAM);
904 
905  /*
906  * Get user specified random number from this loop, with -parameter <
907  * stdev <= parameter
908  *
909  * This loop is executed until the number is in the expected range.
910  *
911  * As the minimum parameter is 2.0, the probability of looping is low:
912  * sqrt(-2 ln(r)) <= 2 => r >= e^{-2} ~ 0.135, then when taking the
913  * average sinus multiplier as 2/pi, we have a 8.6% looping probability in
914  * the worst case. For a parameter value of 5.0, the looping probability
915  * is about e^{-5} * 2 / pi ~ 0.43%.
916  */
917  do
918  {
919  /*
920  * pg_erand48 generates [0,1), but for the basic version of the
921  * Box-Muller transform the two uniformly distributed random numbers
922  * are expected in (0, 1] (see
923  * https://en.wikipedia.org/wiki/Box-Muller_transform)
924  */
925  double rand1 = 1.0 - pg_erand48(random_state->xseed);
926  double rand2 = 1.0 - pg_erand48(random_state->xseed);
927 
928  /* Box-Muller basic form transform */
929  double var_sqrt = sqrt(-2.0 * log(rand1));
930 
931  stdev = var_sqrt * sin(2.0 * M_PI * rand2);
932 
933  /*
934  * we may try with cos, but there may be a bias induced if the
935  * previous value fails the test. To be on the safe side, let us try
936  * over.
937  */
938  }
939  while (stdev < -parameter || stdev >= parameter);
940 
941  /* stdev is in [-parameter, parameter), normalization to [0,1) */
942  rand = (stdev + parameter) / (parameter * 2.0);
943 
944  /* return int64 random number within between min and max */
945  return min + (int64) ((max - min + 1) * rand);
946 }
947 
948 /*
949  * random number generator: generate a value, such that the series of values
950  * will approximate a Poisson distribution centered on the given value.
951  *
952  * Individual results are rounded to integers, though the center value need
953  * not be one.
954  */
955 static int64
956 getPoissonRand(RandomState *random_state, double center)
957 {
958  /*
959  * Use inverse transform sampling to generate a value > 0, such that the
960  * expected (i.e. average) value is the given argument.
961  */
962  double uniform;
963 
964  /* erand in [0, 1), uniform in (0, 1] */
965  uniform = 1.0 - pg_erand48(random_state->xseed);
966 
967  return (int64) (-log(uniform) * center + 0.5);
968 }
969 
970 /*
971  * Computing zipfian using rejection method, based on
972  * "Non-Uniform Random Variate Generation",
973  * Luc Devroye, p. 550-551, Springer 1986.
974  *
975  * This works for s > 1.0, but may perform badly for s very close to 1.0.
976  */
977 static int64
978 computeIterativeZipfian(RandomState *random_state, int64 n, double s)
979 {
980  double b = pow(2.0, s - 1.0);
981  double x,
982  t,
983  u,
984  v;
985 
986  /* Ensure n is sane */
987  if (n <= 1)
988  return 1;
989 
990  while (true)
991  {
992  /* random variates */
993  u = pg_erand48(random_state->xseed);
994  v = pg_erand48(random_state->xseed);
995 
996  x = floor(pow(u, -1.0 / (s - 1.0)));
997 
998  t = pow(1.0 + 1.0 / x, s - 1.0);
999  /* reject if too large or out of bound */
1000  if (v * x * (t - 1.0) / (b - 1.0) <= t / b && x <= n)
1001  break;
1002  }
1003  return (int64) x;
1004 }
1005 
1006 /* random number generator: zipfian distribution from min to max inclusive */
1007 static int64
1008 getZipfianRand(RandomState *random_state, int64 min, int64 max, double s)
1009 {
1010  int64 n = max - min + 1;
1011 
1012  /* abort if parameter is invalid */
1014 
1015  return min - 1 + computeIterativeZipfian(random_state, n, s);
1016 }
1017 
1018 /*
1019  * FNV-1a hash function
1020  */
1021 static int64
1022 getHashFnv1a(int64 val, uint64 seed)
1023 {
1024  int64 result;
1025  int i;
1026 
1027  result = FNV_OFFSET_BASIS ^ seed;
1028  for (i = 0; i < 8; ++i)
1029  {
1030  int32 octet = val & 0xff;
1031 
1032  val = val >> 8;
1033  result = result ^ octet;
1034  result = result * FNV_PRIME;
1035  }
1036 
1037  return result;
1038 }
1039 
1040 /*
1041  * Murmur2 hash function
1042  *
1043  * Based on original work of Austin Appleby
1044  * https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp
1045  */
1046 static int64
1047 getHashMurmur2(int64 val, uint64 seed)
1048 {
1049  uint64 result = seed ^ MM2_MUL_TIMES_8; /* sizeof(int64) */
1050  uint64 k = (uint64) val;
1051 
1052  k *= MM2_MUL;
1053  k ^= k >> MM2_ROT;
1054  k *= MM2_MUL;
1055 
1056  result ^= k;
1057  result *= MM2_MUL;
1058 
1059  result ^= result >> MM2_ROT;
1060  result *= MM2_MUL;
1061  result ^= result >> MM2_ROT;
1062 
1063  return (int64) result;
1064 }
1065 
1066 /*
1067  * Initialize the given SimpleStats struct to all zeroes
1068  */
1069 static void
1071 {
1072  memset(ss, 0, sizeof(SimpleStats));
1073 }
1074 
1075 /*
1076  * Accumulate one value into a SimpleStats struct.
1077  */
1078 static void
1080 {
1081  if (ss->count == 0 || val < ss->min)
1082  ss->min = val;
1083  if (ss->count == 0 || val > ss->max)
1084  ss->max = val;
1085  ss->count++;
1086  ss->sum += val;
1087  ss->sum2 += val * val;
1088 }
1089 
1090 /*
1091  * Merge two SimpleStats objects
1092  */
1093 static void
1095 {
1096  if (acc->count == 0 || ss->min < acc->min)
1097  acc->min = ss->min;
1098  if (acc->count == 0 || ss->max > acc->max)
1099  acc->max = ss->max;
1100  acc->count += ss->count;
1101  acc->sum += ss->sum;
1102  acc->sum2 += ss->sum2;
1103 }
1104 
1105 /*
1106  * Initialize a StatsData struct to mostly zeroes, with its start time set to
1107  * the given value.
1108  */
1109 static void
1111 {
1112  sd->start_time = start_time;
1113  sd->cnt = 0;
1114  sd->skipped = 0;
1115  initSimpleStats(&sd->latency);
1116  initSimpleStats(&sd->lag);
1117 }
1118 
1119 /*
1120  * Accumulate one additional item into the given stats object.
1121  */
1122 static void
1123 accumStats(StatsData *stats, bool skipped, double lat, double lag)
1124 {
1125  stats->cnt++;
1126 
1127  if (skipped)
1128  {
1129  /* no latency to record on skipped transactions */
1130  stats->skipped++;
1131  }
1132  else
1133  {
1134  addToSimpleStats(&stats->latency, lat);
1135 
1136  /* and possibly the same for schedule lag */
1137  if (throttle_delay)
1138  addToSimpleStats(&stats->lag, lag);
1139  }
1140 }
1141 
1142 /* call PQexec() and exit() on failure */
1143 static void
1144 executeStatement(PGconn *con, const char *sql)
1145 {
1146  PGresult *res;
1147 
1148  res = PQexec(con, sql);
1149  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1150  {
1151  fprintf(stderr, "%s", PQerrorMessage(con));
1152  exit(1);
1153  }
1154  PQclear(res);
1155 }
1156 
1157 /* call PQexec() and complain, but without exiting, on failure */
1158 static void
1159 tryExecuteStatement(PGconn *con, const char *sql)
1160 {
1161  PGresult *res;
1162 
1163  res = PQexec(con, sql);
1164  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1165  {
1166  fprintf(stderr, "%s", PQerrorMessage(con));
1167  fprintf(stderr, "(ignoring this error and continuing anyway)\n");
1168  }
1169  PQclear(res);
1170 }
1171 
1172 /* set up a connection to the backend */
1173 static PGconn *
1175 {
1176  PGconn *conn;
1177  bool new_pass;
1178  static bool have_password = false;
1179  static char password[100];
1180 
1181  /*
1182  * Start the connection. Loop until we have a password if requested by
1183  * backend.
1184  */
1185  do
1186  {
1187 #define PARAMS_ARRAY_SIZE 7
1188 
1189  const char *keywords[PARAMS_ARRAY_SIZE];
1190  const char *values[PARAMS_ARRAY_SIZE];
1191 
1192  keywords[0] = "host";
1193  values[0] = pghost;
1194  keywords[1] = "port";
1195  values[1] = pgport;
1196  keywords[2] = "user";
1197  values[2] = login;
1198  keywords[3] = "password";
1199  values[3] = have_password ? password : NULL;
1200  keywords[4] = "dbname";
1201  values[4] = dbName;
1202  keywords[5] = "fallback_application_name";
1203  values[5] = progname;
1204  keywords[6] = NULL;
1205  values[6] = NULL;
1206 
1207  new_pass = false;
1208 
1209  conn = PQconnectdbParams(keywords, values, true);
1210 
1211  if (!conn)
1212  {
1213  fprintf(stderr, "connection to database \"%s\" failed\n",
1214  dbName);
1215  return NULL;
1216  }
1217 
1218  if (PQstatus(conn) == CONNECTION_BAD &&
1219  PQconnectionNeedsPassword(conn) &&
1220  !have_password)
1221  {
1222  PQfinish(conn);
1223  simple_prompt("Password: ", password, sizeof(password), false);
1224  have_password = true;
1225  new_pass = true;
1226  }
1227  } while (new_pass);
1228 
1229  /* check to see that the backend connection was successfully made */
1230  if (PQstatus(conn) == CONNECTION_BAD)
1231  {
1232  fprintf(stderr, "connection to database \"%s\" failed:\n%s",
1233  dbName, PQerrorMessage(conn));
1234  PQfinish(conn);
1235  return NULL;
1236  }
1237 
1238  return conn;
1239 }
1240 
1241 /* qsort comparator for Variable array */
1242 static int
1243 compareVariableNames(const void *v1, const void *v2)
1244 {
1245  return strcmp(((const Variable *) v1)->name,
1246  ((const Variable *) v2)->name);
1247 }
1248 
1249 /* Locate a variable by name; returns NULL if unknown */
1250 static Variable *
1252 {
1253  Variable key;
1254 
1255  /* On some versions of Solaris, bsearch of zero items dumps core */
1256  if (st->nvariables <= 0)
1257  return NULL;
1258 
1259  /* Sort if we have to */
1260  if (!st->vars_sorted)
1261  {
1262  qsort((void *) st->variables, st->nvariables, sizeof(Variable),
1264  st->vars_sorted = true;
1265  }
1266 
1267  /* Now we can search */
1268  key.name = name;
1269  return (Variable *) bsearch((void *) &key,
1270  (void *) st->variables,
1271  st->nvariables,
1272  sizeof(Variable),
1274 }
1275 
1276 /* Get the value of a variable, in string form; returns NULL if unknown */
1277 static char *
1279 {
1280  Variable *var;
1281  char stringform[64];
1282 
1283  var = lookupVariable(st, name);
1284  if (var == NULL)
1285  return NULL; /* not found */
1286 
1287  if (var->svalue)
1288  return var->svalue; /* we have it in string form */
1289 
1290  /* We need to produce a string equivalent of the value */
1291  Assert(var->value.type != PGBT_NO_VALUE);
1292  if (var->value.type == PGBT_NULL)
1293  snprintf(stringform, sizeof(stringform), "NULL");
1294  else if (var->value.type == PGBT_BOOLEAN)
1295  snprintf(stringform, sizeof(stringform),
1296  "%s", var->value.u.bval ? "true" : "false");
1297  else if (var->value.type == PGBT_INT)
1298  snprintf(stringform, sizeof(stringform),
1299  INT64_FORMAT, var->value.u.ival);
1300  else if (var->value.type == PGBT_DOUBLE)
1301  snprintf(stringform, sizeof(stringform),
1302  "%.*g", DBL_DIG, var->value.u.dval);
1303  else /* internal error, unexpected type */
1304  Assert(0);
1305  var->svalue = pg_strdup(stringform);
1306  return var->svalue;
1307 }
1308 
1309 /* Try to convert variable to a value; return false on failure */
1310 static bool
1312 {
1313  size_t slen;
1314 
1315  if (var->value.type != PGBT_NO_VALUE)
1316  return true; /* no work */
1317 
1318  slen = strlen(var->svalue);
1319 
1320  if (slen == 0)
1321  /* what should it do on ""? */
1322  return false;
1323 
1324  if (pg_strcasecmp(var->svalue, "null") == 0)
1325  {
1326  setNullValue(&var->value);
1327  }
1328 
1329  /*
1330  * accept prefixes such as y, ye, n, no... but not for "o". 0/1 are
1331  * recognized later as an int, which is converted to bool if needed.
1332  */
1333  else if (pg_strncasecmp(var->svalue, "true", slen) == 0 ||
1334  pg_strncasecmp(var->svalue, "yes", slen) == 0 ||
1335  pg_strcasecmp(var->svalue, "on") == 0)
1336  {
1337  setBoolValue(&var->value, true);
1338  }
1339  else if (pg_strncasecmp(var->svalue, "false", slen) == 0 ||
1340  pg_strncasecmp(var->svalue, "no", slen) == 0 ||
1341  pg_strcasecmp(var->svalue, "off") == 0 ||
1342  pg_strcasecmp(var->svalue, "of") == 0)
1343  {
1344  setBoolValue(&var->value, false);
1345  }
1346  else if (is_an_int(var->svalue))
1347  {
1348  /* if it looks like an int, it must be an int without overflow */
1349  int64 iv;
1350 
1351  if (!strtoint64(var->svalue, false, &iv))
1352  return false;
1353 
1354  setIntValue(&var->value, iv);
1355  }
1356  else /* type should be double */
1357  {
1358  double dv;
1359 
1360  if (!strtodouble(var->svalue, true, &dv))
1361  {
1362  fprintf(stderr,
1363  "malformed variable \"%s\" value: \"%s\"\n",
1364  var->name, var->svalue);
1365  return false;
1366  }
1367  setDoubleValue(&var->value, dv);
1368  }
1369  return true;
1370 }
1371 
1372 /*
1373  * Check whether a variable's name is allowed.
1374  *
1375  * We allow any non-ASCII character, as well as ASCII letters, digits, and
1376  * underscore.
1377  *
1378  * Keep this in sync with the definitions of variable name characters in
1379  * "src/fe_utils/psqlscan.l", "src/bin/psql/psqlscanslash.l" and
1380  * "src/bin/pgbench/exprscan.l". Also see parseVariable(), below.
1381  *
1382  * Note: this static function is copied from "src/bin/psql/variables.c"
1383  */
1384 static bool
1386 {
1387  const unsigned char *ptr = (const unsigned char *) name;
1388 
1389  /* Mustn't be zero-length */
1390  if (*ptr == '\0')
1391  return false;
1392 
1393  while (*ptr)
1394  {
1395  if (IS_HIGHBIT_SET(*ptr) ||
1396  strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1397  "_0123456789", *ptr) != NULL)
1398  ptr++;
1399  else
1400  return false;
1401  }
1402 
1403  return true;
1404 }
1405 
1406 /*
1407  * Lookup a variable by name, creating it if need be.
1408  * Caller is expected to assign a value to the variable.
1409  * Returns NULL on failure (bad name).
1410  */
1411 static Variable *
1412 lookupCreateVariable(CState *st, const char *context, char *name)
1413 {
1414  Variable *var;
1415 
1416  var = lookupVariable(st, name);
1417  if (var == NULL)
1418  {
1419  Variable *newvars;
1420 
1421  /*
1422  * Check for the name only when declaring a new variable to avoid
1423  * overhead.
1424  */
1425  if (!valid_variable_name(name))
1426  {
1427  fprintf(stderr, "%s: invalid variable name: \"%s\"\n",
1428  context, name);
1429  return NULL;
1430  }
1431 
1432  /* Create variable at the end of the array */
1433  if (st->variables)
1434  newvars = (Variable *) pg_realloc(st->variables,
1435  (st->nvariables + 1) * sizeof(Variable));
1436  else
1437  newvars = (Variable *) pg_malloc(sizeof(Variable));
1438 
1439  st->variables = newvars;
1440 
1441  var = &newvars[st->nvariables];
1442 
1443  var->name = pg_strdup(name);
1444  var->svalue = NULL;
1445  /* caller is expected to initialize remaining fields */
1446 
1447  st->nvariables++;
1448  /* we don't re-sort the array till we have to */
1449  st->vars_sorted = false;
1450  }
1451 
1452  return var;
1453 }
1454 
1455 /* Assign a string value to a variable, creating it if need be */
1456 /* Returns false on failure (bad name) */
1457 static bool
1458 putVariable(CState *st, const char *context, char *name, const char *value)
1459 {
1460  Variable *var;
1461  char *val;
1462 
1463  var = lookupCreateVariable(st, context, name);
1464  if (!var)
1465  return false;
1466 
1467  /* dup then free, in case value is pointing at this variable */
1468  val = pg_strdup(value);
1469 
1470  if (var->svalue)
1471  free(var->svalue);
1472  var->svalue = val;
1473  var->value.type = PGBT_NO_VALUE;
1474 
1475  return true;
1476 }
1477 
1478 /* Assign a value to a variable, creating it if need be */
1479 /* Returns false on failure (bad name) */
1480 static bool
1481 putVariableValue(CState *st, const char *context, char *name,
1482  const PgBenchValue *value)
1483 {
1484  Variable *var;
1485 
1486  var = lookupCreateVariable(st, context, name);
1487  if (!var)
1488  return false;
1489 
1490  if (var->svalue)
1491  free(var->svalue);
1492  var->svalue = NULL;
1493  var->value = *value;
1494 
1495  return true;
1496 }
1497 
1498 /* Assign an integer value to a variable, creating it if need be */
1499 /* Returns false on failure (bad name) */
1500 static bool
1501 putVariableInt(CState *st, const char *context, char *name, int64 value)
1502 {
1503  PgBenchValue val;
1504 
1505  setIntValue(&val, value);
1506  return putVariableValue(st, context, name, &val);
1507 }
1508 
1509 /*
1510  * Parse a possible variable reference (:varname).
1511  *
1512  * "sql" points at a colon. If what follows it looks like a valid
1513  * variable name, return a malloc'd string containing the variable name,
1514  * and set *eaten to the number of characters consumed.
1515  * Otherwise, return NULL.
1516  */
1517 static char *
1518 parseVariable(const char *sql, int *eaten)
1519 {
1520  int i = 0;
1521  char *name;
1522 
1523  do
1524  {
1525  i++;
1526  } while (IS_HIGHBIT_SET(sql[i]) ||
1527  strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1528  "_0123456789", sql[i]) != NULL);
1529  if (i == 1)
1530  return NULL; /* no valid variable name chars */
1531 
1532  name = pg_malloc(i);
1533  memcpy(name, &sql[1], i - 1);
1534  name[i - 1] = '\0';
1535 
1536  *eaten = i;
1537  return name;
1538 }
1539 
1540 static char *
1541 replaceVariable(char **sql, char *param, int len, char *value)
1542 {
1543  int valueln = strlen(value);
1544 
1545  if (valueln > len)
1546  {
1547  size_t offset = param - *sql;
1548 
1549  *sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1);
1550  param = *sql + offset;
1551  }
1552 
1553  if (valueln != len)
1554  memmove(param + valueln, param + len, strlen(param + len) + 1);
1555  memcpy(param, value, valueln);
1556 
1557  return param + valueln;
1558 }
1559 
1560 static char *
1561 assignVariables(CState *st, char *sql)
1562 {
1563  char *p,
1564  *name,
1565  *val;
1566 
1567  p = sql;
1568  while ((p = strchr(p, ':')) != NULL)
1569  {
1570  int eaten;
1571 
1572  name = parseVariable(p, &eaten);
1573  if (name == NULL)
1574  {
1575  while (*p == ':')
1576  {
1577  p++;
1578  }
1579  continue;
1580  }
1581 
1582  val = getVariable(st, name);
1583  free(name);
1584  if (val == NULL)
1585  {
1586  p++;
1587  continue;
1588  }
1589 
1590  p = replaceVariable(&sql, p, eaten, val);
1591  }
1592 
1593  return sql;
1594 }
1595 
1596 static void
1597 getQueryParams(CState *st, const Command *command, const char **params)
1598 {
1599  int i;
1600 
1601  for (i = 0; i < command->argc - 1; i++)
1602  params[i] = getVariable(st, command->argv[i + 1]);
1603 }
1604 
1605 static char *
1607 {
1608  if (pval->type == PGBT_NO_VALUE)
1609  return "none";
1610  else if (pval->type == PGBT_NULL)
1611  return "null";
1612  else if (pval->type == PGBT_INT)
1613  return "int";
1614  else if (pval->type == PGBT_DOUBLE)
1615  return "double";
1616  else if (pval->type == PGBT_BOOLEAN)
1617  return "boolean";
1618  else
1619  {
1620  /* internal error, should never get there */
1621  Assert(false);
1622  return NULL;
1623  }
1624 }
1625 
1626 /* get a value as a boolean, or tell if there is a problem */
1627 static bool
1628 coerceToBool(PgBenchValue *pval, bool *bval)
1629 {
1630  if (pval->type == PGBT_BOOLEAN)
1631  {
1632  *bval = pval->u.bval;
1633  return true;
1634  }
1635  else /* NULL, INT or DOUBLE */
1636  {
1637  fprintf(stderr, "cannot coerce %s to boolean\n", valueTypeName(pval));
1638  *bval = false; /* suppress uninitialized-variable warnings */
1639  return false;
1640  }
1641 }
1642 
1643 /*
1644  * Return true or false from an expression for conditional purposes.
1645  * Non zero numerical values are true, zero and NULL are false.
1646  */
1647 static bool
1649 {
1650  switch (pval->type)
1651  {
1652  case PGBT_NULL:
1653  return false;
1654  case PGBT_BOOLEAN:
1655  return pval->u.bval;
1656  case PGBT_INT:
1657  return pval->u.ival != 0;
1658  case PGBT_DOUBLE:
1659  return pval->u.dval != 0.0;
1660  default:
1661  /* internal error, unexpected type */
1662  Assert(0);
1663  return false;
1664  }
1665 }
1666 
1667 /* get a value as an int, tell if there is a problem */
1668 static bool
1669 coerceToInt(PgBenchValue *pval, int64 *ival)
1670 {
1671  if (pval->type == PGBT_INT)
1672  {
1673  *ival = pval->u.ival;
1674  return true;
1675  }
1676  else if (pval->type == PGBT_DOUBLE)
1677  {
1678  double dval = rint(pval->u.dval);
1679 
1680  if (isnan(dval) || !FLOAT8_FITS_IN_INT64(dval))
1681  {
1682  fprintf(stderr, "double to int overflow for %f\n", dval);
1683  return false;
1684  }
1685  *ival = (int64) dval;
1686  return true;
1687  }
1688  else /* BOOLEAN or NULL */
1689  {
1690  fprintf(stderr, "cannot coerce %s to int\n", valueTypeName(pval));
1691  return false;
1692  }
1693 }
1694 
1695 /* get a value as a double, or tell if there is a problem */
1696 static bool
1697 coerceToDouble(PgBenchValue *pval, double *dval)
1698 {
1699  if (pval->type == PGBT_DOUBLE)
1700  {
1701  *dval = pval->u.dval;
1702  return true;
1703  }
1704  else if (pval->type == PGBT_INT)
1705  {
1706  *dval = (double) pval->u.ival;
1707  return true;
1708  }
1709  else /* BOOLEAN or NULL */
1710  {
1711  fprintf(stderr, "cannot coerce %s to double\n", valueTypeName(pval));
1712  return false;
1713  }
1714 }
1715 
1716 /* assign a null value */
1717 static void
1719 {
1720  pv->type = PGBT_NULL;
1721  pv->u.ival = 0;
1722 }
1723 
1724 /* assign a boolean value */
1725 static void
1727 {
1728  pv->type = PGBT_BOOLEAN;
1729  pv->u.bval = bval;
1730 }
1731 
1732 /* assign an integer value */
1733 static void
1734 setIntValue(PgBenchValue *pv, int64 ival)
1735 {
1736  pv->type = PGBT_INT;
1737  pv->u.ival = ival;
1738 }
1739 
1740 /* assign a double value */
1741 static void
1742 setDoubleValue(PgBenchValue *pv, double dval)
1743 {
1744  pv->type = PGBT_DOUBLE;
1745  pv->u.dval = dval;
1746 }
1747 
1748 static bool
1750 {
1751  return func == PGBENCH_AND || func == PGBENCH_OR || func == PGBENCH_CASE;
1752 }
1753 
1754 /* lazy evaluation of some functions */
1755 static bool
1758 {
1759  PgBenchValue a1,
1760  a2;
1761  bool ba1,
1762  ba2;
1763 
1764  Assert(isLazyFunc(func) && args != NULL && args->next != NULL);
1765 
1766  /* args points to first condition */
1767  if (!evaluateExpr(st, args->expr, &a1))
1768  return false;
1769 
1770  /* second condition for AND/OR and corresponding branch for CASE */
1771  args = args->next;
1772 
1773  switch (func)
1774  {
1775  case PGBENCH_AND:
1776  if (a1.type == PGBT_NULL)
1777  {
1778  setNullValue(retval);
1779  return true;
1780  }
1781 
1782  if (!coerceToBool(&a1, &ba1))
1783  return false;
1784 
1785  if (!ba1)
1786  {
1787  setBoolValue(retval, false);
1788  return true;
1789  }
1790 
1791  if (!evaluateExpr(st, args->expr, &a2))
1792  return false;
1793 
1794  if (a2.type == PGBT_NULL)
1795  {
1796  setNullValue(retval);
1797  return true;
1798  }
1799  else if (!coerceToBool(&a2, &ba2))
1800  return false;
1801  else
1802  {
1803  setBoolValue(retval, ba2);
1804  return true;
1805  }
1806 
1807  return true;
1808 
1809  case PGBENCH_OR:
1810 
1811  if (a1.type == PGBT_NULL)
1812  {
1813  setNullValue(retval);
1814  return true;
1815  }
1816 
1817  if (!coerceToBool(&a1, &ba1))
1818  return false;
1819 
1820  if (ba1)
1821  {
1822  setBoolValue(retval, true);
1823  return true;
1824  }
1825 
1826  if (!evaluateExpr(st, args->expr, &a2))
1827  return false;
1828 
1829  if (a2.type == PGBT_NULL)
1830  {
1831  setNullValue(retval);
1832  return true;
1833  }
1834  else if (!coerceToBool(&a2, &ba2))
1835  return false;
1836  else
1837  {
1838  setBoolValue(retval, ba2);
1839  return true;
1840  }
1841 
1842  case PGBENCH_CASE:
1843  /* when true, execute branch */
1844  if (valueTruth(&a1))
1845  return evaluateExpr(st, args->expr, retval);
1846 
1847  /* now args contains next condition or final else expression */
1848  args = args->next;
1849 
1850  /* final else case? */
1851  if (args->next == NULL)
1852  return evaluateExpr(st, args->expr, retval);
1853 
1854  /* no, another when, proceed */
1855  return evalLazyFunc(st, PGBENCH_CASE, args, retval);
1856 
1857  default:
1858  /* internal error, cannot get here */
1859  Assert(0);
1860  break;
1861  }
1862  return false;
1863 }
1864 
1865 /* maximum number of function arguments */
1866 #define MAX_FARGS 16
1867 
1868 /*
1869  * Recursive evaluation of standard functions,
1870  * which do not require lazy evaluation.
1871  */
1872 static bool
1875  PgBenchValue *retval)
1876 {
1877  /* evaluate all function arguments */
1878  int nargs = 0;
1879  PgBenchValue vargs[MAX_FARGS];
1880  PgBenchExprLink *l = args;
1881  bool has_null = false;
1882 
1883  for (nargs = 0; nargs < MAX_FARGS && l != NULL; nargs++, l = l->next)
1884  {
1885  if (!evaluateExpr(st, l->expr, &vargs[nargs]))
1886  return false;
1887  has_null |= vargs[nargs].type == PGBT_NULL;
1888  }
1889 
1890  if (l != NULL)
1891  {
1892  fprintf(stderr,
1893  "too many function arguments, maximum is %d\n", MAX_FARGS);
1894  return false;
1895  }
1896 
1897  /* NULL arguments */
1898  if (has_null && func != PGBENCH_IS && func != PGBENCH_DEBUG)
1899  {
1900  setNullValue(retval);
1901  return true;
1902  }
1903 
1904  /* then evaluate function */
1905  switch (func)
1906  {
1907  /* overloaded operators */
1908  case PGBENCH_ADD:
1909  case PGBENCH_SUB:
1910  case PGBENCH_MUL:
1911  case PGBENCH_DIV:
1912  case PGBENCH_MOD:
1913  case PGBENCH_EQ:
1914  case PGBENCH_NE:
1915  case PGBENCH_LE:
1916  case PGBENCH_LT:
1917  {
1918  PgBenchValue *lval = &vargs[0],
1919  *rval = &vargs[1];
1920 
1921  Assert(nargs == 2);
1922 
1923  /* overloaded type management, double if some double */
1924  if ((lval->type == PGBT_DOUBLE ||
1925  rval->type == PGBT_DOUBLE) && func != PGBENCH_MOD)
1926  {
1927  double ld,
1928  rd;
1929 
1930  if (!coerceToDouble(lval, &ld) ||
1931  !coerceToDouble(rval, &rd))
1932  return false;
1933 
1934  switch (func)
1935  {
1936  case PGBENCH_ADD:
1937  setDoubleValue(retval, ld + rd);
1938  return true;
1939 
1940  case PGBENCH_SUB:
1941  setDoubleValue(retval, ld - rd);
1942  return true;
1943 
1944  case PGBENCH_MUL:
1945  setDoubleValue(retval, ld * rd);
1946  return true;
1947 
1948  case PGBENCH_DIV:
1949  setDoubleValue(retval, ld / rd);
1950  return true;
1951 
1952  case PGBENCH_EQ:
1953  setBoolValue(retval, ld == rd);
1954  return true;
1955 
1956  case PGBENCH_NE:
1957  setBoolValue(retval, ld != rd);
1958  return true;
1959 
1960  case PGBENCH_LE:
1961  setBoolValue(retval, ld <= rd);
1962  return true;
1963 
1964  case PGBENCH_LT:
1965  setBoolValue(retval, ld < rd);
1966  return true;
1967 
1968  default:
1969  /* cannot get here */
1970  Assert(0);
1971  }
1972  }
1973  else /* we have integer operands, or % */
1974  {
1975  int64 li,
1976  ri,
1977  res;
1978 
1979  if (!coerceToInt(lval, &li) ||
1980  !coerceToInt(rval, &ri))
1981  return false;
1982 
1983  switch (func)
1984  {
1985  case PGBENCH_ADD:
1986  if (pg_add_s64_overflow(li, ri, &res))
1987  {
1988  fprintf(stderr, "bigint add out of range\n");
1989  return false;
1990  }
1991  setIntValue(retval, res);
1992  return true;
1993 
1994  case PGBENCH_SUB:
1995  if (pg_sub_s64_overflow(li, ri, &res))
1996  {
1997  fprintf(stderr, "bigint sub out of range\n");
1998  return false;
1999  }
2000  setIntValue(retval, res);
2001  return true;
2002 
2003  case PGBENCH_MUL:
2004  if (pg_mul_s64_overflow(li, ri, &res))
2005  {
2006  fprintf(stderr, "bigint mul out of range\n");
2007  return false;
2008  }
2009  setIntValue(retval, res);
2010  return true;
2011 
2012  case PGBENCH_EQ:
2013  setBoolValue(retval, li == ri);
2014  return true;
2015 
2016  case PGBENCH_NE:
2017  setBoolValue(retval, li != ri);
2018  return true;
2019 
2020  case PGBENCH_LE:
2021  setBoolValue(retval, li <= ri);
2022  return true;
2023 
2024  case PGBENCH_LT:
2025  setBoolValue(retval, li < ri);
2026  return true;
2027 
2028  case PGBENCH_DIV:
2029  case PGBENCH_MOD:
2030  if (ri == 0)
2031  {
2032  fprintf(stderr, "division by zero\n");
2033  return false;
2034  }
2035  /* special handling of -1 divisor */
2036  if (ri == -1)
2037  {
2038  if (func == PGBENCH_DIV)
2039  {
2040  /* overflow check (needed for INT64_MIN) */
2041  if (li == PG_INT64_MIN)
2042  {
2043  fprintf(stderr, "bigint div out of range\n");
2044  return false;
2045  }
2046  else
2047  setIntValue(retval, -li);
2048  }
2049  else
2050  setIntValue(retval, 0);
2051  return true;
2052  }
2053  /* else divisor is not -1 */
2054  if (func == PGBENCH_DIV)
2055  setIntValue(retval, li / ri);
2056  else /* func == PGBENCH_MOD */
2057  setIntValue(retval, li % ri);
2058 
2059  return true;
2060 
2061  default:
2062  /* cannot get here */
2063  Assert(0);
2064  }
2065  }
2066 
2067  Assert(0);
2068  return false; /* NOTREACHED */
2069  }
2070 
2071  /* integer bitwise operators */
2072  case PGBENCH_BITAND:
2073  case PGBENCH_BITOR:
2074  case PGBENCH_BITXOR:
2075  case PGBENCH_LSHIFT:
2076  case PGBENCH_RSHIFT:
2077  {
2078  int64 li,
2079  ri;
2080 
2081  if (!coerceToInt(&vargs[0], &li) || !coerceToInt(&vargs[1], &ri))
2082  return false;
2083 
2084  if (func == PGBENCH_BITAND)
2085  setIntValue(retval, li & ri);
2086  else if (func == PGBENCH_BITOR)
2087  setIntValue(retval, li | ri);
2088  else if (func == PGBENCH_BITXOR)
2089  setIntValue(retval, li ^ ri);
2090  else if (func == PGBENCH_LSHIFT)
2091  setIntValue(retval, li << ri);
2092  else if (func == PGBENCH_RSHIFT)
2093  setIntValue(retval, li >> ri);
2094  else /* cannot get here */
2095  Assert(0);
2096 
2097  return true;
2098  }
2099 
2100  /* logical operators */
2101  case PGBENCH_NOT:
2102  {
2103  bool b;
2104 
2105  if (!coerceToBool(&vargs[0], &b))
2106  return false;
2107 
2108  setBoolValue(retval, !b);
2109  return true;
2110  }
2111 
2112  /* no arguments */
2113  case PGBENCH_PI:
2114  setDoubleValue(retval, M_PI);
2115  return true;
2116 
2117  /* 1 overloaded argument */
2118  case PGBENCH_ABS:
2119  {
2120  PgBenchValue *varg = &vargs[0];
2121 
2122  Assert(nargs == 1);
2123 
2124  if (varg->type == PGBT_INT)
2125  {
2126  int64 i = varg->u.ival;
2127 
2128  setIntValue(retval, i < 0 ? -i : i);
2129  }
2130  else
2131  {
2132  double d = varg->u.dval;
2133 
2134  Assert(varg->type == PGBT_DOUBLE);
2135  setDoubleValue(retval, d < 0.0 ? -d : d);
2136  }
2137 
2138  return true;
2139  }
2140 
2141  case PGBENCH_DEBUG:
2142  {
2143  PgBenchValue *varg = &vargs[0];
2144 
2145  Assert(nargs == 1);
2146 
2147  fprintf(stderr, "debug(script=%d,command=%d): ",
2148  st->use_file, st->command + 1);
2149 
2150  if (varg->type == PGBT_NULL)
2151  fprintf(stderr, "null\n");
2152  else if (varg->type == PGBT_BOOLEAN)
2153  fprintf(stderr, "boolean %s\n", varg->u.bval ? "true" : "false");
2154  else if (varg->type == PGBT_INT)
2155  fprintf(stderr, "int " INT64_FORMAT "\n", varg->u.ival);
2156  else if (varg->type == PGBT_DOUBLE)
2157  fprintf(stderr, "double %.*g\n", DBL_DIG, varg->u.dval);
2158  else /* internal error, unexpected type */
2159  Assert(0);
2160 
2161  *retval = *varg;
2162 
2163  return true;
2164  }
2165 
2166  /* 1 double argument */
2167  case PGBENCH_DOUBLE:
2168  case PGBENCH_SQRT:
2169  case PGBENCH_LN:
2170  case PGBENCH_EXP:
2171  {
2172  double dval;
2173 
2174  Assert(nargs == 1);
2175 
2176  if (!coerceToDouble(&vargs[0], &dval))
2177  return false;
2178 
2179  if (func == PGBENCH_SQRT)
2180  dval = sqrt(dval);
2181  else if (func == PGBENCH_LN)
2182  dval = log(dval);
2183  else if (func == PGBENCH_EXP)
2184  dval = exp(dval);
2185  /* else is cast: do nothing */
2186 
2187  setDoubleValue(retval, dval);
2188  return true;
2189  }
2190 
2191  /* 1 int argument */
2192  case PGBENCH_INT:
2193  {
2194  int64 ival;
2195 
2196  Assert(nargs == 1);
2197 
2198  if (!coerceToInt(&vargs[0], &ival))
2199  return false;
2200 
2201  setIntValue(retval, ival);
2202  return true;
2203  }
2204 
2205  /* variable number of arguments */
2206  case PGBENCH_LEAST:
2207  case PGBENCH_GREATEST:
2208  {
2209  bool havedouble;
2210  int i;
2211 
2212  Assert(nargs >= 1);
2213 
2214  /* need double result if any input is double */
2215  havedouble = false;
2216  for (i = 0; i < nargs; i++)
2217  {
2218  if (vargs[i].type == PGBT_DOUBLE)
2219  {
2220  havedouble = true;
2221  break;
2222  }
2223  }
2224  if (havedouble)
2225  {
2226  double extremum;
2227 
2228  if (!coerceToDouble(&vargs[0], &extremum))
2229  return false;
2230  for (i = 1; i < nargs; i++)
2231  {
2232  double dval;
2233 
2234  if (!coerceToDouble(&vargs[i], &dval))
2235  return false;
2236  if (func == PGBENCH_LEAST)
2237  extremum = Min(extremum, dval);
2238  else
2239  extremum = Max(extremum, dval);
2240  }
2241  setDoubleValue(retval, extremum);
2242  }
2243  else
2244  {
2245  int64 extremum;
2246 
2247  if (!coerceToInt(&vargs[0], &extremum))
2248  return false;
2249  for (i = 1; i < nargs; i++)
2250  {
2251  int64 ival;
2252 
2253  if (!coerceToInt(&vargs[i], &ival))
2254  return false;
2255  if (func == PGBENCH_LEAST)
2256  extremum = Min(extremum, ival);
2257  else
2258  extremum = Max(extremum, ival);
2259  }
2260  setIntValue(retval, extremum);
2261  }
2262  return true;
2263  }
2264 
2265  /* random functions */
2266  case PGBENCH_RANDOM:
2270  {
2271  int64 imin,
2272  imax;
2273 
2274  Assert(nargs >= 2);
2275 
2276  if (!coerceToInt(&vargs[0], &imin) ||
2277  !coerceToInt(&vargs[1], &imax))
2278  return false;
2279 
2280  /* check random range */
2281  if (imin > imax)
2282  {
2283  fprintf(stderr, "empty range given to random\n");
2284  return false;
2285  }
2286  else if (imax - imin < 0 || (imax - imin) + 1 < 0)
2287  {
2288  /* prevent int overflows in random functions */
2289  fprintf(stderr, "random range is too large\n");
2290  return false;
2291  }
2292 
2293  if (func == PGBENCH_RANDOM)
2294  {
2295  Assert(nargs == 2);
2296  setIntValue(retval, getrand(&st->cs_func_rs, imin, imax));
2297  }
2298  else /* gaussian & exponential */
2299  {
2300  double param;
2301 
2302  Assert(nargs == 3);
2303 
2304  if (!coerceToDouble(&vargs[2], &param))
2305  return false;
2306 
2307  if (func == PGBENCH_RANDOM_GAUSSIAN)
2308  {
2309  if (param < MIN_GAUSSIAN_PARAM)
2310  {
2311  fprintf(stderr,
2312  "gaussian parameter must be at least %f "
2313  "(not %f)\n", MIN_GAUSSIAN_PARAM, param);
2314  return false;
2315  }
2316 
2317  setIntValue(retval,
2319  imin, imax, param));
2320  }
2321  else if (func == PGBENCH_RANDOM_ZIPFIAN)
2322  {
2323  if (param < MIN_ZIPFIAN_PARAM || param > MAX_ZIPFIAN_PARAM)
2324  {
2325  fprintf(stderr,
2326  "zipfian parameter must be in range [%.3f, %.0f]"
2327  " (not %f)\n",
2328  MIN_ZIPFIAN_PARAM, MAX_ZIPFIAN_PARAM, param);
2329  return false;
2330  }
2331 
2332  setIntValue(retval,
2333  getZipfianRand(&st->cs_func_rs, imin, imax, param));
2334  }
2335  else /* exponential */
2336  {
2337  if (param <= 0.0)
2338  {
2339  fprintf(stderr,
2340  "exponential parameter must be greater than zero"
2341  " (not %f)\n", param);
2342  return false;
2343  }
2344 
2345  setIntValue(retval,
2347  imin, imax, param));
2348  }
2349  }
2350 
2351  return true;
2352  }
2353 
2354  case PGBENCH_POW:
2355  {
2356  PgBenchValue *lval = &vargs[0];
2357  PgBenchValue *rval = &vargs[1];
2358  double ld,
2359  rd;
2360 
2361  Assert(nargs == 2);
2362 
2363  if (!coerceToDouble(lval, &ld) ||
2364  !coerceToDouble(rval, &rd))
2365  return false;
2366 
2367  setDoubleValue(retval, pow(ld, rd));
2368 
2369  return true;
2370  }
2371 
2372  case PGBENCH_IS:
2373  {
2374  Assert(nargs == 2);
2375 
2376  /*
2377  * note: this simple implementation is more permissive than
2378  * SQL
2379  */
2380  setBoolValue(retval,
2381  vargs[0].type == vargs[1].type &&
2382  vargs[0].u.bval == vargs[1].u.bval);
2383  return true;
2384  }
2385 
2386  /* hashing */
2387  case PGBENCH_HASH_FNV1A:
2388  case PGBENCH_HASH_MURMUR2:
2389  {
2390  int64 val,
2391  seed;
2392 
2393  Assert(nargs == 2);
2394 
2395  if (!coerceToInt(&vargs[0], &val) ||
2396  !coerceToInt(&vargs[1], &seed))
2397  return false;
2398 
2399  if (func == PGBENCH_HASH_MURMUR2)
2400  setIntValue(retval, getHashMurmur2(val, seed));
2401  else if (func == PGBENCH_HASH_FNV1A)
2402  setIntValue(retval, getHashFnv1a(val, seed));
2403  else
2404  /* cannot get here */
2405  Assert(0);
2406 
2407  return true;
2408  }
2409 
2410  default:
2411  /* cannot get here */
2412  Assert(0);
2413  /* dead code to avoid a compiler warning */
2414  return false;
2415  }
2416 }
2417 
2418 /* evaluate some function */
2419 static bool
2422 {
2423  if (isLazyFunc(func))
2424  return evalLazyFunc(st, func, args, retval);
2425  else
2426  return evalStandardFunc(st, func, args, retval);
2427 }
2428 
2429 /*
2430  * Recursive evaluation of an expression in a pgbench script
2431  * using the current state of variables.
2432  * Returns whether the evaluation was ok,
2433  * the value itself is returned through the retval pointer.
2434  */
2435 static bool
2437 {
2438  switch (expr->etype)
2439  {
2440  case ENODE_CONSTANT:
2441  {
2442  *retval = expr->u.constant;
2443  return true;
2444  }
2445 
2446  case ENODE_VARIABLE:
2447  {
2448  Variable *var;
2449 
2450  if ((var = lookupVariable(st, expr->u.variable.varname)) == NULL)
2451  {
2452  fprintf(stderr, "undefined variable \"%s\"\n",
2453  expr->u.variable.varname);
2454  return false;
2455  }
2456 
2457  if (!makeVariableValue(var))
2458  return false;
2459 
2460  *retval = var->value;
2461  return true;
2462  }
2463 
2464  case ENODE_FUNCTION:
2465  return evalFunc(st,
2466  expr->u.function.function,
2467  expr->u.function.args,
2468  retval);
2469 
2470  default:
2471  /* internal error which should never occur */
2472  fprintf(stderr, "unexpected enode type in evaluation: %d\n",
2473  expr->etype);
2474  exit(1);
2475  }
2476 }
2477 
2478 /*
2479  * Convert command name to meta-command enum identifier
2480  */
2481 static MetaCommand
2482 getMetaCommand(const char *cmd)
2483 {
2484  MetaCommand mc;
2485 
2486  if (cmd == NULL)
2487  mc = META_NONE;
2488  else if (pg_strcasecmp(cmd, "set") == 0)
2489  mc = META_SET;
2490  else if (pg_strcasecmp(cmd, "setshell") == 0)
2491  mc = META_SETSHELL;
2492  else if (pg_strcasecmp(cmd, "shell") == 0)
2493  mc = META_SHELL;
2494  else if (pg_strcasecmp(cmd, "sleep") == 0)
2495  mc = META_SLEEP;
2496  else if (pg_strcasecmp(cmd, "if") == 0)
2497  mc = META_IF;
2498  else if (pg_strcasecmp(cmd, "elif") == 0)
2499  mc = META_ELIF;
2500  else if (pg_strcasecmp(cmd, "else") == 0)
2501  mc = META_ELSE;
2502  else if (pg_strcasecmp(cmd, "endif") == 0)
2503  mc = META_ENDIF;
2504  else if (pg_strcasecmp(cmd, "gset") == 0)
2505  mc = META_GSET;
2506  else
2507  mc = META_NONE;
2508  return mc;
2509 }
2510 
2511 /*
2512  * Run a shell command. The result is assigned to the variable if not NULL.
2513  * Return true if succeeded, or false on error.
2514  */
2515 static bool
2516 runShellCommand(CState *st, char *variable, char **argv, int argc)
2517 {
2518  char command[SHELL_COMMAND_SIZE];
2519  int i,
2520  len = 0;
2521  FILE *fp;
2522  char res[64];
2523  char *endptr;
2524  int retval;
2525 
2526  /*----------
2527  * Join arguments with whitespace separators. Arguments starting with
2528  * exactly one colon are treated as variables:
2529  * name - append a string "name"
2530  * :var - append a variable named 'var'
2531  * ::name - append a string ":name"
2532  *----------
2533  */
2534  for (i = 0; i < argc; i++)
2535  {
2536  char *arg;
2537  int arglen;
2538 
2539  if (argv[i][0] != ':')
2540  {
2541  arg = argv[i]; /* a string literal */
2542  }
2543  else if (argv[i][1] == ':')
2544  {
2545  arg = argv[i] + 1; /* a string literal starting with colons */
2546  }
2547  else if ((arg = getVariable(st, argv[i] + 1)) == NULL)
2548  {
2549  fprintf(stderr, "%s: undefined variable \"%s\"\n",
2550  argv[0], argv[i]);
2551  return false;
2552  }
2553 
2554  arglen = strlen(arg);
2555  if (len + arglen + (i > 0 ? 1 : 0) >= SHELL_COMMAND_SIZE - 1)
2556  {
2557  fprintf(stderr, "%s: shell command is too long\n", argv[0]);
2558  return false;
2559  }
2560 
2561  if (i > 0)
2562  command[len++] = ' ';
2563  memcpy(command + len, arg, arglen);
2564  len += arglen;
2565  }
2566 
2567  command[len] = '\0';
2568 
2569  /* Fast path for non-assignment case */
2570  if (variable == NULL)
2571  {
2572  if (system(command))
2573  {
2574  if (!timer_exceeded)
2575  fprintf(stderr, "%s: could not launch shell command\n", argv[0]);
2576  return false;
2577  }
2578  return true;
2579  }
2580 
2581  /* Execute the command with pipe and read the standard output. */
2582  if ((fp = popen(command, "r")) == NULL)
2583  {
2584  fprintf(stderr, "%s: could not launch shell command\n", argv[0]);
2585  return false;
2586  }
2587  if (fgets(res, sizeof(res), fp) == NULL)
2588  {
2589  if (!timer_exceeded)
2590  fprintf(stderr, "%s: could not read result of shell command\n", argv[0]);
2591  (void) pclose(fp);
2592  return false;
2593  }
2594  if (pclose(fp) < 0)
2595  {
2596  fprintf(stderr, "%s: could not close shell command\n", argv[0]);
2597  return false;
2598  }
2599 
2600  /* Check whether the result is an integer and assign it to the variable */
2601  retval = (int) strtol(res, &endptr, 10);
2602  while (*endptr != '\0' && isspace((unsigned char) *endptr))
2603  endptr++;
2604  if (*res == '\0' || *endptr != '\0')
2605  {
2606  fprintf(stderr, "%s: shell command must return an integer (not \"%s\")\n",
2607  argv[0], res);
2608  return false;
2609  }
2610  if (!putVariableInt(st, "setshell", variable, retval))
2611  return false;
2612 
2613 #ifdef DEBUG
2614  printf("shell parameter name: \"%s\", value: \"%s\"\n", argv[1], res);
2615 #endif
2616  return true;
2617 }
2618 
2619 #define MAX_PREPARE_NAME 32
2620 static void
2621 preparedStatementName(char *buffer, int file, int state)
2622 {
2623  sprintf(buffer, "P%d_%d", file, state);
2624 }
2625 
2626 static void
2627 commandFailed(CState *st, const char *cmd, const char *message)
2628 {
2629  fprintf(stderr,
2630  "client %d aborted in command %d (%s) of script %d; %s\n",
2631  st->id, st->command, cmd, st->use_file, message);
2632 }
2633 
2634 /* return a script number with a weighted choice. */
2635 static int
2637 {
2638  int i = 0;
2639  int64 w;
2640 
2641  if (num_scripts == 1)
2642  return 0;
2643 
2644  w = getrand(&thread->ts_choose_rs, 0, total_weight - 1);
2645  do
2646  {
2647  w -= sql_script[i++].weight;
2648  } while (w >= 0);
2649 
2650  return i - 1;
2651 }
2652 
2653 /* Send a SQL command, using the chosen querymode */
2654 static bool
2656 {
2657  int r;
2658 
2659  if (querymode == QUERY_SIMPLE)
2660  {
2661  char *sql;
2662 
2663  sql = pg_strdup(command->argv[0]);
2664  sql = assignVariables(st, sql);
2665 
2666  if (debug)
2667  fprintf(stderr, "client %d sending %s\n", st->id, sql);
2668  r = PQsendQuery(st->con, sql);
2669  free(sql);
2670  }
2671  else if (querymode == QUERY_EXTENDED)
2672  {
2673  const char *sql = command->argv[0];
2674  const char *params[MAX_ARGS];
2675 
2676  getQueryParams(st, command, params);
2677 
2678  if (debug)
2679  fprintf(stderr, "client %d sending %s\n", st->id, sql);
2680  r = PQsendQueryParams(st->con, sql, command->argc - 1,
2681  NULL, params, NULL, NULL, 0);
2682  }
2683  else if (querymode == QUERY_PREPARED)
2684  {
2685  char name[MAX_PREPARE_NAME];
2686  const char *params[MAX_ARGS];
2687 
2688  if (!st->prepared[st->use_file])
2689  {
2690  int j;
2691  Command **commands = sql_script[st->use_file].commands;
2692 
2693  for (j = 0; commands[j] != NULL; j++)
2694  {
2695  PGresult *res;
2696  char name[MAX_PREPARE_NAME];
2697 
2698  if (commands[j]->type != SQL_COMMAND)
2699  continue;
2700  preparedStatementName(name, st->use_file, j);
2701  res = PQprepare(st->con, name,
2702  commands[j]->argv[0], commands[j]->argc - 1, NULL);
2703  if (PQresultStatus(res) != PGRES_COMMAND_OK)
2704  fprintf(stderr, "%s", PQerrorMessage(st->con));
2705  PQclear(res);
2706  }
2707  st->prepared[st->use_file] = true;
2708  }
2709 
2710  getQueryParams(st, command, params);
2711  preparedStatementName(name, st->use_file, st->command);
2712 
2713  if (debug)
2714  fprintf(stderr, "client %d sending %s\n", st->id, name);
2715  r = PQsendQueryPrepared(st->con, name, command->argc - 1,
2716  params, NULL, NULL, 0);
2717  }
2718  else /* unknown sql mode */
2719  r = 0;
2720 
2721  if (r == 0)
2722  {
2723  if (debug)
2724  fprintf(stderr, "client %d could not send %s\n",
2725  st->id, command->argv[0]);
2726  st->ecnt++;
2727  return false;
2728  }
2729  else
2730  return true;
2731 }
2732 
2733 /*
2734  * Process query response from the backend.
2735  *
2736  * If varprefix is not NULL, it's the variable name prefix where to store
2737  * the results of the *last* command.
2738  *
2739  * Returns true if everything is A-OK, false if any error occurs.
2740  */
2741 static bool
2742 readCommandResponse(CState *st, char *varprefix)
2743 {
2744  PGresult *res;
2745  PGresult *next_res;
2746  int qrynum = 0;
2747 
2748  res = PQgetResult(st->con);
2749 
2750  while (res != NULL)
2751  {
2752  bool is_last;
2753 
2754  /* peek at the next result to know whether the current is last */
2755  next_res = PQgetResult(st->con);
2756  is_last = (next_res == NULL);
2757 
2758  switch (PQresultStatus(res))
2759  {
2760  case PGRES_COMMAND_OK: /* non-SELECT commands */
2761  case PGRES_EMPTY_QUERY: /* may be used for testing no-op overhead */
2762  if (is_last && varprefix != NULL)
2763  {
2764  fprintf(stderr,
2765  "client %d script %d command %d query %d: expected one row, got %d\n",
2766  st->id, st->use_file, st->command, qrynum, 0);
2767  goto error;
2768  }
2769  break;
2770 
2771  case PGRES_TUPLES_OK:
2772  if (is_last && varprefix != NULL)
2773  {
2774  if (PQntuples(res) != 1)
2775  {
2776  fprintf(stderr,
2777  "client %d script %d command %d query %d: expected one row, got %d\n",
2778  st->id, st->use_file, st->command, qrynum, PQntuples(res));
2779  goto error;
2780  }
2781 
2782  /* store results into variables */
2783  for (int fld = 0; fld < PQnfields(res); fld++)
2784  {
2785  char *varname = PQfname(res, fld);
2786 
2787  /* allocate varname only if necessary, freed below */
2788  if (*varprefix != '\0')
2789  varname = psprintf("%s%s", varprefix, varname);
2790 
2791  /* store result as a string */
2792  if (!putVariable(st, "gset", varname,
2793  PQgetvalue(res, 0, fld)))
2794  {
2795  /* internal error */
2796  fprintf(stderr,
2797  "client %d script %d command %d query %d: error storing into variable %s\n",
2798  st->id, st->use_file, st->command, qrynum,
2799  varname);
2800  goto error;
2801  }
2802 
2803  if (*varprefix != '\0')
2804  pg_free(varname);
2805  }
2806  }
2807  /* otherwise the result is simply thrown away by PQclear below */
2808  break;
2809 
2810  default:
2811  /* anything else is unexpected */
2812  fprintf(stderr,
2813  "client %d script %d aborted in command %d query %d: %s",
2814  st->id, st->use_file, st->command, qrynum,
2815  PQerrorMessage(st->con));
2816  goto error;
2817  }
2818 
2819  PQclear(res);
2820  qrynum++;
2821  res = next_res;
2822  }
2823 
2824  if (qrynum == 0)
2825  {
2826  fprintf(stderr, "client %d command %d: no results\n", st->id, st->command);
2827  st->ecnt++;
2828  return false;
2829  }
2830 
2831  return true;
2832 
2833 error:
2834  st->ecnt++;
2835  PQclear(res);
2836  PQclear(next_res);
2837  do
2838  {
2839  res = PQgetResult(st->con);
2840  PQclear(res);
2841  } while (res);
2842 
2843  return false;
2844 }
2845 
2846 /*
2847  * Parse the argument to a \sleep command, and return the requested amount
2848  * of delay, in microseconds. Returns true on success, false on error.
2849  */
2850 static bool
2851 evaluateSleep(CState *st, int argc, char **argv, int *usecs)
2852 {
2853  char *var;
2854  int usec;
2855 
2856  if (*argv[1] == ':')
2857  {
2858  if ((var = getVariable(st, argv[1] + 1)) == NULL)
2859  {
2860  fprintf(stderr, "%s: undefined variable \"%s\"\n",
2861  argv[0], argv[1]);
2862  return false;
2863  }
2864  usec = atoi(var);
2865  }
2866  else
2867  usec = atoi(argv[1]);
2868 
2869  if (argc > 2)
2870  {
2871  if (pg_strcasecmp(argv[2], "ms") == 0)
2872  usec *= 1000;
2873  else if (pg_strcasecmp(argv[2], "s") == 0)
2874  usec *= 1000000;
2875  }
2876  else
2877  usec *= 1000000;
2878 
2879  *usecs = usec;
2880  return true;
2881 }
2882 
2883 /*
2884  * Advance the state machine of a connection.
2885  */
2886 static void
2888 {
2889  instr_time now;
2890 
2891  /*
2892  * gettimeofday() isn't free, so we get the current timestamp lazily the
2893  * first time it's needed, and reuse the same value throughout this
2894  * function after that. This also ensures that e.g. the calculated
2895  * latency reported in the log file and in the totals are the same. Zero
2896  * means "not set yet". Reset "now" when we execute shell commands or
2897  * expressions, which might take a non-negligible amount of time, though.
2898  */
2899  INSTR_TIME_SET_ZERO(now);
2900 
2901  /*
2902  * Loop in the state machine, until we have to wait for a result from the
2903  * server or have to sleep for throttling or \sleep.
2904  *
2905  * Note: In the switch-statement below, 'break' will loop back here,
2906  * meaning "continue in the state machine". Return is used to return to
2907  * the caller, giving the thread the opportunity to advance another
2908  * client.
2909  */
2910  for (;;)
2911  {
2912  Command *command;
2913 
2914  switch (st->state)
2915  {
2916  /* Select transaction (script) to run. */
2917  case CSTATE_CHOOSE_SCRIPT:
2918  st->use_file = chooseScript(thread);
2920 
2921  if (debug)
2922  fprintf(stderr, "client %d executing script \"%s\"\n", st->id,
2923  sql_script[st->use_file].desc);
2924 
2925  /*
2926  * If time is over, we're done; otherwise, get ready to start
2927  * a new transaction, or to get throttled if that's requested.
2928  */
2929  st->state = timer_exceeded ? CSTATE_FINISHED :
2930  throttle_delay > 0 ? CSTATE_PREPARE_THROTTLE : CSTATE_START_TX;
2931  break;
2932 
2933  /* Start new transaction (script) */
2934  case CSTATE_START_TX:
2935 
2936  /* establish connection if needed, i.e. under --connect */
2937  if (st->con == NULL)
2938  {
2939  instr_time start;
2940 
2942  start = now;
2943  if ((st->con = doConnect()) == NULL)
2944  {
2945  fprintf(stderr, "client %d aborted while establishing connection\n",
2946  st->id);
2947  st->state = CSTATE_ABORTED;
2948  break;
2949  }
2951  INSTR_TIME_ACCUM_DIFF(thread->conn_time, now, start);
2952 
2953  /* Reset session-local state */
2954  memset(st->prepared, 0, sizeof(st->prepared));
2955  }
2956 
2957  /* record transaction start time */
2959  st->txn_begin = now;
2960 
2961  /*
2962  * When not throttling, this is also the transaction's
2963  * scheduled start time.
2964  */
2965  if (!throttle_delay)
2967 
2968  /* Begin with the first command */
2970  st->command = 0;
2971  break;
2972 
2973  /*
2974  * Handle throttling once per transaction by sleeping.
2975  */
2977 
2978  /*
2979  * Generate a delay such that the series of delays will
2980  * approximate a Poisson distribution centered on the
2981  * throttle_delay time.
2982  *
2983  * If transactions are too slow or a given wait is shorter
2984  * than a transaction, the next transaction will start right
2985  * away.
2986  */
2987  Assert(throttle_delay > 0);
2988 
2989  thread->throttle_trigger +=
2990  getPoissonRand(&thread->ts_throttle_rs, throttle_delay);
2991  st->txn_scheduled = thread->throttle_trigger;
2992 
2993  /*
2994  * If --latency-limit is used, and this slot is already late
2995  * so that the transaction will miss the latency limit even if
2996  * it completed immediately, skip this time slot and schedule
2997  * to continue running on the next slot that isn't late yet.
2998  * But don't iterate beyond the -t limit, if one is given.
2999  */
3000  if (latency_limit)
3001  {
3002  int64 now_us;
3003 
3005  now_us = INSTR_TIME_GET_MICROSEC(now);
3006 
3007  while (thread->throttle_trigger < now_us - latency_limit &&
3008  (nxacts <= 0 || st->cnt < nxacts))
3009  {
3010  processXactStats(thread, st, &now, true, agg);
3011  /* next rendez-vous */
3012  thread->throttle_trigger +=
3013  getPoissonRand(&thread->ts_throttle_rs, throttle_delay);
3014  st->txn_scheduled = thread->throttle_trigger;
3015  }
3016 
3017  /*
3018  * stop client if -t was exceeded in the previous skip
3019  * loop
3020  */
3021  if (nxacts > 0 && st->cnt >= nxacts)
3022  {
3023  st->state = CSTATE_FINISHED;
3024  break;
3025  }
3026  }
3027 
3028  /*
3029  * stop client if next transaction is beyond pgbench end of
3030  * execution; otherwise, throttle it.
3031  */
3032  st->state = end_time > 0 && st->txn_scheduled > end_time ?
3034  break;
3035 
3036  /*
3037  * Wait until it's time to start next transaction.
3038  */
3039  case CSTATE_THROTTLE:
3041 
3042  if (INSTR_TIME_GET_MICROSEC(now) < st->txn_scheduled)
3043  return; /* still sleeping, nothing to do here */
3044 
3045  /* done sleeping, but don't start transaction if we're done */
3046  st->state = timer_exceeded ? CSTATE_FINISHED : CSTATE_START_TX;
3047  break;
3048 
3049  /*
3050  * Send a command to server (or execute a meta-command)
3051  */
3052  case CSTATE_START_COMMAND:
3053  command = sql_script[st->use_file].commands[st->command];
3054 
3055  /* Transition to script end processing if done */
3056  if (command == NULL)
3057  {
3058  st->state = CSTATE_END_TX;
3059  break;
3060  }
3061 
3062  /* record begin time of next command, and initiate it */
3063  if (report_per_command)
3064  {
3066  st->stmt_begin = now;
3067  }
3068 
3069  /* Execute the command */
3070  if (command->type == SQL_COMMAND)
3071  {
3072  if (!sendCommand(st, command))
3073  {
3074  commandFailed(st, "SQL", "SQL command send failed");
3075  st->state = CSTATE_ABORTED;
3076  }
3077  else
3078  st->state = CSTATE_WAIT_RESULT;
3079  }
3080  else if (command->type == META_COMMAND)
3081  {
3082  /*-----
3083  * Possible state changes when executing meta commands:
3084  * - on errors CSTATE_ABORTED
3085  * - on sleep CSTATE_SLEEP
3086  * - else CSTATE_END_COMMAND
3087  */
3088  st->state = executeMetaCommand(st, &now);
3089  }
3090 
3091  /*
3092  * We're now waiting for an SQL command to complete, or
3093  * finished processing a metacommand, or need to sleep, or
3094  * something bad happened.
3095  */
3096  Assert(st->state == CSTATE_WAIT_RESULT ||
3097  st->state == CSTATE_END_COMMAND ||
3098  st->state == CSTATE_SLEEP ||
3099  st->state == CSTATE_ABORTED);
3100  break;
3101 
3102  /*
3103  * non executed conditional branch
3104  */
3105  case CSTATE_SKIP_COMMAND:
3107  /* quickly skip commands until something to do... */
3108  while (true)
3109  {
3110  Command *command;
3111 
3112  command = sql_script[st->use_file].commands[st->command];
3113 
3114  /* cannot reach end of script in that state */
3115  Assert(command != NULL);
3116 
3117  /*
3118  * if this is conditional related, update conditional
3119  * state
3120  */
3121  if (command->type == META_COMMAND &&
3122  (command->meta == META_IF ||
3123  command->meta == META_ELIF ||
3124  command->meta == META_ELSE ||
3125  command->meta == META_ENDIF))
3126  {
3127  switch (conditional_stack_peek(st->cstack))
3128  {
3129  case IFSTATE_FALSE:
3130  if (command->meta == META_IF ||
3131  command->meta == META_ELIF)
3132  {
3133  /* we must evaluate the condition */
3135  }
3136  else if (command->meta == META_ELSE)
3137  {
3138  /* we must execute next command */
3142  st->command++;
3143  }
3144  else if (command->meta == META_ENDIF)
3145  {
3148  if (conditional_active(st->cstack))
3150 
3151  /*
3152  * else state remains in
3153  * CSTATE_SKIP_COMMAND
3154  */
3155  st->command++;
3156  }
3157  break;
3158 
3159  case IFSTATE_IGNORED:
3160  case IFSTATE_ELSE_FALSE:
3161  if (command->meta == META_IF)
3163  IFSTATE_IGNORED);
3164  else if (command->meta == META_ENDIF)
3165  {
3168  if (conditional_active(st->cstack))
3170  }
3171  /* could detect "else" & "elif" after "else" */
3172  st->command++;
3173  break;
3174 
3175  case IFSTATE_NONE:
3176  case IFSTATE_TRUE:
3177  case IFSTATE_ELSE_TRUE:
3178  default:
3179 
3180  /*
3181  * inconsistent if inactive, unreachable dead
3182  * code
3183  */
3184  Assert(false);
3185  }
3186  }
3187  else
3188  {
3189  /* skip and consider next */
3190  st->command++;
3191  }
3192 
3193  if (st->state != CSTATE_SKIP_COMMAND)
3194  /* out of quick skip command loop */
3195  break;
3196  }
3197  break;
3198 
3199  /*
3200  * Wait for the current SQL command to complete
3201  */
3202  case CSTATE_WAIT_RESULT:
3203  if (debug)
3204  fprintf(stderr, "client %d receiving\n", st->id);
3205  if (!PQconsumeInput(st->con))
3206  {
3207  /* there's something wrong */
3208  commandFailed(st, "SQL", "perhaps the backend died while processing");
3209  st->state = CSTATE_ABORTED;
3210  break;
3211  }
3212  if (PQisBusy(st->con))
3213  return; /* don't have the whole result yet */
3214 
3215  /* store or discard the query results */
3216  if (readCommandResponse(st, sql_script[st->use_file].commands[st->command]->varprefix))
3217  st->state = CSTATE_END_COMMAND;
3218  else
3219  st->state = CSTATE_ABORTED;
3220  break;
3221 
3222  /*
3223  * Wait until sleep is done. This state is entered after a
3224  * \sleep metacommand. The behavior is similar to
3225  * CSTATE_THROTTLE, but proceeds to CSTATE_START_COMMAND
3226  * instead of CSTATE_START_TX.
3227  */
3228  case CSTATE_SLEEP:
3230  if (INSTR_TIME_GET_MICROSEC(now) < st->sleep_until)
3231  return; /* still sleeping, nothing to do here */
3232  /* Else done sleeping. */
3233  st->state = CSTATE_END_COMMAND;
3234  break;
3235 
3236  /*
3237  * End of command: record stats and proceed to next command.
3238  */
3239  case CSTATE_END_COMMAND:
3240 
3241  /*
3242  * command completed: accumulate per-command execution times
3243  * in thread-local data structure, if per-command latencies
3244  * are requested.
3245  */
3246  if (report_per_command)
3247  {
3248  Command *command;
3249 
3251 
3252  command = sql_script[st->use_file].commands[st->command];
3253  /* XXX could use a mutex here, but we choose not to */
3254  addToSimpleStats(&command->stats,
3255  INSTR_TIME_GET_DOUBLE(now) -
3257  }
3258 
3259  /* Go ahead with next command, to be executed or skipped */
3260  st->command++;
3261  st->state = conditional_active(st->cstack) ?
3263  break;
3264 
3265  /*
3266  * End of transaction (end of script, really).
3267  */
3268  case CSTATE_END_TX:
3269 
3270  /* transaction finished: calculate latency and do log */
3271  processXactStats(thread, st, &now, false, agg);
3272 
3273  /*
3274  * missing \endif... cannot happen if CheckConditional was
3275  * okay
3276  */
3278 
3279  if (is_connect)
3280  {
3281  finishCon(st);
3282  INSTR_TIME_SET_ZERO(now);
3283  }
3284 
3285  if ((st->cnt >= nxacts && duration <= 0) || timer_exceeded)
3286  {
3287  /* script completed */
3288  st->state = CSTATE_FINISHED;
3289  break;
3290  }
3291 
3292  /* next transaction (script) */
3294 
3295  /*
3296  * Ensure that we always return on this point, so as to avoid
3297  * an infinite loop if the script only contains meta commands.
3298  */
3299  return;
3300 
3301  /*
3302  * Final states. Close the connection if it's still open.
3303  */
3304  case CSTATE_ABORTED:
3305  case CSTATE_FINISHED:
3306  finishCon(st);
3307  return;
3308  }
3309  }
3310 }
3311 
3312 /*
3313  * Subroutine for advanceConnectionState -- initiate or execute the current
3314  * meta command, and return the next state to set.
3315  *
3316  * *now is updated to the current time, unless the command is expected to
3317  * take no time to execute.
3318  */
3319 static ConnectionStateEnum
3321 {
3322  Command *command = sql_script[st->use_file].commands[st->command];
3323  int argc;
3324  char **argv;
3325 
3326  Assert(command != NULL && command->type == META_COMMAND);
3327 
3328  argc = command->argc;
3329  argv = command->argv;
3330 
3331  if (debug)
3332  {
3333  fprintf(stderr, "client %d executing \\%s", st->id, argv[0]);
3334  for (int i = 1; i < argc; i++)
3335  fprintf(stderr, " %s", argv[i]);
3336  fprintf(stderr, "\n");
3337  }
3338 
3339  if (command->meta == META_SLEEP)
3340  {
3341  int usec;
3342 
3343  /*
3344  * A \sleep doesn't execute anything, we just get the delay from the
3345  * argument, and enter the CSTATE_SLEEP state. (The per-command
3346  * latency will be recorded in CSTATE_SLEEP state, not here, after the
3347  * delay has elapsed.)
3348  */
3349  if (!evaluateSleep(st, argc, argv, &usec))
3350  {
3351  commandFailed(st, "sleep", "execution of meta-command failed");
3352  return CSTATE_ABORTED;
3353  }
3354 
3356  st->sleep_until = INSTR_TIME_GET_MICROSEC(*now) + usec;
3357  return CSTATE_SLEEP;
3358  }
3359  else if (command->meta == META_SET)
3360  {
3361  PgBenchExpr *expr = command->expr;
3362  PgBenchValue result;
3363 
3364  if (!evaluateExpr(st, expr, &result))
3365  {
3366  commandFailed(st, argv[0], "evaluation of meta-command failed");
3367  return CSTATE_ABORTED;
3368  }
3369 
3370  if (!putVariableValue(st, argv[0], argv[1], &result))
3371  {
3372  commandFailed(st, "set", "assignment of meta-command failed");
3373  return CSTATE_ABORTED;
3374  }
3375  }
3376  else if (command->meta == META_IF)
3377  {
3378  /* backslash commands with an expression to evaluate */
3379  PgBenchExpr *expr = command->expr;
3380  PgBenchValue result;
3381  bool cond;
3382 
3383  if (!evaluateExpr(st, expr, &result))
3384  {
3385  commandFailed(st, argv[0], "evaluation of meta-command failed");
3386  return CSTATE_ABORTED;
3387  }
3388 
3389  cond = valueTruth(&result);
3391  }
3392  else if (command->meta == META_ELIF)
3393  {
3394  /* backslash commands with an expression to evaluate */
3395  PgBenchExpr *expr = command->expr;
3396  PgBenchValue result;
3397  bool cond;
3398 
3400  {
3401  /* elif after executed block, skip eval and wait for endif. */
3403  return CSTATE_END_COMMAND;
3404  }
3405 
3406  if (!evaluateExpr(st, expr, &result))
3407  {
3408  commandFailed(st, argv[0], "evaluation of meta-command failed");
3409  return CSTATE_ABORTED;
3410  }
3411 
3412  cond = valueTruth(&result);
3415  }
3416  else if (command->meta == META_ELSE)
3417  {
3418  switch (conditional_stack_peek(st->cstack))
3419  {
3420  case IFSTATE_TRUE:
3422  break;
3423  case IFSTATE_FALSE: /* inconsistent if active */
3424  case IFSTATE_IGNORED: /* inconsistent if active */
3425  case IFSTATE_NONE: /* else without if */
3426  case IFSTATE_ELSE_TRUE: /* else after else */
3427  case IFSTATE_ELSE_FALSE: /* else after else */
3428  default:
3429  /* dead code if conditional check is ok */
3430  Assert(false);
3431  }
3432  }
3433  else if (command->meta == META_ENDIF)
3434  {
3437  }
3438  else if (command->meta == META_SETSHELL)
3439  {
3440  if (!runShellCommand(st, argv[1], argv + 2, argc - 2))
3441  {
3442  commandFailed(st, "setshell", "execution of meta-command failed");
3443  return CSTATE_ABORTED;
3444  }
3445  }
3446  else if (command->meta == META_SHELL)
3447  {
3448  if (!runShellCommand(st, NULL, argv + 1, argc - 1))
3449  {
3450  commandFailed(st, "shell", "execution of meta-command failed");
3451  return CSTATE_ABORTED;
3452  }
3453  }
3454 
3455  /*
3456  * executing the expression or shell command might have taken a
3457  * non-negligible amount of time, so reset 'now'
3458  */
3459  INSTR_TIME_SET_ZERO(*now);
3460 
3461  return CSTATE_END_COMMAND;
3462 }
3463 
3464 /*
3465  * Print log entry after completing one transaction.
3466  *
3467  * We print Unix-epoch timestamps in the log, so that entries can be
3468  * correlated against other logs. On some platforms this could be obtained
3469  * from the instr_time reading the caller has, but rather than get entangled
3470  * with that, we just eat the cost of an extra syscall in all cases.
3471  */
3472 static void
3473 doLog(TState *thread, CState *st,
3474  StatsData *agg, bool skipped, double latency, double lag)
3475 {
3476  FILE *logfile = thread->logfile;
3477 
3478  Assert(use_log);
3479 
3480  /*
3481  * Skip the log entry if sampling is enabled and this row doesn't belong
3482  * to the random sample.
3483  */
3484  if (sample_rate != 0.0 &&
3485  pg_erand48(thread->ts_sample_rs.xseed) > sample_rate)
3486  return;
3487 
3488  /* should we aggregate the results or not? */
3489  if (agg_interval > 0)
3490  {
3491  /*
3492  * Loop until we reach the interval of the current moment, and print
3493  * any empty intervals in between (this may happen with very low tps,
3494  * e.g. --rate=0.1).
3495  */
3496  time_t now = time(NULL);
3497 
3498  while (agg->start_time + agg_interval <= now)
3499  {
3500  /* print aggregated report to logfile */
3501  fprintf(logfile, "%ld " INT64_FORMAT " %.0f %.0f %.0f %.0f",
3502  (long) agg->start_time,
3503  agg->cnt,
3504  agg->latency.sum,
3505  agg->latency.sum2,
3506  agg->latency.min,
3507  agg->latency.max);
3508  if (throttle_delay)
3509  {
3510  fprintf(logfile, " %.0f %.0f %.0f %.0f",
3511  agg->lag.sum,
3512  agg->lag.sum2,
3513  agg->lag.min,
3514  agg->lag.max);
3515  if (latency_limit)
3516  fprintf(logfile, " " INT64_FORMAT, agg->skipped);
3517  }
3518  fputc('\n', logfile);
3519 
3520  /* reset data and move to next interval */
3521  initStats(agg, agg->start_time + agg_interval);
3522  }
3523 
3524  /* accumulate the current transaction */
3525  accumStats(agg, skipped, latency, lag);
3526  }
3527  else
3528  {
3529  /* no, print raw transactions */
3530  struct timeval tv;
3531 
3532  gettimeofday(&tv, NULL);
3533  if (skipped)
3534  fprintf(logfile, "%d " INT64_FORMAT " skipped %d %ld %ld",
3535  st->id, st->cnt, st->use_file,
3536  (long) tv.tv_sec, (long) tv.tv_usec);
3537  else
3538  fprintf(logfile, "%d " INT64_FORMAT " %.0f %d %ld %ld",
3539  st->id, st->cnt, latency, st->use_file,
3540  (long) tv.tv_sec, (long) tv.tv_usec);
3541  if (throttle_delay)
3542  fprintf(logfile, " %.0f", lag);
3543  fputc('\n', logfile);
3544  }
3545 }
3546 
3547 /*
3548  * Accumulate and report statistics at end of a transaction.
3549  *
3550  * (This is also called when a transaction is late and thus skipped.
3551  * Note that even skipped transactions are counted in the "cnt" fields.)
3552  */
3553 static void
3555  bool skipped, StatsData *agg)
3556 {
3557  double latency = 0.0,
3558  lag = 0.0;
3559  bool thread_details = progress || throttle_delay || latency_limit,
3560  detailed = thread_details || use_log || per_script_stats;
3561 
3562  if (detailed && !skipped)
3563  {
3565 
3566  /* compute latency & lag */
3567  latency = INSTR_TIME_GET_MICROSEC(*now) - st->txn_scheduled;
3569  }
3570 
3571  if (thread_details)
3572  {
3573  /* keep detailed thread stats */
3574  accumStats(&thread->stats, skipped, latency, lag);
3575 
3576  /* count transactions over the latency limit, if needed */
3577  if (latency_limit && latency > latency_limit)
3578  thread->latency_late++;
3579  }
3580  else
3581  {
3582  /* no detailed stats, just count */
3583  thread->stats.cnt++;
3584  }
3585 
3586  /* client stat is just counting */
3587  st->cnt++;
3588 
3589  if (use_log)
3590  doLog(thread, st, agg, skipped, latency, lag);
3591 
3592  /* XXX could use a mutex here, but we choose not to */
3593  if (per_script_stats)
3594  accumStats(&sql_script[st->use_file].stats, skipped, latency, lag);
3595 }
3596 
3597 
3598 /* discard connections */
3599 static void
3601 {
3602  int i;
3603 
3604  for (i = 0; i < length; i++)
3605  finishCon(&state[i]);
3606 }
3607 
3608 /*
3609  * Remove old pgbench tables, if any exist
3610  */
3611 static void
3613 {
3614  fprintf(stderr, "dropping old tables...\n");
3615 
3616  /*
3617  * We drop all the tables in one command, so that whether there are
3618  * foreign key dependencies or not doesn't matter.
3619  */
3620  executeStatement(con, "drop table if exists "
3621  "pgbench_accounts, "
3622  "pgbench_branches, "
3623  "pgbench_history, "
3624  "pgbench_tellers");
3625 }
3626 
3627 /*
3628  * Create "pgbench_accounts" partitions if needed.
3629  *
3630  * This is the larger table of pgbench default tpc-b like schema
3631  * with a known size, so we choose to partition it.
3632  */
3633 static void
3635 {
3636  char ff[64];
3637 
3638  ff[0] = '\0';
3639 
3640  /*
3641  * Per ddlinfo in initCreateTables, fillfactor is needed on table
3642  * pgbench_accounts.
3643  */
3644  append_fillfactor(ff, sizeof(ff));
3645 
3646  /* we must have to create some partitions */
3647  Assert(partitions > 0);
3648 
3649  fprintf(stderr, "creating %d partitions...\n", partitions);
3650 
3651  for (int p = 1; p <= partitions; p++)
3652  {
3653  char query[256];
3654 
3655  if (partition_method == PART_RANGE)
3656  {
3657  int64 part_size = (naccounts * (int64) scale + partitions - 1) / partitions;
3658  char minvalue[32],
3659  maxvalue[32];
3660 
3661  /*
3662  * For RANGE, we use open-ended partitions at the beginning and
3663  * end to allow any valid value for the primary key. Although the
3664  * actual minimum and maximum values can be derived from the
3665  * scale, it is more generic and the performance is better.
3666  */
3667  if (p == 1)
3668  sprintf(minvalue, "minvalue");
3669  else
3670  sprintf(minvalue, INT64_FORMAT, (p - 1) * part_size + 1);
3671 
3672  if (p < partitions)
3673  sprintf(maxvalue, INT64_FORMAT, p * part_size + 1);
3674  else
3675  sprintf(maxvalue, "maxvalue");
3676 
3677  snprintf(query, sizeof(query),
3678  "create%s table pgbench_accounts_%d\n"
3679  " partition of pgbench_accounts\n"
3680  " for values from (%s) to (%s)%s\n",
3681  unlogged_tables ? " unlogged" : "", p,
3682  minvalue, maxvalue, ff);
3683  }
3684  else if (partition_method == PART_HASH)
3685  snprintf(query, sizeof(query),
3686  "create%s table pgbench_accounts_%d\n"
3687  " partition of pgbench_accounts\n"
3688  " for values with (modulus %d, remainder %d)%s\n",
3689  unlogged_tables ? " unlogged" : "", p,
3690  partitions, p - 1, ff);
3691  else /* cannot get there */
3692  Assert(0);
3693 
3694  executeStatement(con, query);
3695  }
3696 }
3697 
3698 /*
3699  * Create pgbench's standard tables
3700  */
3701 static void
3703 {
3704  /*
3705  * Note: TPC-B requires at least 100 bytes per row, and the "filler"
3706  * fields in these table declarations were intended to comply with that.
3707  * The pgbench_accounts table complies with that because the "filler"
3708  * column is set to blank-padded empty string. But for all other tables
3709  * the columns default to NULL and so don't actually take any space. We
3710  * could fix that by giving them non-null default values. However, that
3711  * would completely break comparability of pgbench results with prior
3712  * versions. Since pgbench has never pretended to be fully TPC-B compliant
3713  * anyway, we stick with the historical behavior.
3714  */
3715  struct ddlinfo
3716  {
3717  const char *table; /* table name */
3718  const char *smcols; /* column decls if accountIDs are 32 bits */
3719  const char *bigcols; /* column decls if accountIDs are 64 bits */
3720  int declare_fillfactor;
3721  };
3722  static const struct ddlinfo DDLs[] = {
3723  {
3724  "pgbench_history",
3725  "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
3726  "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)",
3727  0
3728  },
3729  {
3730  "pgbench_tellers",
3731  "tid int not null,bid int,tbalance int,filler char(84)",
3732  "tid int not null,bid int,tbalance int,filler char(84)",
3733  1
3734  },
3735  {
3736  "pgbench_accounts",
3737  "aid int not null,bid int,abalance int,filler char(84)",
3738  "aid bigint not null,bid int,abalance int,filler char(84)",
3739  1
3740  },
3741  {
3742  "pgbench_branches",
3743  "bid int not null,bbalance int,filler char(88)",
3744  "bid int not null,bbalance int,filler char(88)",
3745  1
3746  }
3747  };
3748  int i;
3749 
3750  fprintf(stderr, "creating tables...\n");
3751 
3752  for (i = 0; i < lengthof(DDLs); i++)
3753  {
3754  char opts[256];
3755  char buffer[256];
3756  const struct ddlinfo *ddl = &DDLs[i];
3757  const char *cols;
3758 
3759  /* Construct new create table statement. */
3760  opts[0] = '\0';
3761 
3762  /* Partition pgbench_accounts table */
3763  if (partition_method != PART_NONE && strcmp(ddl->table, "pgbench_accounts") == 0)
3764  snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
3765  " partition by %s (aid)", PARTITION_METHOD[partition_method]);
3766  else if (ddl->declare_fillfactor)
3767  /* fillfactor is only expected on actual tables */
3768  append_fillfactor(opts, sizeof(opts));
3769 
3770  if (tablespace != NULL)
3771  {
3772  char *escape_tablespace;
3773 
3774  escape_tablespace = PQescapeIdentifier(con, tablespace,
3775  strlen(tablespace));
3776  snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
3777  " tablespace %s", escape_tablespace);
3778  PQfreemem(escape_tablespace);
3779  }
3780 
3781  cols = (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols;
3782 
3783  snprintf(buffer, sizeof(buffer), "create%s table %s(%s)%s",
3784  unlogged_tables ? " unlogged" : "",
3785  ddl->table, cols, opts);
3786 
3787  executeStatement(con, buffer);
3788  }
3789 
3790  if (partition_method != PART_NONE)
3791  createPartitions(con);
3792 }
3793 
3794 /*
3795  * add fillfactor percent option.
3796  *
3797  * XXX - As default is 100, it could be removed in this case.
3798  */
3799 static void
3800 append_fillfactor(char *opts, int len)
3801 {
3802  snprintf(opts + strlen(opts), len - strlen(opts),
3803  " with (fillfactor=%d)", fillfactor);
3804 }
3805 
3806 /*
3807  * Truncate away any old data, in one command in case there are foreign keys
3808  */
3809 static void
3811 {
3812  executeStatement(con, "truncate table "
3813  "pgbench_accounts, "
3814  "pgbench_branches, "
3815  "pgbench_history, "
3816  "pgbench_tellers");
3817 }
3818 
3819 /*
3820  * Fill the standard tables with some data generated and sent from the client
3821  */
3822 static void
3824 {
3825  char sql[256];
3826  PGresult *res;
3827  int i;
3828  int64 k;
3829 
3830  /* used to track elapsed time and estimate of the remaining time */
3831  instr_time start,
3832  diff;
3833  double elapsed_sec,
3834  remaining_sec;
3835  int log_interval = 1;
3836 
3837  fprintf(stderr, "generating data (client-side)...\n");
3838 
3839  /*
3840  * we do all of this in one transaction to enable the backend's
3841  * data-loading optimizations
3842  */
3843  executeStatement(con, "begin");
3844 
3845  /* truncate away any old data */
3846  initTruncateTables(con);
3847 
3848  /*
3849  * fill branches, tellers, accounts in that order in case foreign keys
3850  * already exist
3851  */
3852  for (i = 0; i < nbranches * scale; i++)
3853  {
3854  /* "filler" column defaults to NULL */
3855  snprintf(sql, sizeof(sql),
3856  "insert into pgbench_branches(bid,bbalance) values(%d,0)",
3857  i + 1);
3858  executeStatement(con, sql);
3859  }
3860 
3861  for (i = 0; i < ntellers * scale; i++)
3862  {
3863  /* "filler" column defaults to NULL */
3864  snprintf(sql, sizeof(sql),
3865  "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)",
3866  i + 1, i / ntellers + 1);
3867  executeStatement(con, sql);
3868  }
3869 
3870  /*
3871  * accounts is big enough to be worth using COPY and tracking runtime
3872  */
3873  res = PQexec(con, "copy pgbench_accounts from stdin");
3874  if (PQresultStatus(res) != PGRES_COPY_IN)
3875  {
3876  fprintf(stderr, "%s", PQerrorMessage(con));
3877  exit(1);
3878  }
3879  PQclear(res);
3880 
3881  INSTR_TIME_SET_CURRENT(start);
3882 
3883  for (k = 0; k < (int64) naccounts * scale; k++)
3884  {
3885  int64 j = k + 1;
3886 
3887  /* "filler" column defaults to blank padded empty string */
3888  snprintf(sql, sizeof(sql),
3889  INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n",
3890  j, k / naccounts + 1, 0);
3891  if (PQputline(con, sql))
3892  {
3893  fprintf(stderr, "PQputline failed\n");
3894  exit(1);
3895  }
3896 
3897  /*
3898  * If we want to stick with the original logging, print a message each
3899  * 100k inserted rows.
3900  */
3901  if ((!use_quiet) && (j % 100000 == 0))
3902  {
3903  INSTR_TIME_SET_CURRENT(diff);
3904  INSTR_TIME_SUBTRACT(diff, start);
3905 
3906  elapsed_sec = INSTR_TIME_GET_DOUBLE(diff);
3907  remaining_sec = ((double) scale * naccounts - j) * elapsed_sec / j;
3908 
3909  fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)\n",
3910  j, (int64) naccounts * scale,
3911  (int) (((int64) j * 100) / (naccounts * (int64) scale)),
3912  elapsed_sec, remaining_sec);
3913  }
3914  /* let's not call the timing for each row, but only each 100 rows */
3915  else if (use_quiet && (j % 100 == 0))
3916  {
3917  INSTR_TIME_SET_CURRENT(diff);
3918  INSTR_TIME_SUBTRACT(diff, start);
3919 
3920  elapsed_sec = INSTR_TIME_GET_DOUBLE(diff);
3921  remaining_sec = ((double) scale * naccounts - j) * elapsed_sec / j;
3922 
3923  /* have we reached the next interval (or end)? */
3924  if ((j == scale * naccounts) || (elapsed_sec >= log_interval * LOG_STEP_SECONDS))
3925  {
3926  fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)\n",
3927  j, (int64) naccounts * scale,
3928  (int) (((int64) j * 100) / (naccounts * (int64) scale)), elapsed_sec, remaining_sec);
3929 
3930  /* skip to the next interval */
3931  log_interval = (int) ceil(elapsed_sec / LOG_STEP_SECONDS);
3932  }
3933  }
3934 
3935  }
3936  if (PQputline(con, "\\.\n"))
3937  {
3938  fprintf(stderr, "very last PQputline failed\n");
3939  exit(1);
3940  }
3941  if (PQendcopy(con))
3942  {
3943  fprintf(stderr, "PQendcopy failed\n");
3944  exit(1);
3945  }
3946 
3947  executeStatement(con, "commit");
3948 }
3949 
3950 /*
3951  * Fill the standard tables with some data generated on the server
3952  *
3953  * As already the case with the client-side data generation, the filler
3954  * column defaults to NULL in pgbench_branches and pgbench_tellers,
3955  * and is a blank-padded string in pgbench_accounts.
3956  */
3957 static void
3959 {
3960  char sql[256];
3961 
3962  fprintf(stderr, "generating data (server-side)...\n");
3963 
3964  /*
3965  * we do all of this in one transaction to enable the backend's
3966  * data-loading optimizations
3967  */
3968  executeStatement(con, "begin");
3969 
3970  /* truncate away any old data */
3971  initTruncateTables(con);
3972 
3973  snprintf(sql, sizeof(sql),
3974  "insert into pgbench_branches(bid,bbalance) "
3975  "select bid, 0 "
3976  "from generate_series(1, %d) as bid", nbranches * scale);
3977  executeStatement(con, sql);
3978 
3979  snprintf(sql, sizeof(sql),
3980  "insert into pgbench_tellers(tid,bid,tbalance) "
3981  "select tid, (tid - 1) / %d + 1, 0 "
3982  "from generate_series(1, %d) as tid", ntellers, ntellers * scale);
3983  executeStatement(con, sql);
3984 
3985  snprintf(sql, sizeof(sql),
3986  "insert into pgbench_accounts(aid,bid,abalance,filler) "
3987  "select aid, (aid - 1) / %d + 1, 0, '' "
3988  "from generate_series(1, "INT64_FORMAT") as aid",
3989  naccounts, (int64) naccounts * scale);
3990  executeStatement(con, sql);
3991 
3992  executeStatement(con, "commit");
3993 }
3994 
3995 /*
3996  * Invoke vacuum on the standard tables
3997  */
3998 static void
4000 {
4001  fprintf(stderr, "vacuuming...\n");
4002  executeStatement(con, "vacuum analyze pgbench_branches");
4003  executeStatement(con, "vacuum analyze pgbench_tellers");
4004  executeStatement(con, "vacuum analyze pgbench_accounts");
4005  executeStatement(con, "vacuum analyze pgbench_history");
4006 }
4007 
4008 /*
4009  * Create primary keys on the standard tables
4010  */
4011 static void
4013 {
4014  static const char *const DDLINDEXes[] = {
4015  "alter table pgbench_branches add primary key (bid)",
4016  "alter table pgbench_tellers add primary key (tid)",
4017  "alter table pgbench_accounts add primary key (aid)"
4018  };
4019  int i;
4020 
4021  fprintf(stderr, "creating primary keys...\n");
4022  for (i = 0; i < lengthof(DDLINDEXes); i++)
4023  {
4024  char buffer[256];
4025 
4026  strlcpy(buffer, DDLINDEXes[i], sizeof(buffer));
4027 
4028  if (index_tablespace != NULL)
4029  {
4030  char *escape_tablespace;
4031 
4032  escape_tablespace = PQescapeIdentifier(con, index_tablespace,
4033  strlen(index_tablespace));
4034  snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
4035  " using index tablespace %s", escape_tablespace);
4036  PQfreemem(escape_tablespace);
4037  }
4038 
4039  executeStatement(con, buffer);
4040  }
4041 }
4042 
4043 /*
4044  * Create foreign key constraints between the standard tables
4045  */
4046 static void
4048 {
4049  static const char *const DDLKEYs[] = {
4050  "alter table pgbench_tellers add constraint pgbench_tellers_bid_fkey foreign key (bid) references pgbench_branches",
4051  "alter table pgbench_accounts add constraint pgbench_accounts_bid_fkey foreign key (bid) references pgbench_branches",
4052  "alter table pgbench_history add constraint pgbench_history_bid_fkey foreign key (bid) references pgbench_branches",
4053  "alter table pgbench_history add constraint pgbench_history_tid_fkey foreign key (tid) references pgbench_tellers",
4054  "alter table pgbench_history add constraint pgbench_history_aid_fkey foreign key (aid) references pgbench_accounts"
4055  };
4056  int i;
4057 
4058  fprintf(stderr, "creating foreign keys...\n");
4059  for (i = 0; i < lengthof(DDLKEYs); i++)
4060  {
4061  executeStatement(con, DDLKEYs[i]);
4062  }
4063 }
4064 
4065 /*
4066  * Validate an initialization-steps string
4067  *
4068  * (We could just leave it to runInitSteps() to fail if there are wrong
4069  * characters, but since initialization can take awhile, it seems friendlier
4070  * to check during option parsing.)
4071  */
4072 static void
4073 checkInitSteps(const char *initialize_steps)
4074 {
4075  if (initialize_steps[0] == '\0')
4076  {
4077  fprintf(stderr, "no initialization steps specified\n");
4078  exit(1);
4079  }
4080 
4081  for (const char *step = initialize_steps; *step != '\0'; step++)
4082  {
4083  if (strchr(ALL_INIT_STEPS " ", *step) == NULL)
4084  {
4085  fprintf(stderr,
4086  "unrecognized initialization step \"%c\"\n",
4087  *step);
4088  fprintf(stderr,
4089  "Allowed step characters are: \"" ALL_INIT_STEPS "\".\n");
4090  exit(1);
4091  }
4092  }
4093 }
4094 
4095 /*
4096  * Invoke each initialization step in the given string
4097  */
4098 static void
4099 runInitSteps(const char *initialize_steps)
4100 {
4101  PQExpBufferData stats;
4102  PGconn *con;
4103  const char *step;
4104  double run_time = 0.0;
4105  bool first = true;
4106 
4107  initPQExpBuffer(&stats);
4108 
4109  if ((con = doConnect()) == NULL)
4110  exit(1);
4111 
4112  for (step = initialize_steps; *step != '\0'; step++)
4113  {
4114  instr_time start;
4115  char *op = NULL;
4116 
4117  INSTR_TIME_SET_CURRENT(start);
4118 
4119  switch (*step)
4120  {
4121  case 'd':
4122  op = "drop tables";
4123  initDropTables(con);
4124  break;
4125  case 't':
4126  op = "create tables";
4127  initCreateTables(con);
4128  break;
4129  case 'g':
4130  op = "client-side generate";
4132  break;
4133  case 'G':
4134  op = "server-side generate";
4136  break;
4137  case 'v':
4138  op = "vacuum";
4139  initVacuum(con);
4140  break;
4141  case 'p':
4142  op = "primary keys";
4143  initCreatePKeys(con);
4144  break;
4145  case 'f':
4146  op = "foreign keys";
4147  initCreateFKeys(con);
4148  break;
4149  case ' ':
4150  break; /* ignore */
4151  default:
4152  fprintf(stderr, "unrecognized initialization step \"%c\"\n",
4153  *step);
4154  PQfinish(con);
4155  exit(1);
4156  }
4157 
4158  if (op != NULL)
4159  {
4160  instr_time diff;
4161  double elapsed_sec;
4162 
4163  INSTR_TIME_SET_CURRENT(diff);
4164  INSTR_TIME_SUBTRACT(diff, start);
4165  elapsed_sec = INSTR_TIME_GET_DOUBLE(diff);
4166 
4167  if (!first)
4168  appendPQExpBufferStr(&stats, ", ");
4169  else
4170  first = false;
4171 
4172  appendPQExpBuffer(&stats, "%s %.2f s", op, elapsed_sec);
4173 
4174  run_time += elapsed_sec;
4175  }
4176  }
4177 
4178  fprintf(stderr, "done in %.2f s (%s).\n", run_time, stats.data);
4179  PQfinish(con);
4180  termPQExpBuffer(&stats);
4181 }
4182 
4183 /*
4184  * Extract pgbench table informations into global variables scale,
4185  * partition_method and partitions.
4186  */
4187 static void
4188 GetTableInfo(PGconn *con, bool scale_given)
4189 {
4190  PGresult *res;
4191 
4192  /*
4193  * get the scaling factor that should be same as count(*) from
4194  * pgbench_branches if this is not a custom query
4195  */
4196  res = PQexec(con, "select count(*) from pgbench_branches");
4197  if (PQresultStatus(res) != PGRES_TUPLES_OK)
4198  {
4199  char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
4200 
4201  fprintf(stderr, "%s", PQerrorMessage(con));
4202  if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) == 0)
4203  {
4204  fprintf(stderr, "Perhaps you need to do initialization (\"pgbench -i\") in database \"%s\"\n", PQdb(con));
4205  }
4206 
4207  exit(1);
4208  }
4209  scale = atoi(PQgetvalue(res, 0, 0));
4210  if (scale < 0)
4211  {
4212  fprintf(stderr, "invalid count(*) from pgbench_branches: \"%s\"\n",
4213  PQgetvalue(res, 0, 0));
4214  exit(1);
4215  }
4216  PQclear(res);
4217 
4218  /* warn if we override user-given -s switch */
4219  if (scale_given)
4220  fprintf(stderr,
4221  "scale option ignored, using count from pgbench_branches table (%d)\n",
4222  scale);
4223 
4224  /*
4225  * Get the partition information for the first "pgbench_accounts" table
4226  * found in search_path.
4227  *
4228  * The result is empty if no "pgbench_accounts" is found.
4229  *
4230  * Otherwise, it always returns one row even if the table is not
4231  * partitioned (in which case the partition strategy is NULL).
4232  *
4233  * The number of partitions can be 0 even for partitioned tables, if no
4234  * partition is attached.
4235  *
4236  * We assume no partitioning on any failure, so as to avoid failing on an
4237  * old version without "pg_partitioned_table".
4238  */
4239  res = PQexec(con,
4240  "select o.n, p.partstrat, pg_catalog.count(i.inhparent) "
4241  "from pg_catalog.pg_class as c "
4242  "join pg_catalog.pg_namespace as n on (n.oid = c.relnamespace) "
4243  "cross join lateral (select pg_catalog.array_position(pg_catalog.current_schemas(true), n.nspname)) as o(n) "
4244  "left join pg_catalog.pg_partitioned_table as p on (p.partrelid = c.oid) "
4245  "left join pg_catalog.pg_inherits as i on (c.oid = i.inhparent) "
4246  "where c.relname = 'pgbench_accounts' and o.n is not null "
4247  "group by 1, 2 "
4248  "order by 1 asc "
4249  "limit 1");
4250 
4251  if (PQresultStatus(res) != PGRES_TUPLES_OK)
4252  {
4253  /* probably an older version, coldly assume no partitioning */
4254  partition_method = PART_NONE;
4255  partitions = 0;
4256  }
4257  else if (PQntuples(res) == 0)
4258  {
4259  /*
4260  * This case is unlikely as pgbench already found "pgbench_branches"
4261  * above to compute the scale.
4262  */
4263  fprintf(stderr,
4264  "no pgbench_accounts table found in search_path\n"
4265  "Perhaps you need to do initialization (\"pgbench -i\") in database \"%s\".\n", PQdb(con));
4266  exit(1);
4267  }
4268  else /* PQntupes(res) == 1 */
4269  {
4270  /* normal case, extract partition information */
4271  if (PQgetisnull(res, 0, 1))
4272  partition_method = PART_NONE;
4273  else
4274  {
4275  char *ps = PQgetvalue(res, 0, 1);
4276 
4277  /* column must be there */
4278  Assert(ps != NULL);
4279 
4280  if (strcmp(ps, "r") == 0)
4281  partition_method = PART_RANGE;
4282  else if (strcmp(ps, "h") == 0)
4283  partition_method = PART_HASH;
4284  else
4285  {
4286  /* possibly a newer version with new partition method */
4287  fprintf(stderr, "unexpected partition method: \"%s\"\n", ps);
4288  exit(1);
4289  }
4290  }
4291 
4292  partitions = atoi(PQgetvalue(res, 0, 2));
4293  }
4294 
4295  PQclear(res);
4296 }
4297 
4298 /*
4299  * Replace :param with $n throughout the command's SQL text, which
4300  * is a modifiable string in cmd->lines.
4301  */
4302 static bool
4304 {
4305  char *sql,
4306  *p;
4307 
4308  cmd->argc = 1;
4309 
4310  p = sql = pg_strdup(cmd->lines.data);
4311  while ((p = strchr(p, ':')) != NULL)
4312  {
4313  char var[13];
4314  char *name;
4315  int eaten;
4316 
4317  name = parseVariable(p, &eaten);
4318  if (name == NULL)
4319  {
4320  while (*p == ':')
4321  {
4322  p++;
4323  }
4324  continue;
4325  }
4326 
4327  /*
4328  * cmd->argv[0] is the SQL statement itself, so the max number of
4329  * arguments is one less than MAX_ARGS
4330  */
4331  if (cmd->argc >= MAX_ARGS)
4332  {
4333  fprintf(stderr, "statement has too many arguments (maximum is %d): %s\n",
4334  MAX_ARGS - 1, cmd->lines.data);
4335  pg_free(name);
4336  return false;
4337  }
4338 
4339  sprintf(var, "$%d", cmd->argc);
4340  p = replaceVariable(&sql, p, eaten, var);
4341 
4342  cmd->argv[cmd->argc] = name;
4343  cmd->argc++;
4344  }
4345 
4346  Assert(cmd->argv[0] == NULL);
4347  cmd->argv[0] = sql;
4348  return true;
4349 }
4350 
4351 /*
4352  * syntax error while parsing a script (in practice, while parsing a
4353  * backslash command, because we don't detect syntax errors in SQL)
4354  *
4355  * source: source of script (filename or builtin-script ID)
4356  * lineno: line number within script (count from 1)
4357  * line: whole line of backslash command, if available
4358  * command: backslash command name, if available
4359  * msg: the actual error message
4360  * more: optional extra message
4361  * column: zero-based column number, or -1 if unknown
4362  */
4363 void
4364 syntax_error(const char *source, int lineno,
4365  const char *line, const char *command,
4366  const char *msg, const char *more, int column)
4367 {
4368  fprintf(stderr, "%s:%d: %s", source, lineno, msg);
4369  if (more != NULL)
4370  fprintf(stderr, " (%s)", more);
4371  if (column >= 0 && line == NULL)
4372  fprintf(stderr, " at column %d", column + 1);
4373  if (command != NULL)
4374  fprintf(stderr, " in command \"%s\"", command);
4375  fprintf(stderr, "\n");
4376  if (line != NULL)
4377  {
4378  fprintf(stderr, "%s\n", line);
4379  if (column >= 0)
4380  {
4381  int i;
4382 
4383  for (i = 0; i < column; i++)
4384  fprintf(stderr, " ");
4385  fprintf(stderr, "^ error found here\n");
4386  }
4387  }
4388  exit(1);
4389 }
4390 
4391 /*
4392  * Return a pointer to the start of the SQL command, after skipping over
4393  * whitespace and "--" comments.
4394  * If the end of the string is reached, return NULL.
4395  */
4396 static char *
4397 skip_sql_comments(char *sql_command)
4398 {
4399  char *p = sql_command;
4400 
4401  /* Skip any leading whitespace, as well as "--" style comments */
4402  for (;;)
4403  {
4404  if (isspace((unsigned char) *p))
4405  p++;
4406  else if (strncmp(p, "--", 2) == 0)
4407  {
4408  p = strchr(p, '\n');
4409  if (p == NULL)
4410  return NULL;
4411  p++;
4412  }
4413  else
4414  break;
4415  }
4416 
4417  /* NULL if there's nothing but whitespace and comments */
4418  if (*p == '\0')
4419  return NULL;
4420 
4421  return p;
4422 }
4423 
4424 /*
4425  * Parse a SQL command; return a Command struct, or NULL if it's a comment
4426  *
4427  * On entry, psqlscan.l has collected the command into "buf", so we don't
4428  * really need to do much here except check for comments and set up a Command
4429  * struct.
4430  */
4431 static Command *
4432 create_sql_command(PQExpBuffer buf, const char *source)
4433 {
4434  Command *my_command;
4435  char *p = skip_sql_comments(buf->data);
4436 
4437  if (p == NULL)
4438  return NULL;
4439 
4440  /* Allocate and initialize Command structure */
4441  my_command = (Command *) pg_malloc(sizeof(Command));
4442  initPQExpBuffer(&my_command->lines);
4443  appendPQExpBufferStr(&my_command->lines, p);
4444  my_command->first_line = NULL; /* this is set later */
4445  my_command->type = SQL_COMMAND;
4446  my_command->meta = META_NONE;
4447  my_command->argc = 0;
4448  memset(my_command->argv, 0, sizeof(my_command->argv));
4449  my_command->varprefix = NULL; /* allocated later, if needed */
4450  my_command->expr = NULL;
4451  initSimpleStats(&my_command->stats);
4452 
4453  return my_command;
4454 }
4455 
4456 /* Free a Command structure and associated data */
4457 static void
4459 {
4460  termPQExpBuffer(&command->lines);
4461  if (command->first_line)
4462  pg_free(command->first_line);
4463  for (int i = 0; i < command->argc; i++)
4464  pg_free(command->argv[i]);
4465  if (command->varprefix)
4466  pg_free(command->varprefix);
4467 
4468  /*
4469  * It should also free expr recursively, but this is currently not needed
4470  * as only gset commands (which do not have an expression) are freed.
4471  */
4472  pg_free(command);
4473 }
4474 
4475 /*
4476  * Once an SQL command is fully parsed, possibly by accumulating several
4477  * parts, complete other fields of the Command structure.
4478  */
4479 static void
4481 {
4482  char buffer[128];
4483 
4484  Assert(my_command->type == SQL_COMMAND);
4485 
4486  /* Save the first line for error display. */
4487  strlcpy(buffer, my_command->lines.data, sizeof(buffer));
4488  buffer[strcspn(buffer, "\n\r")] = '\0';
4489  my_command->first_line = pg_strdup(buffer);
4490 
4491  /* parse query if necessary */
4492  switch (querymode)
4493  {
4494  case QUERY_SIMPLE:
4495  my_command->argv[0] = my_command->lines.data;
4496  my_command->argc++;
4497  break;
4498  case QUERY_EXTENDED:
4499  case QUERY_PREPARED:
4500  if (!parseQuery(my_command))
4501  exit(1);
4502  break;
4503  default:
4504  exit(1);
4505  }
4506 }
4507 
4508 /*
4509  * Parse a backslash command; return a Command struct, or NULL if comment
4510  *
4511  * At call, we have scanned only the initial backslash.
4512  */
4513 static Command *
4514 process_backslash_command(PsqlScanState sstate, const char *source)
4515 {
4516  Command *my_command;
4517  PQExpBufferData word_buf;
4518  int word_offset;
4519  int offsets[MAX_ARGS]; /* offsets of argument words */
4520  int start_offset;
4521  int lineno;
4522  int j;
4523 
4524  initPQExpBuffer(&word_buf);
4525 
4526  /* Remember location of the backslash */
4527  start_offset = expr_scanner_offset(sstate) - 1;
4528  lineno = expr_scanner_get_lineno(sstate, start_offset);
4529 
4530  /* Collect first word of command */
4531  if (!expr_lex_one_word(sstate, &word_buf, &word_offset))
4532  {
4533  termPQExpBuffer(&word_buf);
4534  return NULL;
4535  }
4536 
4537  /* Allocate and initialize Command structure */
4538  my_command = (Command *) pg_malloc0(sizeof(Command));
4539  my_command->type = META_COMMAND;
4540  my_command->argc = 0;
4541  initSimpleStats(&my_command->stats);
4542 
4543  /* Save first word (command name) */
4544  j = 0;
4545  offsets[j] = word_offset;
4546  my_command->argv[j++] = pg_strdup(word_buf.data);
4547  my_command->argc++;
4548 
4549  /* ... and convert it to enum form */
4550  my_command->meta = getMetaCommand(my_command->argv[0]);
4551 
4552  if (my_command->meta == META_SET ||
4553  my_command->meta == META_IF ||
4554  my_command->meta == META_ELIF)
4555  {
4557 
4558  /* For \set, collect var name */
4559  if (my_command->meta == META_SET)
4560  {
4561  if (!expr_lex_one_word(sstate, &word_buf, &word_offset))
4562  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4563  "missing argument", NULL, -1);
4564 
4565  offsets[j] = word_offset;
4566  my_command->argv[j++] = pg_strdup(word_buf.data);
4567  my_command->argc++;
4568  }
4569 
4570  /* then for all parse the expression */
4571  yyscanner = expr_scanner_init(sstate, source, lineno, start_offset,
4572  my_command->argv[0]);
4573 
4574  if (expr_yyparse(yyscanner) != 0)
4575  {
4576  /* dead code: exit done from syntax_error called by yyerror */
4577  exit(1);
4578  }
4579 
4580  my_command->expr = expr_parse_result;
4581 
4582  /* Save line, trimming any trailing newline */
4583  my_command->first_line =
4585  start_offset,
4586  expr_scanner_offset(sstate),
4587  true);
4588 
4589  expr_scanner_finish(yyscanner);
4590 
4591  termPQExpBuffer(&word_buf);
4592 
4593  return my_command;
4594  }
4595 
4596  /* For all other commands, collect remaining words. */
4597  while (expr_lex_one_word(sstate, &word_buf, &word_offset))
4598  {
4599  /*
4600  * my_command->argv[0] is the command itself, so the max number of
4601  * arguments is one less than MAX_ARGS
4602  */
4603  if (j >= MAX_ARGS)
4604  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4605  "too many arguments", NULL, -1);
4606 
4607  offsets[j] = word_offset;
4608  my_command->argv[j++] = pg_strdup(word_buf.data);
4609  my_command->argc++;
4610  }
4611 
4612  /* Save line, trimming any trailing newline */
4613  my_command->first_line =
4615  start_offset,
4616  expr_scanner_offset(sstate),
4617  true);
4618 
4619  if (my_command->meta == META_SLEEP)
4620  {
4621  if (my_command->argc < 2)
4622  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4623  "missing argument", NULL, -1);
4624 
4625  if (my_command->argc > 3)
4626  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4627  "too many arguments", NULL,
4628  offsets[3] - start_offset);
4629 
4630  /*
4631  * Split argument into number and unit to allow "sleep 1ms" etc. We
4632  * don't have to terminate the number argument with null because it
4633  * will be parsed with atoi, which ignores trailing non-digit
4634  * characters.
4635  */
4636  if (my_command->argc == 2 && my_command->argv[1][0] != ':')
4637  {
4638  char *c = my_command->argv[1];
4639 
4640  while (isdigit((unsigned char) *c))
4641  c++;
4642  if (*c)
4643  {
4644  my_command->argv[2] = c;
4645  offsets[2] = offsets[1] + (c - my_command->argv[1]);
4646  my_command->argc = 3;
4647  }
4648  }
4649 
4650  if (my_command->argc == 3)
4651  {
4652  if (pg_strcasecmp(my_command->argv[2], "us") != 0 &&
4653  pg_strcasecmp(my_command->argv[2], "ms") != 0 &&
4654  pg_strcasecmp(my_command->argv[2], "s") != 0)
4655  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4656  "unrecognized time unit, must be us, ms or s",
4657  my_command->argv[2], offsets[2] - start_offset);
4658  }
4659  }
4660  else if (my_command->meta == META_SETSHELL)
4661  {
4662  if (my_command->argc < 3)
4663  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4664  "missing argument", NULL, -1);
4665  }
4666  else if (my_command->meta == META_SHELL)
4667  {
4668  if (my_command->argc < 2)
4669  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4670  "missing command", NULL, -1);
4671  }
4672  else if (my_command->meta == META_ELSE || my_command->meta == META_ENDIF)
4673  {
4674  if (my_command->argc != 1)
4675  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4676  "unexpected argument", NULL, -1);
4677  }
4678  else if (my_command->meta == META_GSET)
4679  {
4680  if (my_command->argc > 2)
4681  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4682  "too many arguments", NULL, -1);
4683  }
4684  else
4685  {
4686  /* my_command->meta == META_NONE */
4687  syntax_error(source, lineno, my_command->first_line, my_command->argv[0],
4688  "invalid command", NULL, -1);
4689  }
4690 
4691  termPQExpBuffer(&word_buf);
4692 
4693  return my_command;
4694 }
4695 
4696 static void
4697 ConditionError(const char *desc, int cmdn, const char *msg)
4698 {
4699  fprintf(stderr,
4700  "condition error in script \"%s\" command %d: %s\n",
4701  desc, cmdn, msg);
4702  exit(1);
4703 }
4704 
4705 /*
4706  * Partial evaluation of conditionals before recording and running the script.
4707  */
4708 static void
4710 {
4711  /* statically check conditional structure */
4713  int i;
4714 
4715  for (i = 0; ps.commands[i] != NULL; i++)
4716  {
4717  Command *cmd = ps.commands[i];
4718 
4719  if (cmd->type == META_COMMAND)
4720  {
4721  switch (cmd->meta)
4722  {
4723  case META_IF:
4725  break;
4726  case META_ELIF:
4727  if (conditional_stack_empty(cs))
4728  ConditionError(ps.desc, i + 1, "\\elif without matching \\if");
4730  ConditionError(ps.desc, i + 1, "\\elif after \\else");
4731  break;
4732  case META_ELSE:
4733  if (conditional_stack_empty(cs))
4734  ConditionError(ps.desc, i + 1, "\\else without matching \\if");
4736  ConditionError(ps.desc, i + 1, "\\else after \\else");
4738  break;
4739  case META_ENDIF:
4740  if (!conditional_stack_pop(cs))
4741  ConditionError(ps.desc, i + 1, "\\endif without matching \\if");
4742  break;
4743  default:
4744  /* ignore anything else... */
4745  break;
4746  }
4747  }
4748  }
4749  if (!conditional_stack_empty(cs))
4750  ConditionError(ps.desc, i + 1, "\\if without matching \\endif");
4752 }
4753 
4754 /*
4755  * Parse a script (either the contents of a file, or a built-in script)
4756  * and add it to the list of scripts.
4757  */
4758 static void
4759 ParseScript(const char *script, const char *desc, int weight)
4760 {
4761  ParsedScript ps;
4762  PsqlScanState sstate;
4763  PQExpBufferData line_buf;
4764  int alloc_num;
4765  int index;
4766  int lineno;
4767  int start_offset;
4768 
4769 #define COMMANDS_ALLOC_NUM 128
4770  alloc_num = COMMANDS_ALLOC_NUM;
4771 
4772  /* Initialize all fields of ps */
4773  ps.desc = desc;
4774  ps.weight = weight;
4775  ps.commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
4776  initStats(&ps.stats, 0);
4777 
4778  /* Prepare to parse script */
4779  sstate = psql_scan_create(&pgbench_callbacks);
4780 
4781  /*
4782  * Ideally, we'd scan scripts using the encoding and stdstrings settings
4783  * we get from a DB connection. However, without major rearrangement of
4784  * pgbench's argument parsing, we can't have a DB connection at the time
4785  * we parse scripts. Using SQL_ASCII (encoding 0) should work well enough
4786  * with any backend-safe encoding, though conceivably we could be fooled
4787  * if a script file uses a client-only encoding. We also assume that
4788  * stdstrings should be true, which is a bit riskier.
4789  */
4790  psql_scan_setup(sstate, script, strlen(script), 0, true);
4791  start_offset = expr_scanner_offset(sstate) - 1;
4792 
4793  initPQExpBuffer(&line_buf);
4794 
4795  index = 0;
4796 
4797  for (;;)
4798  {
4799  PsqlScanResult sr;
4800  promptStatus_t prompt;
4801  Command *command = NULL;
4802 
4803  resetPQExpBuffer(&line_buf);
4804  lineno = expr_scanner_get_lineno(sstate, start_offset);
4805 
4806  sr = psql_scan(sstate, &line_buf, &prompt);
4807 
4808  /* If we collected a new SQL command, process that */
4809  command = create_sql_command(&line_buf, desc);
4810 
4811  /* store new command */
4812  if (command)
4813  ps.commands[index++] = command;
4814 
4815  /* If we reached a backslash, process that */
4816  if (sr == PSCAN_BACKSLASH)
4817  {
4818  command = process_backslash_command(sstate, desc);
4819 
4820  if (command)
4821  {
4822  /*
4823  * If this is gset, merge into the preceding command. (We
4824  * don't use a command slot in this case).
4825  */
4826  if (command->meta == META_GSET)
4827  {
4828  Command *cmd;
4829 
4830  if (index == 0)
4831  syntax_error(desc, lineno, NULL, NULL,
4832  "\\gset must follow a SQL command",
4833  NULL, -1);
4834 
4835  cmd = ps.commands[index - 1];
4836 
4837  if (cmd->type != SQL_COMMAND ||
4838  cmd->varprefix != NULL)
4839  syntax_error(desc, lineno, NULL, NULL,
4840  "\\gset must follow a SQL command",
4841  cmd->first_line, -1);
4842 
4843  /* get variable prefix */
4844  if (command->argc <= 1 || command->argv[1][0] == '\0')
4845  cmd->varprefix = pg_strdup("");
4846  else
4847  cmd->varprefix = pg_strdup(command->argv[1]);
4848 
4849  /* cleanup unused command */
4850  free_command(command);
4851 
4852  continue;
4853  }
4854 
4855  /* Attach any other backslash command as a new command */
4856  ps.commands[index++] = command;
4857  }
4858  }
4859 
4860  /*
4861  * Since we used a command slot, allocate more if needed. Note we
4862  * always allocate one more in order to accommodate the NULL
4863  * terminator below.
4864  */
4865  if (index >= alloc_num)
4866  {
4867  alloc_num += COMMANDS_ALLOC_NUM;
4868  ps.commands = (Command **)
4869  pg_realloc(ps.commands, sizeof(Command *) * alloc_num);
4870  }
4871 
4872  /* Done if we reached EOF */
4873  if (sr == PSCAN_INCOMPLETE || sr == PSCAN_EOL)
4874  break;
4875  }
4876 
4877  ps.commands[index] = NULL;
4878 
4879  addScript(ps);
4880 
4881  termPQExpBuffer(&line_buf);
4882  psql_scan_finish(sstate);
4883  psql_scan_destroy(sstate);
4884 }
4885 
4886 /*
4887  * Read the entire contents of file fd, and return it in a malloc'd buffer.
4888  *
4889  * The buffer will typically be larger than necessary, but we don't care
4890  * in this program, because we'll free it as soon as we've parsed the script.
4891  */
4892 static char *
4894 {
4895  char *buf;
4896  size_t buflen = BUFSIZ;
4897  size_t used = 0;
4898 
4899  buf = (char *) pg_malloc(buflen);
4900 
4901  for (;;)
4902  {
4903  size_t nread;
4904 
4905  nread = fread(buf + used, 1, BUFSIZ, fd);
4906  used += nread;
4907  /* If fread() read less than requested, must be EOF or error */
4908  if (nread < BUFSIZ)
4909  break;
4910  /* Enlarge buf so we can read some more */
4911  buflen += BUFSIZ;
4912  buf = (char *) pg_realloc(buf, buflen);
4913  }
4914  /* There is surely room for a terminator */
4915  buf[used] = '\0';
4916 
4917  return buf;
4918 }
4919 
4920 /*
4921  * Given a file name, read it and add its script to the list.
4922  * "-" means to read stdin.
4923  * NB: filename must be storage that won't disappear.
4924  */
4925 static void
4926 process_file(const char *filename, int weight)
4927 {
4928  FILE *fd;
4929  char *buf;
4930 
4931  /* Slurp the file contents into "buf" */
4932  if (strcmp(filename, "-") == 0)
4933  fd = stdin;
4934  else if ((fd = fopen(filename, "r")) == NULL)
4935  {
4936  fprintf(stderr, "could not open file \"%s\": %s\n",
4937  filename, strerror(errno));
4938  exit(1);
4939  }
4940 
4941  buf = read_file_contents(fd);
4942 
4943  if (ferror(fd))
4944  {
4945  fprintf(stderr, "could not read file \"%s\": %s\n",
4946  filename, strerror(errno));
4947  exit(1);
4948  }
4949 
4950  if (fd != stdin)
4951  fclose(fd);
4952 
4953  ParseScript(buf, filename, weight);
4954 
4955  free(buf);
4956 }
4957 
4958 /* Parse the given builtin script and add it to the list. */
4959 static void
4960 process_builtin(const BuiltinScript *bi, int weight)
4961 {
4962  ParseScript(bi->script, bi->desc, weight);
4963 }
4964 
4965 /* show available builtin scripts */
4966 static void
4968 {
4969  int i;
4970 
4971  fprintf(stderr, "Available builtin scripts:\n");
4972  for (i = 0; i < lengthof(builtin_script); i++)
4973  fprintf(stderr, " %13s: %s\n", builtin_script[i].name, builtin_script[i].desc);
4974  fprintf(stderr, "\n");
4975 }
4976 
4977 /* return builtin script "name" if unambiguous, fails if not found */
4978 static const BuiltinScript *
4979 findBuiltin(const char *name)
4980 {
4981  int i,
4982  found = 0,
4983  len = strlen(name);
4984  const BuiltinScript *result = NULL;
4985 
4986  for (i = 0; i < lengthof(builtin_script); i++)
4987  {
4988  if (strncmp(builtin_script[i].name, name, len) == 0)
4989  {
4990  result = &builtin_script[i];
4991  found++;
4992  }
4993  }
4994 
4995  /* ok, unambiguous result */
4996  if (found == 1)
4997  return result;
4998 
4999  /* error cases */
5000  if (found == 0)
5001  fprintf(stderr, "no builtin script found for name \"%s\"\n", name);
5002  else /* found > 1 */
5003  fprintf(stderr,
5004  "ambiguous builtin name: %d builtin scripts found for prefix \"%s\"\n", found, name);
5005 
5007  exit(1);
5008 }
5009 
5010 /*
5011  * Determine the weight specification from a script option (-b, -f), if any,
5012  * and return it as an integer (1 is returned if there's no weight). The
5013  * script name is returned in *script as a malloc'd string.
5014  */
5015 static int
5016 parseScriptWeight(const char *option, char **script)
5017 {
5018  char *sep;
5019  int weight;
5020 
5021  if ((sep = strrchr(option, WSEP)))
5022  {
5023  int namelen = sep - option;
5024  long wtmp;
5025  char *badp;
5026 
5027  /* generate the script name */
5028  *script = pg_malloc(namelen + 1);
5029  strncpy(*script, option, namelen);
5030  (*script)[namelen] = '\0';
5031 
5032  /* process digits of the weight spec */
5033  errno = 0;
5034  wtmp = strtol(sep + 1, &badp, 10);
5035  if (errno != 0 || badp == sep + 1 || *badp != '\0')
5036  {
5037  fprintf(stderr, "invalid weight specification: %s\n", sep);
5038  exit(1);
5039  }
5040  if (wtmp > INT_MAX || wtmp < 0)
5041  {
5042  fprintf(stderr,
5043  "weight specification out of range (0 .. %u): " INT64_FORMAT "\n",
5044  INT_MAX, (int64) wtmp);
5045  exit(1);
5046  }
5047  weight = wtmp;
5048  }
5049  else
5050  {
5051  *script = pg_strdup(option);
5052  weight = 1;
5053  }
5054 
5055  return weight;
5056 }
5057 
5058 /* append a script to the list of scripts to process */
5059 static void
5061 {
5062  if (script.commands == NULL || script.commands[0] == NULL)
5063  {
5064  fprintf(stderr, "empty command list for script \"%s\"\n", script.desc);
5065  exit(1);
5066  }
5067 
5068  if (num_scripts >= MAX_SCRIPTS)
5069  {
5070  fprintf(stderr, "at most %d SQL scripts are allowed\n", MAX_SCRIPTS);
5071  exit(1);
5072  }
5073 
5074  CheckConditional(script);
5075 
5076  sql_script[num_scripts] = script;
5077  num_scripts++;
5078 }
5079 
5080 /*
5081  * Print progress report.
5082  *
5083  * On entry, *last and *last_report contain the statistics and time of last
5084  * progress report. On exit, they are updated with the new stats.
5085  */
5086 static void
5087 printProgressReport(TState *threads, int64 test_start, int64 now,
5088  StatsData *last, int64 *last_report)
5089 {
5090  /* generate and show report */
5091  int64 run = now - *last_report,
5092  ntx;
5093  double tps,
5094  total_run,
5095  latency,
5096  sqlat,
5097  lag,
5098  stdev;
5099  char tbuf[315];
5100  StatsData cur;
5101 
5102  /*
5103  * Add up the statistics of all threads.
5104  *
5105  * XXX: No locking. There is no guarantee that we get an atomic snapshot
5106  * of the transaction count and latencies, so these figures can well be
5107  * off by a small amount. The progress report's purpose is to give a
5108  * quick overview of how the test is going, so that shouldn't matter too
5109  * much. (If a read from a 64-bit integer is not atomic, you might get a
5110  * "torn" read and completely bogus latencies though!)
5111  */
5112  initStats(&cur, 0);
5113  for (int i = 0; i < nthreads; i++)
5114  {
5115  mergeSimpleStats(&cur.latency, &threads[i].stats.latency);
5116  mergeSimpleStats(&cur.lag, &threads[i].stats.lag);
5117  cur.cnt += threads[i].stats.cnt;
5118  cur.skipped += threads[i].stats.skipped;
5119  }
5120 
5121  /* we count only actually executed transactions */
5122  ntx = (cur.cnt - cur.skipped) - (last->cnt - last->skipped);
5123  total_run = (now - test_start) / 1000000.0;
5124  tps = 1000000.0 * ntx / run;
5125  if (ntx > 0)
5126  {
5127  latency = 0.001 * (cur.latency.sum - last->latency.sum) / ntx;
5128  sqlat = 1.0 * (cur.latency.sum2 - last->latency.sum2) / ntx;
5129  stdev = 0.001 * sqrt(sqlat - 1000000.0 * latency * latency);
5130  lag = 0.001 * (cur.lag.sum - last->lag.sum) / ntx;
5131  }
5132  else
5133  {
5134  latency = sqlat = stdev = lag = 0;
5135  }
5136 
5137  if (progress_timestamp)
5138  {
5139  /*
5140  * On some platforms the current system timestamp is available in
5141  * now_time, but rather than get entangled with that, we just eat the
5142  * cost of an extra syscall in all cases.
5143  */
5144  struct timeval tv;
5145 
5146  gettimeofday(&tv, NULL);
5147  snprintf(tbuf, sizeof(tbuf), "%ld.%03ld s",
5148  (long) tv.tv_sec, (long) (tv.tv_usec / 1000));
5149  }
5150  else
5151  {
5152  /* round seconds are expected, but the thread may be late */
5153  snprintf(tbuf, sizeof(tbuf), "%.1f s", total_run);
5154  }
5155 
5156  fprintf(stderr,
5157  "progress: %s, %.1f tps, lat %.3f ms stddev %.3f",
5158  tbuf, tps, latency, stdev);
5159 
5160  if (throttle_delay)
5161  {
5162  fprintf(stderr, ", lag %.3f ms", lag);
5163  if (latency_limit)
5164  fprintf(stderr, ", " INT64_FORMAT " skipped",
5165  cur.skipped - last->skipped);
5166  }
5167  fprintf(stderr, "\n");
5168 
5169  *last = cur;
5170  *last_report = now;
5171 }
5172 
5173 static void
5174 printSimpleStats(const char *prefix, SimpleStats *ss)
5175 {
5176  if (ss->count > 0)
5177  {
5178  double latency = ss->sum / ss->count;
5179  double stddev = sqrt(ss->sum2 / ss->count - latency * latency);
5180 
5181  printf("%s average = %.3f ms\n", prefix, 0.001 * latency);
5182  printf("%s stddev = %.3f ms\n", prefix, 0.001 * stddev);
5183  }
5184 }
5185 
5186 /* print out results */
5187 static void
5188 printResults(StatsData *total, instr_time total_time,
5189  instr_time conn_total_time, int64 latency_late)
5190 {
5191  double time_include,
5192  tps_include,
5193  tps_exclude;
5194  int64 ntx = total->cnt - total->skipped;
5195 
5196  time_include = INSTR_TIME_GET_DOUBLE(total_time);
5197 
5198  /* tps is about actually executed transactions */
5199  tps_include = ntx / time_include;
5200  tps_exclude = ntx /
5201  (time_include - (INSTR_TIME_GET_DOUBLE(conn_total_time) / nclients));
5202 
5203  /* Report test parameters. */
5204  printf("transaction type: %s\n",
5205  num_scripts == 1 ? sql_script[0].desc : "multiple scripts");
5206  printf("scaling factor: %d\n", scale);
5207  /* only print partitioning information if some partitioning was detected */
5208  if (partition_method != PART_NONE)
5209  printf("partition method: %s\npartitions: %d\n",
5210  PARTITION_METHOD[partition_method], partitions);
5211  printf("query mode: %s\n", QUERYMODE[querymode]);
5212  printf("number of clients: %d\n", nclients);
5213  printf("number of threads: %d\n", nthreads);
5214  if (duration <= 0)
5215  {
5216  printf("number of transactions per client: %d\n", nxacts);
5217  printf("number of transactions actually processed: " INT64_FORMAT "/%d\n",
5218  ntx, nxacts * nclients);
5219  }
5220  else
5221  {
5222  printf("duration: %d s\n", duration);
5223  printf("number of transactions actually processed: " INT64_FORMAT "\n",
5224  ntx);
5225  }
5226 
5227  /* Remaining stats are nonsensical if we failed to execute any xacts */
5228  if (total->cnt <= 0)
5229  return;
5230 
5231  if (throttle_delay && latency_limit)
5232  printf("number of transactions skipped: " INT64_FORMAT " (%.3f %%)\n",
5233  total->skipped,
5234  100.0 * total->skipped / total->cnt);
5235 
5236  if (latency_limit)
5237  printf("number of transactions above the %.1f ms latency limit: " INT64_FORMAT "/" INT64_FORMAT " (%.3f %%)\n",
5238  latency_limit / 1000.0, latency_late, ntx,
5239  (ntx > 0) ? 100.0 * latency_late / ntx : 0.0);
5240 
5241  if (throttle_delay || progress || latency_limit)
5242  printSimpleStats("latency", &total->latency);
5243  else
5244  {
5245  /* no measurement, show average latency computed from run time */
5246  printf("latency average = %.3f ms\n",
5247  1000.0 * time_include * nclients / total->cnt);
5248  }
5249 
5250  if (throttle_delay)
5251  {
5252  /*
5253  * Report average transaction lag under rate limit throttling. This
5254  * is the delay between scheduled and actual start times for the
5255  * transaction. The measured lag may be caused by thread/client load,
5256  * the database load, or the Poisson throttling process.
5257  */
5258  printf("rate limit schedule lag: avg %.3f (max %.3f) ms\n",
5259  0.001 * total->lag.sum / total->cnt, 0.001 * total->lag.max);
5260  }
5261 
5262  printf("tps = %f (including connections establishing)\n", tps_include);
5263  printf("tps = %f (excluding connections establishing)\n", tps_exclude);
5264 
5265  /* Report per-script/command statistics */
5266  if (per_script_stats || report_per_command)
5267  {
5268  int i;
5269 
5270  for (i = 0; i < num_scripts; i++)
5271  {
5272  if (per_script_stats)
5273  {
5274  StatsData *sstats = &sql_script[i].stats;
5275 
5276  printf("SQL script %d: %s\n"
5277  " - weight: %d (targets %.1f%% of total)\n"
5278  " - " INT64_FORMAT " transactions (%.1f%% of total, tps = %f)\n",
5279  i + 1, sql_script[i].desc,
5280  sql_script[i].weight,
5281  100.0 * sql_script[i].weight / total_weight,
5282  sstats->cnt,
5283  100.0 * sstats->cnt / total->cnt,
5284  (sstats->cnt - sstats->skipped) / time_include);
5285 
5286  if (throttle_delay && latency_limit && sstats->cnt > 0)
5287  printf(" - number of transactions skipped: " INT64_FORMAT " (%.3f%%)\n",
5288  sstats->skipped,
5289  100.0 * sstats->skipped / sstats->cnt);
5290 
5291  printSimpleStats(" - latency", &sstats->latency);
5292  }
5293 
5294  /* Report per-command latencies */
5295  if (report_per_command)
5296  {
5297  Command **commands;
5298 
5299  if (per_script_stats)
5300  printf(" - statement latencies in milliseconds:\n");
5301  else
5302  printf("statement latencies in milliseconds:\n");
5303 
5304  for (commands = sql_script[i].commands;
5305  *commands != NULL;
5306  commands++)
5307  {
5308  SimpleStats *cstats = &(*commands)->stats;
5309 
5310  printf(" %11.3f %s\n",
5311  (cstats->count > 0) ?
5312  1000.0 * cstats->sum / cstats->count : 0.0,
5313  (*commands)->first_line);
5314  }
5315  }
5316  }
5317  }
5318 }
5319 
5320 /*
5321  * Set up a random seed according to seed parameter (NULL means default),
5322  * and initialize base_random_sequence for use in initializing other sequences.
5323  */
5324 static bool
5325 set_random_seed(const char *seed)
5326 {
5327  uint64 iseed;
5328 
5329  if (seed == NULL || strcmp(seed, "time") == 0)
5330  {
5331  /* rely on current time */
5332  instr_time now;
5333 
5335  iseed = (uint64) INSTR_TIME_GET_MICROSEC(now);
5336  }
5337  else if (strcmp(seed, "rand") == 0)
5338  {
5339  /* use some "strong" random source */
5340  if (!pg_strong_random(&iseed, sizeof(iseed)))
5341  {
5342  fprintf(stderr, "could not generate random seed.\n");
5343  return false;
5344  }
5345  }
5346  else
5347  {
5348  /* parse unsigned-int seed value */
5349  unsigned long ulseed;
5350  char garbage;
5351 
5352  /* Don't try to use UINT64_FORMAT here; it might not work for sscanf */
5353  if (sscanf(seed, "%lu%c", &ulseed, &garbage) != 1)
5354  {
5355  fprintf(stderr,
5356  "unrecognized random seed option \"%s\": expecting an unsigned integer, \"time\" or \"rand\"\n",
5357  seed);
5358  return false;
5359  }
5360  iseed = (uint64) ulseed;
5361  }
5362 
5363  if (seed != NULL)
5364  fprintf(stderr, "setting random seed to " UINT64_FORMAT "\n", iseed);
5365  random_seed = iseed;
5366 
5367  /* Fill base_random_sequence with low-order bits of seed */
5368  base_random_sequence.xseed[0] = iseed & 0xFFFF;
5369  base_random_sequence.xseed[1] = (iseed >> 16) & 0xFFFF;
5370  base_random_sequence.xseed[2] = (iseed >> 32) & 0xFFFF;
5371 
5372  return true;
5373 }
5374 
5375 int
5376 main(int argc, char **argv)
5377 {
5378  static struct option long_options[] = {
5379  /* systematic long/short named options */
5380  {"builtin", required_argument, NULL, 'b'},
5381  {"client", required_argument, NULL, 'c'},
5382  {"connect", no_argument, NULL, 'C'},
5383  {"debug", no_argument, NULL, 'd'},
5384  {"define", required_argument, NULL, 'D'},
5385  {"file", required_argument, NULL, 'f'},
5386  {"fillfactor", required_argument, NULL, 'F'},
5387  {"host", required_argument, NULL, 'h'},
5388  {"initialize", no_argument, NULL, 'i'},
5389  {"init-steps", required_argument, NULL, 'I'},
5390  {"jobs", required_argument, NULL, 'j'},
5391  {"log", no_argument, NULL, 'l'},
5392  {"latency-limit", required_argument, NULL, 'L'},
5393  {"no-vacuum", no_argument, NULL, 'n'},
5394  {"port", required_argument, NULL, 'p'},
5395  {"progress", required_argument, NULL, 'P'},
5396  {"protocol", required_argument, NULL, 'M'},
5397  {"quiet", no_argument, NULL, 'q'},
5398  {"report-latencies", no_argument, NULL, 'r'},
5399  {"rate", required_argument, NULL, 'R'},
5400  {"scale", required_argument, NULL, 's'},
5401  {"select-only", no_argument, NULL, 'S'},
5402  {"skip-some-updates", no_argument, NULL, 'N'},
5403  {"time", required_argument, NULL, 'T'},
5404  {"transactions", required_argument, NULL, 't'},
5405  {"username", required_argument, NULL, 'U'},
5406  {"vacuum-all", no_argument, NULL, 'v'},
5407  /* long-named only options */
5408  {"unlogged-tables", no_argument, NULL, 1},
5409  {"tablespace", required_argument, NULL, 2},
5410  {"index-tablespace", required_argument, NULL, 3},
5411  {"sampling-rate", required_argument, NULL, 4},
5412  {"aggregate-interval", required_argument, NULL, 5},
5413  {"progress-timestamp", no_argument, NULL, 6},
5414  {"log-prefix", required_argument, NULL, 7},
5415  {"foreign-keys", no_argument, NULL, 8},
5416  {"random-seed", required_argument, NULL, 9},
5417  {"show-script", required_argument, NULL, 10},
5418  {"partitions", required_argument, NULL, 11},
5419  {"partition-method", required_argument, NULL, 12},
5420  {NULL, 0, NULL, 0}
5421  };
5422 
5423  int c;
5424  bool is_init_mode = false; /* initialize mode? */
5425  char *initialize_steps = NULL;
5426  bool foreign_keys = false;
5427  bool is_no_vacuum = false;
5428  bool do_vacuum_accounts = false; /* vacuum accounts table? */
5429  int optindex;
5430  bool scale_given = false;
5431 
5432  bool benchmarking_option_set = false;
5433  bool initialization_option_set = false;
5434  bool internal_script_used = false;
5435 
5436  CState *state; /* status of clients */
5437  TState *threads; /* array of thread */
5438 
5439  instr_time start_time; /* start up time */
5440  instr_time total_time;
5441  instr_time conn_total_time;
5442  int64 latency_late = 0;
5443  StatsData stats;
5444  int weight;
5445 
5446  int i;
5447  int nclients_dealt;
5448 
5449 #ifdef HAVE_GETRLIMIT
5450  struct rlimit rlim;
5451 #endif
5452 
5453  PGconn *con;
5454  char *env;
5455 
5456  int exit_code = 0;
5457 
5458  pg_logging_init(argv[0]);
5459  progname = get_progname(argv[0]);
5460 
5461  if (argc > 1)
5462  {
5463  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
5464  {
5465  usage();
5466  exit(0);
5467  }
5468  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
5469  {
5470  puts("pgbench (PostgreSQL) " PG_VERSION);
5471  exit(0);
5472  }
5473  }
5474 
5475  if ((env = getenv("PGHOST")) != NULL && *env != '\0')
5476  pghost = env;
5477  if ((env = getenv("PGPORT")) != NULL && *env != '\0')
5478  pgport = env;
5479  else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
5480  login = env;
5481 
5482  state = (CState *) pg_malloc0(sizeof(CState));
5483 
5484  /* set random seed early, because it may be used while parsing scripts. */
5485  if (!set_random_seed(getenv("PGBENCH_RANDOM_SEED")))
5486  {
5487  fprintf(stderr, "error while setting random seed from PGBENCH_RANDOM_SEED environment variable\n");
5488  exit(1);
5489  }
5490 
5491  while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1)
5492  {
5493  char *script;
5494 
5495  switch (c)
5496  {
5497  case 'i':
5498  is_init_mode = true;
5499  break;
5500  case 'I':
5501  if (initialize_steps)
5502  pg_free(initialize_steps);
5503  initialize_steps = pg_strdup(optarg);
5504  checkInitSteps(initialize_steps);
5505  initialization_option_set = true;
5506  break;
5507  case 'h':
5508  pghost = pg_strdup(optarg);
5509  break;
5510  case 'n':
5511  is_no_vacuum = true;
5512  break;
5513  case 'v':
5514  benchmarking_option_set = true;
5515  do_vacuum_accounts = true;
5516  break;
5517  case 'p':
5518  pgport = pg_strdup(optarg);
5519  break;
5520  case 'd':
5521  debug++;
5522  break;
5523  case 'c':
5524  benchmarking_option_set = true;
5525  nclients = atoi(optarg);
5526  if (nclients <= 0)
5527  {
5528  fprintf(stderr, "invalid number of clients: \"%s\"\n",
5529  optarg);
5530  exit(1);
5531  }
5532 #ifdef HAVE_GETRLIMIT
5533 #ifdef RLIMIT_NOFILE /* most platforms use RLIMIT_NOFILE */
5534  if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
5535 #else /* but BSD doesn't ... */
5536  if (getrlimit(RLIMIT_OFILE, &rlim) == -1)
5537 #endif /* RLIMIT_NOFILE */
5538  {
5539  fprintf(stderr, "getrlimit failed: %s\n", strerror(errno));
5540  exit(1);
5541  }
5542  if (rlim.rlim_cur < nclients + 3)
5543  {
5544  fprintf(stderr, "need at least %d open files, but system limit is %ld\n",
5545  nclients + 3, (long) rlim.rlim_cur);
5546  fprintf(stderr, "Reduce number of clients, or use limit/ulimit to increase the system limit.\n");
5547  exit(1);
5548  }
5549 #endif /* HAVE_GETRLIMIT */
5550  break;
5551  case 'j': /* jobs */
5552  benchmarking_option_set = true;
5553  nthreads = atoi(optarg);
5554  if (nthreads <= 0)
5555  {
5556  fprintf(stderr, "invalid number of threads: \"%s\"\n",
5557  optarg);
5558  exit(1);
5559  }
5560 #ifndef ENABLE_THREAD_SAFETY
5561  if (nthreads != 1)
5562  {
5563  fprintf(stderr, "threads are not supported on this platform; use -j1\n");
5564  exit(1);
5565  }
5566 #endif /* !ENABLE_THREAD_SAFETY */
5567  break;
5568  case 'C':
5569  benchmarking_option_set = true;
5570  is_connect = true;
5571  break;
5572  case 'r':
5573  benchmarking_option_set = true;
5574  report_per_command = true;
5575  break;
5576  case 's':
5577  scale_given = true;
5578  scale = atoi(optarg);
5579  if (scale <= 0)
5580  {
5581  fprintf(stderr, "invalid scaling factor: \"%s\"\n", optarg);
5582  exit(1);
5583  }
5584  break;
5585  case 't':
5586  benchmarking_option_set = true;
5587  nxacts = atoi(optarg);
5588  if (nxacts <= 0)
5589  {
5590  fprintf(stderr, "invalid number of transactions: \"%s\"\n",
5591  optarg);
5592  exit(1);
5593  }
5594  break;
5595  case 'T':
5596  benchmarking_option_set = true;
5597  duration = atoi(optarg);
5598  if (duration <= 0)
5599  {
5600  fprintf(stderr, "invalid duration: \"%s\"\n", optarg);
5601  exit(1);
5602  }
5603  break;
5604  case 'U':
5605  login = pg_strdup(optarg);
5606  break;
5607  case 'l':
5608  benchmarking_option_set = true;
5609  use_log = true;
5610  break;
5611  case 'q':
5612  initialization_option_set = true;
5613  use_quiet = true;
5614  break;
5615  case 'b':
5616  if (strcmp(optarg, "list") == 0)
5617  {
5619  exit(0);
5620  }
5621  weight = parseScriptWeight(optarg, &script);
5622  process_builtin(findBuiltin(script), weight);
5623  benchmarking_option_set = true;
5624  internal_script_used = true;
5625  break;
5626  case 'S':
5627  process_builtin(findBuiltin("select-only"), 1);
5628  benchmarking_option_set = true;
5629  internal_script_used = true;
5630  break;
5631  case 'N':
5632  process_builtin(findBuiltin("simple-update"), 1);
5633  benchmarking_option_set = true;
5634  internal_script_used = true;
5635  break;
5636  case 'f':
5637  weight = parseScriptWeight(optarg, &script);
5638  process_file(script, weight);
5639  benchmarking_option_set = true;
5640  break;
5641  case 'D':
5642  {
5643  char *p;
5644 
5645  benchmarking_option_set = true;
5646 
5647  if ((p = strchr(optarg, '=')) == NULL || p == optarg || *(p + 1) == '\0')
5648  {
5649  fprintf(stderr, "invalid variable definition: \"%s\"\n",
5650  optarg);
5651  exit(1);
5652  }
5653 
5654  *p++ = '\0';
5655  if (!putVariable(&state[0], "option", optarg, p))
5656  exit(1);
5657  }
5658  break;
5659  case 'F':
5660  initialization_option_set = true;
5661  fillfactor = atoi(optarg);
5662  if (fillfactor < 10 || fillfactor > 100)
5663  {
5664  fprintf(stderr, "invalid fillfactor: \"%s\"\n", optarg);
5665  exit(1);
5666  }
5667  break;
5668  case 'M':
5669  benchmarking_option_set = true;
5670  for (querymode = 0; querymode < NUM_QUERYMODE; querymode++)
5671  if (strcmp(optarg, QUERYMODE[querymode]) == 0)
5672  break;
5673  if (querymode >= NUM_QUERYMODE)
5674  {
5675  fprintf(stderr, "invalid query mode (-M): \"%s\"\n",
5676  optarg);
5677  exit(1);
5678  }
5679  break;
5680  case 'P':
5681  benchmarking_option_set = true;
5682  progress = atoi(optarg);
5683  if (progress <= 0)
5684  {
5685  fprintf(stderr, "invalid thread progress delay: \"%s\"\n",
5686  optarg);
5687  exit(1);
5688  }
5689  break;
5690  case 'R':
5691  {
5692  /* get a double from the beginning of option value */
5693  double throttle_value = atof(optarg);
5694 
5695  benchmarking_option_set = true;
5696 
5697  if (throttle_value <= 0.0)
5698  {
5699  fprintf(stderr, "invalid rate limit: \"%s\"\n", optarg);
5700  exit(1);
5701  }
5702  /* Invert rate limit into per-transaction delay in usec */
5703  throttle_delay = 1000000.0 / throttle_value;
5704  }
5705  break;
5706  case 'L':
5707  {
5708  double limit_ms = atof(optarg);
5709 
5710  if (limit_ms <= 0.0)
5711  {
5712  fprintf(stderr, "invalid latency limit: \"%s\"\n",
5713  optarg);
5714  exit(1);
5715  }
5716  benchmarking_option_set = true;
5717  latency_limit = (int64) (limit_ms * 1000);
5718  }
5719  break;
5720  case 1: /* unlogged-tables */
5721  initialization_option_set = true;
5722  unlogged_tables = true;
5723  break;
5724  case 2: /* tablespace */
5725  initialization_option_set = true;
5726  tablespace = pg_strdup(optarg);
5727  break;
5728  case 3: /* index-tablespace */
5729  initialization_option_set = true;
5730  index_tablespace = pg_strdup(optarg);
5731  break;
5732  case 4: /* sampling-rate */
5733  benchmarking_option_set = true;
5734  sample_rate = atof(optarg);
5735  if (sample_rate <= 0.0 || sample_rate > 1.0)
5736  {
5737  fprintf(stderr, "invalid sampling rate: \"%s\"\n", optarg);
5738  exit(1);
5739  }
5740  break;
5741  case 5: /* aggregate-interval */
5742  benchmarking_option_set = true;
5743  agg_interval = atoi(optarg);
5744  if (agg_interval <= 0)
5745  {
5746  fprintf(stderr, "invalid number of seconds for aggregation: \"%s\"\n",
5747  optarg);
5748  exit(1);
5749  }
5750  break;
5751  case 6: /* progress-timestamp */
5752  progress_timestamp = true;
5753  benchmarking_option_set = true;
5754  break;
5755  case 7: /* log-prefix */
5756  benchmarking_option_set = true;
5757  logfile_prefix = pg_strdup(optarg);
5758  break;
5759  case 8: /* foreign-keys */
5760  initialization_option_set = true;
5761  foreign_keys = true;
5762  break;
5763  case 9: /* random-seed */
5764  benchmarking_option_set = true;
5765  if (!set_random_seed(optarg))
5766  {
5767  fprintf(stderr, "error while setting random seed from --random-seed option\n");
5768  exit(1);
5769  }
5770  break;
5771  case 10: /* list */
5772  {
5773  const BuiltinScript *s = findBuiltin(optarg);
5774 
5775  fprintf(stderr, "-- %s: %s\n%s\n", s->name, s->desc, s->script);
5776  exit(0);
5777  }
5778  break;
5779  case 11: /* partitions */
5780  initialization_option_set = true;
5781  partitions = atoi(optarg);
5782  if (partitions < 0)
5783  {
5784  fprintf(stderr, "invalid number of partitions: \"%s\"\n",
5785  optarg);
5786  exit(1);
5787  }
5788  break;
5789  case 12: /* partition-method */
5790  initialization_option_set = true;
5791  if (pg_strcasecmp(optarg, "range") == 0)
5792  partition_method = PART_RANGE;
5793  else if (pg_strcasecmp(optarg, "hash") == 0)
5794  partition_method = PART_HASH;
5795  else
5796  {
5797  fprintf(stderr, "invalid partition method, expecting \"range\" or \"hash\","
5798  " got: \"%s\"\n", optarg);
5799  exit(1);
5800  }
5801  break;
5802  default:
5803  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
5804  exit(1);
5805  break;
5806  }
5807  }
5808 
5809  /* set default script if none */
5810  if (num_scripts == 0 && !is_init_mode)
5811  {
5812  process_builtin(findBuiltin("tpcb-like"), 1);
5813  benchmarking_option_set = true;
5814  internal_script_used = true;
5815  }
5816 
5817  /* complete SQL command initialization and compute total weight */
5818  for (i = 0; i < num_scripts; i++)
5819  {
5820  Command **commands = sql_script[i].commands;
5821 
5822  for (int j = 0; commands[j] != NULL; j++)
5823  if (commands[j]->type == SQL_COMMAND)
5824  postprocess_sql_command(commands[j]);
5825 
5826  /* cannot overflow: weight is 32b, total_weight 64b */
5827  total_weight += sql_script[i].weight;
5828  }
5829 
5830  if (total_weight == 0 && !is_init_mode)
5831  {
5832  fprintf(stderr, "total script weight must not be zero\n");
5833  exit(1);
5834  }
5835 
5836  /* show per script stats if several scripts are used */
5837  if (num_scripts > 1)
5838  per_script_stats = true;
5839 
5840  /*
5841  * Don't need more threads than there are clients. (This is not merely an
5842  * optimization; throttle_delay is calculated incorrectly below if some
5843  * threads have no clients assigned to them.)
5844  */
5845  if (nthreads > nclients)
5846  nthreads = nclients;
5847 
5848  /*
5849  * Convert throttle_delay to a per-thread delay time. Note that this
5850  * might be a fractional number of usec, but that's OK, since it's just
5851  * the center of a Poisson distribution of delays.
5852  */
5853  throttle_delay *= nthreads;
5854 
5855  if (argc > optind)
5856  dbName = argv[optind++];
5857  else
5858  {
5859  if ((env = getenv("PGDATABASE")) != NULL && *env != '\0')
5860  dbName = env;
5861  else if (login != NULL && *login != '\0')
5862  dbName = login;
5863  else
5864  dbName = "";
5865  }
5866 
5867  if (optind < argc)
5868  {
5869  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
5870  progname, argv[optind]);
5871  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
5872  exit(1);
5873  }
5874 
5875  if (is_init_mode)
5876  {
5877  if (benchmarking_option_set)
5878  {
5879  fprintf(stderr, "some of the specified options cannot be used in initialization (-i) mode\n");
5880  exit(1);
5881  }
5882 
5883  if (partitions == 0 && partition_method != PART_NONE)
5884  {
5885  fprintf(stderr, "--partition-method requires greater than zero --partitions\n");
5886  exit(1);
5887  }