Skip to content

Commit 777d07d

Browse files
committed
Fix non-C89-compatible coding in pgbench.
C89 says that compound initializers may only contain constant expressions; a restriction violated by commit 89d00cb. While we've had no actual field complaints about this, C89 is still the project standard, and it's not saving all that much code to break compatibility here. So let's adhere to the old restriction. In passing, replace a bunch of hardwired constants "256" with sizeof(target-variable), just because the latter is more readable and less breakable. And const-ify where possible. Back-patch to 9.3 where the nonportable code was added. Andres Freund and Tom Lane
1 parent d6a9767 commit 777d07d

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

contrib/pgbench/pgbench.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,9 @@ disconnect_all(CState *state, int length)
14441444
static void
14451445
init(bool is_no_vacuum)
14461446
{
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.
14491450
*
14501451
* Although the actual threshold is 21474, we use 20000 because it is easier to
14511452
* document and remember, and isn't that far away from the real threshold.
@@ -1463,42 +1464,43 @@ init(bool is_no_vacuum)
14631464
*/
14641465
struct ddlinfo
14651466
{
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 */
14681470
int declare_fillfactor;
14691471
};
1470-
struct ddlinfo DDLs[] = {
1472+
static const struct ddlinfo DDLs[] = {
14711473
{
14721474
"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)",
14761477
0
14771478
},
14781479
{
14791480
"pgbench_tellers",
14801481
"tid int not null,bid int,tbalance int,filler char(84)",
1482+
"tid int not null,bid int,tbalance int,filler char(84)",
14811483
1
14821484
},
14831485
{
14841486
"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)",
14881489
1
14891490
},
14901491
{
14911492
"pgbench_branches",
14921493
"bid int not null,bbalance int,filler char(88)",
1494+
"bid int not null,bbalance int,filler char(88)",
14931495
1
14941496
}
14951497
};
1496-
static char *DDLAFTERs[] = {
1498+
static const char *const DDLINDEXes[] = {
14971499
"alter table pgbench_branches add primary key (bid)",
14981500
"alter table pgbench_tellers add primary key (tid)",
14991501
"alter table pgbench_accounts add primary key (aid)"
15001502
};
1501-
static char *DDLKEYs[] = {
1503+
static const char *const DDLKEYs[] = {
15021504
"alter table pgbench_tellers add foreign key (bid) references pgbench_branches",
15031505
"alter table pgbench_accounts add foreign key (bid) references pgbench_branches",
15041506
"alter table pgbench_history add foreign key (bid) references pgbench_branches",
@@ -1526,30 +1528,34 @@ init(bool is_no_vacuum)
15261528
{
15271529
char opts[256];
15281530
char buffer[256];
1529-
struct ddlinfo *ddl = &DDLs[i];
1531+
const struct ddlinfo *ddl = &DDLs[i];
1532+
const char *cols;
15301533

15311534
/* 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);
15331536
executeStatement(con, buffer);
15341537

15351538
/* Construct new create table statement. */
15361539
opts[0] = '\0';
15371540
if (ddl->declare_fillfactor)
1538-
snprintf(opts + strlen(opts), 256 - strlen(opts),
1541+
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
15391542
" with (fillfactor=%d)", fillfactor);
15401543
if (tablespace != NULL)
15411544
{
15421545
char *escape_tablespace;
15431546

15441547
escape_tablespace = PQescapeIdentifier(con, tablespace,
15451548
strlen(tablespace));
1546-
snprintf(opts + strlen(opts), 256 - strlen(opts),
1549+
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
15471550
" tablespace %s", escape_tablespace);
15481551
PQfreemem(escape_tablespace);
15491552
}
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",
15511557
unlogged_tables ? " unlogged" : "",
1552-
ddl->table, ddl->cols, opts);
1558+
ddl->table, cols, opts);
15531559

15541560
executeStatement(con, buffer);
15551561
}
@@ -1558,13 +1564,16 @@ init(bool is_no_vacuum)
15581564

15591565
for (i = 0; i < nbranches * scale; i++)
15601566
{
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);
15621570
executeStatement(con, sql);
15631571
}
15641572

15651573
for (i = 0; i < ntellers * scale; i++)
15661574
{
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)",
15681577
i + 1, i / ntellers + 1);
15691578
executeStatement(con, sql);
15701579
}
@@ -1593,7 +1602,9 @@ init(bool is_no_vacuum)
15931602
{
15941603
int64 j = k + 1;
15951604

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);
15971608
if (PQputline(con, sql))
15981609
{
15991610
fprintf(stderr, "PQputline failed\n");
@@ -1665,19 +1676,19 @@ init(bool is_no_vacuum)
16651676
* create indexes
16661677
*/
16671678
fprintf(stderr, "set primary keys...\n");
1668-
for (i = 0; i < lengthof(DDLAFTERs); i++)
1679+
for (i = 0; i < lengthof(DDLINDEXes); i++)
16691680
{
16701681
char buffer[256];
16711682

1672-
strncpy(buffer, DDLAFTERs[i], 256);
1683+
strlcpy(buffer, DDLINDEXes[i], sizeof(buffer));
16731684

16741685
if (index_tablespace != NULL)
16751686
{
16761687
char *escape_tablespace;
16771688

16781689
escape_tablespace = PQescapeIdentifier(con, index_tablespace,
16791690
strlen(index_tablespace));
1680-
snprintf(buffer + strlen(buffer), 256 - strlen(buffer),
1691+
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
16811692
" using index tablespace %s", escape_tablespace);
16821693
PQfreemem(escape_tablespace);
16831694
}
@@ -1697,7 +1708,6 @@ init(bool is_no_vacuum)
16971708
}
16981709
}
16991710

1700-
17011711
fprintf(stderr, "done.\n");
17021712
PQfinish(con);
17031713
}

0 commit comments

Comments
 (0)