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-2025, 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#if defined(WIN32) && FD_SETSIZE < 1024
31#error FD_SETSIZE needs to have been increased
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#include <sys/resource.h> /* for getrlimit */
44
45/* For testing, PGBENCH_USE_SELECT can be defined to force use of that code */
46#if defined(HAVE_PPOLL) && !defined(PGBENCH_USE_SELECT)
47#define POLL_USING_PPOLL
48#ifdef HAVE_POLL_H
49#include <poll.h>
50#endif
51#else /* no ppoll(), so use select() */
52#define POLL_USING_SELECT
53#include <sys/select.h>
54#endif
55
56#include "catalog/pg_class_d.h"
57#include "common/int.h"
58#include "common/logging.h"
59#include "common/pg_prng.h"
60#include "common/string.h"
61#include "common/username.h"
62#include "fe_utils/cancel.h"
66#include "getopt_long.h"
67#include "libpq-fe.h"
68#include "pgbench.h"
69#include "port/pg_bitutils.h"
71
72/* X/Open (XSI) requires <math.h> to provide M_PI, but core POSIX does not */
73#ifndef M_PI
74#define M_PI 3.14159265358979323846
75#endif
76
77#define ERRCODE_T_R_SERIALIZATION_FAILURE "40001"
78#define ERRCODE_T_R_DEADLOCK_DETECTED "40P01"
79#define ERRCODE_UNDEFINED_TABLE "42P01"
80
81/*
82 * Hashing constants
83 */
84#define FNV_PRIME UINT64CONST(0x100000001b3)
85#define FNV_OFFSET_BASIS UINT64CONST(0xcbf29ce484222325)
86#define MM2_MUL UINT64CONST(0xc6a4a7935bd1e995)
87#define MM2_MUL_TIMES_8 UINT64CONST(0x35253c9ade8f4ca8)
88#define MM2_ROT 47
89
90/*
91 * Multi-platform socket set implementations
92 */
93
94#ifdef POLL_USING_PPOLL
95#define SOCKET_WAIT_METHOD "ppoll"
96
97typedef struct socket_set
98{
99 int maxfds; /* allocated length of pollfds[] array */
100 int curfds; /* number currently in use */
101 struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER];
102} socket_set;
103
104#endif /* POLL_USING_PPOLL */
105
106#ifdef POLL_USING_SELECT
107#define SOCKET_WAIT_METHOD "select"
108
109typedef struct socket_set
110{
111 int maxfd; /* largest FD currently set in fds */
112 fd_set fds;
114
115#endif /* POLL_USING_SELECT */
116
117/*
118 * Multi-platform thread implementations
119 */
120
121#ifdef WIN32
122/* Use Windows threads */
123#include <windows.h>
124#define GETERRNO() (_dosmaperr(GetLastError()), errno)
125#define THREAD_T HANDLE
126#define THREAD_FUNC_RETURN_TYPE unsigned
127#define THREAD_FUNC_RETURN return 0
128#define THREAD_FUNC_CC __stdcall
129#define THREAD_CREATE(handle, function, arg) \
130 ((*(handle) = (HANDLE) _beginthreadex(NULL, 0, (function), (arg), 0, NULL)) == 0 ? errno : 0)
131#define THREAD_JOIN(handle) \
132 (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0 ? \
133 GETERRNO() : CloseHandle(handle) ? 0 : GETERRNO())
134#define THREAD_BARRIER_T SYNCHRONIZATION_BARRIER
135#define THREAD_BARRIER_INIT(barrier, n) \
136 (InitializeSynchronizationBarrier((barrier), (n), 0) ? 0 : GETERRNO())
137#define THREAD_BARRIER_WAIT(barrier) \
138 EnterSynchronizationBarrier((barrier), \
139 SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY)
140#define THREAD_BARRIER_DESTROY(barrier)
141#else
142/* Use POSIX threads */
143#include "port/pg_pthread.h"
144#define THREAD_T pthread_t
145#define THREAD_FUNC_RETURN_TYPE void *
146#define THREAD_FUNC_RETURN return NULL
147#define THREAD_FUNC_CC
148#define THREAD_CREATE(handle, function, arg) \
149 pthread_create((handle), NULL, (function), (arg))
150#define THREAD_JOIN(handle) \
151 pthread_join((handle), NULL)
152#define THREAD_BARRIER_T pthread_barrier_t
153#define THREAD_BARRIER_INIT(barrier, n) \
154 pthread_barrier_init((barrier), NULL, (n))
155#define THREAD_BARRIER_WAIT(barrier) pthread_barrier_wait((barrier))
156#define THREAD_BARRIER_DESTROY(barrier) pthread_barrier_destroy((barrier))
157#endif
158
159
160/********************************************************************
161 * some configurable parameters */
162
163#define DEFAULT_INIT_STEPS "dtgvp" /* default -I setting */
164#define ALL_INIT_STEPS "dtgGvpf" /* all possible steps */
165
166#define LOG_STEP_SECONDS 5 /* seconds between log messages */
167#define DEFAULT_NXACTS 10 /* default nxacts */
168
169#define MIN_GAUSSIAN_PARAM 2.0 /* minimum parameter for gauss */
170
171#define MIN_ZIPFIAN_PARAM 1.001 /* minimum parameter for zipfian */
172#define MAX_ZIPFIAN_PARAM 1000.0 /* maximum parameter for zipfian */
173
174static int nxacts = 0; /* number of transactions per client */
175static int duration = 0; /* duration in seconds */
176static int64 end_time = 0; /* when to stop in micro seconds, under -T */
177
178/*
179 * scaling factor. for example, scale = 10 will make 1000000 tuples in
180 * pgbench_accounts table.
181 */
182static int scale = 1;
183
184/*
185 * fillfactor. for example, fillfactor = 90 will use only 90 percent
186 * space during inserts and leave 10 percent free.
187 */
188static int fillfactor = 100;
189
190/*
191 * use unlogged tables?
192 */
193static bool unlogged_tables = false;
194
195/*
196 * log sampling rate (1.0 = log everything, 0.0 = option not given)
197 */
198static double sample_rate = 0.0;
199
200/*
201 * When threads are throttled to a given rate limit, this is the target delay
202 * to reach that rate in usec. 0 is the default and means no throttling.
203 */
204static double throttle_delay = 0;
205
206/*
207 * Transactions which take longer than this limit (in usec) are counted as
208 * late, and reported as such, although they are completed anyway. When
209 * throttling is enabled, execution time slots that are more than this late
210 * are skipped altogether, and counted separately.
211 */
213
214/*
215 * tablespace selection
216 */
217static char *tablespace = NULL;
218static char *index_tablespace = NULL;
219
220/*
221 * Number of "pgbench_accounts" partitions. 0 is the default and means no
222 * partitioning.
223 */
224static int partitions = 0;
225
226/* partitioning strategy for "pgbench_accounts" */
227typedef enum
228{
229 PART_NONE, /* no partitioning */
230 PART_RANGE, /* range partitioning */
231 PART_HASH, /* hash partitioning */
233
235static const char *const PARTITION_METHOD[] = {"none", "range", "hash"};
236
237/* random seed used to initialize base_random_sequence */
238static int64 random_seed = -1;
239
240/*
241 * end of configurable parameters
242 *********************************************************************/
243
244#define nbranches 1 /* Makes little sense to change this. Change
245 * -s instead */
246#define ntellers 10
247#define naccounts 100000
248
249/*
250 * The scale factor at/beyond which 32bit integers are incapable of storing
251 * 64bit values.
252 *
253 * Although the actual threshold is 21474, we use 20000 because it is easier to
254 * document and remember, and isn't that far away from the real threshold.
255 */
256#define SCALE_32BIT_THRESHOLD 20000
258static bool use_log; /* log transaction latencies to a file */
259static bool use_quiet; /* quiet logging onto stderr */
260static int agg_interval; /* log aggregates instead of individual
261 * transactions */
262static bool per_script_stats = false; /* whether to collect stats per script */
263static int progress = 0; /* thread progress report every this seconds */
264static bool progress_timestamp = false; /* progress report with Unix time */
265static int nclients = 1; /* number of clients */
266static int nthreads = 1; /* number of threads */
267static bool is_connect; /* establish connection for each transaction */
268static bool report_per_command = false; /* report per-command latencies,
269 * retries after errors and failures
270 * (errors without retrying) */
271static int main_pid; /* main process id used in log filename */
272
273/*
274 * There are different types of restrictions for deciding that the current
275 * transaction with a serialization/deadlock error can no longer be retried and
276 * should be reported as failed:
277 * - max_tries (--max-tries) can be used to limit the number of tries;
278 * - latency_limit (-L) can be used to limit the total time of tries;
279 * - duration (-T) can be used to limit the total benchmark time.
280 *
281 * They can be combined together, and you need to use at least one of them to
282 * retry the transactions with serialization/deadlock errors. If none of them is
283 * used, the default value of max_tries is 1 and such transactions will not be
284 * retried.
285 */
286
287/*
288 * We cannot retry a transaction after the serialization/deadlock error if its
289 * number of tries reaches this maximum; if its value is zero, it is not used.
290 */
291static uint32 max_tries = 1;
293static bool failures_detailed = false; /* whether to group failures in
294 * reports or logs by basic types */
296static const char *pghost = NULL;
297static const char *pgport = NULL;
298static const char *username = NULL;
299static const char *dbName = NULL;
300static char *logfile_prefix = NULL;
301static const char *progname;
303#define WSEP '@' /* weight separator */
305static volatile sig_atomic_t timer_exceeded = false; /* flag from signal
306 * handler */
307
308/*
309 * We don't want to allocate variables one by one; for efficiency, add a
310 * constant margin each time it overflows.
311 */
312#define VARIABLES_ALLOC_MARGIN 8
313
314/*
315 * Variable definitions.
316 *
317 * If a variable only has a string value, "svalue" is that value, and value is
318 * "not set". If the value is known, "value" contains the value (in any
319 * variant).
320 *
321 * In this case "svalue" contains the string equivalent of the value, if we've
322 * had occasion to compute that, or NULL if we haven't.
323 */
324typedef struct
326 char *name; /* variable's name */
327 char *svalue; /* its value in string form, if known */
328 PgBenchValue value; /* actual variable's value */
329} Variable;
330
331/*
332 * Data structure for client variables.
333 */
334typedef struct
336 Variable *vars; /* array of variable definitions */
337 int nvars; /* number of variables */
338
339 /*
340 * The maximum number of variables that we can currently store in 'vars'
341 * without having to reallocate more space. We must always have max_vars
342 * >= nvars.
343 */
344 int max_vars;
346 bool vars_sorted; /* are variables sorted by name? */
347} Variables;
349#define MAX_SCRIPTS 128 /* max number of SQL scripts allowed */
350#define SHELL_COMMAND_SIZE 256 /* maximum size allowed for shell command */
351
352/*
353 * Simple data structure to keep stats about something.
354 *
355 * XXX probably the first value should be kept and used as an offset for
356 * better numerical stability...
357 */
358typedef struct SimpleStats
360 int64 count; /* how many values were encountered */
361 double min; /* the minimum seen */
362 double max; /* the maximum seen */
363 double sum; /* sum of values */
364 double sum2; /* sum of squared values */
366
367/*
368 * The instr_time type is expensive when dealing with time arithmetic. Define
369 * a type to hold microseconds instead. Type int64 is good enough for about
370 * 584500 years.
371 */
372typedef int64 pg_time_usec_t;
373
374/*
375 * Data structure to hold various statistics: per-thread and per-script stats
376 * are maintained and merged together.
377 */
378typedef struct StatsData
380 pg_time_usec_t start_time; /* interval start time, for aggregates */
381
382 /*----------
383 * Transactions are counted depending on their execution and outcome.
384 * First a transaction may have started or not: skipped transactions occur
385 * under --rate and --latency-limit when the client is too late to execute
386 * them. Secondly, a started transaction may ultimately succeed or fail,
387 * possibly after some retries when --max-tries is not one. Thus
388 *
389 * the number of all transactions =
390 * 'skipped' (it was too late to execute them) +
391 * 'cnt' (the number of successful transactions) +
392 * 'failed' (the number of failed transactions).
393 *
394 * A successful transaction can have several unsuccessful tries before a
395 * successful run. Thus
396 *
397 * 'cnt' (the number of successful transactions) =
398 * successfully retried transactions (they got a serialization or a
399 * deadlock error(s), but were
400 * successfully retried from the very
401 * beginning) +
402 * directly successful transactions (they were successfully completed on
403 * the first try).
404 *
405 * A failed transaction is defined as unsuccessfully retried transactions.
406 * It can be one of two types:
407 *
408 * failed (the number of failed transactions) =
409 * 'serialization_failures' (they got a serialization error and were not
410 * successfully retried) +
411 * 'deadlock_failures' (they got a deadlock error and were not
412 * successfully retried).
413 *
414 * If the transaction was retried after a serialization or a deadlock
415 * error this does not guarantee that this retry was successful. Thus
416 *
417 * 'retries' (number of retries) =
418 * number of retries in all retried transactions =
419 * number of retries in (successfully retried transactions +
420 * failed transactions);
421 *
422 * 'retried' (number of all retried transactions) =
423 * successfully retried transactions +
424 * failed transactions.
425 *----------
426 */
427 int64 cnt; /* number of successful transactions, not
428 * including 'skipped' */
429 int64 skipped; /* number of transactions skipped under --rate
430 * and --latency-limit */
431 int64 retries; /* number of retries after a serialization or
432 * a deadlock error in all the transactions */
433 int64 retried; /* number of all transactions that were
434 * retried after a serialization or a deadlock
435 * error (perhaps the last try was
436 * unsuccessful) */
437 int64 serialization_failures; /* number of transactions that were
438 * not successfully retried after a
439 * serialization error */
440 int64 deadlock_failures; /* number of transactions that were not
441 * successfully retried after a deadlock
442 * error */
445} StatsData;
446
447/*
448 * For displaying Unix epoch timestamps, as some time functions may have
449 * another reference.
450 */
452
453/*
454 * Error status for errors during script execution.
455 */
456typedef enum EStatus
460
461 /* SQL errors */
465} EStatus;
466
467/*
468 * Transaction status at the end of a command.
469 */
470typedef enum TStatus
476} TStatus;
477
478/* Various random sequences are initialized from this one. */
480
481/* Synchronization barrier for start and connection */
483
484/*
485 * Connection state machine states.
486 */
487typedef enum
488{
489 /*
490 * The client must first choose a script to execute. Once chosen, it can
491 * either be throttled (state CSTATE_PREPARE_THROTTLE under --rate), start
492 * right away (state CSTATE_START_TX) or not start at all if the timer was
493 * exceeded (state CSTATE_FINISHED).
494 */
496
497 /*
498 * CSTATE_START_TX performs start-of-transaction processing. Establishes
499 * a new connection for the transaction in --connect mode, records the
500 * transaction start time, and proceed to the first command.
501 *
502 * Note: once a script is started, it will either error or run till its
503 * end, where it may be interrupted. It is not interrupted while running,
504 * so pgbench --time is to be understood as tx are allowed to start in
505 * that time, and will finish when their work is completed.
506 */
508
509 /*
510 * In CSTATE_PREPARE_THROTTLE state, we calculate when to begin the next
511 * transaction, and advance to CSTATE_THROTTLE. CSTATE_THROTTLE state
512 * sleeps until that moment, then advances to CSTATE_START_TX, or
513 * CSTATE_FINISHED if the next transaction would start beyond the end of
514 * the run.
515 */
518
519 /*
520 * We loop through these states, to process each command in the script:
521 *
522 * CSTATE_START_COMMAND starts the execution of a command. On a SQL
523 * command, the command is sent to the server, and we move to
524 * CSTATE_WAIT_RESULT state unless in pipeline mode. On a \sleep
525 * meta-command, the timer is set, and we enter the CSTATE_SLEEP state to
526 * wait for it to expire. Other meta-commands are executed immediately. If
527 * the command about to start is actually beyond the end of the script,
528 * advance to CSTATE_END_TX.
529 *
530 * CSTATE_WAIT_RESULT waits until we get a result set back from the server
531 * for the current command.
532 *
533 * CSTATE_SLEEP waits until the end of \sleep.
534 *
535 * CSTATE_END_COMMAND records the end-of-command timestamp, increments the
536 * command counter, and loops back to CSTATE_START_COMMAND state.
537 *
538 * CSTATE_SKIP_COMMAND is used by conditional branches which are not
539 * executed. It quickly skip commands that do not need any evaluation.
540 * This state can move forward several commands, till there is something
541 * to do or the end of the script.
542 */
548
549 /*
550 * States for failed commands.
551 *
552 * If the SQL/meta command fails, in CSTATE_ERROR clean up after an error:
553 * (1) clear the conditional stack; (2) if we have an unterminated
554 * (possibly failed) transaction block, send the rollback command to the
555 * server and wait for the result in CSTATE_WAIT_ROLLBACK_RESULT. If
556 * something goes wrong with rolling back, go to CSTATE_ABORTED.
557 *
558 * But if everything is ok we are ready for future transactions: if this
559 * is a serialization or deadlock error and we can re-execute the
560 * transaction from the very beginning, go to CSTATE_RETRY; otherwise go
561 * to CSTATE_FAILURE.
562 *
563 * In CSTATE_RETRY report an error, set the same parameters for the
564 * transaction execution as in the previous tries and process the first
565 * transaction command in CSTATE_START_COMMAND.
566 *
567 * In CSTATE_FAILURE report a failure, set the parameters for the
568 * transaction execution as they were before the first run of this
569 * transaction (except for a random state) and go to CSTATE_END_TX to
570 * complete this transaction.
571 */
576
577 /*
578 * CSTATE_END_TX performs end-of-transaction processing. It calculates
579 * latency, and logs the transaction. In --connect mode, it closes the
580 * current connection.
581 *
582 * Then either starts over in CSTATE_CHOOSE_SCRIPT, or enters
583 * CSTATE_FINISHED if we have no more work to do.
584 */
586
587 /*
588 * Final states. CSTATE_ABORTED means that the script execution was
589 * aborted because a command failed, CSTATE_FINISHED means success.
590 */
594
595/*
596 * Connection state.
597 */
598typedef struct
600 PGconn *con; /* connection handle to DB */
601 int id; /* client No. */
602 ConnectionStateEnum state; /* state machine's current state. */
603 ConditionalStack cstack; /* enclosing conditionals state */
604
605 /*
606 * Separate randomness for each client. This is used for random functions
607 * PGBENCH_RANDOM_* during the execution of the script.
608 */
609 pg_prng_state cs_func_rs;
611 int use_file; /* index in sql_script for this client */
612 int command; /* command number in script */
613 int num_syncs; /* number of ongoing sync commands */
614
615 /* client variables */
616 Variables variables;
617
618 /* various times about current transaction in microseconds */
619 pg_time_usec_t txn_scheduled; /* scheduled start time of transaction */
620 pg_time_usec_t sleep_until; /* scheduled start time of next cmd */
621 pg_time_usec_t txn_begin; /* used for measuring schedule lag times */
622 pg_time_usec_t stmt_begin; /* used for measuring statement latencies */
623
624 /* whether client prepared each command of each script */
625 bool **prepared;
626
627 /*
628 * For processing failures and repeating transactions with serialization
629 * or deadlock errors:
630 */
631 EStatus estatus; /* the error status of the current transaction
632 * execution; this is ESTATUS_NO_ERROR if
633 * there were no errors */
634 pg_prng_state random_state; /* random state */
635 uint32 tries; /* how many times have we already tried the
636 * current transaction? */
637
638 /* per client collected stats */
639 int64 cnt; /* client transaction count, for -t; skipped
640 * and failed transactions are also counted
641 * here */
642} CState;
643
644/*
645 * Thread state
646 */
647typedef struct
649 int tid; /* thread id */
650 THREAD_T thread; /* thread handle */
651 CState *state; /* array of CState */
652 int nstate; /* length of state[] */
653
654 /*
655 * Separate randomness for each thread. Each thread option uses its own
656 * random state to make all of them independent of each other and
657 * therefore deterministic at the thread level.
658 */
659 pg_prng_state ts_choose_rs; /* random state for selecting a script */
660 pg_prng_state ts_throttle_rs; /* random state for transaction throttling */
661 pg_prng_state ts_sample_rs; /* random state for log sampling */
663 int64 throttle_trigger; /* previous/next throttling (us) */
664 FILE *logfile; /* where to log, or NULL */
665
666 /* per thread collected stats in microseconds */
667 pg_time_usec_t create_time; /* thread creation time */
668 pg_time_usec_t started_time; /* thread is running */
669 pg_time_usec_t bench_start; /* thread is benchmarking */
670 pg_time_usec_t conn_duration; /* cumulated connection and disconnection
671 * delays */
674 int64 latency_late; /* count executed but late transactions */
675} TState;
676
677/*
678 * queries read from files
679 */
680#define SQL_COMMAND 1
681#define META_COMMAND 2
682
683/*
684 * max number of backslash command arguments or SQL variables,
685 * including the command or SQL statement itself
686 */
687#define MAX_ARGS 256
689typedef enum MetaCommand
691 META_NONE, /* not a known meta-command */
692 META_SET, /* \set */
693 META_SETSHELL, /* \setshell */
694 META_SHELL, /* \shell */
695 META_SLEEP, /* \sleep */
696 META_GSET, /* \gset */
697 META_ASET, /* \aset */
698 META_IF, /* \if */
699 META_ELIF, /* \elif */
700 META_ELSE, /* \else */
701 META_ENDIF, /* \endif */
702 META_STARTPIPELINE, /* \startpipeline */
703 META_SYNCPIPELINE, /* \syncpipeline */
704 META_ENDPIPELINE, /* \endpipeline */
707typedef enum QueryMode
709 QUERY_SIMPLE, /* simple query */
710 QUERY_EXTENDED, /* extended query */
711 QUERY_PREPARED, /* extended query with prepared statements */
713} QueryMode;
716static const char *const QUERYMODE[] = {"simple", "extended", "prepared"};
717
718/*
719 * struct Command represents one command in a script.
720 *
721 * lines The raw, possibly multi-line command text. Variable substitution
722 * not applied.
723 * first_line A short, single-line extract of 'lines', for error reporting.
724 * type SQL_COMMAND or META_COMMAND
725 * meta The type of meta-command, with META_NONE/GSET/ASET if command
726 * is SQL.
727 * argc Number of arguments of the command, 0 if not yet processed.
728 * argv Command arguments, the first of which is the command or SQL
729 * string itself. For SQL commands, after post-processing
730 * argv[0] is the same as 'lines' with variables substituted.
731 * prepname The name that this command is prepared under, in prepare mode
732 * varprefix SQL commands terminated with \gset or \aset have this set
733 * to a non NULL value. If nonempty, it's used to prefix the
734 * variable name that receives the value.
735 * aset do gset on all possible queries of a combined query (\;).
736 * expr Parsed expression, if needed.
737 * stats Time spent in this command.
738 * retries Number of retries after a serialization or deadlock error in the
739 * current command.
740 * failures Number of errors in the current command that were not retried.
741 */
742typedef struct Command
746 int type;
748 int argc;
750 char *prepname;
756} Command;
758typedef struct ParsedScript
760 const char *desc; /* script descriptor (eg, file name) */
761 int weight; /* selection weight */
762 Command **commands; /* NULL-terminated array of Commands */
763 StatsData stats; /* total time spent in script */
766static ParsedScript sql_script[MAX_SCRIPTS]; /* SQL script files */
767static int num_scripts; /* number of scripts in sql_script[] */
768static int64 total_weight = 0;
770static bool verbose_errors = false; /* print verbose messages of all errors */
772static bool exit_on_abort = false; /* exit when any client is aborted */
773
774/* Builtin test scripts */
775typedef struct BuiltinScript
777 const char *name; /* very short name for -b ... */
778 const char *desc; /* short description */
779 const char *script; /* actual pgbench script */
782static const BuiltinScript builtin_script[] =
783{
784 {
785 "tpcb-like",
786 "<builtin: TPC-B (sort of)>",
787 "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
788 "\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
789 "\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
790 "\\set delta random(-5000, 5000)\n"
791 "BEGIN;\n"
792 "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
793 "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
794 "UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;\n"
795 "UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;\n"
796 "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
797 "END;\n"
798 },
799 {
800 "simple-update",
801 "<builtin: simple update>",
802 "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
803 "\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
804 "\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
805 "\\set delta random(-5000, 5000)\n"
806 "BEGIN;\n"
807 "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
808 "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
809 "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
810 "END;\n"
811 },
812 {
813 "select-only",
814 "<builtin: select only>",
815 "\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
816 "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
817 }
818};
819
820
821/* Function prototypes */
822static void setNullValue(PgBenchValue *pv);
823static void setBoolValue(PgBenchValue *pv, bool bval);
824static void setIntValue(PgBenchValue *pv, int64 ival);
825static void setDoubleValue(PgBenchValue *pv, double dval);
826static bool evaluateExpr(CState *st, PgBenchExpr *expr,
827 PgBenchValue *retval);
829static void doLog(TState *thread, CState *st,
830 StatsData *agg, bool skipped, double latency, double lag);
831static void processXactStats(TState *thread, CState *st, pg_time_usec_t *now,
832 bool skipped, StatsData *agg);
833static void addScript(const ParsedScript *script);
835static void finishCon(CState *st);
836static void setalarm(int seconds);
837static socket_set *alloc_socket_set(int count);
838static void free_socket_set(socket_set *sa);
839static void clear_socket_set(socket_set *sa);
840static void add_socket_to_set(socket_set *sa, int fd, int idx);
841static int wait_on_socket_set(socket_set *sa, int64 usecs);
842static bool socket_has_input(socket_set *sa, int fd, int idx);
843
844/* callback used to build rows for COPY during data loading */
845typedef void (*initRowMethod) (PQExpBufferData *sql, int64 curr);
846
847/* callback functions for our flex lexer */
849 NULL, /* don't need get_variable functionality */
850};
851
852static char
853get_table_relkind(PGconn *con, const char *table)
854{
855 PGresult *res;
856 char *val;
857 char relkind;
858 const char *params[1] = {table};
859 const char *sql =
860 "SELECT relkind FROM pg_catalog.pg_class WHERE oid=$1::pg_catalog.regclass";
861
862 res = PQexecParams(con, sql, 1, NULL, params, NULL, NULL, 0);
864 {
865 pg_log_error("query failed: %s", PQerrorMessage(con));
866 pg_log_error_detail("Query was: %s", sql);
867 exit(1);
868 }
869 val = PQgetvalue(res, 0, 0);
870 Assert(strlen(val) == 1);
871 relkind = val[0];
872 PQclear(res);
873
874 return relkind;
875}
876
877static inline pg_time_usec_t
878pg_time_now(void)
879{
881
883
885}
886
887static inline void
889{
890 if ((*now) == 0)
891 (*now) = pg_time_now();
892}
894#define PG_TIME_GET_DOUBLE(t) (0.000001 * (t))
895
896static void
897usage(void)
898{
899 printf("%s is a benchmarking tool for PostgreSQL.\n\n"
900 "Usage:\n"
901 " %s [OPTION]... [DBNAME]\n"
902 "\nInitialization options:\n"
903 " -i, --initialize invokes initialization mode\n"
904 " -I, --init-steps=[" ALL_INIT_STEPS "]+ (default \"" DEFAULT_INIT_STEPS "\")\n"
905 " run selected initialization steps, in the specified order\n"
906 " d: drop any existing pgbench tables\n"
907 " t: create the tables used by the standard pgbench scenario\n"
908 " g: generate data, client-side\n"
909 " G: generate data, server-side\n"
910 " v: invoke VACUUM on the standard tables\n"
911 " p: create primary key indexes on the standard tables\n"
912 " f: create foreign keys between the standard tables\n"
913 " -F, --fillfactor=NUM set fill factor\n"
914 " -n, --no-vacuum do not run VACUUM during initialization\n"
915 " -q, --quiet quiet logging (one message each 5 seconds)\n"
916 " -s, --scale=NUM scaling factor\n"
917 " --foreign-keys create foreign key constraints between tables\n"
918 " --index-tablespace=TABLESPACE\n"
919 " create indexes in the specified tablespace\n"
920 " --partition-method=(range|hash)\n"
921 " partition pgbench_accounts with this method (default: range)\n"
922 " --partitions=NUM partition pgbench_accounts into NUM parts (default: 0)\n"
923 " --tablespace=TABLESPACE create tables in the specified tablespace\n"
924 " --unlogged-tables create tables as unlogged tables\n"
925 "\nOptions to select what to run:\n"
926 " -b, --builtin=NAME[@W] add builtin script NAME weighted at W (default: 1)\n"
927 " (use \"-b list\" to list available scripts)\n"
928 " -f, --file=FILENAME[@W] add script FILENAME weighted at W (default: 1)\n"
929 " -N, --skip-some-updates skip updates of pgbench_tellers and pgbench_branches\n"
930 " (same as \"-b simple-update\")\n"
931 " -S, --select-only perform SELECT-only transactions\n"
932 " (same as \"-b select-only\")\n"
933 "\nBenchmarking options:\n"
934 " -c, --client=NUM number of concurrent database clients (default: 1)\n"
935 " -C, --connect establish new connection for each transaction\n"
936 " -D, --define=VARNAME=VALUE\n"
937 " define variable for use by custom script\n"
938 " -j, --jobs=NUM number of threads (default: 1)\n"
939 " -l, --log write transaction times to log file\n"
940 " -L, --latency-limit=NUM count transactions lasting more than NUM ms as late\n"
941 " -M, --protocol=simple|extended|prepared\n"
942 " protocol for submitting queries (default: simple)\n"
943 " -n, --no-vacuum do not run VACUUM before tests\n"
944 " -P, --progress=NUM show thread progress report every NUM seconds\n"
945 " -r, --report-per-command report latencies, failures, and retries per command\n"
946 " -R, --rate=NUM target rate in transactions per second\n"
947 " -s, --scale=NUM report this scale factor in output\n"
948 " -t, --transactions=NUM number of transactions each client runs (default: 10)\n"
949 " -T, --time=NUM duration of benchmark test in seconds\n"
950 " -v, --vacuum-all vacuum all four standard tables before tests\n"
951 " --aggregate-interval=NUM aggregate data over NUM seconds\n"
952 " --exit-on-abort exit when any client is aborted\n"
953 " --failures-detailed report the failures grouped by basic types\n"
954 " --log-prefix=PREFIX prefix for transaction time log file\n"
955 " (default: \"pgbench_log\")\n"
956 " --max-tries=NUM max number of tries to run transaction (default: 1)\n"
957 " --progress-timestamp use Unix epoch timestamps for progress\n"
958 " --random-seed=SEED set random seed (\"time\", \"rand\", integer)\n"
959 " --sampling-rate=NUM fraction of transactions to log (e.g., 0.01 for 1%%)\n"
960 " --show-script=NAME show builtin script code, then exit\n"
961 " --verbose-errors print messages of all errors\n"
962 "\nCommon options:\n"
963 " --debug print debugging output\n"
964 " -d, --dbname=DBNAME database name to connect to\n"
965 " -h, --host=HOSTNAME database server host or socket directory\n"
966 " -p, --port=PORT database server port number\n"
967 " -U, --username=USERNAME connect as specified database user\n"
968 " -V, --version output version information, then exit\n"
969 " -?, --help show this help, then exit\n"
970 "\n"
971 "Report bugs to <%s>.\n"
972 "%s home page: <%s>\n",
973 progname, progname, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL);
974}
975
976/* return whether str matches "^\s*[-+]?[0-9]+$" */
977static bool
978is_an_int(const char *str)
979{
980 const char *ptr = str;
981
982 /* skip leading spaces; cast is consistent with strtoint64 */
983 while (*ptr && isspace((unsigned char) *ptr))
984 ptr++;
985
986 /* skip sign */
987 if (*ptr == '+' || *ptr == '-')
988 ptr++;
989
990 /* at least one digit */
991 if (*ptr && !isdigit((unsigned char) *ptr))
992 return false;
993
994 /* eat all digits */
995 while (*ptr && isdigit((unsigned char) *ptr))
996 ptr++;
997
998 /* must have reached end of string */
999 return *ptr == '\0';
1000}
1001
1002
1003/*
1004 * strtoint64 -- convert a string to 64-bit integer
1005 *
1006 * This function is a slightly modified version of pg_strtoint64() from
1007 * src/backend/utils/adt/numutils.c.
1008 *
1009 * The function returns whether the conversion worked, and if so
1010 * "*result" is set to the result.
1011 *
1012 * If not errorOK, an error message is also printed out on errors.
1013 */
1014bool
1015strtoint64(const char *str, bool errorOK, int64 *result)
1016{
1017 const char *ptr = str;
1018 int64 tmp = 0;
1019 bool neg = false;
1020
1021 /*
1022 * Do our own scan, rather than relying on sscanf which might be broken
1023 * for long long.
1024 *
1025 * As INT64_MIN can't be stored as a positive 64 bit integer, accumulate
1026 * value as a negative number.
1027 */
1028
1029 /* skip leading spaces */
1030 while (*ptr && isspace((unsigned char) *ptr))
1031 ptr++;
1032
1033 /* handle sign */
1034 if (*ptr == '-')
1035 {
1036 ptr++;
1037 neg = true;
1038 }
1039 else if (*ptr == '+')
1040 ptr++;
1041
1042 /* require at least one digit */
1043 if (unlikely(!isdigit((unsigned char) *ptr)))
1044 goto invalid_syntax;
1045
1046 /* process digits */
1047 while (*ptr && isdigit((unsigned char) *ptr))
1048 {
1049 int8 digit = (*ptr++ - '0');
1050
1051 if (unlikely(pg_mul_s64_overflow(tmp, 10, &tmp)) ||
1052 unlikely(pg_sub_s64_overflow(tmp, digit, &tmp)))
1053 goto out_of_range;
1054 }
1055
1056 /* allow trailing whitespace, but not other trailing chars */
1057 while (*ptr != '\0' && isspace((unsigned char) *ptr))
1058 ptr++;
1059
1060 if (unlikely(*ptr != '\0'))
1061 goto invalid_syntax;
1062
1063 if (!neg)
1064 {
1065 if (unlikely(tmp == PG_INT64_MIN))
1066 goto out_of_range;
1067 tmp = -tmp;
1068 }
1069
1070 *result = tmp;
1071 return true;
1072
1073out_of_range:
1074 if (!errorOK)
1075 pg_log_error("value \"%s\" is out of range for type bigint", str);
1076 return false;
1077
1078invalid_syntax:
1079 if (!errorOK)
1080 pg_log_error("invalid input syntax for type bigint: \"%s\"", str);
1081 return false;
1082}
1083
1084/* convert string to double, detecting overflows/underflows */
1085bool
1086strtodouble(const char *str, bool errorOK, double *dv)
1087{
1088 char *end;
1089
1090 errno = 0;
1091 *dv = strtod(str, &end);
1092
1093 if (unlikely(errno != 0))
1094 {
1095 if (!errorOK)
1096 pg_log_error("value \"%s\" is out of range for type double", str);
1097 return false;
1098 }
1099
1100 if (unlikely(end == str || *end != '\0'))
1101 {
1102 if (!errorOK)
1103 pg_log_error("invalid input syntax for type double: \"%s\"", str);
1104 return false;
1105 }
1106 return true;
1107}
1108
1109/*
1110 * Initialize a prng state struct.
1111 *
1112 * We derive the seed from base_random_sequence, which must be set up already.
1113 */
1114static void
1116{
1118}
1119
1120
1121/*
1122 * random number generator: uniform distribution from min to max inclusive.
1123 *
1124 * Although the limits are expressed as int64, you can't generate the full
1125 * int64 range in one call, because the difference of the limits mustn't
1126 * overflow int64. This is not checked.
1127 */
1128static int64
1130{
1131 return min + (int64) pg_prng_uint64_range(state, 0, max - min);
1132}
1133
1134/*
1135 * random number generator: exponential distribution from min to max inclusive.
1136 * the parameter is so that the density of probability for the last cut-off max
1137 * value is exp(-parameter).
1138 */
1139static int64
1141 double parameter)
1142{
1143 double cut,
1144 uniform,
1145 rand;
1146
1147 /* abort if wrong parameter, but must really be checked beforehand */
1148 Assert(parameter > 0.0);
1149 cut = exp(-parameter);
1150 /* pg_prng_double value in [0, 1), uniform in (0, 1] */
1151 uniform = 1.0 - pg_prng_double(state);
1152
1153 /*
1154 * inner expression in (cut, 1] (if parameter > 0), rand in [0, 1)
1155 */
1156 Assert((1.0 - cut) != 0.0);
1157 rand = -log(cut + (1.0 - cut) * uniform) / parameter;
1158 /* return int64 random number within between min and max */
1159 return min + (int64) ((max - min + 1) * rand);
1160}
1161
1162/* random number generator: gaussian distribution from min to max inclusive */
1163static int64
1165 double parameter)
1166{
1167 double stdev;
1168 double rand;
1169
1170 /* abort if parameter is too low, but must really be checked beforehand */
1171 Assert(parameter >= MIN_GAUSSIAN_PARAM);
1172
1173 /*
1174 * Get normally-distributed random number in the range -parameter <= stdev
1175 * < parameter.
1176 *
1177 * This loop is executed until the number is in the expected range.
1178 *
1179 * As the minimum parameter is 2.0, the probability of looping is low:
1180 * sqrt(-2 ln(r)) <= 2 => r >= e^{-2} ~ 0.135, then when taking the
1181 * average sinus multiplier as 2/pi, we have a 8.6% looping probability in
1182 * the worst case. For a parameter value of 5.0, the looping probability
1183 * is about e^{-5} * 2 / pi ~ 0.43%.
1184 */
1185 do
1186 {
1188 }
1189 while (stdev < -parameter || stdev >= parameter);
1190
1191 /* stdev is in [-parameter, parameter), normalization to [0,1) */
1192 rand = (stdev + parameter) / (parameter * 2.0);
1193
1194 /* return int64 random number within between min and max */
1195 return min + (int64) ((max - min + 1) * rand);
1196}
1197
1198/*
1199 * random number generator: generate a value, such that the series of values
1200 * will approximate a Poisson distribution centered on the given value.
1201 *
1202 * Individual results are rounded to integers, though the center value need
1203 * not be one.
1204 */
1205static int64
1206getPoissonRand(pg_prng_state *state, double center)
1207{
1208 /*
1209 * Use inverse transform sampling to generate a value > 0, such that the
1210 * expected (i.e. average) value is the given argument.
1211 */
1212 double uniform;
1213
1214 /* pg_prng_double value in [0, 1), uniform in (0, 1] */
1215 uniform = 1.0 - pg_prng_double(state);
1216
1217 return (int64) (-log(uniform) * center + 0.5);
1218}
1219
1220/*
1221 * Computing zipfian using rejection method, based on
1222 * "Non-Uniform Random Variate Generation",
1223 * Luc Devroye, p. 550-551, Springer 1986.
1224 *
1225 * This works for s > 1.0, but may perform badly for s very close to 1.0.
1226 */
1227static int64
1229{
1230 double b = pow(2.0, s - 1.0);
1231 double x,
1232 t,
1233 u,
1234 v;
1235
1236 /* Ensure n is sane */
1237 if (n <= 1)
1238 return 1;
1239
1240 while (true)
1241 {
1242 /* random variates */
1243 u = pg_prng_double(state);
1244 v = pg_prng_double(state);
1245
1246 x = floor(pow(u, -1.0 / (s - 1.0)));
1247
1248 t = pow(1.0 + 1.0 / x, s - 1.0);
1249 /* reject if too large or out of bound */
1250 if (v * x * (t - 1.0) / (b - 1.0) <= t / b && x <= n)
1251 break;
1252 }
1253 return (int64) x;
1254}
1255
1256/* random number generator: zipfian distribution from min to max inclusive */
1257static int64
1258getZipfianRand(pg_prng_state *state, int64 min, int64 max, double s)
1259{
1260 int64 n = max - min + 1;
1261
1262 /* abort if parameter is invalid */
1264
1265 return min - 1 + computeIterativeZipfian(state, n, s);
1266}
1267
1268/*
1269 * FNV-1a hash function
1270 */
1271static int64
1273{
1274 int64 result;
1275 int i;
1276
1277 result = FNV_OFFSET_BASIS ^ seed;
1278 for (i = 0; i < 8; ++i)
1279 {
1280 int32 octet = val & 0xff;
1281
1282 val = val >> 8;
1283 result = result ^ octet;
1284 result = result * FNV_PRIME;
1285 }
1286
1287 return result;
1288}
1289
1290/*
1291 * Murmur2 hash function
1292 *
1293 * Based on original work of Austin Appleby
1294 * https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp
1295 */
1296static int64
1298{
1299 uint64 result = seed ^ MM2_MUL_TIMES_8; /* sizeof(int64) */
1300 uint64 k = (uint64) val;
1301
1302 k *= MM2_MUL;
1303 k ^= k >> MM2_ROT;
1304 k *= MM2_MUL;
1305
1306 result ^= k;
1307 result *= MM2_MUL;
1308
1309 result ^= result >> MM2_ROT;
1310 result *= MM2_MUL;
1311 result ^= result >> MM2_ROT;
1312
1313 return (int64) result;
1314}
1315
1316/*
1317 * Pseudorandom permutation function
1318 *
1319 * For small sizes, this generates each of the (size!) possible permutations
1320 * of integers in the range [0, size) with roughly equal probability. Once
1321 * the size is larger than 20, the number of possible permutations exceeds the
1322 * number of distinct states of the internal pseudorandom number generator,
1323 * and so not all possible permutations can be generated, but the permutations
1324 * chosen should continue to give the appearance of being random.
1325 *
1326 * THIS FUNCTION IS NOT CRYPTOGRAPHICALLY SECURE.
1327 * DO NOT USE FOR SUCH PURPOSE.
1328 */
1329static int64
1330permute(const int64 val, const int64 isize, const int64 seed)
1331{
1332 /* using a high-end PRNG is probably overkill */
1334 uint64 size;
1335 uint64 v;
1336 int masklen;
1337 uint64 mask;
1338 int i;
1339
1340 if (isize < 2)
1341 return 0; /* nothing to permute */
1342
1343 /* Initialize prng state using the seed */
1344 pg_prng_seed(&state, (uint64) seed);
1345
1346 /* Computations are performed on unsigned values */
1347 size = (uint64) isize;
1348 v = (uint64) val % size;
1349
1350 /* Mask to work modulo largest power of 2 less than or equal to size */
1351 masklen = pg_leftmost_one_pos64(size);
1352 mask = (((uint64) 1) << masklen) - 1;
1353
1354 /*
1355 * Permute the input value by applying several rounds of pseudorandom
1356 * bijective transformations. The intention here is to distribute each
1357 * input uniformly randomly across the range, and separate adjacent inputs
1358 * approximately uniformly randomly from each other, leading to a fairly
1359 * random overall choice of permutation.
1360 *
1361 * To separate adjacent inputs, we multiply by a random number modulo
1362 * (mask + 1), which is a power of 2. For this to be a bijection, the
1363 * multiplier must be odd. Since this is known to lead to less randomness
1364 * in the lower bits, we also apply a rotation that shifts the topmost bit
1365 * into the least significant bit. In the special cases where size <= 3,
1366 * mask = 1 and each of these operations is actually a no-op, so we also
1367 * XOR the value with a different random number to inject additional
1368 * randomness. Since the size is generally not a power of 2, we apply
1369 * this bijection on overlapping upper and lower halves of the input.
1370 *
1371 * To distribute the inputs uniformly across the range, we then also apply
1372 * a random offset modulo the full range.
1373 *
1374 * Taken together, these operations resemble a modified linear
1375 * congruential generator, as is commonly used in pseudorandom number
1376 * generators. The number of rounds is fairly arbitrary, but six has been
1377 * found empirically to give a fairly good tradeoff between performance
1378 * and uniform randomness. For small sizes it selects each of the (size!)
1379 * possible permutations with roughly equal probability. For larger
1380 * sizes, not all permutations can be generated, but the intended random
1381 * spread is still produced.
1382 */
1383 for (i = 0; i < 6; i++)
1384 {
1385 uint64 m,
1386 r,
1387 t;
1388
1389 /* Random multiply (by an odd number), XOR and rotate of lower half */
1390 m = (pg_prng_uint64(&state) & mask) | 1;
1391 r = pg_prng_uint64(&state) & mask;
1392 if (v <= mask)
1393 {
1394 v = ((v * m) ^ r) & mask;
1395 v = ((v << 1) & mask) | (v >> (masklen - 1));
1396 }
1397
1398 /* Random multiply (by an odd number), XOR and rotate of upper half */
1399 m = (pg_prng_uint64(&state) & mask) | 1;
1400 r = pg_prng_uint64(&state) & mask;
1401 t = size - 1 - v;
1402 if (t <= mask)
1403 {
1404 t = ((t * m) ^ r) & mask;
1405 t = ((t << 1) & mask) | (t >> (masklen - 1));
1406 v = size - 1 - t;
1407 }
1408
1409 /* Random offset */
1410 r = pg_prng_uint64_range(&state, 0, size - 1);
1411 v = (v + r) % size;
1412 }
1413
1414 return (int64) v;
1415}
1416
1417/*
1418 * Initialize the given SimpleStats struct to all zeroes
1419 */
1420static void
1422{
1423 memset(ss, 0, sizeof(SimpleStats));
1424}
1425
1426/*
1427 * Accumulate one value into a SimpleStats struct.
1428 */
1429static void
1430addToSimpleStats(SimpleStats *ss, double val)
1431{
1432 if (ss->count == 0 || val < ss->min)
1433 ss->min = val;
1434 if (ss->count == 0 || val > ss->max)
1435 ss->max = val;
1436 ss->count++;
1437 ss->sum += val;
1438 ss->sum2 += val * val;
1439}
1440
1441/*
1442 * Merge two SimpleStats objects
1443 */
1444static void
1446{
1447 if (acc->count == 0 || ss->min < acc->min)
1448 acc->min = ss->min;
1449 if (acc->count == 0 || ss->max > acc->max)
1450 acc->max = ss->max;
1451 acc->count += ss->count;
1452 acc->sum += ss->sum;
1453 acc->sum2 += ss->sum2;
1454}
1455
1456/*
1457 * Initialize a StatsData struct to mostly zeroes, with its start time set to
1458 * the given value.
1459 */
1460static void
1462{
1463 sd->start_time = start;
1464 sd->cnt = 0;
1465 sd->skipped = 0;
1466 sd->retries = 0;
1467 sd->retried = 0;
1468 sd->serialization_failures = 0;
1469 sd->deadlock_failures = 0;
1471 initSimpleStats(&sd->lag);
1472}
1473
1474/*
1475 * Accumulate one additional item into the given stats object.
1476 */
1477static void
1478accumStats(StatsData *stats, bool skipped, double lat, double lag,
1479 EStatus estatus, int64 tries)
1480{
1481 /* Record the skipped transaction */
1482 if (skipped)
1483 {
1484 /* no latency to record on skipped transactions */
1485 stats->skipped++;
1486 return;
1487 }
1488
1489 /*
1490 * Record the number of retries regardless of whether the transaction was
1491 * successful or failed.
1492 */
1493 if (tries > 1)
1494 {
1495 stats->retries += (tries - 1);
1496 stats->retried++;
1497 }
1498
1499 switch (estatus)
1500 {
1501 /* Record the successful transaction */
1502 case ESTATUS_NO_ERROR:
1503 stats->cnt++;
1504
1505 addToSimpleStats(&stats->latency, lat);
1506
1507 /* and possibly the same for schedule lag */
1508 if (throttle_delay)
1509 addToSimpleStats(&stats->lag, lag);
1510 break;
1511
1512 /* Record the failed transaction */
1514 stats->serialization_failures++;
1515 break;
1517 stats->deadlock_failures++;
1518 break;
1519 default:
1520 /* internal error which should never occur */
1521 pg_fatal("unexpected error status: %d", estatus);
1522 }
1523}
1524
1525/* call PQexec() and exit() on failure */
1526static void
1527executeStatement(PGconn *con, const char *sql)
1528{
1529 PGresult *res;
1530
1531 res = PQexec(con, sql);
1533 {
1534 pg_log_error("query failed: %s", PQerrorMessage(con));
1535 pg_log_error_detail("Query was: %s", sql);
1536 exit(1);
1537 }
1538 PQclear(res);
1539}
1540
1541/* call PQexec() and complain, but without exiting, on failure */
1542static void
1543tryExecuteStatement(PGconn *con, const char *sql)
1544{
1545 PGresult *res;
1546
1547 res = PQexec(con, sql);
1549 {
1550 pg_log_error("%s", PQerrorMessage(con));
1551 pg_log_error_detail("(ignoring this error and continuing anyway)");
1552 }
1553 PQclear(res);
1554}
1555
1556/* set up a connection to the backend */
1557static PGconn *
1558doConnect(void)
1559{
1560 PGconn *conn;
1561 bool new_pass;
1562 static char *password = NULL;
1563
1564 /*
1565 * Start the connection. Loop until we have a password if requested by
1566 * backend.
1567 */
1568 do
1569 {
1570#define PARAMS_ARRAY_SIZE 7
1571
1572 const char *keywords[PARAMS_ARRAY_SIZE];
1573 const char *values[PARAMS_ARRAY_SIZE];
1574
1575 keywords[0] = "host";
1576 values[0] = pghost;
1577 keywords[1] = "port";
1578 values[1] = pgport;
1579 keywords[2] = "user";
1580 values[2] = username;
1581 keywords[3] = "password";
1582 values[3] = password;
1583 keywords[4] = "dbname";
1584 values[4] = dbName;
1585 keywords[5] = "fallback_application_name";
1586 values[5] = progname;
1587 keywords[6] = NULL;
1588 values[6] = NULL;
1589
1590 new_pass = false;
1591
1593
1594 if (!conn)
1595 {
1596 pg_log_error("connection to database \"%s\" failed", dbName);
1597 return NULL;
1598 }
1599
1600 if (PQstatus(conn) == CONNECTION_BAD &&
1602 !password)
1603 {
1604 PQfinish(conn);
1605 password = simple_prompt("Password: ", false);
1606 new_pass = true;
1607 }
1608 } while (new_pass);
1609
1610 /* check to see that the backend connection was successfully made */
1612 {
1614 PQfinish(conn);
1615 return NULL;
1616 }
1617
1618 return conn;
1619}
1620
1621/* qsort comparator for Variable array */
1622static int
1623compareVariableNames(const void *v1, const void *v2)
1624{
1625 return strcmp(((const Variable *) v1)->name,
1626 ((const Variable *) v2)->name);
1627}
1628
1629/* Locate a variable by name; returns NULL if unknown */
1630static Variable *
1631lookupVariable(Variables *variables, char *name)
1632{
1633 Variable key;
1634
1635 /* On some versions of Solaris, bsearch of zero items dumps core */
1636 if (variables->nvars <= 0)
1637 return NULL;
1638
1639 /* Sort if we have to */
1640 if (!variables->vars_sorted)
1641 {
1642 qsort(variables->vars, variables->nvars, sizeof(Variable),
1644 variables->vars_sorted = true;
1645 }
1646
1647 /* Now we can search */
1648 key.name = name;
1649 return (Variable *) bsearch(&key,
1650 variables->vars,
1651 variables->nvars,
1652 sizeof(Variable),
1654}
1655
1656/* Get the value of a variable, in string form; returns NULL if unknown */
1657static char *
1658getVariable(Variables *variables, char *name)
1659{
1660 Variable *var;
1661 char stringform[64];
1662
1663 var = lookupVariable(variables, name);
1664 if (var == NULL)
1665 return NULL; /* not found */
1666
1667 if (var->svalue)
1668 return var->svalue; /* we have it in string form */
1669
1670 /* We need to produce a string equivalent of the value */
1671 Assert(var->value.type != PGBT_NO_VALUE);
1672 if (var->value.type == PGBT_NULL)
1673 snprintf(stringform, sizeof(stringform), "NULL");
1674 else if (var->value.type == PGBT_BOOLEAN)
1675 snprintf(stringform, sizeof(stringform),
1676 "%s", var->value.u.bval ? "true" : "false");
1677 else if (var->value.type == PGBT_INT)
1678 snprintf(stringform, sizeof(stringform),
1679 INT64_FORMAT, var->value.u.ival);
1680 else if (var->value.type == PGBT_DOUBLE)
1681 snprintf(stringform, sizeof(stringform),
1682 "%.*g", DBL_DIG, var->value.u.dval);
1683 else /* internal error, unexpected type */
1684 Assert(0);
1685 var->svalue = pg_strdup(stringform);
1686 return var->svalue;
1687}
1688
1689/* Try to convert variable to a value; return false on failure */
1690static bool
1692{
1693 size_t slen;
1694
1695 if (var->value.type != PGBT_NO_VALUE)
1696 return true; /* no work */
1697
1698 slen = strlen(var->svalue);
1699
1700 if (slen == 0)
1701 /* what should it do on ""? */
1702 return false;
1703
1704 if (pg_strcasecmp(var->svalue, "null") == 0)
1705 {
1706 setNullValue(&var->value);
1707 }
1708
1709 /*
1710 * accept prefixes such as y, ye, n, no... but not for "o". 0/1 are
1711 * recognized later as an int, which is converted to bool if needed.
1712 */
1713 else if (pg_strncasecmp(var->svalue, "true", slen) == 0 ||
1714 pg_strncasecmp(var->svalue, "yes", slen) == 0 ||
1715 pg_strcasecmp(var->svalue, "on") == 0)
1716 {
1717 setBoolValue(&var->value, true);
1718 }
1719 else if (pg_strncasecmp(var->svalue, "false", slen) == 0 ||
1720 pg_strncasecmp(var->svalue, "no", slen) == 0 ||
1721 pg_strcasecmp(var->svalue, "off") == 0 ||
1722 pg_strcasecmp(var->svalue, "of") == 0)
1723 {
1724 setBoolValue(&var->value, false);
1725 }
1726 else if (is_an_int(var->svalue))
1727 {
1728 /* if it looks like an int, it must be an int without overflow */
1729 int64 iv;
1730
1731 if (!strtoint64(var->svalue, false, &iv))
1732 return false;
1733
1734 setIntValue(&var->value, iv);
1735 }
1736 else /* type should be double */
1737 {
1738 double dv;
1739
1740 if (!strtodouble(var->svalue, true, &dv))
1741 {
1742 pg_log_error("malformed variable \"%s\" value: \"%s\"",
1743 var->name, var->svalue);
1744 return false;
1745 }
1746 setDoubleValue(&var->value, dv);
1747 }
1748 return true;
1749}
1750
1751/*
1752 * Check whether a variable's name is allowed.
1753 *
1754 * We allow any non-ASCII character, as well as ASCII letters, digits, and
1755 * underscore.
1756 *
1757 * Keep this in sync with the definitions of variable name characters in
1758 * "src/fe_utils/psqlscan.l", "src/bin/psql/psqlscanslash.l" and
1759 * "src/bin/pgbench/exprscan.l". Also see parseVariable(), below.
1760 *
1761 * Note: this static function is copied from "src/bin/psql/variables.c"
1762 * but changed to disallow variable names starting with a digit.
1763 */
1764static bool
1765valid_variable_name(const char *name)
1766{
1767 const unsigned char *ptr = (const unsigned char *) name;
1768
1769 /* Mustn't be zero-length */
1770 if (*ptr == '\0')
1771 return false;
1772
1773 /* must not start with [0-9] */
1774 if (IS_HIGHBIT_SET(*ptr) ||
1775 strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1776 "_", *ptr) != NULL)
1777 ptr++;
1778 else
1779 return false;
1780
1781 /* remaining characters can include [0-9] */
1782 while (*ptr)
1783 {
1784 if (IS_HIGHBIT_SET(*ptr) ||
1785 strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1786 "_0123456789", *ptr) != NULL)
1787 ptr++;
1788 else
1789 return false;
1790 }
1791
1792 return true;
1793}
1794
1795/*
1796 * Make sure there is enough space for 'needed' more variable in the variables
1797 * array.
1798 */
1799static void
1800enlargeVariables(Variables *variables, int needed)
1801{
1802 /* total number of variables required now */
1803 needed += variables->nvars;
1804
1805 if (variables->max_vars < needed)
1806 {
1807 variables->max_vars = needed + VARIABLES_ALLOC_MARGIN;
1808 variables->vars = (Variable *)
1809 pg_realloc(variables->vars, variables->max_vars * sizeof(Variable));
1810 }
1811}
1812
1813/*
1814 * Lookup a variable by name, creating it if need be.
1815 * Caller is expected to assign a value to the variable.
1816 * Returns NULL on failure (bad name).
1817 */
1818static Variable *
1819lookupCreateVariable(Variables *variables, const char *context, char *name)
1820{
1821 Variable *var;
1822
1823 var = lookupVariable(variables, name);
1824 if (var == NULL)
1825 {
1826 /*
1827 * Check for the name only when declaring a new variable to avoid
1828 * overhead.
1829 */
1831 {
1832 pg_log_error("%s: invalid variable name: \"%s\"", context, name);
1833 return NULL;
1834 }
1835
1836 /* Create variable at the end of the array */
1837 enlargeVariables(variables, 1);
1838
1839 var = &(variables->vars[variables->nvars]);
1840
1841 var->name = pg_strdup(name);
1842 var->svalue = NULL;
1843 /* caller is expected to initialize remaining fields */
1844
1845 variables->nvars++;
1846 /* we don't re-sort the array till we have to */
1847 variables->vars_sorted = false;
1848 }
1849
1850 return var;
1851}
1852
1853/* Assign a string value to a variable, creating it if need be */
1854/* Returns false on failure (bad name) */
1855static bool
1856putVariable(Variables *variables, const char *context, char *name,
1857 const char *value)
1858{
1859 Variable *var;
1860 char *val;
1861
1862 var = lookupCreateVariable(variables, context, name);
1863 if (!var)
1864 return false;
1865
1866 /* dup then free, in case value is pointing at this variable */
1867 val = pg_strdup(value);
1868
1869 free(var->svalue);
1870 var->svalue = val;
1871 var->value.type = PGBT_NO_VALUE;
1872
1873 return true;
1874}
1875
1876/* Assign a value to a variable, creating it if need be */
1877/* Returns false on failure (bad name) */
1878static bool
1879putVariableValue(Variables *variables, const char *context, char *name,
1880 const PgBenchValue *value)
1881{
1882 Variable *var;
1883
1884 var = lookupCreateVariable(variables, context, name);
1885 if (!var)
1886 return false;
1887
1888 free(var->svalue);
1889 var->svalue = NULL;
1890 var->value = *value;
1891
1892 return true;
1893}
1894
1895/* Assign an integer value to a variable, creating it if need be */
1896/* Returns false on failure (bad name) */
1897static bool
1898putVariableInt(Variables *variables, const char *context, char *name,
1899 int64 value)
1900{
1902
1904 return putVariableValue(variables, context, name, &val);
1905}
1906
1907/*
1908 * Parse a possible variable reference (:varname).
1909 *
1910 * "sql" points at a colon. If what follows it looks like a valid
1911 * variable name, return a malloc'd string containing the variable name,
1912 * and set *eaten to the number of characters consumed (including the colon).
1913 * Otherwise, return NULL.
1914 */
1915static char *
1916parseVariable(const char *sql, int *eaten)
1917{
1918 int i = 1; /* starting at 1 skips the colon */
1919 char *name;
1920
1921 /* keep this logic in sync with valid_variable_name() */
1922 if (IS_HIGHBIT_SET(sql[i]) ||
1923 strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1924 "_", sql[i]) != NULL)
1925 i++;
1926 else
1927 return NULL;
1928
1929 while (IS_HIGHBIT_SET(sql[i]) ||
1930 strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1931 "_0123456789", sql[i]) != NULL)
1932 i++;
1933
1934 name = pg_malloc(i);
1935 memcpy(name, &sql[1], i - 1);
1936 name[i - 1] = '\0';
1937
1938 *eaten = i;
1939 return name;
1940}
1941
1942static char *
1943replaceVariable(char **sql, char *param, int len, char *value)
1944{
1945 int valueln = strlen(value);
1946
1947 if (valueln > len)
1948 {
1949 size_t offset = param - *sql;
1950
1951 *sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1);
1952 param = *sql + offset;
1953 }
1954
1955 if (valueln != len)
1956 memmove(param + valueln, param + len, strlen(param + len) + 1);
1957 memcpy(param, value, valueln);
1958
1959 return param + valueln;
1960}
1961
1962static char *
1963assignVariables(Variables *variables, char *sql)
1964{
1965 char *p,
1966 *name,
1967 *val;
1968
1969 p = sql;
1970 while ((p = strchr(p, ':')) != NULL)
1971 {
1972 int eaten;
1973
1974 name = parseVariable(p, &eaten);
1975 if (name == NULL)
1976 {
1977 while (*p == ':')
1978 {
1979 p++;
1980 }
1981 continue;
1982 }
1983
1984 val = getVariable(variables, name);
1985 free(name);
1986 if (val == NULL)
1987 {
1988 p++;
1989 continue;
1990 }
1991
1992 p = replaceVariable(&sql, p, eaten, val);
1993 }
1994
1995 return sql;
1996}
1997
1998static void
1999getQueryParams(Variables *variables, const Command *command,
2000 const char **params)
2001{
2002 int i;
2003
2004 for (i = 0; i < command->argc - 1; i++)
2005 params[i] = getVariable(variables, command->argv[i + 1]);
2006}
2007
2008static char *
2010{
2011 if (pval->type == PGBT_NO_VALUE)
2012 return "none";
2013 else if (pval->type == PGBT_NULL)
2014 return "null";
2015 else if (pval->type == PGBT_INT)
2016 return "int";
2017 else if (pval->type == PGBT_DOUBLE)
2018 return "double";
2019 else if (pval->type == PGBT_BOOLEAN)
2020 return "boolean";
2021 else
2022 {
2023 /* internal error, should never get there */
2024 Assert(false);
2025 return NULL;
2026 }
2027}
2028
2029/* get a value as a boolean, or tell if there is a problem */
2030static bool
2031coerceToBool(PgBenchValue *pval, bool *bval)
2032{
2033 if (pval->type == PGBT_BOOLEAN)
2034 {
2035 *bval = pval->u.bval;
2036 return true;
2037 }
2038 else /* NULL, INT or DOUBLE */
2039 {
2040 pg_log_error("cannot coerce %s to boolean", valueTypeName(pval));
2041 *bval = false; /* suppress uninitialized-variable warnings */
2042 return false;
2043 }
2044}
2045
2046/*
2047 * Return true or false from an expression for conditional purposes.
2048 * Non zero numerical values are true, zero and NULL are false.
2049 */
2050static bool
2052{
2053 switch (pval->type)
2054 {
2055 case PGBT_NULL:
2056 return false;
2057 case PGBT_BOOLEAN:
2058 return pval->u.bval;
2059 case PGBT_INT:
2060 return pval->u.ival != 0;
2061 case PGBT_DOUBLE:
2062 return pval->u.dval != 0.0;
2063 default:
2064 /* internal error, unexpected type */
2065 Assert(0);
2066 return false;
2067 }
2068}
2069
2070/* get a value as an int, tell if there is a problem */
2071static bool
2072coerceToInt(PgBenchValue *pval, int64 *ival)
2073{
2074 if (pval->type == PGBT_INT)
2075 {
2076 *ival = pval->u.ival;
2077 return true;
2078 }
2079 else if (pval->type == PGBT_DOUBLE)
2080 {
2081 double dval = rint(pval->u.dval);
2082
2083 if (isnan(dval) || !FLOAT8_FITS_IN_INT64(dval))
2084 {
2085 pg_log_error("double to int overflow for %f", dval);
2086 return false;
2087 }
2088 *ival = (int64) dval;
2089 return true;
2090 }
2091 else /* BOOLEAN or NULL */
2092 {
2093 pg_log_error("cannot coerce %s to int", valueTypeName(pval));
2094 return false;
2095 }
2096}
2097
2098/* get a value as a double, or tell if there is a problem */
2099static bool
2100coerceToDouble(PgBenchValue *pval, double *dval)
2101{
2102 if (pval->type == PGBT_DOUBLE)
2103 {
2104 *dval = pval->u.dval;
2105 return true;
2106 }
2107 else if (pval->type == PGBT_INT)
2108 {
2109 *dval = (double) pval->u.ival;
2110 return true;
2111 }
2112 else /* BOOLEAN or NULL */
2113 {
2114 pg_log_error("cannot coerce %s to double", valueTypeName(pval));
2115 return false;
2116 }
2117}
2118
2119/* assign a null value */
2120static void
2122{
2123 pv->type = PGBT_NULL;
2124 pv->u.ival = 0;
2125}
2126
2127/* assign a boolean value */
2128static void
2129setBoolValue(PgBenchValue *pv, bool bval)
2130{
2131 pv->type = PGBT_BOOLEAN;
2132 pv->u.bval = bval;
2133}
2134
2135/* assign an integer value */
2136static void
2138{
2139 pv->type = PGBT_INT;
2140 pv->u.ival = ival;
2141}
2142
2143/* assign a double value */
2144static void
2145setDoubleValue(PgBenchValue *pv, double dval)
2146{
2147 pv->type = PGBT_DOUBLE;
2148 pv->u.dval = dval;
2149}
2150
2151static bool
2153{
2154 return func == PGBENCH_AND || func == PGBENCH_OR || func == PGBENCH_CASE;
2155}
2156
2157/* lazy evaluation of some functions */
2158static bool
2161{
2163 a2;
2164 bool ba1,
2165 ba2;
2166
2167 Assert(isLazyFunc(func) && args != NULL && args->next != NULL);
2168
2169 /* args points to first condition */
2170 if (!evaluateExpr(st, args->expr, &a1))
2171 return false;
2172
2173 /* second condition for AND/OR and corresponding branch for CASE */
2174 args = args->next;
2175
2176 switch (func)
2177 {
2178 case PGBENCH_AND:
2179 if (a1.type == PGBT_NULL)
2180 {
2181 setNullValue(retval);
2182 return true;
2183 }
2184
2185 if (!coerceToBool(&a1, &ba1))
2186 return false;
2187
2188 if (!ba1)
2189 {
2190 setBoolValue(retval, false);
2191 return true;
2192 }
2193
2194 if (!evaluateExpr(st, args->expr, &a2))
2195 return false;
2196
2197 if (a2.type == PGBT_NULL)
2198 {
2199 setNullValue(retval);
2200 return true;
2201 }
2202 else if (!coerceToBool(&a2, &ba2))
2203 return false;
2204 else
2205 {
2206 setBoolValue(retval, ba2);
2207 return true;
2208 }
2209
2210 return true;
2211
2212 case PGBENCH_OR:
2213
2214 if (a1.type == PGBT_NULL)
2215 {
2216 setNullValue(retval);
2217 return true;
2218 }
2219
2220 if (!coerceToBool(&a1, &ba1))
2221 return false;
2222
2223 if (ba1)
2224 {
2225 setBoolValue(retval, true);
2226 return true;
2227 }
2228
2229 if (!evaluateExpr(st, args->expr, &a2))
2230 return false;
2231
2232 if (a2.type == PGBT_NULL)
2233 {
2234 setNullValue(retval);
2235 return true;
2236 }
2237 else if (!coerceToBool(&a2, &ba2))
2238 return false;
2239 else
2240 {
2241 setBoolValue(retval, ba2);
2242 return true;
2243 }
2244
2245 case PGBENCH_CASE:
2246 /* when true, execute branch */
2247 if (valueTruth(&a1))
2248 return evaluateExpr(st, args->expr, retval);
2249
2250 /* now args contains next condition or final else expression */
2251 args = args->next;
2252
2253 /* final else case? */
2254 if (args->next == NULL)
2255 return evaluateExpr(st, args->expr, retval);
2256
2257 /* no, another when, proceed */
2258 return evalLazyFunc(st, PGBENCH_CASE, args, retval);
2259
2260 default:
2261 /* internal error, cannot get here */
2262 Assert(0);
2263 break;
2264 }
2265 return false;
2266}
2267
2268/* maximum number of function arguments */
2269#define MAX_FARGS 16
2270
2271/*
2272 * Recursive evaluation of standard functions,
2273 * which do not require lazy evaluation.
2274 */
2275static bool
2278 PgBenchValue *retval)
2279{
2280 /* evaluate all function arguments */
2281 int nargs = 0;
2282 PgBenchExprLink *l = args;
2283 bool has_null = false;
2284
2285 /*
2286 * This value is double braced to workaround GCC bug 53119, which seems to
2287 * exist at least on gcc (Debian 4.7.2-5) 4.7.2, 32-bit.
2288 */
2289 PgBenchValue vargs[MAX_FARGS] = {{0}};
2290
2291 for (nargs = 0; nargs < MAX_FARGS && l != NULL; nargs++, l = l->next)
2292 {
2293 if (!evaluateExpr(st, l->expr, &vargs[nargs]))
2294 return false;
2295 has_null |= vargs[nargs].type == PGBT_NULL;
2296 }
2297
2298 if (l != NULL)
2299 {
2300 pg_log_error("too many function arguments, maximum is %d", MAX_FARGS);
2301 return false;
2302 }
2303
2304 /* NULL arguments */
2305 if (has_null && func != PGBENCH_IS && func != PGBENCH_DEBUG)
2306 {
2307 setNullValue(retval);
2308 return true;
2309 }
2310
2311 /* then evaluate function */
2312 switch (func)
2313 {
2314 /* overloaded operators */
2315 case PGBENCH_ADD:
2316 case PGBENCH_SUB:
2317 case PGBENCH_MUL:
2318 case PGBENCH_DIV:
2319 case PGBENCH_MOD:
2320 case PGBENCH_EQ:
2321 case PGBENCH_NE:
2322 case PGBENCH_LE:
2323 case PGBENCH_LT:
2324 {
2325 PgBenchValue *lval = &vargs[0],
2326 *rval = &vargs[1];
2327
2328 Assert(nargs == 2);
2329
2330 /* overloaded type management, double if some double */
2331 if ((lval->type == PGBT_DOUBLE ||
2332 rval->type == PGBT_DOUBLE) && func != PGBENCH_MOD)
2333 {
2334 double ld,
2335 rd;
2336
2337 if (!coerceToDouble(lval, &ld) ||
2338 !coerceToDouble(rval, &rd))
2339 return false;
2340
2341 switch (func)
2342 {
2343 case PGBENCH_ADD:
2344 setDoubleValue(retval, ld + rd);
2345 return true;
2346
2347 case PGBENCH_SUB:
2348 setDoubleValue(retval, ld - rd);
2349 return true;
2350
2351 case PGBENCH_MUL:
2352 setDoubleValue(retval, ld * rd);
2353 return true;
2354
2355 case PGBENCH_DIV:
2356 setDoubleValue(retval, ld / rd);
2357 return true;
2358
2359 case PGBENCH_EQ:
2360 setBoolValue(retval, ld == rd);
2361 return true;
2362
2363 case PGBENCH_NE:
2364 setBoolValue(retval, ld != rd);
2365 return true;
2366
2367 case PGBENCH_LE:
2368 setBoolValue(retval, ld <= rd);
2369 return true;
2370
2371 case PGBENCH_LT:
2372 setBoolValue(retval, ld < rd);
2373 return true;
2374
2375 default:
2376 /* cannot get here */
2377 Assert(0);
2378 }
2379 }
2380 else /* we have integer operands, or % */
2381 {
2382 int64 li,
2383 ri,
2384 res;
2385
2386 if (!coerceToInt(lval, &li) ||
2387 !coerceToInt(rval, &ri))
2388 return false;
2389
2390 switch (func)
2391 {
2392 case PGBENCH_ADD:
2393 if (pg_add_s64_overflow(li, ri, &res))
2394 {
2395 pg_log_error("bigint add out of range");
2396 return false;
2397 }
2398 setIntValue(retval, res);
2399 return true;
2400
2401 case PGBENCH_SUB:
2402 if (pg_sub_s64_overflow(li, ri, &res))
2403 {
2404 pg_log_error("bigint sub out of range");
2405 return false;
2406 }
2407 setIntValue(retval, res);
2408 return true;
2409
2410 case PGBENCH_MUL:
2411 if (pg_mul_s64_overflow(li, ri, &res))
2412 {
2413 pg_log_error("bigint mul out of range");
2414 return false;
2415 }
2416 setIntValue(retval, res);
2417 return true;
2418
2419 case PGBENCH_EQ:
2420 setBoolValue(retval, li == ri);
2421 return true;
2422
2423 case PGBENCH_NE:
2424 setBoolValue(retval, li != ri);
2425 return true;
2426
2427 case PGBENCH_LE:
2428 setBoolValue(retval, li <= ri);
2429 return true;
2430
2431 case PGBENCH_LT:
2432 setBoolValue(retval, li < ri);
2433 return true;
2434
2435 case PGBENCH_DIV:
2436 case PGBENCH_MOD:
2437 if (ri == 0)
2438 {
2439 pg_log_error("division by zero");
2440 return false;
2441 }
2442 /* special handling of -1 divisor */
2443 if (ri == -1)
2444 {
2445 if (func == PGBENCH_DIV)
2446 {
2447 /* overflow check (needed for INT64_MIN) */
2448 if (li == PG_INT64_MIN)
2449 {
2450 pg_log_error("bigint div out of range");
2451 return false;
2452 }
2453 else
2454 setIntValue(retval, -li);
2455 }
2456 else
2457 setIntValue(retval, 0);
2458 return true;
2459 }
2460 /* else divisor is not -1 */
2461 if (func == PGBENCH_DIV)
2462 setIntValue(retval, li / ri);
2463 else /* func == PGBENCH_MOD */
2464 setIntValue(retval, li % ri);
2465
2466 return true;
2467
2468 default:
2469 /* cannot get here */
2470 Assert(0);
2471 }
2472 }
2473
2474 Assert(0);
2475 return false; /* NOTREACHED */
2476 }
2477
2478 /* integer bitwise operators */
2479 case PGBENCH_BITAND:
2480 case PGBENCH_BITOR:
2481 case PGBENCH_BITXOR:
2482 case PGBENCH_LSHIFT:
2483 case PGBENCH_RSHIFT:
2484 {
2485 int64 li,
2486 ri;
2487
2488 if (!coerceToInt(&vargs[0], &li) || !coerceToInt(&vargs[1], &ri))
2489 return false;
2490
2491 if (func == PGBENCH_BITAND)
2492 setIntValue(retval, li & ri);
2493 else if (func == PGBENCH_BITOR)
2494 setIntValue(retval, li | ri);
2495 else if (func == PGBENCH_BITXOR)
2496 setIntValue(retval, li ^ ri);
2497 else if (func == PGBENCH_LSHIFT)
2498 setIntValue(retval, li << ri);
2499 else if (func == PGBENCH_RSHIFT)
2500 setIntValue(retval, li >> ri);
2501 else /* cannot get here */
2502 Assert(0);
2503
2504 return true;
2505 }
2506
2507 /* logical operators */
2508 case PGBENCH_NOT:
2509 {
2510 bool b;
2511
2512 if (!coerceToBool(&vargs[0], &b))
2513 return false;
2514
2515 setBoolValue(retval, !b);
2516 return true;
2517 }
2518
2519 /* no arguments */
2520 case PGBENCH_PI:
2521 setDoubleValue(retval, M_PI);
2522 return true;
2523
2524 /* 1 overloaded argument */
2525 case PGBENCH_ABS:
2526 {
2527 PgBenchValue *varg = &vargs[0];
2528
2529 Assert(nargs == 1);
2530
2531 if (varg->type == PGBT_INT)
2532 {
2533 int64 i = varg->u.ival;
2534
2535 setIntValue(retval, i < 0 ? -i : i);
2536 }
2537 else
2538 {
2539 double d = varg->u.dval;
2540
2541 Assert(varg->type == PGBT_DOUBLE);
2542 setDoubleValue(retval, d < 0.0 ? -d : d);
2543 }
2544
2545 return true;
2546 }
2547
2548 case PGBENCH_DEBUG:
2549 {
2550 PgBenchValue *varg = &vargs[0];
2551
2552 Assert(nargs == 1);
2553
2554 fprintf(stderr, "debug(script=%d,command=%d): ",
2555 st->use_file, st->command + 1);
2556
2557 if (varg->type == PGBT_NULL)
2558 fprintf(stderr, "null\n");
2559 else if (varg->type == PGBT_BOOLEAN)
2560 fprintf(stderr, "boolean %s\n", varg->u.bval ? "true" : "false");
2561 else if (varg->type == PGBT_INT)
2562 fprintf(stderr, "int " INT64_FORMAT "\n", varg->u.ival);
2563 else if (varg->type == PGBT_DOUBLE)
2564 fprintf(stderr, "double %.*g\n", DBL_DIG, varg->u.dval);
2565 else /* internal error, unexpected type */
2566 Assert(0);
2567
2568 *retval = *varg;
2569
2570 return true;
2571 }
2572
2573 /* 1 double argument */
2574 case PGBENCH_DOUBLE:
2575 case PGBENCH_SQRT:
2576 case PGBENCH_LN:
2577 case PGBENCH_EXP:
2578 {
2579 double dval;
2580
2581 Assert(nargs == 1);
2582
2583 if (!coerceToDouble(&vargs[0], &dval))
2584 return false;
2585
2586 if (func == PGBENCH_SQRT)
2587 dval = sqrt(dval);
2588 else if (func == PGBENCH_LN)
2589 dval = log(dval);
2590 else if (func == PGBENCH_EXP)
2591 dval = exp(dval);
2592 /* else is cast: do nothing */
2593
2594 setDoubleValue(retval, dval);
2595 return true;
2596 }
2597
2598 /* 1 int argument */
2599 case PGBENCH_INT:
2600 {
2601 int64 ival;
2602
2603 Assert(nargs == 1);
2604
2605 if (!coerceToInt(&vargs[0], &ival))
2606 return false;
2607
2608 setIntValue(retval, ival);
2609 return true;
2610 }
2611
2612 /* variable number of arguments */
2613 case PGBENCH_LEAST:
2614 case PGBENCH_GREATEST:
2615 {
2616 bool havedouble;
2617 int i;
2618
2619 Assert(nargs >= 1);
2620
2621 /* need double result if any input is double */
2622 havedouble = false;
2623 for (i = 0; i < nargs; i++)
2624 {
2625 if (vargs[i].type == PGBT_DOUBLE)
2626 {
2627 havedouble = true;
2628 break;
2629 }
2630 }
2631 if (havedouble)
2632 {
2633 double extremum;
2634
2635 if (!coerceToDouble(&vargs[0], &extremum))
2636 return false;
2637 for (i = 1; i < nargs; i++)
2638 {
2639 double dval;
2640
2641 if (!coerceToDouble(&vargs[i], &dval))
2642 return false;
2643 if (func == PGBENCH_LEAST)
2644 extremum = Min(extremum, dval);
2645 else
2646 extremum = Max(extremum, dval);
2647 }
2648 setDoubleValue(retval, extremum);
2649 }
2650 else
2651 {
2652 int64 extremum;
2653
2654 if (!coerceToInt(&vargs[0], &extremum))
2655 return false;
2656 for (i = 1; i < nargs; i++)
2657 {
2658 int64 ival;
2659
2660 if (!coerceToInt(&vargs[i], &ival))
2661 return false;
2662 if (func == PGBENCH_LEAST)
2663 extremum = Min(extremum, ival);
2664 else
2665 extremum = Max(extremum, ival);
2666 }
2667 setIntValue(retval, extremum);
2668 }
2669 return true;
2670 }
2671
2672 /* random functions */
2673 case PGBENCH_RANDOM:
2677 {
2678 int64 imin,
2679 imax,
2680 delta;
2681
2682 Assert(nargs >= 2);
2683
2684 if (!coerceToInt(&vargs[0], &imin) ||
2685 !coerceToInt(&vargs[1], &imax))
2686 return false;
2687
2688 /* check random range */
2689 if (unlikely(imin > imax))
2690 {
2691 pg_log_error("empty range given to random");
2692 return false;
2693 }
2694 else if (unlikely(pg_sub_s64_overflow(imax, imin, &delta) ||
2695 pg_add_s64_overflow(delta, 1, &delta)))
2696 {
2697 /* prevent int overflows in random functions */
2698 pg_log_error("random range is too large");
2699 return false;
2700 }
2701
2702 if (func == PGBENCH_RANDOM)
2703 {
2704 Assert(nargs == 2);
2705 setIntValue(retval, getrand(&st->cs_func_rs, imin, imax));
2706 }
2707 else /* gaussian & exponential */
2708 {
2709 double param;
2710
2711 Assert(nargs == 3);
2712
2713 if (!coerceToDouble(&vargs[2], &param))
2714 return false;
2715
2716 if (func == PGBENCH_RANDOM_GAUSSIAN)
2717 {
2718 if (param < MIN_GAUSSIAN_PARAM)
2719 {
2720 pg_log_error("gaussian parameter must be at least %f (not %f)",
2721 MIN_GAUSSIAN_PARAM, param);
2722 return false;
2723 }
2724
2725 setIntValue(retval,
2727 imin, imax, param));
2728 }
2729 else if (func == PGBENCH_RANDOM_ZIPFIAN)
2730 {
2731 if (param < MIN_ZIPFIAN_PARAM || param > MAX_ZIPFIAN_PARAM)
2732 {
2733 pg_log_error("zipfian parameter must be in range [%.3f, %.0f] (not %f)",
2735 return false;
2736 }
2737
2738 setIntValue(retval,
2739 getZipfianRand(&st->cs_func_rs, imin, imax, param));
2740 }
2741 else /* exponential */
2742 {
2743 if (param <= 0.0)
2744 {
2745 pg_log_error("exponential parameter must be greater than zero (not %f)",
2746 param);
2747 return false;
2748 }
2749
2750 setIntValue(retval,
2752 imin, imax, param));
2753 }
2754 }
2755
2756 return true;
2757 }
2758
2759 case PGBENCH_POW:
2760 {
2761 PgBenchValue *lval = &vargs[0];
2762 PgBenchValue *rval = &vargs[1];
2763 double ld,
2764 rd;
2765
2766 Assert(nargs == 2);
2767
2768 if (!coerceToDouble(lval, &ld) ||
2769 !coerceToDouble(rval, &rd))
2770 return false;
2771
2772 setDoubleValue(retval, pow(ld, rd));
2773
2774 return true;
2775 }
2776
2777 case PGBENCH_IS:
2778 {
2779 Assert(nargs == 2);
2780
2781 /*
2782 * note: this simple implementation is more permissive than
2783 * SQL
2784 */
2785 setBoolValue(retval,
2786 vargs[0].type == vargs[1].type &&
2787 vargs[0].u.bval == vargs[1].u.bval);
2788 return true;
2789 }
2790
2791 /* hashing */
2792 case PGBENCH_HASH_FNV1A:
2794 {
2795 int64 val,
2796 seed;
2797
2798 Assert(nargs == 2);
2799
2800 if (!coerceToInt(&vargs[0], &val) ||
2801 !coerceToInt(&vargs[1], &seed))
2802 return false;
2803
2804 if (func == PGBENCH_HASH_MURMUR2)
2805 setIntValue(retval, getHashMurmur2(val, seed));
2806 else if (func == PGBENCH_HASH_FNV1A)
2807 setIntValue(retval, getHashFnv1a(val, seed));
2808 else
2809 /* cannot get here */
2810 Assert(0);
2811
2812 return true;
2813 }
2814
2815 case PGBENCH_PERMUTE:
2816 {
2817 int64 val,
2818 size,
2819 seed;
2820
2821 Assert(nargs == 3);
2822
2823 if (!coerceToInt(&vargs[0], &val) ||
2824 !coerceToInt(&vargs[1], &size) ||
2825 !coerceToInt(&vargs[2], &seed))
2826 return false;
2827
2828 if (size <= 0)
2829 {
2830 pg_log_error("permute size parameter must be greater than zero");
2831 return false;
2832 }
2833
2834 setIntValue(retval, permute(val, size, seed));
2835 return true;
2836 }
2837
2838 default:
2839 /* cannot get here */
2840 Assert(0);
2841 /* dead code to avoid a compiler warning */
2842 return false;
2843 }
2844}
2845
2846/* evaluate some function */
2847static bool
2848evalFunc(CState *st,
2850{
2851 if (isLazyFunc(func))
2852 return evalLazyFunc(st, func, args, retval);
2853 else
2854 return evalStandardFunc(st, func, args, retval);
2855}
2856
2857/*
2858 * Recursive evaluation of an expression in a pgbench script
2859 * using the current state of variables.
2860 * Returns whether the evaluation was ok,
2861 * the value itself is returned through the retval pointer.
2862 */
2863static bool
2864evaluateExpr(CState *st, PgBenchExpr *expr, PgBenchValue *retval)
2865{
2866 switch (expr->etype)
2867 {
2868 case ENODE_CONSTANT:
2869 {
2870 *retval = expr->u.constant;
2871 return true;
2872 }
2873
2874 case ENODE_VARIABLE:
2875 {
2876 Variable *var;
2877
2878 if ((var = lookupVariable(&st->variables, expr->u.variable.varname)) == NULL)
2879 {
2880 pg_log_error("undefined variable \"%s\"", expr->u.variable.varname);
2881 return false;
2882 }
2883
2884 if (!makeVariableValue(var))
2885 return false;
2886
2887 *retval = var->value;
2888 return true;
2889 }
2890
2891 case ENODE_FUNCTION:
2892 return evalFunc(st,
2893 expr->u.function.function,
2894 expr->u.function.args,
2895 retval);
2896
2897 default:
2898 /* internal error which should never occur */
2899 pg_fatal("unexpected enode type in evaluation: %d", expr->etype);
2900 }
2901}
2902
2903/*
2904 * Convert command name to meta-command enum identifier
2905 */
2907getMetaCommand(const char *cmd)
2908{
2909 MetaCommand mc;
2910
2911 if (cmd == NULL)
2912 mc = META_NONE;
2913 else if (pg_strcasecmp(cmd, "set") == 0)
2914 mc = META_SET;
2915 else if (pg_strcasecmp(cmd, "setshell") == 0)
2916 mc = META_SETSHELL;
2917 else if (pg_strcasecmp(cmd, "shell") == 0)
2918 mc = META_SHELL;
2919 else if (pg_strcasecmp(cmd, "sleep") == 0)
2920 mc = META_SLEEP;
2921 else if (pg_strcasecmp(cmd, "if") == 0)
2922 mc = META_IF;
2923 else if (pg_strcasecmp(cmd, "elif") == 0)
2924 mc = META_ELIF;
2925 else if (pg_strcasecmp(cmd, "else") == 0)
2926 mc = META_ELSE;
2927 else if (pg_strcasecmp(cmd, "endif") == 0)
2928 mc = META_ENDIF;
2929 else if (pg_strcasecmp(cmd, "gset") == 0)
2930 mc = META_GSET;
2931 else if (pg_strcasecmp(cmd, "aset") == 0)
2932 mc = META_ASET;
2933 else if (pg_strcasecmp(cmd, "startpipeline") == 0)
2934 mc = META_STARTPIPELINE;
2935 else if (pg_strcasecmp(cmd, "syncpipeline") == 0)
2936 mc = META_SYNCPIPELINE;
2937 else if (pg_strcasecmp(cmd, "endpipeline") == 0)
2938 mc = META_ENDPIPELINE;
2939 else
2940 mc = META_NONE;
2941 return mc;
2942}
2943
2944/*
2945 * Run a shell command. The result is assigned to the variable if not NULL.
2946 * Return true if succeeded, or false on error.
2947 */
2948static bool
2949runShellCommand(Variables *variables, char *variable, char **argv, int argc)
2950{
2951 char command[SHELL_COMMAND_SIZE];
2952 int i,
2953 len = 0;
2954 FILE *fp;
2955 char res[64];
2956 char *endptr;
2957 int retval;
2958
2959 /*----------
2960 * Join arguments with whitespace separators. Arguments starting with
2961 * exactly one colon are treated as variables:
2962 * name - append a string "name"
2963 * :var - append a variable named 'var'
2964 * ::name - append a string ":name"
2965 *----------
2966 */
2967 for (i = 0; i < argc; i++)
2968 {
2969 char *arg;
2970 int arglen;
2971
2972 if (argv[i][0] != ':')
2973 {
2974 arg = argv[i]; /* a string literal */
2975 }
2976 else if (argv[i][1] == ':')
2977 {
2978 arg = argv[i] + 1; /* a string literal starting with colons */
2979 }
2980 else if ((arg = getVariable(variables, argv[i] + 1)) == NULL)
2981 {
2982 pg_log_error("%s: undefined variable \"%s\"", argv[0], argv[i]);
2983 return false;
2984 }
2985
2986 arglen = strlen(arg);
2987 if (len + arglen + (i > 0 ? 1 : 0) >= SHELL_COMMAND_SIZE - 1)
2988 {
2989 pg_log_error("%s: shell command is too long", argv[0]);
2990 return false;
2991 }
2992
2993 if (i > 0)
2994 command[len++] = ' ';
2995 memcpy(command + len, arg, arglen);
2996 len += arglen;
2997 }
2998
2999 command[len] = '\0';
3000
3001 fflush(NULL); /* needed before either system() or popen() */
3002
3003 /* Fast path for non-assignment case */
3004 if (variable == NULL)
3005 {
3006 if (system(command))
3007 {
3008 if (!timer_exceeded)
3009 pg_log_error("%s: could not launch shell command", argv[0]);
3010 return false;
3011 }
3012 return true;
3013 }
3014
3015 /* Execute the command with pipe and read the standard output. */
3016 if ((fp = popen(command, "r")) == NULL)
3017 {
3018 pg_log_error("%s: could not launch shell command", argv[0]);
3019 return false;
3020 }
3021 if (fgets(res, sizeof(res), fp) == NULL)
3022 {
3023 if (!timer_exceeded)
3024 pg_log_error("%s: could not read result of shell command", argv[0]);
3025 (void) pclose(fp);
3026 return false;
3027 }
3028 if (pclose(fp) < 0)
3029 {
3030 pg_log_error("%s: could not run shell command: %m", argv[0]);
3031 return false;
3032 }
3033
3034 /* Check whether the result is an integer and assign it to the variable */
3035 retval = (int) strtol(res, &endptr, 10);
3036 while (*endptr != '\0' && isspace((unsigned char) *endptr))
3037 endptr++;
3038 if (*res == '\0' || *endptr != '\0')
3039 {
3040 pg_log_error("%s: shell command must return an integer (not \"%s\")", argv[0], res);
3041 return false;
3042 }
3043 if (!putVariableInt(variables, "setshell", variable, retval))
3044 return false;
3045
3046 pg_log_debug("%s: shell parameter name: \"%s\", value: \"%s\"", argv[0], argv[1], res);
3047
3048 return true;
3049}
3050
3051/*
3052 * Report the abortion of the client when processing SQL commands.
3053 */
3054static void
3055commandFailed(CState *st, const char *cmd, const char *message)
3056{
3057 pg_log_error("client %d aborted in command %d (%s) of script %d; %s",
3058 st->id, st->command, cmd, st->use_file, message);
3059}
3060
3061/*
3062 * Report the error in the command while the script is executing.
3063 */
3064static void
3065commandError(CState *st, const char *message)
3066{
3068 pg_log_info("client %d got an error in command %d (SQL) of script %d; %s",
3069 st->id, st->command, st->use_file, message);
3070}
3071
3072/* return a script number with a weighted choice. */
3073static int
3074chooseScript(TState *thread)
3075{
3076 int i = 0;
3077 int64 w;
3078
3079 if (num_scripts == 1)
3080 return 0;
3081
3082 w = getrand(&thread->ts_choose_rs, 0, total_weight - 1);
3083 do
3084 {
3085 w -= sql_script[i++].weight;
3086 } while (w >= 0);
3087
3088 return i - 1;
3089}
3090
3091/*
3092 * Allocate space for CState->prepared: we need one boolean for each command
3093 * of each script.
3094 */
3095static void
3097{
3098 Assert(st->prepared == NULL);
3099
3100 st->prepared = pg_malloc(sizeof(bool *) * num_scripts);
3101 for (int i = 0; i < num_scripts; i++)
3102 {
3103 ParsedScript *script = &sql_script[i];
3104 int numcmds;
3105
3106 for (numcmds = 0; script->commands[numcmds] != NULL; numcmds++)
3107 ;
3108 st->prepared[i] = pg_malloc0(sizeof(bool) * numcmds);
3109 }
3110}
3111
3112/*
3113 * Prepare the SQL command from st->use_file at command_num.
3114 */
3115static void
3116prepareCommand(CState *st, int command_num)
3117{
3118 Command *command = sql_script[st->use_file].commands[command_num];
3119
3120 /* No prepare for non-SQL commands */
3121 if (command->type != SQL_COMMAND)
3122 return;
3123
3124 if (!st->prepared)
3126
3127 if (!st->prepared[st->use_file][command_num])
3128 {
3129 PGresult *res;
3130
3131 pg_log_debug("client %d preparing %s", st->id, command->prepname);
3132 res = PQprepare(st->con, command->prepname,
3133 command->argv[0], command->argc - 1, NULL);
3135 pg_log_error("%s", PQerrorMessage(st->con));
3136 PQclear(res);
3137 st->prepared[st->use_file][command_num] = true;
3138 }
3139}
3140
3141/*
3142 * Prepare all the commands in the script that come after the \startpipeline
3143 * that's at position st->command, and the first \endpipeline we find.
3144 *
3145 * This sets the ->prepared flag for each relevant command as well as the
3146 * \startpipeline itself, but doesn't move the st->command counter.
3147 */
3148static void
3150{
3151 int j;
3152 Command **commands = sql_script[st->use_file].commands;
3153
3154 Assert(commands[st->command]->type == META_COMMAND &&
3155 commands[st->command]->meta == META_STARTPIPELINE);
3156
3157 if (!st->prepared)
3159
3160 /*
3161 * We set the 'prepared' flag on the \startpipeline itself to flag that we
3162 * don't need to do this next time without calling prepareCommand(), even
3163 * though we don't actually prepare this command.
3164 */
3165 if (st->prepared[st->use_file][st->command])
3166 return;
3167
3168 for (j = st->command + 1; commands[j] != NULL; j++)
3169 {
3170 if (commands[j]->type == META_COMMAND &&
3171 commands[j]->meta == META_ENDPIPELINE)
3172 break;
3173
3174 prepareCommand(st, j);
3175 }
3176
3177 st->prepared[st->use_file][st->command] = true;
3178}
3179
3180/* Send a SQL command, using the chosen querymode */
3181static bool
3182sendCommand(CState *st, Command *command)
3183{
3184 int r;
3185
3186 if (querymode == QUERY_SIMPLE)
3187 {
3188 char *sql;
3189
3190 sql = pg_strdup(command->argv[0]);
3191 sql = assignVariables(&st->variables, sql);
3192
3193 pg_log_debug("client %d sending %s", st->id, sql);
3194 r = PQsendQuery(st->con, sql);
3195 free(sql);
3196 }
3197 else if (querymode == QUERY_EXTENDED)
3198 {
3199 const char *sql = command->argv[0];
3200 const char *params[MAX_ARGS];
3201
3202 getQueryParams(&st->variables, command, params);
3203
3204 pg_log_debug("client %d sending %s", st->id, sql);
3205 r = PQsendQueryParams(st->con, sql, command->argc - 1,
3206 NULL, params, NULL, NULL, 0);
3207 }
3208 else if (querymode == QUERY_PREPARED)
3209 {
3210 const char *params[MAX_ARGS];
3211
3212 prepareCommand(st, st->command);
3213 getQueryParams(&st->variables, command, params);
3214
3215 pg_log_debug("client %d sending %s", st->id, command->prepname);
3216 r = PQsendQueryPrepared(st->con, command->prepname, command->argc - 1,
3217 params, NULL, NULL, 0);
3218 }
3219 else /* unknown sql mode */
3220 r = 0;
3221
3222 if (r == 0)
3223 {
3224 pg_log_debug("client %d could not send %s", st->id, command->argv[0]);
3225 return false;
3226 }
3227 else
3228 return true;
3229}
3230
3231/*
3232 * Get the error status from the error code.
3233 */
3235getSQLErrorStatus(const char *sqlState)
3236{
3237 if (sqlState != NULL)
3238 {
3239 if (strcmp(sqlState, ERRCODE_T_R_SERIALIZATION_FAILURE) == 0)
3241 else if (strcmp(sqlState, ERRCODE_T_R_DEADLOCK_DETECTED) == 0)
3243 }
3244
3246}
3247
3248/*
3249 * Returns true if this type of error can be retried.
3250 */
3251static bool
3252canRetryError(EStatus estatus)
3253{
3254 return (estatus == ESTATUS_SERIALIZATION_ERROR ||
3255 estatus == ESTATUS_DEADLOCK_ERROR);
3256}
3257
3258/*
3259 * Process query response from the backend.
3260 *
3261 * If varprefix is not NULL, it's the variable name prefix where to store
3262 * the results of the *last* command (META_GSET) or *all* commands
3263 * (META_ASET).
3264 *
3265 * Returns true if everything is A-OK, false if any error occurs.
3266 */
3267static bool
3268readCommandResponse(CState *st, MetaCommand meta, char *varprefix)
3269{
3270 PGresult *res;
3271 PGresult *next_res;
3272 int qrynum = 0;
3273
3274 /*
3275 * varprefix should be set only with \gset or \aset, and \endpipeline and
3276 * SQL commands do not need it.
3277 */
3278 Assert((meta == META_NONE && varprefix == NULL) ||
3279 ((meta == META_ENDPIPELINE) && varprefix == NULL) ||
3280 ((meta == META_GSET || meta == META_ASET) && varprefix != NULL));
3281
3282 res = PQgetResult(st->con);
3283
3284 while (res != NULL)
3285 {
3286 bool is_last;
3287
3288 /* peek at the next result to know whether the current is last */
3289 next_res = PQgetResult(st->con);
3290 is_last = (next_res == NULL);
3291
3292 switch (PQresultStatus(res))
3293 {
3294 case PGRES_COMMAND_OK: /* non-SELECT commands */
3295 case PGRES_EMPTY_QUERY: /* may be used for testing no-op overhead */
3296 if (is_last && meta == META_GSET)
3297 {
3298 pg_log_error("client %d script %d command %d query %d: expected one row, got %d",
3299 st->id, st->use_file, st->command, qrynum, 0);
3301 goto error;
3302 }
3303 break;
3304
3305 case PGRES_TUPLES_OK:
3306 if ((is_last && meta == META_GSET) || meta == META_ASET)
3307 {
3308 int ntuples = PQntuples(res);
3309
3310 if (meta == META_GSET && ntuples != 1)
3311 {
3312 /* under \gset, report the error */
3313 pg_log_error("client %d script %d command %d query %d: expected one row, got %d",
3314 st->id, st->use_file, st->command, qrynum, PQntuples(res));
3316 goto error;
3317 }
3318 else if (meta == META_ASET && ntuples <= 0)
3319 {
3320 /* coldly skip empty result under \aset */
3321 break;
3322 }
3323
3324 /* store results into variables */
3325 for (int fld = 0; fld < PQnfields(res); fld++)
3326 {
3327 char *varname = PQfname(res, fld);
3328
3329 /* allocate varname only if necessary, freed below */
3330 if (*varprefix != '\0')
3331 varname = psprintf("%s%s", varprefix, varname);
3332
3333 /* store last row result as a string */
3334 if (!putVariable(&st->variables, meta == META_ASET ? "aset" : "gset", varname,
3335 PQgetvalue(res, ntuples - 1, fld)))
3336 {
3337 /* internal error */
3338 pg_log_error("client %d script %d command %d query %d: error storing into variable %s",
3339 st->id, st->use_file, st->command, qrynum, varname);
3341 goto error;
3342 }
3343
3344 if (*varprefix != '\0')
3345 pg_free(varname);
3346 }
3347 }
3348 /* otherwise the result is simply thrown away by PQclear below */
3349 break;
3350
3352 pg_log_debug("client %d pipeline ending, ongoing syncs: %d",
3353 st->id, st->num_syncs);
3354 st->num_syncs--;
3355 if (st->num_syncs == 0 && PQexitPipelineMode(st->con) != 1)
3356 pg_log_error("client %d failed to exit pipeline mode: %s", st->id,
3357 PQerrorMessage(st->con));
3358 break;
3359
3361 case PGRES_FATAL_ERROR:
3364 if (canRetryError(st->estatus))
3365 {
3366 if (verbose_errors)
3368 goto error;
3369 }
3370 /* fall through */
3371
3372 default:
3373 /* anything else is unexpected */
3374 pg_log_error("client %d script %d aborted in command %d query %d: %s",
3375 st->id, st->use_file, st->command, qrynum,
3376 PQerrorMessage(st->con));
3377 goto error;
3378 }
3379
3380 PQclear(res);
3381 qrynum++;
3382 res = next_res;
3383 }
3384
3385 if (qrynum == 0)
3386 {
3387 pg_log_error("client %d command %d: no results", st->id, st->command);
3388 return false;
3389 }
3390
3391 return true;
3392
3393error:
3394 PQclear(res);
3395 PQclear(next_res);
3396 do
3397 {
3398 res = PQgetResult(st->con);
3399 PQclear(res);
3400 } while (res);
3401
3402 return false;
3403}
3404
3405/*
3406 * Parse the argument to a \sleep command, and return the requested amount
3407 * of delay, in microseconds. Returns true on success, false on error.
3408 */
3409static bool
3410evaluateSleep(Variables *variables, int argc, char **argv, int *usecs)
3411{
3412 char *var;
3413 int usec;
3414
3415 if (*argv[1] == ':')
3416 {
3417 if ((var = getVariable(variables, argv[1] + 1)) == NULL)
3418 {
3419 pg_log_error("%s: undefined variable \"%s\"", argv[0], argv[1] + 1);
3420 return false;
3421 }
3422
3423 usec = atoi(var);
3424
3425 /* Raise an error if the value of a variable is not a number */
3426 if (usec == 0 && !isdigit((unsigned char) *var))
3427 {
3428 pg_log_error("%s: invalid sleep time \"%s\" for variable \"%s\"",
3429 argv[0], var, argv[1] + 1);
3430 return false;
3431 }
3432 }
3433 else
3434 usec = atoi(argv[1]);
3435
3436 if (argc > 2)
3437 {
3438 if (pg_strcasecmp(argv[2], "ms") == 0)
3439 usec *= 1000;
3440 else if (pg_strcasecmp(argv[2], "s") == 0)
3441 usec *= 1000000;
3442 }
3443 else
3444 usec *= 1000000;
3445
3446 *usecs = usec;
3447 return true;
3448}
3449
3450
3451/*
3452 * Returns true if the error can be retried.
3453 */
3454static bool
3456{
3458
3459 /* We can only retry serialization or deadlock errors. */
3460 if (!canRetryError(st->estatus))
3461 return false;
3462
3463 /*
3464 * We must have at least one option to limit the retrying of transactions
3465 * that got an error.
3466 */
3468
3469 /*
3470 * We cannot retry the error if we have reached the maximum number of
3471 * tries.
3472 */
3473 if (max_tries && st->tries >= max_tries)
3474 return false;
3475
3476 /*
3477 * We cannot retry the error if we spent too much time on this
3478 * transaction.
3479 */
3480 if (latency_limit)
3481 {
3483 if (*now - st->txn_scheduled > latency_limit)
3484 return false;
3485 }
3486
3487 /*
3488 * We cannot retry the error if the benchmark duration is over.
3489 */
3490 if (timer_exceeded)
3491 return false;
3492
3493 /* OK */
3494 return true;
3495}
3496
3497/*
3498 * Read results and discard it until a sync point.
3499 */
3500static int
3502{
3503 /* send a sync */
3504 if (!PQpipelineSync(st->con))
3505 {
3506 pg_log_error("client %d aborted: failed to send a pipeline sync",
3507 st->id);
3508 return 0;
3509 }
3510
3511 /* receive PGRES_PIPELINE_SYNC and null following it */
3512 for (;;)
3513 {
3514 PGresult *res = PQgetResult(st->con);
3515
3517 {
3518 PQclear(res);
3519 res = PQgetResult(st->con);
3520 Assert(res == NULL);
3521 break;
3522 }
3523 PQclear(res);
3524 }
3525
3526 /* exit pipeline */
3527 if (PQexitPipelineMode(st->con) != 1)
3528 {
3529 pg_log_error("client %d aborted: failed to exit pipeline mode for rolling back the failed transaction",
3530 st->id);
3531 return 0;
3532 }
3533 return 1;
3534}
3535
3536/*
3537 * Get the transaction status at the end of a command especially for
3538 * checking if we are in a (failed) transaction block.
3539 */
3542{
3543 PGTransactionStatusType tx_status;
3544
3545 tx_status = PQtransactionStatus(con);
3546 switch (tx_status)
3547 {
3548 case PQTRANS_IDLE:
3549 return TSTATUS_IDLE;
3550 case PQTRANS_INTRANS:
3551 case PQTRANS_INERROR:
3552 return TSTATUS_IN_BLOCK;
3553 case PQTRANS_UNKNOWN:
3554 /* PQTRANS_UNKNOWN is expected given a broken connection */
3555 if (PQstatus(con) == CONNECTION_BAD)
3556 return TSTATUS_CONN_ERROR;
3557 /* fall through */
3558 case PQTRANS_ACTIVE:
3559 default:
3560
3561 /*
3562 * We cannot find out whether we are in a transaction block or
3563 * not. Internal error which should never occur.
3564 */
3565 pg_log_error("unexpected transaction status %d", tx_status);
3566 return TSTATUS_OTHER_ERROR;
3567 }
3568
3569 /* not reached */
3570 Assert(false);
3571 return TSTATUS_OTHER_ERROR;
3572}
3573
3574/*
3575 * Print verbose messages of an error
3576 */
3577static void
3579{
3580 static PQExpBuffer buf = NULL;
3581
3582 if (buf == NULL)
3584 else
3586
3587 printfPQExpBuffer(buf, "client %d ", st->id);
3588 appendPQExpBufferStr(buf, (is_retry ?
3589 "repeats the transaction after the error" :
3590 "ends the failed transaction"));
3591 appendPQExpBuffer(buf, " (try %u", st->tries);
3592
3593 /* Print max_tries if it is not unlimited. */
3594 if (max_tries)
3596
3597 /*
3598 * If the latency limit is used, print a percentage of the current
3599 * transaction latency from the latency limit.
3600 */
3601 if (latency_limit)
3602 {
3604 appendPQExpBuffer(buf, ", %.3f%% of the maximum time of tries was used",
3605 (100.0 * (*now - st->txn_scheduled) / latency_limit));
3606 }
3607 appendPQExpBufferStr(buf, ")\n");
3608
3609 pg_log_info("%s", buf->data);
3610}
3611
3612/*
3613 * Advance the state machine of a connection.
3614 */
3615static void
3617{
3618
3619 /*
3620 * gettimeofday() isn't free, so we get the current timestamp lazily the
3621 * first time it's needed, and reuse the same value throughout this
3622 * function after that. This also ensures that e.g. the calculated
3623 * latency reported in the log file and in the totals are the same. Zero
3624 * means "not set yet". Reset "now" when we execute shell commands or
3625 * expressions, which might take a non-negligible amount of time, though.
3626 */
3627 pg_time_usec_t now = 0;
3628
3629 /*
3630 * Loop in the state machine, until we have to wait for a result from the
3631 * server or have to sleep for throttling or \sleep.
3632 *
3633 * Note: In the switch-statement below, 'break' will loop back here,
3634 * meaning "continue in the state machine". Return is used to return to
3635 * the caller, giving the thread the opportunity to advance another
3636 * client.
3637 */
3638 for (;;)
3639 {
3640 Command *command;
3641
3642 switch (st->state)
3643 {
3644 /* Select transaction (script) to run. */
3646 st->use_file = chooseScript(thread);
3648
3649 /* reset transaction variables to default values */
3651 st->tries = 1;
3652
3653 pg_log_debug("client %d executing script \"%s\"",
3654 st->id, sql_script[st->use_file].desc);
3655
3656 /*
3657 * If time is over, we're done; otherwise, get ready to start
3658 * a new transaction, or to get throttled if that's requested.
3659 */
3662 break;
3663
3664 /* Start new transaction (script) */
3665 case CSTATE_START_TX:
3667
3668 /* establish connection if needed, i.e. under --connect */
3669 if (st->con == NULL)
3670 {
3672
3673 if ((st->con = doConnect()) == NULL)
3674 {
3675 /*
3676 * as the bench is already running, we do not abort
3677 * the process
3678 */
3679 pg_log_error("client %d aborted while establishing connection", st->id);
3680 st->state = CSTATE_ABORTED;
3681 break;
3682 }
3683
3684 /* reset now after connection */
3685 now = pg_time_now();
3686
3687 thread->conn_duration += now - start;
3688
3689 /* Reset session-local state */
3690 pg_free(st->prepared);
3691 st->prepared = NULL;
3692 }
3693
3694 /*
3695 * It is the first try to run this transaction. Remember the
3696 * random state: maybe it will get an error and we will need
3697 * to run it again.
3698 */
3699 st->random_state = st->cs_func_rs;
3700
3701 /* record transaction start time */
3702 st->txn_begin = now;
3703
3704 /*
3705 * When not throttling, this is also the transaction's
3706 * scheduled start time.
3707 */
3708 if (!throttle_delay)
3709 st->txn_scheduled = now;
3710
3711 /* Begin with the first command */
3713 st->command = 0;
3714 break;
3715
3716 /*
3717 * Handle throttling once per transaction by sleeping.
3718 */
3720
3721 /*
3722 * Generate a delay such that the series of delays will
3723 * approximate a Poisson distribution centered on the
3724 * throttle_delay time.
3725 *
3726 * If transactions are too slow or a given wait is shorter
3727 * than a transaction, the next transaction will start right
3728 * away.
3729 */
3731
3732 thread->throttle_trigger +=
3734 st->txn_scheduled = thread->throttle_trigger;
3735
3736 /*
3737 * If --latency-limit is used, and this slot is already late
3738 * so that the transaction will miss the latency limit even if
3739 * it completed immediately, skip this time slot and loop to
3740 * reschedule.
3741 */
3742 if (latency_limit)
3743 {
3745
3746 if (thread->throttle_trigger < now - latency_limit)
3747 {
3748 processXactStats(thread, st, &now, true, agg);
3749
3750 /*
3751 * Finish client if -T or -t was exceeded.
3752 *
3753 * Stop counting skipped transactions under -T as soon
3754 * as the timer is exceeded. Because otherwise it can
3755 * take a very long time to count all of them
3756 * especially when quite a lot of them happen with
3757 * unrealistically high rate setting in -R, which
3758 * would prevent pgbench from ending immediately.
3759 * Because of this behavior, note that there is no
3760 * guarantee that all skipped transactions are counted
3761 * under -T though there is under -t. This is OK in
3762 * practice because it's very unlikely to happen with
3763 * realistic setting.
3764 */
3765 if (timer_exceeded || (nxacts > 0 && st->cnt >= nxacts))
3766 st->state = CSTATE_FINISHED;
3767
3768 /* Go back to top of loop with CSTATE_PREPARE_THROTTLE */
3769 break;
3770 }
3771 }
3772
3773 /*
3774 * stop client if next transaction is beyond pgbench end of
3775 * execution; otherwise, throttle it.
3776 */
3777 st->state = end_time > 0 && st->txn_scheduled > end_time ?
3779 break;
3780
3781 /*
3782 * Wait until it's time to start next transaction.
3783 */
3784 case CSTATE_THROTTLE:
3786
3787 if (now < st->txn_scheduled)
3788 return; /* still sleeping, nothing to do here */
3789
3790 /* done sleeping, but don't start transaction if we're done */
3792 break;
3793
3794 /*
3795 * Send a command to server (or execute a meta-command)
3796 */
3798 command = sql_script[st->use_file].commands[st->command];
3799
3800 /*
3801 * Transition to script end processing if done, but close up
3802 * shop if a pipeline is open at this point.
3803 */
3804 if (command == NULL)
3805 {
3807 st->state = CSTATE_END_TX;
3808 else
3809 {
3810 pg_log_error("client %d aborted: end of script reached with pipeline open",
3811 st->id);
3812 st->state = CSTATE_ABORTED;
3813 }
3814
3815 break;
3816 }
3817
3818 /* record begin time of next command, and initiate it */
3820 {
3822 st->stmt_begin = now;
3823 }
3824
3825 /* Execute the command */
3826 if (command->type == SQL_COMMAND)
3827 {
3828 /* disallow \aset and \gset in pipeline mode */
3830 {
3831 if (command->meta == META_GSET)
3832 {
3833 commandFailed(st, "gset", "\\gset is not allowed in pipeline mode");
3834 st->state = CSTATE_ABORTED;
3835 break;
3836 }
3837 else if (command->meta == META_ASET)
3838 {
3839 commandFailed(st, "aset", "\\aset is not allowed in pipeline mode");
3840 st->state = CSTATE_ABORTED;
3841 break;
3842 }
3843 }
3844
3845 if (!sendCommand(st, command))
3846 {
3847 commandFailed(st, "SQL", "SQL command send failed");
3848 st->state = CSTATE_ABORTED;
3849 }
3850 else
3851 {
3852 /* Wait for results, unless in pipeline mode */
3855 else
3857 }
3858 }
3859 else if (command->type == META_COMMAND)
3860 {
3861 /*-----
3862 * Possible state changes when executing meta commands:
3863 * - on errors CSTATE_ABORTED
3864 * - on sleep CSTATE_SLEEP
3865 * - else CSTATE_END_COMMAND
3866 */
3867 st->state = executeMetaCommand(st, &now);
3868 if (st->state == CSTATE_ABORTED)
3870 }
3871
3872 /*
3873 * We're now waiting for an SQL command to complete, or
3874 * finished processing a metacommand, or need to sleep, or
3875 * something bad happened.
3876 */
3878 st->state == CSTATE_END_COMMAND ||
3879 st->state == CSTATE_SLEEP ||
3880 st->state == CSTATE_ABORTED);
3881 break;
3882
3883 /*
3884 * non executed conditional branch
3885 */
3888 /* quickly skip commands until something to do... */
3889 while (true)
3890 {
3891 command = sql_script[st->use_file].commands[st->command];
3892
3893 /* cannot reach end of script in that state */
3894 Assert(command != NULL);
3895
3896 /*
3897 * if this is conditional related, update conditional
3898 * state
3899 */
3900 if (command->type == META_COMMAND &&
3901 (command->meta == META_IF ||
3902 command->meta == META_ELIF ||
3903 command->meta == META_ELSE ||
3904 command->meta == META_ENDIF))
3905 {
3906 switch (conditional_stack_peek(st->cstack))
3907 {
3908 case IFSTATE_FALSE:
3909 if (command->meta == META_IF)
3910 {
3911 /* nested if in skipped branch - ignore */
3914 st->command++;
3915 }
3916 else if (command->meta == META_ELIF)
3917 {
3918 /* we must evaluate the condition */
3920 }
3921 else if (command->meta == META_ELSE)
3922 {
3923 /* we must execute next command */
3927 st->command++;
3928 }
3929 else if (command->meta == META_ENDIF)
3930 {
3933 if (conditional_active(st->cstack))
3935 /* else state remains CSTATE_SKIP_COMMAND */
3936 st->command++;
3937 }
3938 break;
3939
3940 case IFSTATE_IGNORED:
3941 case IFSTATE_ELSE_FALSE:
3942 if (command->meta == META_IF)
3945 else if (command->meta == META_ENDIF)
3946 {
3949 if (conditional_active(st->cstack))
3951 }
3952 /* could detect "else" & "elif" after "else" */
3953 st->command++;
3954 break;
3955
3956 case IFSTATE_NONE:
3957 case IFSTATE_TRUE:
3958 case IFSTATE_ELSE_TRUE:
3959 default:
3960
3961 /*
3962 * inconsistent if inactive, unreachable dead
3963 * code
3964 */
3965 Assert(false);
3966 }
3967 }
3968 else
3969 {
3970 /* skip and consider next */
3971 st->command++;
3972 }
3973
3974 if (st->state != CSTATE_SKIP_COMMAND)
3975 /* out of quick skip command loop */
3976 break;
3977 }
3978 break;
3979
3980 /*
3981 * Wait for the current SQL command to complete
3982 */
3983 case CSTATE_WAIT_RESULT:
3984 pg_log_debug("client %d receiving", st->id);
3985
3986 /*
3987 * Only check for new network data if we processed all data
3988 * fetched prior. Otherwise we end up doing a syscall for each
3989 * individual pipelined query, which has a measurable
3990 * performance impact.
3991 */
3992 if (PQisBusy(st->con) && !PQconsumeInput(st->con))
3993 {
3994 /* there's something wrong */
3995 commandFailed(st, "SQL", "perhaps the backend died while processing");
3996 st->state = CSTATE_ABORTED;
3997 break;
3998 }
3999 if (PQisBusy(st->con))
4000 return; /* don't have the whole result yet */
4001
4002 /* store or discard the query results */
4003 if (readCommandResponse(st,
4006 {
4007 /*
4008 * outside of pipeline mode: stop reading results.
4009 * pipeline mode: continue reading results until an
4010 * end-of-pipeline response.
4011 */
4014 }
4015 else if (canRetryError(st->estatus))
4016 st->state = CSTATE_ERROR;
4017 else
4018 st->state = CSTATE_ABORTED;
4019 break;
4020
4021 /*
4022 * Wait until sleep is done. This state is entered after a
4023 * \sleep metacommand. The behavior is similar to
4024 * CSTATE_THROTTLE, but proceeds to CSTATE_START_COMMAND
4025 * instead of CSTATE_START_TX.
4026 */
4027 case CSTATE_SLEEP:
4029 if (now < st->sleep_until)
4030 return; /* still sleeping, nothing to do here */
4031 /* Else done sleeping. */
4033 break;
4034
4035 /*
4036 * End of command: record stats and proceed to next command.
4037 */
4038 case CSTATE_END_COMMAND:
4039
4040 /*
4041 * command completed: accumulate per-command execution times
4042 * in thread-local data structure, if per-command latencies
4043 * are requested.
4044 */
4046 {
4048
4049 command = sql_script[st->use_file].commands[st->command];
4050 /* XXX could use a mutex here, but we choose not to */
4051 addToSimpleStats(&command->stats,
4053 }
4054
4055 /* Go ahead with next command, to be executed or skipped */
4056 st->command++;
4057 st->state = conditional_active(st->cstack) ?
4059 break;
4060
4061 /*
4062 * Clean up after an error.
4063 */
4064 case CSTATE_ERROR:
4065 {
4066 TStatus tstatus;
4067
4069
4070 /* Clear the conditional stack */
4072
4073 /* Read and discard until a sync point in pipeline mode */
4075 {
4076 if (!discardUntilSync(st))
4077 {
4078 st->state = CSTATE_ABORTED;
4079 break;
4080 }
4081 }
4082
4083 /*
4084 * Check if we have a (failed) transaction block or not,
4085 * and roll it back if any.
4086 */
4087 tstatus = getTransactionStatus(st->con);
4088 if (tstatus == TSTATUS_IN_BLOCK)
4089 {
4090 /* Try to rollback a (failed) transaction block. */
4091 if (!PQsendQuery(st->con, "ROLLBACK"))
4092 {
4093 pg_log_error("client %d aborted: failed to send sql command for rolling back the failed transaction",
4094 st->id);
4095 st->state = CSTATE_ABORTED;
4096 }
4097 else
4099 }
4100 else if (tstatus == TSTATUS_IDLE)
4101 {
4102 /*
4103 * If time is over, we're done; otherwise, check if we
4104 * can retry the error.
4105 */
4108 }
4109 else
4110 {
4111 if (tstatus == TSTATUS_CONN_ERROR)
4112 pg_log_error("perhaps the backend died while processing");
4113
4114 pg_log_error("client %d aborted while receiving the transaction status", st->id);
4115 st->state = CSTATE_ABORTED;
4116 }
4117 break;
4118 }
4119
4120 /*
4121 * Wait for the rollback command to complete
4122 */
4124 {
4125 PGresult *res;
4126
4127 pg_log_debug("client %d receiving", st->id);
4128 if (!PQconsumeInput(st->con))
4129 {
4130 pg_log_error("client %d aborted while rolling back the transaction after an error; perhaps the backend died while processing",
4131 st->id);
4132 st->state = CSTATE_ABORTED;
4133 break;
4134 }
4135 if (PQisBusy(st->con))
4136 return; /* don't have the whole result yet */
4137
4138 /*
4139 * Read and discard the query result;
4140 */
4141 res = PQgetResult(st->con);
4142 switch (PQresultStatus(res))
4143 {
4144 case PGRES_COMMAND_OK:
4145 /* OK */
4146 PQclear(res);
4147 /* null must be returned */
4148 res = PQgetResult(st->con);
4149 Assert(res == NULL);
4150
4151 /*
4152 * If time is over, we're done; otherwise, check
4153 * if we can retry the error.
4154 */
4157 break;
4158 default:
4159 pg_log_error("client %d aborted while rolling back the transaction after an error; %s",
4160 st->id, PQerrorMessage(st->con));
4161 PQclear(res);
4162 st->state = CSTATE_ABORTED;
4163 break;
4164 }
4165 break;
4166 }
4167
4168 /*
4169 * Retry the transaction after an error.
4170 */
4171 case CSTATE_RETRY:
4172 command = sql_script[st->use_file].commands[st->command];
4173
4174 /*
4175 * Inform that the transaction will be retried after the
4176 * error.
4177 */
4178 if (verbose_errors)
4179 printVerboseErrorMessages(st, &now, true);
4180
4181 /* Count tries and retries */
4182 st->tries++;
4183 command->retries++;
4184
4185 /*
4186 * Reset the random state as they were at the beginning of the
4187 * transaction.
4188 */
4189 st->cs_func_rs = st->random_state;
4190
4191 /* Process the first transaction command. */
4192 st->command = 0;
4195 break;
4196
4197 /*
4198 * Record a failed transaction.
4199 */
4200 case CSTATE_FAILURE:
4201 command = sql_script[st->use_file].commands[st->command];
4202
4203 /* Accumulate the failure. */
4204 command->failures++;
4205
4206 /*
4207 * Inform that the failed transaction will not be retried.
4208 */
4209 if (verbose_errors)
4210 printVerboseErrorMessages(st, &now, false);
4211
4212 /* End the failed transaction. */
4213 st->state = CSTATE_END_TX;
4214 break;
4215
4216 /*
4217 * End of transaction (end of script, really).
4218 */
4219 case CSTATE_END_TX:
4220 {
4221 TStatus tstatus;
4222
4223 /* transaction finished: calculate latency and do log */
4224 processXactStats(thread, st, &now, false, agg);
4225
4226 /*
4227 * missing \endif... cannot happen if CheckConditional was
4228 * okay
4229 */
4231
4232 /*
4233 * We must complete all the transaction blocks that were
4234 * started in this script.
4235 */
4236 tstatus = getTransactionStatus(st->con);
4237 if (tstatus == TSTATUS_IN_BLOCK)
4238 {
4239 pg_log_error("client %d aborted: end of script reached without completing the last transaction",
4240 st->id);
4241 st->state = CSTATE_ABORTED;
4242 break;
4243 }
4244 else if (tstatus != TSTATUS_IDLE)
4245 {
4246 if (tstatus == TSTATUS_CONN_ERROR)
4247 pg_log_error("perhaps the backend died while processing");
4248
4249 pg_log_error("client %d aborted while receiving the transaction status", st->id);
4250 st->state = CSTATE_ABORTED;
4251 break;
4252 }
4253
4254 if (is_connect)
4255 {
4257
4259 finishCon(st);
4260 now = pg_time_now();
4261 thread->conn_duration += now - start;
4262 }
4263
4264 if ((st->cnt >= nxacts && duration <= 0) || timer_exceeded)
4265 {
4266 /* script completed */
4267 st->state = CSTATE_FINISHED;
4268 break;
4269 }
4270
4271 /* next transaction (script) */
4273
4274 /*
4275 * Ensure that we always return on this point, so as to
4276 * avoid an infinite loop if the script only contains meta
4277 * commands.
4278 */
4279 return;
4280 }
4281
4282 /*
4283 * Final states. Close the connection if it's still open.
4284 */
4285 case CSTATE_ABORTED:
4286 case CSTATE_FINISHED:
4287
4288 /*
4289 * Don't measure the disconnection delays here even if in
4290 * CSTATE_FINISHED and -C/--connect option is specified.
4291 * Because in this case all the connections that this thread
4292 * established are closed at the end of transactions and the
4293 * disconnection delays should have already been measured at
4294 * that moment.
4295 *
4296 * In CSTATE_ABORTED state, the measurement is no longer
4297 * necessary because we cannot report complete results anyways
4298 * in this case.
4299 */
4300 finishCon(st);
4301 return;
4302 }
4303 }
4304}
4305
4306/*
4307 * Subroutine for advanceConnectionState -- initiate or execute the current
4308 * meta command, and return the next state to set.
4309 *
4310 * *now is updated to the current time, unless the command is expected to
4311 * take no time to execute.
4312 */
4315{
4316 Command *command = sql_script[st->use_file].commands[st->command];
4317 int argc;
4318 char **argv;
4319
4320 Assert(command != NULL && command->type == META_COMMAND);
4321
4322 argc = command->argc;
4323 argv = command->argv;
4324
4326 {
4328
4330
4331 printfPQExpBuffer(&buf, "client %d executing \\%s", st->id, argv[0]);
4332 for (int i = 1; i < argc; i++)
4333 appendPQExpBuffer(&buf, " %s", argv[i]);
4334
4335 pg_log_debug("%s", buf.data);
4336
4338 }
4339
4340 if (command->meta == META_SLEEP)
4341 {
4342 int usec;
4343
4344 /*
4345 * A \sleep doesn't execute anything, we just get the delay from the
4346 * argument, and enter the CSTATE_SLEEP state. (The per-command
4347 * latency will be recorded in CSTATE_SLEEP state, not here, after the
4348 * delay has elapsed.)
4349 */
4350 if (!evaluateSleep(&st->variables, argc, argv, &usec))
4351 {
4352 commandFailed(st, "sleep", "execution of meta-command failed");
4353 return CSTATE_ABORTED;
4354 }
4355
4357 st->sleep_until = (*now) + usec;
4358 return CSTATE_SLEEP;
4359 }
4360 else if (command->meta == META_SET)
4361 {
4362 PgBenchExpr *expr = command->expr;
4363 PgBenchValue result;
4364
4365 if (!evaluateExpr(st, expr, &result))
4366 {
4367 commandFailed(st, argv[0], "evaluation of meta-command failed");
4368 return CSTATE_ABORTED;
4369 }
4370
4371 if (!putVariableValue(&st->variables, argv[0], argv[1], &result))
4372 {
4373 commandFailed(st, "set", "assignment of meta-command failed");
4374 return CSTATE_ABORTED;
4375 }
4376 }
4377 else if (command->meta == META_IF)
4378 {
4379 /* backslash commands with an expression to evaluate */
4380 PgBenchExpr *expr = command->expr;
4381 PgBenchValue result;
4382 bool cond;
4383
4384 if (!evaluateExpr(st, expr, &result))
4385 {
4386 commandFailed(st, argv[0], "evaluation of meta-command failed");
4387 return CSTATE_ABORTED;
4388 }
4389
4390 cond = valueTruth(&result);
4392 }
4393 else if (command->meta == META_ELIF)
4394 {
4395 /* backslash commands with an expression to evaluate */
4396 PgBenchExpr *expr = command->expr;
4397 PgBenchValue result;
4398 bool cond;
4399
4401 {
4402 /* elif after executed block, skip eval and wait for endif. */
4404 return CSTATE_END_COMMAND;
4405 }
4406
4407 if (!evaluateExpr(st, expr, &result))
4408 {
4409 commandFailed(st, argv[0], "evaluation of meta-command failed");
4410 return CSTATE_ABORTED;
4411 }
4412
4413 cond = valueTruth(&result);
4416 }
4417 else if (command->meta == META_ELSE)
4418 {
4419 switch (conditional_stack_peek(st->cstack))
4420 {
4421 case IFSTATE_TRUE:
4423 break;
4424 case IFSTATE_FALSE: /* inconsistent if active */
4425 case IFSTATE_IGNORED: /* inconsistent if active */
4426 case IFSTATE_NONE: /* else without if */
4427 case IFSTATE_ELSE_TRUE: /* else after else */
4428 case IFSTATE_ELSE_FALSE: /* else after else */
4429 default:
4430 /* dead code if conditional check is ok */
4431 Assert(false);
4432 }
4433 }
4434 else if (command->meta == META_ENDIF)
4435 {
4438 }
4439 else if (command->meta == META_SETSHELL)
4440 {
4441 if (!runShellCommand(&st->variables, argv[1], argv + 2, argc - 2))
4442 {
4443 commandFailed(st, "setshell", "execution of meta-command failed");
4444 return CSTATE_ABORTED;
4445 }
4446 }
4447 else if (command->meta == META_SHELL)
4448 {
4449 if (!runShellCommand(&st->variables, NULL, argv + 1, argc - 1))
4450 {
4451 commandFailed(st, "shell", "execution of meta-command failed");
4452 return CSTATE_ABORTED;
4453 }
4454 }
4455 else if (command->meta == META_STARTPIPELINE)
4456 {
4457 /*
4458 * In pipeline mode, we use a workflow based on libpq pipeline
4459 * functions.
4460 */
4461 if (querymode == QUERY_SIMPLE)
4462 {
4463 commandFailed(st, "startpipeline", "cannot use pipeline mode with the simple query protocol");
4464 return CSTATE_ABORTED;
4465 }
4466
4467 /*
4468 * If we're in prepared-query mode, we need to prepare all the
4469 * commands that are inside the pipeline before we actually start the
4470 * pipeline itself. This solves the problem that running BEGIN
4471 * ISOLATION LEVEL SERIALIZABLE in a pipeline would fail due to a
4472 * snapshot having been acquired by the prepare within the pipeline.
4473 */
4476
4478 {
4479 commandFailed(st, "startpipeline", "already in pipeline mode");
4480 return CSTATE_ABORTED;
4481 }
4482 if (PQenterPipelineMode(st->con) == 0)
4483 {
4484 commandFailed(st, "startpipeline", "failed to enter pipeline mode");
4485 return CSTATE_ABORTED;
4486 }
4487 }
4488 else if (command->meta == META_SYNCPIPELINE)
4489 {
4491 {
4492 commandFailed(st, "syncpipeline", "not in pipeline mode");
4493 return CSTATE_ABORTED;
4494 }
4495 if (PQsendPipelineSync(st->con) == 0)
4496 {
4497 commandFailed(st, "syncpipeline", "failed to send a pipeline sync");
4498 return CSTATE_ABORTED;
4499 }
4500 st->num_syncs++;
4501 }
4502 else if (command->meta == META_ENDPIPELINE)
4503 {
4505 {
4506 commandFailed(st, "endpipeline", "not in pipeline mode");
4507 return CSTATE_ABORTED;
4508 }
4509 if (!PQpipelineSync(st->con))
4510 {
4511 commandFailed(st, "endpipeline", "failed to send a pipeline sync");
4512 return CSTATE_ABORTED;
4513 }
4514 st->num_syncs++;
4515 /* Now wait for the PGRES_PIPELINE_SYNC and exit pipeline mode there */
4516 /* collect pending results before getting out of pipeline mode */
4517 return CSTATE_WAIT_RESULT;
4518 }
4519
4520 /*
4521 * executing the expression or shell command might have taken a
4522 * non-negligible amount of time, so reset 'now'
4523 */
4524 *now = 0;
4525
4526 return CSTATE_END_COMMAND;
4527}
4528
4529/*
4530 * Return the number of failed transactions.
4531 */
4532static int64
4533getFailures(const StatsData *stats)
4534{
4535 return (stats->serialization_failures +
4536 stats->deadlock_failures);
4537}
4538
4539/*
4540 * Return a string constant representing the result of a transaction
4541 * that is not successfully processed.
4542 */
4543static const char *
4544getResultString(bool skipped, EStatus estatus)
4545{
4546 if (skipped)
4547 return "skipped";
4548 else if (failures_detailed)
4549 {
4550 switch (estatus)
4551 {
4553 return "serialization";
4555 return "deadlock";
4556 default:
4557 /* internal error which should never occur */
4558 pg_fatal("unexpected error status: %d", estatus);
4559 }
4560 }
4561 else
4562 return "failed";
4563}
4564
4565/*
4566 * Print log entry after completing one transaction.
4567 *
4568 * We print Unix-epoch timestamps in the log, so that entries can be
4569 * correlated against other logs.
4570 *
4571 * XXX We could obtain the time from the caller and just shift it here, to
4572 * avoid the cost of an extra call to pg_time_now().
4573 */
4574static void
4575doLog(TState *thread, CState *st,
4576 StatsData *agg, bool skipped, double latency, double lag)
4577{
4578 FILE *logfile = thread->logfile;
4580
4581 Assert(use_log);
4582
4583 /*
4584 * Skip the log entry if sampling is enabled and this row doesn't belong
4585 * to the random sample.
4586 */
4587 if (sample_rate != 0.0 &&
4589 return;
4590
4591 /* should we aggregate the results or not? */
4592 if (agg_interval > 0)
4593 {
4595
4596 /*
4597 * Loop until we reach the interval of the current moment, and print
4598 * any empty intervals in between (this may happen with very low tps,
4599 * e.g. --rate=0.1).
4600 */
4601
4602 while ((next = agg->start_time + agg_interval * INT64CONST(1000000)) <= now)
4603 {
4604 double lag_sum = 0.0;
4605 double lag_sum2 = 0.0;
4606 double lag_min = 0.0;
4607 double lag_max = 0.0;
4608 int64 skipped = 0;
4609 int64 serialization_failures = 0;
4610 int64 deadlock_failures = 0;
4611 int64 retried = 0;
4612 int64 retries = 0;
4613
4614 /* print aggregated report to logfile */
4615 fprintf(logfile, INT64_FORMAT " " INT64_FORMAT " %.0f %.0f %.0f %.0f",
4616 agg->start_time / 1000000, /* seconds since Unix epoch */
4617 agg->cnt,
4618 agg->latency.sum,
4619 agg->latency.sum2,
4620 agg->latency.min,
4621 agg->latency.max);
4622
4623 if (throttle_delay)
4624 {
4625 lag_sum = agg->lag.sum;
4626 lag_sum2 = agg->lag.sum2;
4627 lag_min = agg->lag.min;
4628 lag_max = agg->lag.max;
4629 }
4630 fprintf(logfile, " %.0f %.0f %.0f %.0f",
4631 lag_sum,
4632 lag_sum2,
4633 lag_min,
4634 lag_max);
4635
4636 if (latency_limit)
4637 skipped = agg->skipped;
4638 fprintf(logfile, " " INT64_FORMAT, skipped);
4639
4640 if (max_tries != 1)
4641 {
4642 retried = agg->retried;
4643 retries = agg->retries;
4644 }
4645 fprintf(logfile, " " INT64_FORMAT " " INT64_FORMAT, retried, retries);
4646
4648 {
4649 serialization_failures = agg->serialization_failures;
4650 deadlock_failures = agg->deadlock_failures;
4651 }
4653 serialization_failures,
4654 deadlock_failures);
4655
4656 fputc('\n', logfile);
4657
4658 /* reset data and move to next interval */
4659 initStats(agg, next);
4660 }
4661
4662 /* accumulate the current transaction */
4663 accumStats(agg, skipped, latency, lag, st->estatus, st->tries);
4664 }
4665 else
4666 {
4667 /* no, print raw transactions */
4668 if (!skipped && st->estatus == ESTATUS_NO_ERROR)
4669 fprintf(logfile, "%d " INT64_FORMAT " %.0f %d " INT64_FORMAT " "
4671 st->id, st->cnt, latency, st->use_file,
4672 now / 1000000, now % 1000000);
4673 else
4674 fprintf(logfile, "%d " INT64_FORMAT " %s %d " INT64_FORMAT " "
4676 st->id, st->cnt, getResultString(skipped, st->estatus),
4677 st->use_file, now / 1000000, now % 1000000);
4678
4679 if (throttle_delay)
4680 fprintf(logfile, " %.0f", lag);
4681 if (max_tries != 1)
4682 fprintf(logfile, " %u", st->tries - 1);
4683 fputc('\n', logfile);
4684 }
4685}
4686
4687/*
4688 * Accumulate and report statistics at end of a transaction.
4689 *
4690 * (This is also called when a transaction is late and thus skipped.
4691 * Note that even skipped and failed transactions are counted in the CState
4692 * "cnt" field.)
4693 */
4694static void
4696 bool skipped, StatsData *agg)
4697{
4698 double latency = 0.0,
4699 lag = 0.0;
4700 bool detailed = progress || throttle_delay || latency_limit ||
4702
4703 if (detailed && !skipped && st->estatus == ESTATUS_NO_ERROR)
4704 {
4706
4707 /* compute latency & lag */
4708 latency = (*now) - st->txn_scheduled;
4709 lag = st->txn_begin - st->txn_scheduled;
4710 }
4711
4712 /* keep detailed thread stats */
4713 accumStats(&thread->stats, skipped, latency, lag, st->estatus, st->tries);
4714
4715 /* count transactions over the latency limit, if needed */
4716 if (latency_limit && latency > latency_limit)
4717 thread->latency_late++;
4718
4719 /* client stat is just counting */
4720 st->cnt++;
4721
4722 if (use_log)
4723 doLog(thread, st, agg, skipped, latency, lag);
4724
4725 /* XXX could use a mutex here, but we choose not to */
4726 if (per_script_stats)
4727 accumStats(&sql_script[st->use_file].stats, skipped, latency, lag,
4728 st->estatus, st->tries);
4729}
4730
4731
4732/* discard connections */
4733static void
4734disconnect_all(CState *state, int length)
4735{
4736 int i;
4737
4738 for (i = 0; i < length; i++)
4739 finishCon(&state[i]);
4740}
4741
4742/*
4743 * Remove old pgbench tables, if any exist
4744 */
4745static void
4747{
4748 fprintf(stderr, "dropping old tables...\n");
4749
4750 /*
4751 * We drop all the tables in one command, so that whether there are
4752 * foreign key dependencies or not doesn't matter.
4753 */
4754 executeStatement(con, "drop table if exists "
4755 "pgbench_accounts, "
4756 "pgbench_branches, "
4757 "pgbench_history, "
4758 "pgbench_tellers");
4759}
4760
4761/*
4762 * Create "pgbench_accounts" partitions if needed.
4763 *
4764 * This is the larger table of pgbench default tpc-b like schema
4765 * with a known size, so we choose to partition it.
4766 */
4767static void
4769{
4770 PQExpBufferData query;
4771
4772 /* we must have to create some partitions */
4773 Assert(partitions > 0);
4774
4775 fprintf(stderr, "creating %d partitions...\n", partitions);
4776
4777 initPQExpBuffer(&query);
4778
4779 for (int p = 1; p <= partitions; p++)
4780 {
4782 {
4783 int64 part_size = (naccounts * (int64) scale + partitions - 1) / partitions;
4784
4785 printfPQExpBuffer(&query,
4786 "create%s table pgbench_accounts_%d\n"
4787 " partition of pgbench_accounts\n"
4788 " for values from (",
4789 unlogged_tables ? " unlogged" : "", p);
4790
4791 /*
4792 * For RANGE, we use open-ended partitions at the beginning and
4793 * end to allow any valid value for the primary key. Although the
4794 * actual minimum and maximum values can be derived from the
4795 * scale, it is more generic and the performance is better.
4796 */
4797 if (p == 1)
4798 appendPQExpBufferStr(&query, "minvalue");
4799 else
4800 appendPQExpBuffer(&query, INT64_FORMAT, (p - 1) * part_size + 1);
4801
4802 appendPQExpBufferStr(&query, ") to (");
4803
4804 if (p < partitions)
4805 appendPQExpBuffer(&query, INT64_FORMAT, p * part_size + 1);
4806 else
4807 appendPQExpBufferStr(&query, "maxvalue");
4808
4809 appendPQExpBufferChar(&query, ')');
4810 }
4811 else if (partition_method == PART_HASH)
4812 printfPQExpBuffer(&query,
4813 "create%s table pgbench_accounts_%d\n"
4814 " partition of pgbench_accounts\n"
4815 " for values with (modulus %d, remainder %d)",
4816 unlogged_tables ? " unlogged" : "", p,
4817 partitions, p - 1);
4818 else /* cannot get there */
4819 Assert(0);
4820
4821 /*
4822 * Per ddlinfo in initCreateTables, fillfactor is needed on table
4823 * pgbench_accounts.
4824 */
4825 appendPQExpBuffer(&query, " with (fillfactor=%d)", fillfactor);
4826
4827 executeStatement(con, query.data);
4828 }
4829
4830 termPQExpBuffer(&query);
4831}
4832
4833/*
4834 * Create pgbench's standard tables
4835 */
4836static void
4838{
4839 /*
4840 * Note: TPC-B requires at least 100 bytes per row, and the "filler"
4841 * fields in these table declarations were intended to comply with that.
4842 * The pgbench_accounts table complies with that because the "filler"
4843 * column is set to blank-padded empty string. But for all other tables
4844 * the columns default to NULL and so don't actually take any space. We
4845 * could fix that by giving them non-null default values. However, that
4846 * would completely break comparability of pgbench results with prior
4847 * versions. Since pgbench has never pretended to be fully TPC-B compliant
4848 * anyway, we stick with the historical behavior.
4849 */
4850 struct ddlinfo
4851 {
4852 const char *table; /* table name */
4853 const char *smcols; /* column decls if accountIDs are 32 bits */
4854 const char *bigcols; /* column decls if accountIDs are 64 bits */
4855 int declare_fillfactor;
4856 };
4857 static const struct ddlinfo DDLs[] = {
4858 {
4859 "pgbench_history",
4860 "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
4861 "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)",
4862 0
4863 },
4864 {
4865 "pgbench_tellers",
4866 "tid int not null,bid int,tbalance int,filler char(84)",
4867 "tid int not null,bid int,tbalance int,filler char(84)",
4868 1
4869 },
4870 {
4871 "pgbench_accounts",
4872 "aid int not null,bid int,abalance int,filler char(84)",
4873 "aid bigint not null,bid int,abalance int,filler char(84)",
4874 1
4875 },
4876 {
4877 "pgbench_branches",
4878 "bid int not null,bbalance int,filler char(88)",
4879 "bid int not null,bbalance int,filler char(88)",
4880 1
4881 }
4882 };
4883 int i;
4884 PQExpBufferData query;
4885
4886 fprintf(stderr, "creating tables...\n");
4887
4888 initPQExpBuffer(&query);
4889
4890 for (i = 0; i < lengthof(DDLs); i++)
4891 {
4892 const struct ddlinfo *ddl = &DDLs[i];
4893
4894 /* Construct new create table statement. */
4895 printfPQExpBuffer(&query, "create%s table %s(%s)",
4896 (unlogged_tables && partition_method == PART_NONE) ? " unlogged" : "",
4897 ddl->table,
4898 (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols);
4899
4900 /* Partition pgbench_accounts table */
4901 if (partition_method != PART_NONE && strcmp(ddl->table, "pgbench_accounts") == 0)
4902 appendPQExpBuffer(&query,
4903 " partition by %s (aid)", PARTITION_METHOD[partition_method]);
4904 else if (ddl->declare_fillfactor)
4905 {
4906 /* fillfactor is only expected on actual tables */
4907 appendPQExpBuffer(&query, " with (fillfactor=%d)", fillfactor);
4908 }
4909
4910 if (tablespace != NULL)
4911 {
4912 char *escape_tablespace;
4913
4914 escape_tablespace = PQescapeIdentifier(con, tablespace, strlen(tablespace));
4915 appendPQExpBuffer(&query, " tablespace %s", escape_tablespace);
4916 PQfreemem(escape_tablespace);
4917 }
4918
4919 executeStatement(con, query.data);
4920 }
4921
4922 termPQExpBuffer(&query);
4923
4925 createPartitions(con);
4926}
4927
4928/*
4929 * Truncate away any old data, in one command in case there are foreign keys
4930 */
4931static void
4933{
4934 executeStatement(con, "truncate table "
4935 "pgbench_accounts, "
4936 "pgbench_branches, "
4937 "pgbench_history, "
4938 "pgbench_tellers");
4939}
4940
4941static void
4943{
4944 /* "filler" column uses NULL */
4946 INT64_FORMAT "\t0\t\\N\n",
4947 curr + 1);
4948}
4949
4950static void
4952{
4953 /* "filler" column uses NULL */
4955 INT64_FORMAT "\t" INT64_FORMAT "\t0\t\\N\n",
4956 curr + 1, curr / ntellers + 1);
4957}
4958
4959static void
4961{
4962 /* "filler" column defaults to blank padded empty string */
4964 INT64_FORMAT "\t" INT64_FORMAT "\t0\t\n",
4965 curr + 1, curr / naccounts + 1);
4966}
4967
4968static void
4969initPopulateTable(PGconn *con, const char *table, int64 base,
4970 initRowMethod init_row)
4971{
4972 int n;
4973 int64 k;
4974 int chars = 0;
4975 int prev_chars = 0;
4976 PGresult *res;
4977 PQExpBufferData sql;
4978 char copy_statement[256];
4979 const char *copy_statement_fmt = "copy %s from stdin";
4980 int64 total = base * scale;
4981
4982 /* used to track elapsed time and estimate of the remaining time */
4984 int log_interval = 1;
4985
4986 /* Stay on the same line if reporting to a terminal */
4987 char eol = isatty(fileno(stderr)) ? '\r' : '\n';
4988
4989 initPQExpBuffer(&sql);
4990
4991 /* Use COPY with FREEZE on v14 and later for all ordinary tables */
4992 if ((PQserverVersion(con) >= 140000) &&
4993 get_table_relkind(con, table) == RELKIND_RELATION)
4994 copy_statement_fmt = "copy %s from stdin with (freeze on)";
4995
4996
4997 n = pg_snprintf(copy_statement, sizeof(copy_statement), copy_statement_fmt, table);
4998 if (n >= sizeof(copy_statement))
4999 pg_fatal("invalid buffer size: must be at least %d characters long", n);
5000 else if (n == -1)
5001 pg_fatal("invalid format string");
5002
5003 res = PQexec(con, copy_statement);
5004
5006 pg_fatal("unexpected copy in result: %s", PQerrorMessage(con));
5007 PQclear(res);
5008
5009 start = pg_time_now();
5010
5011 for (k = 0; k < total; k++)
5012 {
5013 int64 j = k + 1;
5014
5015 init_row(&sql, k);
5016 if (PQputline(con, sql.data))
5017 pg_fatal("PQputline failed");
5018
5019 if (CancelRequested)
5020 break;
5021
5022 /*
5023 * If we want to stick with the original logging, print a message each
5024 * 100k inserted rows.
5025 */
5026 if ((!use_quiet) && (j % 100000 == 0))
5027 {
5028 double elapsed_sec = PG_TIME_GET_DOUBLE(pg_time_now() - start);
5029 double remaining_sec = ((double) total - j) * elapsed_sec / j;
5030
5031 chars = fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) of %s done (elapsed %.2f s, remaining %.2f s)",
5032 j, total,
5033 (int) ((j * 100) / total),
5034 table, elapsed_sec, remaining_sec);
5035
5036 /*
5037 * If the previous progress message is longer than the current
5038 * one, add spaces to the current line to fully overwrite any
5039 * remaining characters from the previous message.
5040 */
5041 if (prev_chars > chars)
5042 fprintf(stderr, "%*c", prev_chars - chars, ' ');
5043 fputc(eol, stderr);
5044 prev_chars = chars;
5045 }
5046 /* let's not call the timing for each row, but only each 100 rows */
5047 else if (use_quiet && (j % 100 == 0))
5048 {
5049 double elapsed_sec = PG_TIME_GET_DOUBLE(pg_time_now() - start);
5050 double remaining_sec = ((double) total - j) * elapsed_sec / j;
5051
5052 /* have we reached the next interval (or end)? */
5053 if ((j == total) || (elapsed_sec >= log_interval * LOG_STEP_SECONDS))
5054 {
5055 chars = fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) of %s done (elapsed %.2f s, remaining %.2f s)",
5056 j, total,
5057 (int) ((j * 100) / total),
5058 table, elapsed_sec, remaining_sec);
5059
5060 /*
5061 * If the previous progress message is longer than the current
5062 * one, add spaces to the current line to fully overwrite any
5063 * remaining characters from the previous message.
5064 */
5065 if (prev_chars > chars)
5066 fprintf(stderr, "%*c", prev_chars - chars, ' ');
5067 fputc(eol, stderr);
5068 prev_chars = chars;
5069
5070 /* skip to the next interval */
5071 log_interval = (int) ceil(elapsed_sec / LOG_STEP_SECONDS);
5072 }
5073 }
5074 }
5075
5076 if (chars != 0 && eol != '\n')
5077 fprintf(stderr, "%*c\r", chars, ' '); /* Clear the current line */
5078
5079 if (PQputline(con, "\\.\n"))
5080 pg_fatal("very last PQputline failed");
5081 if (PQendcopy(con))
5082 pg_fatal("PQendcopy failed");
5083
5084 termPQExpBuffer(&sql);
5085}
5086
5087/*
5088 * Fill the standard tables with some data generated and sent from the client.
5089 *
5090 * The filler column is NULL in pgbench_branches and pgbench_tellers, and is
5091 * a blank-padded string in pgbench_accounts.
5092 */
5093static void
5095{
5096 fprintf(stderr, "generating data (client-side)...\n");
5097
5098 /*
5099 * we do all of this in one transaction to enable the backend's
5100 * data-loading optimizations
5101 */
5102 executeStatement(con, "begin");
5103
5104 /* truncate away any old data */
5105 initTruncateTables(con);
5106
5107 /*
5108 * fill branches, tellers, accounts in that order in case foreign keys
5109 * already exist
5110 */
5111 initPopulateTable(con, "pgbench_branches", nbranches, initBranch);
5112 initPopulateTable(con, "pgbench_tellers", ntellers, initTeller);
5113 initPopulateTable(con,