@@ -119,6 +119,11 @@ int scale = 1;
119119 */
120120int 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 },
0 commit comments