From f2a29da4a08e2b4243a6e273014ca5a16ec1e9f2 Mon Sep 17 00:00:00 2001 From: Hannu Krosing Date: Sun, 4 Oct 2026 11:24:47 +0000 Subject: [PATCH v4 11/12] Refactor pgbench script: decouple chooseScript() from TState Change chooseScript() to accept a pg_prng_state * pointer directly instead of a TState * pointer: - In script.c/h, change chooseScript(TState *thread) to chooseScript(pg_prng_state *random_state). - In engine.c, update advanceConnectionState() to pass &thread->ts_choose_rs to chooseScript(). - Remove #include "engine.h" from script.h that was previously required only so chooseScript() could see the TState typedef. --- src/bin/pgbench/engine.c | 2 +- src/bin/pgbench/script.c | 4 ++-- src/bin/pgbench/script.h | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/bin/pgbench/engine.c b/src/bin/pgbench/engine.c index 895f324fc30..3bbcf09ebfc 100644 --- a/src/bin/pgbench/engine.c +++ b/src/bin/pgbench/engine.c @@ -746,7 +746,7 @@ advanceConnectionState(TState *thread, CState *st, StatsData *agg) { /* Select transaction (script) to run. */ case CSTATE_CHOOSE_SCRIPT: - st->use_file = chooseScript(thread); + st->use_file = chooseScript(&thread->ts_choose_rs); Assert(conditional_stack_empty(st->cstack)); /* reset transaction variables to default values */ diff --git a/src/bin/pgbench/script.c b/src/bin/pgbench/script.c index e4727071055..11a518ec466 100644 --- a/src/bin/pgbench/script.c +++ b/src/bin/pgbench/script.c @@ -1141,7 +1141,7 @@ getMetaCommand(const char *cmd) /* return a script number with a weighted choice. */ int -chooseScript(TState *thread) +chooseScript(pg_prng_state *random_state) { int i = 0; int64 w; @@ -1149,7 +1149,7 @@ chooseScript(TState *thread) if (num_scripts == 1) return 0; - w = getrand(&thread->ts_choose_rs, 0, total_weight - 1); + w = getrand(random_state, 0, total_weight - 1); do { w -= sql_script[i++].weight; diff --git a/src/bin/pgbench/script.h b/src/bin/pgbench/script.h index 444eb4afd74..ec0d1c795ee 100644 --- a/src/bin/pgbench/script.h +++ b/src/bin/pgbench/script.h @@ -14,7 +14,6 @@ #define SCRIPT_H #include "common/pg_prng.h" -#include "engine.h" #include "pgbench.h" #include "pqexpbuffer.h" #include "stats.h" @@ -159,7 +158,7 @@ typedef struct BuiltinScript } BuiltinScript; extern bool evaluateExpr(CState *st, PgBenchExpr *expr, PgBenchValue *retval); -extern int chooseScript(TState *thread); +extern int chooseScript(pg_prng_state *random_state); extern void postprocess_sql_command(Command *my_command); extern void process_file(const char *filename, int weight); -- 2.56.0.385.gd3acb90ef8-goog