Skip to content

Commit f472495

Browse files
postsqlCommitfest Bot
authored andcommitted
added new and old style SQL functions and documentation
1 parent 928da6f commit f472495

File tree

2 files changed

+223
-7
lines changed

2 files changed

+223
-7
lines changed

doc/src/sgml/ref/pgbench.sgml

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ pgbench -i <optional> <replaceable>other-options</replaceable> </optional> <repl
105105
<literal>pgbench -i</literal> creates four tables <structname>pgbench_accounts</structname>,
106106
<structname>pgbench_branches</structname>, <structname>pgbench_history</structname>, and
107107
<structname>pgbench_tellers</structname>,
108-
destroying any existing tables of these names.
109-
Be very careful to use another database if you have tables having these
108+
destroying any existing tables of these names and their dependencies.
109+
It also creates 6 functions starting with <structname>pgbench_</structname>.
110+
Be very careful to use another database if you have tables or functions having these
110111
names!
111112
</para>
112113
</caution>
@@ -361,6 +362,15 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
361362
</listitem>
362363
</varlistentry>
363364

365+
<varlistentry id="pgbench-option-no-functions">
366+
<term><option>--no-functions</option></term>
367+
<listitem>
368+
<para>
369+
Do not create pl/pgsql and SQL functions for internal scripts.
370+
</para>
371+
</listitem>
372+
</varlistentry>
373+
364374
<varlistentry id="pgbench-option-partition-method">
365375
<term><option>--partition-method=<replaceable>NAME</replaceable></option></term>
366376
<listitem>
@@ -427,8 +437,35 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
427437
Available built-in scripts are: <literal>tpcb-like</literal>,
428438
<literal>simple-update</literal> and <literal>select-only</literal>.
429439
Unambiguous prefixes of built-in names are accepted.
440+
</para>
441+
<para>
442+
Unless disabled with <literal>--no-functions</literal> option at database
443+
init the <literal>tpcb-like</literal> and <literal>simple-update</literal>
444+
scripts are also implemented as User-Defined functions in the database which
445+
can be tested using scripts named <literal>plpgsql-tpcb-like</literal>,
446+
<literal>sqlfunc-tpcb-like</literal>, <literal>oldsqlf-tpcb-like</literal>,
447+
<literal>plpgsql-simple-update</literal>, <literal>sqlfunc-simple-update</literal>
448+
and <literal>oldsqlf-simple-update</literal>.
449+
The <literal>sqlfunc-*</literal> versions use the new SQL-standard SQL functions and
450+
the <literal>oldsqlf-*</literal> use the SQL functions defined using <literal>LANGUAGE SQL</literal>.
451+
Use <literal>--show-script=scriptname</literal> to see what is actually run.
452+
</para>
453+
<para>
430454
With the special name <literal>list</literal>, show the list of built-in scripts
431-
and exit immediately.
455+
and exit immediately :
456+
<programlisting>
457+
$ pgbench -b list
458+
Available builtin scripts:
459+
tpcb-like: <builtin: TPC-B (sort of)>
460+
plpgsql-tpcb-like: <builtin: TPC-B (sort of) - pl/pgsql UDF>
461+
sqlfunc-tpcb-like: <builtin: TPC-B (sort of) - 'BEGIN ATOMIC' SQL UDF>
462+
oldsqlf-tpcb-like: <builtin: TPC-B (sort of) - LANGUAGE SQL UDF>
463+
simple-update: <builtin: simple update>
464+
plpgsql-simple-update: <builtin: simple update - pl/pgsql UDF>
465+
sqlfunc-simple-update: <builtin: simple update - 'BEGIN ATOMIC' SQL UDF>
466+
oldsqlf-simple-update: <builtin: simple update - LANGUAGE SQL UDF>
467+
select-only: <builtin: select only>
468+
</programlisting>
432469
</para>
433470
<para>
434471
Optionally, write an integer weight after <literal>@</literal> to

src/bin/pgbench/pgbench.c

Lines changed: 183 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ typedef struct socket_set
160160
/********************************************************************
161161
* some configurable parameters */
162162

163-
#define DEFAULT_INIT_STEPS "dtgvp" /* default -I setting */
164-
#define ALL_INIT_STEPS "dtgGvpf" /* all possible steps */
163+
#define DEFAULT_INIT_STEPS "dYtgvpy" /* default -I setting */
164+
#define ALL_INIT_STEPS "dYtgGvpfy" /* all possible steps */
165165

166166
#define LOG_STEP_SECONDS 5 /* seconds between log messages */
167167
#define DEFAULT_NXACTS 10 /* default nxacts */
@@ -796,6 +796,33 @@ static const BuiltinScript builtin_script[] =
796796
"INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
797797
"END;\n"
798798
},
799+
{
800+
"plpgsql-tpcb-like",
801+
"<builtin: TPC-B (sort of) - pl/pgsql UDF>",
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+
"SELECT 1 FROM pgbench_tpcb_like(:aid, :bid, :tid, :delta);\n"
807+
},
808+
{
809+
"sqlfunc-tpcb-like",
810+
"<builtin: TPC-B (sort of) - 'BEGIN ATOMIC' SQL UDF>",
811+
"\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
812+
"\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
813+
"\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
814+
"\\set delta random(-5000, 5000)\n"
815+
"SELECT 1 FROM pgbench_tpcb_like_sqlfunc(:aid, :bid, :tid, :delta);\n"
816+
},
817+
{
818+
"oldsqlf-tpcb-like",
819+
"<builtin: TPC-B (sort of) - LANGUAGE SQL UDF>",
820+
"\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
821+
"\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
822+
"\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
823+
"\\set delta random(-5000, 5000)\n"
824+
"SELECT 1 FROM pgbench_tpcb_like_oldsqlfunc(:aid, :bid, :tid, :delta);\n"
825+
},
799826
{
800827
"simple-update",
801828
"<builtin: simple update>",
@@ -809,6 +836,33 @@ static const BuiltinScript builtin_script[] =
809836
"INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
810837
"END;\n"
811838
},
839+
{
840+
"plpgsql-simple-update",
841+
"<builtin: simple update - pl/pgsql UDF>",
842+
"\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
843+
"\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
844+
"\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
845+
"\\set delta random(-5000, 5000)\n"
846+
"SELECT 1 FROM pgbench_simple_update(:aid, :bid, :tid, :delta);\n"
847+
},
848+
{
849+
"sqlfunc-simple-update",
850+
"<builtin: simple update - 'BEGIN ATOMIC' SQL UDF>",
851+
"\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
852+
"\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
853+
"\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
854+
"\\set delta random(-5000, 5000)\n"
855+
"SELECT 1 FROM pgbench_simple_update_sqlfunc(:aid, :bid, :tid, :delta);\n"
856+
},
857+
{
858+
"oldsqlf-simple-update",
859+
"<builtin: simple update - LANGUAGE SQL UDF>",
860+
"\\set aid random(1, " CppAsString2(naccounts) " * :scale)\n"
861+
"\\set bid random(1, " CppAsString2(nbranches) " * :scale)\n"
862+
"\\set tid random(1, " CppAsString2(ntellers) " * :scale)\n"
863+
"\\set delta random(-5000, 5000)\n"
864+
"SELECT 1 FROM pgbench_simple_update_oldsqlfunc(:aid, :bid, :tid, :delta);\n"
865+
},
812866
{
813867
"select-only",
814868
"<builtin: select only>",
@@ -915,6 +969,7 @@ usage(void)
915969
" -q, --quiet quiet logging (one message each 5 seconds)\n"
916970
" -s, --scale=NUM scaling factor\n"
917971
" --foreign-keys create foreign key constraints between tables\n"
972+
" --no-functions do not create pl/pgsql and SQL functions for internal scripts\n"
918973
" --index-tablespace=TABLESPACE\n"
919974
" create indexes in the specified tablespace\n"
920975
" --partition-method=(range|hash)\n"
@@ -4763,7 +4818,7 @@ initDropTables(PGconn *con)
47634818
"pgbench_accounts, "
47644819
"pgbench_branches, "
47654820
"pgbench_history, "
4766-
"pgbench_tellers");
4821+
"pgbench_tellers cascade");
47674822
}
47684823

47694824
/*
@@ -4838,6 +4893,107 @@ createPartitions(PGconn *con)
48384893
termPQExpBuffer(&query);
48394894
}
48404895

4896+
/*
4897+
* Create the functions needed for plpgsql-* builting scripts
4898+
*/
4899+
static void
4900+
initCreateFuntions(PGconn *con)
4901+
{
4902+
fprintf(stderr, "creating functions...\n");
4903+
4904+
executeStatement(con,
4905+
"CREATE FUNCTION pgbench_tpcb_like(_aid int, _bid int, _tid int, _delta int)\n"
4906+
"RETURNS void\n"
4907+
"LANGUAGE plpgsql\n"
4908+
"AS $plpgsql$\n"
4909+
"BEGIN\n"
4910+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4911+
" PERFORM abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4912+
" UPDATE pgbench_tellers SET tbalance = tbalance + _delta WHERE tid = _tid;\n"
4913+
" UPDATE pgbench_branches SET bbalance = bbalance + _delta WHERE bid = _bid;\n"
4914+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4915+
"END;\n"
4916+
"$plpgsql$;\n");
4917+
executeStatement(con,
4918+
"CREATE FUNCTION pgbench_simple_update(_aid int, _bid int, _tid int, _delta int)\n"
4919+
"RETURNS void\n"
4920+
"LANGUAGE plpgsql\n"
4921+
"AS $plpgsql$\n"
4922+
"BEGIN\n"
4923+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4924+
" PERFORM abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4925+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4926+
"END;\n"
4927+
"$plpgsql$;\n");
4928+
if ((PQserverVersion(con) >= 140000))
4929+
{
4930+
executeStatement(con,
4931+
"CREATE FUNCTION pgbench_tpcb_like_sqlfunc(_aid int, _bid int, _tid int, _delta int)\n"
4932+
"RETURNS void\n"
4933+
"BEGIN ATOMIC\n"
4934+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4935+
" SELECT abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4936+
" UPDATE pgbench_tellers SET tbalance = tbalance + _delta WHERE tid = _tid;\n"
4937+
" UPDATE pgbench_branches SET bbalance = bbalance + _delta WHERE bid = _bid;\n"
4938+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4939+
"END;\n");
4940+
executeStatement(con,
4941+
"CREATE FUNCTION pgbench_simple_update_sqlfunc(_aid int, _bid int, _tid int, _delta int)\n"
4942+
"RETURNS void\n"
4943+
"BEGIN ATOMIC\n"
4944+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4945+
" SELECT abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4946+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4947+
"END;\n");
4948+
}
4949+
executeStatement(con,
4950+
"CREATE FUNCTION pgbench_tpcb_like_oldsqlfunc(_aid int, _bid int, _tid int, _delta int)\n"
4951+
"RETURNS void\n"
4952+
"LANGUAGE sql\n"
4953+
"AS $sql$\n"
4954+
"-- BEGIN\n"
4955+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4956+
" SELECT abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4957+
" UPDATE pgbench_tellers SET tbalance = tbalance + _delta WHERE tid = _tid;\n"
4958+
" UPDATE pgbench_branches SET bbalance = bbalance + _delta WHERE bid = _bid;\n"
4959+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4960+
"-- END;\n"
4961+
"$sql$;\n");
4962+
executeStatement(con,
4963+
"CREATE FUNCTION pgbench_simple_update_oldsqlfunc(_aid int, _bid int, _tid int, _delta int)\n"
4964+
"RETURNS void\n"
4965+
"LANGUAGE sql\n"
4966+
"AS $sql$\n"
4967+
"-- BEGIN\n"
4968+
" UPDATE pgbench_accounts SET abalance = abalance + _delta WHERE aid = _aid;\n"
4969+
" SELECT abalance FROM pgbench_accounts WHERE aid = _aid;\n"
4970+
" INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (_tid, _bid, _aid, _delta, CURRENT_TIMESTAMP);\n"
4971+
"-- END;\n"
4972+
"$sql$;\n");
4973+
}
4974+
4975+
/*
4976+
* Remove old pgbench functions, if any exist
4977+
*/
4978+
static void
4979+
initDropFunctions(PGconn *con)
4980+
{
4981+
fprintf(stderr, "dropping old functions...\n");
4982+
4983+
executeStatement(con,
4984+
"DROP FUNCTION IF EXISTS pgbench_tpcb_like(_aid int, _bid int, _tid int, _delta int);\n");
4985+
executeStatement(con,
4986+
"DROP FUNCTION IF EXISTS pgbench_simple_update(_aid int, _bid int, _tid int, _delta int);\n");
4987+
executeStatement(con,
4988+
"DROP FUNCTION IF EXISTS pgbench_tpcb_like_sqlfunc(_aid int, _bid int, _tid int, _delta int);\n");
4989+
executeStatement(con,
4990+
"DROP FUNCTION IF EXISTS pgbench_simple_update_sqlfunc(_aid int, _bid int, _tid int, _delta int);\n");
4991+
executeStatement(con,
4992+
"DROP FUNCTION IF EXISTS pgbench_tpcb_like_oldsqlfunc(_aid int, _bid int, _tid int, _delta int);\n");
4993+
executeStatement(con,
4994+
"DROP FUNCTION IF EXISTS pgbench_simple_update_oldsqlfunc(_aid int, _bid int, _tid int, _delta int);\n");
4995+
}
4996+
48414997
/*
48424998
* Create pgbench's standard tables
48434999
*/
@@ -5324,6 +5480,14 @@ runInitSteps(const char *initialize_steps)
53245480
op = "foreign keys";
53255481
initCreateFKeys(con);
53265482
break;
5483+
case 'Y':
5484+
op = "drop functions";
5485+
initDropFunctions(con);
5486+
break;
5487+
case 'y':
5488+
op = "create functions";
5489+
initCreateFuntions(con);
5490+
break;
53275491
case ' ':
53285492
break; /* ignore */
53295493
default:
@@ -6159,7 +6323,7 @@ listAvailableScripts(void)
61596323

61606324
fprintf(stderr, "Available builtin scripts:\n");
61616325
for (i = 0; i < lengthof(builtin_script); i++)
6162-
fprintf(stderr, " %13s: %s\n", builtin_script[i].name, builtin_script[i].desc);
6326+
fprintf(stderr, " %21s: %s\n", builtin_script[i].name, builtin_script[i].desc);
61636327
fprintf(stderr, "\n");
61646328
}
61656329

@@ -6718,13 +6882,15 @@ main(int argc, char **argv)
67186882
{"verbose-errors", no_argument, NULL, 15},
67196883
{"exit-on-abort", no_argument, NULL, 16},
67206884
{"debug", no_argument, NULL, 17},
6885+
{"no-functions", no_argument, NULL, 18},
67216886
{NULL, 0, NULL, 0}
67226887
};
67236888

67246889
int c;
67256890
bool is_init_mode = false; /* initialize mode? */
67266891
char *initialize_steps = NULL;
67276892
bool foreign_keys = false;
6893+
bool no_functions = false;
67286894
bool is_no_vacuum = false;
67296895
bool do_vacuum_accounts = false; /* vacuum accounts table? */
67306896
int optindex;
@@ -7071,6 +7237,10 @@ main(int argc, char **argv)
70717237
case 17: /* debug */
70727238
pg_logging_increase_verbosity();
70737239
break;
7240+
case 18: /* no-functions */
7241+
initialization_option_set = true;
7242+
no_functions = true;
7243+
break;
70747244
default:
70757245
/* getopt_long already emitted a complaint */
70767246
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
@@ -7168,6 +7338,15 @@ main(int argc, char **argv)
71687338
*p = ' ';
71697339
}
71707340

7341+
if (no_functions)
7342+
{
7343+
/* Remove create function step in initialize_steps */
7344+
char *p;
7345+
7346+
while ((p = strchr(initialize_steps, 'y')) != NULL)
7347+
*p = ' ';
7348+
}
7349+
71717350
if (foreign_keys)
71727351
{
71737352
/* Add 'f' to end of initialize_steps, if not already there */

0 commit comments

Comments
 (0)