@@ -1548,8 +1548,9 @@ disconnect_all(CState *state, int length)
15481548static void
15491549init (bool is_no_vacuum )
15501550{
1551- /* The scale factor at/beyond which 32bit integers are incapable of storing
1552- * 64bit values.
1551+ /*
1552+ * The scale factor at/beyond which 32-bit integers are insufficient for
1553+ * storing TPC-B account IDs.
15531554 *
15541555 * Although the actual threshold is 21474, we use 20000 because it is easier to
15551556 * document and remember, and isn't that far away from the real threshold.
@@ -1561,50 +1562,51 @@ init(bool is_no_vacuum)
15611562 * fields in these table declarations were intended to comply with that.
15621563 * The pgbench_accounts table complies with that because the "filler"
15631564 * column is set to blank-padded empty string. But for all other tables
1564- * the column defaults to NULL and so don't actually take any space. We
1565+ * the columns default to NULL and so don't actually take any space. We
15651566 * could fix that by giving them non-null default values. However, that
15661567 * would completely break comparability of pgbench results with prior
15671568 * versions. Since pgbench has never pretended to be fully TPC-B compliant
15681569 * anyway, we stick with the historical behavior.
15691570 */
15701571 struct ddlinfo
15711572 {
1572- char * table ;
1573- char * cols ;
1573+ const char * table ; /* table name */
1574+ const char * smcols ; /* column decls if accountIDs are 32 bits */
1575+ const char * bigcols ; /* column decls if accountIDs are 64 bits */
15741576 int declare_fillfactor ;
15751577 };
1576- struct ddlinfo DDLs [] = {
1578+ static const struct ddlinfo DDLs [] = {
15771579 {
15781580 "pgbench_history" ,
1579- scale >= SCALE_32BIT_THRESHOLD
1580- ? "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)"
1581- : "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)" ,
1581+ "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)" ,
1582+ "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)" ,
15821583 0
15831584 },
15841585 {
15851586 "pgbench_tellers" ,
15861587 "tid int not null,bid int,tbalance int,filler char(84)" ,
1588+ "tid int not null,bid int,tbalance int,filler char(84)" ,
15871589 1
15881590 },
15891591 {
15901592 "pgbench_accounts" ,
1591- scale >= SCALE_32BIT_THRESHOLD
1592- ? "aid bigint not null,bid int,abalance int,filler char(84)"
1593- : "aid int not null,bid int,abalance int,filler char(84)" ,
1593+ "aid int not null,bid int,abalance int,filler char(84)" ,
1594+ "aid bigint not null,bid int,abalance int,filler char(84)" ,
15941595 1
15951596 },
15961597 {
15971598 "pgbench_branches" ,
15981599 "bid int not null,bbalance int,filler char(88)" ,
1600+ "bid int not null,bbalance int,filler char(88)" ,
15991601 1
16001602 }
16011603 };
1602- static char * DDLAFTERs [] = {
1604+ static const char * const DDLINDEXes [] = {
16031605 "alter table pgbench_branches add primary key (bid)" ,
16041606 "alter table pgbench_tellers add primary key (tid)" ,
16051607 "alter table pgbench_accounts add primary key (aid)"
16061608 };
1607- static char * DDLKEYs [] = {
1609+ static const char * const DDLKEYs [] = {
16081610 "alter table pgbench_tellers add foreign key (bid) references pgbench_branches" ,
16091611 "alter table pgbench_accounts add foreign key (bid) references pgbench_branches" ,
16101612 "alter table pgbench_history add foreign key (bid) references pgbench_branches" ,
@@ -1632,30 +1634,34 @@ init(bool is_no_vacuum)
16321634 {
16331635 char opts [256 ];
16341636 char buffer [256 ];
1635- struct ddlinfo * ddl = & DDLs [i ];
1637+ const struct ddlinfo * ddl = & DDLs [i ];
1638+ const char * cols ;
16361639
16371640 /* Remove old table, if it exists. */
1638- snprintf (buffer , 256 , "drop table if exists %s" , ddl -> table );
1641+ snprintf (buffer , sizeof ( buffer ) , "drop table if exists %s" , ddl -> table );
16391642 executeStatement (con , buffer );
16401643
16411644 /* Construct new create table statement. */
16421645 opts [0 ] = '\0' ;
16431646 if (ddl -> declare_fillfactor )
1644- snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1647+ snprintf (opts + strlen (opts ), sizeof ( opts ) - strlen (opts ),
16451648 " with (fillfactor=%d)" , fillfactor );
16461649 if (tablespace != NULL )
16471650 {
16481651 char * escape_tablespace ;
16491652
16501653 escape_tablespace = PQescapeIdentifier (con , tablespace ,
16511654 strlen (tablespace ));
1652- snprintf (opts + strlen (opts ), 256 - strlen (opts ),
1655+ snprintf (opts + strlen (opts ), sizeof ( opts ) - strlen (opts ),
16531656 " tablespace %s" , escape_tablespace );
16541657 PQfreemem (escape_tablespace );
16551658 }
1656- snprintf (buffer , 256 , "create%s table %s(%s)%s" ,
1659+
1660+ cols = (scale >= SCALE_32BIT_THRESHOLD ) ? ddl -> bigcols : ddl -> smcols ;
1661+
1662+ snprintf (buffer , sizeof (buffer ), "create%s table %s(%s)%s" ,
16571663 unlogged_tables ? " unlogged" : "" ,
1658- ddl -> table , ddl -> cols , opts );
1664+ ddl -> table , cols , opts );
16591665
16601666 executeStatement (con , buffer );
16611667 }
@@ -1665,14 +1671,17 @@ init(bool is_no_vacuum)
16651671 for (i = 0 ; i < nbranches * scale ; i ++ )
16661672 {
16671673 /* "filler" column defaults to NULL */
1668- snprintf (sql , 256 , "insert into pgbench_branches(bid,bbalance) values(%d,0)" , i + 1 );
1674+ snprintf (sql , sizeof (sql ),
1675+ "insert into pgbench_branches(bid,bbalance) values(%d,0)" ,
1676+ i + 1 );
16691677 executeStatement (con , sql );
16701678 }
16711679
16721680 for (i = 0 ; i < ntellers * scale ; i ++ )
16731681 {
16741682 /* "filler" column defaults to NULL */
1675- snprintf (sql , 256 , "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
1683+ snprintf (sql , sizeof (sql ),
1684+ "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
16761685 i + 1 , i / ntellers + 1 );
16771686 executeStatement (con , sql );
16781687 }
@@ -1702,7 +1711,9 @@ init(bool is_no_vacuum)
17021711 int64 j = k + 1 ;
17031712
17041713 /* "filler" column defaults to blank padded empty string */
1705- snprintf (sql , 256 , INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" , j , k / naccounts + 1 , 0 );
1714+ snprintf (sql , sizeof (sql ),
1715+ INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" ,
1716+ j , k / naccounts + 1 , 0 );
17061717 if (PQputline (con , sql ))
17071718 {
17081719 fprintf (stderr , "PQputline failed\n" );
@@ -1774,19 +1785,19 @@ init(bool is_no_vacuum)
17741785 * create indexes
17751786 */
17761787 fprintf (stderr , "set primary keys...\n" );
1777- for (i = 0 ; i < lengthof (DDLAFTERs ); i ++ )
1788+ for (i = 0 ; i < lengthof (DDLINDEXes ); i ++ )
17781789 {
17791790 char buffer [256 ];
17801791
1781- strncpy (buffer , DDLAFTERs [i ], 256 );
1792+ strlcpy (buffer , DDLINDEXes [i ], sizeof ( buffer ) );
17821793
17831794 if (index_tablespace != NULL )
17841795 {
17851796 char * escape_tablespace ;
17861797
17871798 escape_tablespace = PQescapeIdentifier (con , index_tablespace ,
17881799 strlen (index_tablespace ));
1789- snprintf (buffer + strlen (buffer ), 256 - strlen (buffer ),
1800+ snprintf (buffer + strlen (buffer ), sizeof ( buffer ) - strlen (buffer ),
17901801 " using index tablespace %s" , escape_tablespace );
17911802 PQfreemem (escape_tablespace );
17921803 }
@@ -1806,7 +1817,6 @@ init(bool is_no_vacuum)
18061817 }
18071818 }
18081819
1809-
18101820 fprintf (stderr , "done.\n" );
18111821 PQfinish (con );
18121822}
0 commit comments