@@ -124,6 +124,12 @@ intfillfactor = 100;
124124 */
125125int unlogged_tables = 0 ;
126126
127+ /*
128+ * tablespace selection
129+ */
130+ char * tablespace = NULL ;
131+ char * index_tablespace = NULL ;
132+
127133/*
128134 * end of configurable parameters
129135 *********************************************************************/
@@ -359,6 +365,10 @@ usage(const char *progname)
359365" -h HOSTNAME database server host or socket directory\n"
360366" -p PORT database server port number\n"
361367" -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"
362372" --unlogged-tables\n"
363373" create tables as unlogged tables\n"
364374" --help show this help, then exit\n"
@@ -1237,15 +1247,32 @@ init(void)
12371247 * versions. Since pgbench has never pretended to be fully TPC-B
12381248 * compliant anyway, we stick with the historical behavior.
12391249 */
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+ }
12491276};
12501277static char * DDLAFTERs []= {
12511278"alter table pgbench_branches add primary key (bid)" ,
@@ -1263,31 +1290,33 @@ init(void)
12631290
12641291for (i = 0 ;i < lengthof (DDLs );i ++ )
12651292{
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 ];
12691296
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 );
12801300
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 )
12851307{
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 );
12881314}
1315+ snprintf (buffer ,256 ,"create%s table %s(%s)%s" ,
1316+ unlogged_tables ?" unlogged" :"" ,
1317+ ddl -> table ,ddl -> cols ,opts );
12891318
1290- executeStatement (con ,qry );
1319+ executeStatement (con ,buffer );
12911320}
12921321
12931322executeStatement (con ,"begin" );
@@ -1354,7 +1383,23 @@ init(void)
13541383 */
13551384fprintf (stderr ,"set primary key...\n" );
13561385for (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+ }
13581403
13591404/* vacuum */
13601405fprintf (stderr ,"vacuum..." );
@@ -1796,6 +1841,8 @@ main(int argc, char **argv)
17961841int i ;
17971842
17981843static struct option long_options []= {
1844+ {"index-tablespace" ,required_argument ,NULL ,3 },
1845+ {"tablespace" ,required_argument ,NULL ,2 },
17991846{"unlogged-tables" ,no_argument ,& unlogged_tables ,1 },
18001847{NULL ,0 ,NULL ,0 }
18011848};
@@ -1996,7 +2043,13 @@ main(int argc, char **argv)
19962043}
19972044break ;
19982045case 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 ;
20002053break ;
20012054default :
20022055fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );