@@ -124,6 +124,12 @@ int fillfactor = 100;
124
124
*/
125
125
int unlogged_tables = 0 ;
126
126
127
+ /*
128
+ * tablespace selection
129
+ */
130
+ char * tablespace = NULL ;
131
+ char * index_tablespace = NULL ;
132
+
127
133
/*
128
134
* end of configurable parameters
129
135
*********************************************************************/
@@ -359,6 +365,10 @@ usage(const char *progname)
359
365
" -h HOSTNAME database server host or socket directory\n"
360
366
" -p PORT database server port number\n"
361
367
" -U USERNAME connect as specified database user\n"
368
+ " --index-tablespace=TABLESPACE\n"
369
+ " create indexes in the specified tablespace\n"
370
+ " --tablespace=TABLESPACE\n"
371
+ " create tables in the specified tablespace\n"
362
372
" --unlogged-tables\n"
363
373
" create tables as unlogged tables\n"
364
374
" --help show this help, then exit\n"
@@ -1237,15 +1247,32 @@ init(void)
1237
1247
* versions. Since pgbench has never pretended to be fully TPC-B
1238
1248
* compliant anyway, we stick with the historical behavior.
1239
1249
*/
1240
- static char * DDLs [] = {
1241
- "drop table if exists pgbench_branches" ,
1242
- "create table pgbench_branches(bid int not null,bbalance int,filler char(88)) with (fillfactor=%d)" ,
1243
- "drop table if exists pgbench_tellers" ,
1244
- "create table pgbench_tellers(tid int not null,bid int,tbalance int,filler char(84)) with (fillfactor=%d)" ,
1245
- "drop table if exists pgbench_accounts" ,
1246
- "create table pgbench_accounts(aid int not null,bid int,abalance int,filler char(84)) with (fillfactor=%d)" ,
1247
- "drop table if exists pgbench_history" ,
1248
- "create table pgbench_history(tid int,bid int,aid int,delta int,mtime timestamp,filler char(22))"
1250
+ struct ddlinfo {
1251
+ char * table ;
1252
+ char * cols ;
1253
+ int declare_fillfactor ;
1254
+ };
1255
+ struct ddlinfo DDLs [] = {
1256
+ {
1257
+ "pgbench_branches" ,
1258
+ "bid int not null,bbalance int,filler char(88)" ,
1259
+ 1
1260
+ },
1261
+ {
1262
+ "pgbench_tellers" ,
1263
+ "tid int not null,bid int,tbalance int,filler char(84)" ,
1264
+ 1
1265
+ },
1266
+ {
1267
+ "pgbench_accounts" ,
1268
+ "aid int not null,bid int,abalance int,filler char(84)" ,
1269
+ 1
1270
+ },
1271
+ {
1272
+ "pgbench_history" ,
1273
+ "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)" ,
1274
+ 0
1275
+ }
1249
1276
};
1250
1277
static char * DDLAFTERs [] = {
1251
1278
"alter table pgbench_branches add primary key (bid)" ,
@@ -1263,31 +1290,33 @@ init(void)
1263
1290
1264
1291
for (i = 0 ; i < lengthof (DDLs ); i ++ )
1265
1292
{
1266
- char buffer1 [ 128 ];
1267
- char buffer2 [ 128 ];
1268
- char * qry = DDLs [i ];
1293
+ char opts [ 256 ];
1294
+ char buffer [ 256 ];
1295
+ struct ddlinfo * ddl = & DDLs [i ];
1269
1296
1270
- /*
1271
- * set fillfactor for branches, tellers and accounts tables
1272
- */
1273
- if ((strstr (qry , "create table pgbench_branches" ) == DDLs [i ]) ||
1274
- (strstr (qry , "create table pgbench_tellers" ) == DDLs [i ]) ||
1275
- (strstr (qry , "create table pgbench_accounts" ) == DDLs [i ]))
1276
- {
1277
- snprintf (buffer1 , 128 , qry , fillfactor );
1278
- qry = buffer1 ;
1279
- }
1297
+ /* Remove old table, if it exists. */
1298
+ snprintf (buffer , 256 , "drop table if exists %s" , ddl -> table );
1299
+ executeStatement (con , buffer );
1280
1300
1281
- /*
1282
- * set unlogged tables, if requested
1283
- */
1284
- if (unlogged_tables && strncmp (qry , "create table" , 12 ) == 0 )
1301
+ /* Construct new create table statement. */
1302
+ opts [0 ] = '\0' ;
1303
+ if (ddl -> declare_fillfactor )
1304
+ snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1305
+ " with (fillfactor=%d)" , fillfactor );
1306
+ if (tablespace != NULL )
1285
1307
{
1286
- snprintf (buffer2 , 128 , "create unlogged%s" , qry + 6 );
1287
- qry = buffer2 ;
1308
+ char * escape_tablespace ;
1309
+ escape_tablespace = PQescapeIdentifier (con , tablespace ,
1310
+ strlen (tablespace ));
1311
+ snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1312
+ " tablespace %s" , escape_tablespace );
1313
+ PQfreemem (escape_tablespace );
1288
1314
}
1315
+ snprintf (buffer , 256 , "create%s table %s(%s)%s" ,
1316
+ unlogged_tables ? " unlogged" : "" ,
1317
+ ddl -> table , ddl -> cols , opts );
1289
1318
1290
- executeStatement (con , qry );
1319
+ executeStatement (con , buffer );
1291
1320
}
1292
1321
1293
1322
executeStatement (con , "begin" );
@@ -1354,7 +1383,23 @@ init(void)
1354
1383
*/
1355
1384
fprintf (stderr , "set primary key...\n" );
1356
1385
for (i = 0 ; i < lengthof (DDLAFTERs ); i ++ )
1357
- executeStatement (con , DDLAFTERs [i ]);
1386
+ {
1387
+ char buffer [256 ];
1388
+
1389
+ strncpy (buffer , DDLAFTERs [i ], 256 );
1390
+
1391
+ if (index_tablespace != NULL )
1392
+ {
1393
+ char * escape_tablespace ;
1394
+ escape_tablespace = PQescapeIdentifier (con , index_tablespace ,
1395
+ strlen (index_tablespace ));
1396
+ snprintf (buffer + strlen (buffer ), 256 - strlen (buffer ),
1397
+ " using index tablespace %s" , escape_tablespace );
1398
+ PQfreemem (escape_tablespace );
1399
+ }
1400
+
1401
+ executeStatement (con , buffer );
1402
+ }
1358
1403
1359
1404
/* vacuum */
1360
1405
fprintf (stderr , "vacuum..." );
@@ -1796,6 +1841,8 @@ main(int argc, char **argv)
1796
1841
int i ;
1797
1842
1798
1843
static struct option long_options [] = {
1844
+ {"index-tablespace" , required_argument , NULL , 3 },
1845
+ {"tablespace" , required_argument , NULL , 2 },
1799
1846
{"unlogged-tables" , no_argument , & unlogged_tables , 1 },
1800
1847
{NULL , 0 , NULL , 0 }
1801
1848
};
@@ -1996,7 +2043,13 @@ main(int argc, char **argv)
1996
2043
}
1997
2044
break ;
1998
2045
case 0 :
1999
- /* This covers the long options. */
2046
+ /* This covers long options which take no argument. */
2047
+ break ;
2048
+ case 2 : /* tablespace */
2049
+ tablespace = optarg ;
2050
+ break ;
2051
+ case 3 : /* index-tablespace */
2052
+ index_tablespace = optarg ;
2000
2053
break ;
2001
2054
default :
2002
2055
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
0 commit comments