Skip to content

Commit afe1c51

Browse files
committed
Add pgbench option to add foreign key constraints to the standard scenario.
The option --foreign-keys, used at initialization time, will create foreign key constraints for the columns that represent references to other tables' primary keys. This can help in benchmarking FK performance. Jeff Janes
1 parent 11b335a commit afe1c51

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

contrib/pgbench/pgbench.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ int scale = 1;
119119
*/
120120
int fillfactor = 100;
121121

122+
/*
123+
* create foreign key constraints on the tables?
124+
*/
125+
int foreign_keys = 0;
126+
122127
/*
123128
* use unlogged tables?
124129
*/
@@ -343,6 +348,8 @@ usage(const char *progname)
343348
" -i invokes initialization mode\n"
344349
" -F NUM fill factor\n"
345350
" -s NUM scaling factor\n"
351+
" --foreign-keys\n"
352+
" create foreign key constraints between tables\n"
346353
" --index-tablespace=TABLESPACE\n"
347354
" create indexes in the specified tablespace\n"
348355
" --tablespace=TABLESPACE\n"
@@ -1275,9 +1282,9 @@ init(void)
12751282
};
12761283
struct ddlinfo DDLs[] = {
12771284
{
1278-
"pgbench_branches",
1279-
"bid int not null,bbalance int,filler char(88)",
1280-
1
1285+
"pgbench_history",
1286+
"tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
1287+
0
12811288
},
12821289
{
12831290
"pgbench_tellers",
@@ -1290,16 +1297,23 @@ init(void)
12901297
1
12911298
},
12921299
{
1293-
"pgbench_history",
1294-
"tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
1295-
0
1300+
"pgbench_branches",
1301+
"bid int not null,bbalance int,filler char(88)",
1302+
1
12961303
}
12971304
};
12981305
static char *DDLAFTERs[] = {
12991306
"alter table pgbench_branches add primary key (bid)",
13001307
"alter table pgbench_tellers add primary key (tid)",
13011308
"alter table pgbench_accounts add primary key (aid)"
13021309
};
1310+
static char *DDLKEYs[] = {
1311+
"alter table pgbench_tellers add foreign key (bid) references pgbench_branches",
1312+
"alter table pgbench_accounts add foreign key (bid) references pgbench_branches",
1313+
"alter table pgbench_history add foreign key (bid) references pgbench_branches",
1314+
"alter table pgbench_history add foreign key (tid) references pgbench_tellers",
1315+
"alter table pgbench_history add foreign key (aid) references pgbench_accounts"
1316+
};
13031317

13041318
PGconn *con;
13051319
PGresult *res;
@@ -1403,7 +1417,7 @@ init(void)
14031417
/*
14041418
* create indexes
14051419
*/
1406-
fprintf(stderr, "set primary key...\n");
1420+
fprintf(stderr, "set primary keys...\n");
14071421
for (i = 0; i < lengthof(DDLAFTERs); i++)
14081422
{
14091423
char buffer[256];
@@ -1424,6 +1438,18 @@ init(void)
14241438
executeStatement(con, buffer);
14251439
}
14261440

1441+
/*
1442+
* create foreign keys
1443+
*/
1444+
if (foreign_keys)
1445+
{
1446+
fprintf(stderr, "set foreign keys...\n");
1447+
for (i = 0; i < lengthof(DDLKEYs); i++)
1448+
{
1449+
executeStatement(con, DDLKEYs[i]);
1450+
}
1451+
}
1452+
14271453
/* vacuum */
14281454
fprintf(stderr, "vacuum...");
14291455
executeStatement(con, "vacuum analyze pgbench_branches");
@@ -1864,6 +1890,7 @@ main(int argc, char **argv)
18641890
int i;
18651891

18661892
static struct option long_options[] = {
1893+
{"foreign-keys", no_argument, &foreign_keys, 1},
18671894
{"index-tablespace", required_argument, NULL, 3},
18681895
{"tablespace", required_argument, NULL, 2},
18691896
{"unlogged-tables", no_argument, &unlogged_tables, 1},

doc/src/sgml/pgbench.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
180180
</listitem>
181181
</varlistentry>
182182

183+
<varlistentry>
184+
<term><option>--foreign-keys</option></term>
185+
<listitem>
186+
<para>
187+
Create foreign key constraints between the standard tables.
188+
</para>
189+
</listitem>
190+
</varlistentry>
191+
183192
<varlistentry>
184193
<term><option>--index-tablespace=<replaceable>index_tablespace</replaceable></option></term>
185194
<listitem>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy