@@ -1444,8 +1444,9 @@ disconnect_all(CState *state, int length)
1444
1444
static void
1445
1445
init (bool is_no_vacuum )
1446
1446
{
1447
- /* The scale factor at/beyond which 32bit integers are incapable of storing
1448
- * 64bit values.
1447
+ /*
1448
+ * The scale factor at/beyond which 32-bit integers are insufficient for
1449
+ * storing TPC-B account IDs.
1449
1450
*
1450
1451
* Although the actual threshold is 21474, we use 20000 because it is easier to
1451
1452
* document and remember, and isn't that far away from the real threshold.
@@ -1463,42 +1464,43 @@ init(bool is_no_vacuum)
1463
1464
*/
1464
1465
struct ddlinfo
1465
1466
{
1466
- char * table ;
1467
- char * cols ;
1467
+ const char * table ; /* table name */
1468
+ const char * smcols ; /* column decls if accountIDs are 32 bits */
1469
+ const char * bigcols ; /* column decls if accountIDs are 64 bits */
1468
1470
int declare_fillfactor ;
1469
1471
};
1470
- struct ddlinfo DDLs [] = {
1472
+ static const struct ddlinfo DDLs [] = {
1471
1473
{
1472
1474
"pgbench_history" ,
1473
- scale >= SCALE_32BIT_THRESHOLD
1474
- ? "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)"
1475
- : "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)" ,
1475
+ "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)" ,
1476
+ "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)" ,
1476
1477
0
1477
1478
},
1478
1479
{
1479
1480
"pgbench_tellers" ,
1480
1481
"tid int not null,bid int,tbalance int,filler char(84)" ,
1482
+ "tid int not null,bid int,tbalance int,filler char(84)" ,
1481
1483
1
1482
1484
},
1483
1485
{
1484
1486
"pgbench_accounts" ,
1485
- scale >= SCALE_32BIT_THRESHOLD
1486
- ? "aid bigint not null,bid int,abalance int,filler char(84)"
1487
- : "aid int not null,bid int,abalance int,filler char(84)" ,
1487
+ "aid int not null,bid int,abalance int,filler char(84)" ,
1488
+ "aid bigint not null,bid int,abalance int,filler char(84)" ,
1488
1489
1
1489
1490
},
1490
1491
{
1491
1492
"pgbench_branches" ,
1492
1493
"bid int not null,bbalance int,filler char(88)" ,
1494
+ "bid int not null,bbalance int,filler char(88)" ,
1493
1495
1
1494
1496
}
1495
1497
};
1496
- static char * DDLAFTERs [] = {
1498
+ static const char * const DDLINDEXes [] = {
1497
1499
"alter table pgbench_branches add primary key (bid)" ,
1498
1500
"alter table pgbench_tellers add primary key (tid)" ,
1499
1501
"alter table pgbench_accounts add primary key (aid)"
1500
1502
};
1501
- static char * DDLKEYs [] = {
1503
+ static const char * const DDLKEYs [] = {
1502
1504
"alter table pgbench_tellers add foreign key (bid) references pgbench_branches" ,
1503
1505
"alter table pgbench_accounts add foreign key (bid) references pgbench_branches" ,
1504
1506
"alter table pgbench_history add foreign key (bid) references pgbench_branches" ,
@@ -1526,30 +1528,34 @@ init(bool is_no_vacuum)
1526
1528
{
1527
1529
char opts [256 ];
1528
1530
char buffer [256 ];
1529
- struct ddlinfo * ddl = & DDLs [i ];
1531
+ const struct ddlinfo * ddl = & DDLs [i ];
1532
+ const char * cols ;
1530
1533
1531
1534
/* Remove old table, if it exists. */
1532
- snprintf (buffer , 256 , "drop table if exists %s" , ddl -> table );
1535
+ snprintf (buffer , sizeof ( buffer ) , "drop table if exists %s" , ddl -> table );
1533
1536
executeStatement (con , buffer );
1534
1537
1535
1538
/* Construct new create table statement. */
1536
1539
opts [0 ] = '\0' ;
1537
1540
if (ddl -> declare_fillfactor )
1538
- snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1541
+ snprintf (opts + strlen (opts ), sizeof ( opts ) - strlen (opts ),
1539
1542
" with (fillfactor=%d)" , fillfactor );
1540
1543
if (tablespace != NULL )
1541
1544
{
1542
1545
char * escape_tablespace ;
1543
1546
1544
1547
escape_tablespace = PQescapeIdentifier (con , tablespace ,
1545
1548
strlen (tablespace ));
1546
- snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1549
+ snprintf (opts + strlen (opts ), sizeof ( opts ) - strlen (opts ),
1547
1550
" tablespace %s" , escape_tablespace );
1548
1551
PQfreemem (escape_tablespace );
1549
1552
}
1550
- snprintf (buffer , 256 , "create%s table %s(%s)%s" ,
1553
+
1554
+ cols = (scale >= SCALE_32BIT_THRESHOLD ) ? ddl -> bigcols : ddl -> smcols ;
1555
+
1556
+ snprintf (buffer , sizeof (buffer ), "create%s table %s(%s)%s" ,
1551
1557
unlogged_tables ? " unlogged" : "" ,
1552
- ddl -> table , ddl -> cols , opts );
1558
+ ddl -> table , cols , opts );
1553
1559
1554
1560
executeStatement (con , buffer );
1555
1561
}
@@ -1558,13 +1564,16 @@ init(bool is_no_vacuum)
1558
1564
1559
1565
for (i = 0 ; i < nbranches * scale ; i ++ )
1560
1566
{
1561
- snprintf (sql , 256 , "insert into pgbench_branches(bid,bbalance) values(%d,0)" , i + 1 );
1567
+ snprintf (sql , sizeof (sql ),
1568
+ "insert into pgbench_branches(bid,bbalance) values(%d,0)" ,
1569
+ i + 1 );
1562
1570
executeStatement (con , sql );
1563
1571
}
1564
1572
1565
1573
for (i = 0 ; i < ntellers * scale ; i ++ )
1566
1574
{
1567
- snprintf (sql , 256 , "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
1575
+ snprintf (sql , sizeof (sql ),
1576
+ "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
1568
1577
i + 1 , i / ntellers + 1 );
1569
1578
executeStatement (con , sql );
1570
1579
}
@@ -1593,7 +1602,9 @@ init(bool is_no_vacuum)
1593
1602
{
1594
1603
int64 j = k + 1 ;
1595
1604
1596
- snprintf (sql , 256 , INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" , j , k / naccounts + 1 , 0 );
1605
+ snprintf (sql , sizeof (sql ),
1606
+ INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" ,
1607
+ j , k / naccounts + 1 , 0 );
1597
1608
if (PQputline (con , sql ))
1598
1609
{
1599
1610
fprintf (stderr , "PQputline failed\n" );
@@ -1665,19 +1676,19 @@ init(bool is_no_vacuum)
1665
1676
* create indexes
1666
1677
*/
1667
1678
fprintf (stderr , "set primary keys...\n" );
1668
- for (i = 0 ; i < lengthof (DDLAFTERs ); i ++ )
1679
+ for (i = 0 ; i < lengthof (DDLINDEXes ); i ++ )
1669
1680
{
1670
1681
char buffer [256 ];
1671
1682
1672
- strncpy (buffer , DDLAFTERs [i ], 256 );
1683
+ strlcpy (buffer , DDLINDEXes [i ], sizeof ( buffer ) );
1673
1684
1674
1685
if (index_tablespace != NULL )
1675
1686
{
1676
1687
char * escape_tablespace ;
1677
1688
1678
1689
escape_tablespace = PQescapeIdentifier (con , index_tablespace ,
1679
1690
strlen (index_tablespace ));
1680
- snprintf (buffer + strlen (buffer ), 256 - strlen (buffer ),
1691
+ snprintf (buffer + strlen (buffer ), sizeof ( buffer ) - strlen (buffer ),
1681
1692
" using index tablespace %s" , escape_tablespace );
1682
1693
PQfreemem (escape_tablespace );
1683
1694
}
@@ -1697,7 +1708,6 @@ init(bool is_no_vacuum)
1697
1708
}
1698
1709
}
1699
1710
1700
-
1701
1711
fprintf (stderr , "done.\n" );
1702
1712
PQfinish (con );
1703
1713
}
0 commit comments