@@ -603,7 +603,6 @@ static void doLog(TState *thread, CState *st,
603
603
StatsData * agg , bool skipped , double latency , double lag );
604
604
static void processXactStats (TState * thread , CState * st , instr_time * now ,
605
605
bool skipped , StatsData * agg );
606
- static void append_fillfactor (char * opts , int len );
607
606
static void addScript (ParsedScript script );
608
607
static void * threadRun (void * arg );
609
608
static void finishCon (CState * st );
@@ -3630,30 +3629,26 @@ initDropTables(PGconn *con)
3630
3629
static void
3631
3630
createPartitions (PGconn * con )
3632
3631
{
3633
- char ff [64 ];
3634
-
3635
- ff [0 ] = '\0' ;
3636
-
3637
- /*
3638
- * Per ddlinfo in initCreateTables, fillfactor is needed on table
3639
- * pgbench_accounts.
3640
- */
3641
- append_fillfactor (ff , sizeof (ff ));
3632
+ PQExpBufferData query ;
3642
3633
3643
3634
/* we must have to create some partitions */
3644
3635
Assert (partitions > 0 );
3645
3636
3646
3637
fprintf (stderr , "creating %d partitions...\n" , partitions );
3647
3638
3639
+ initPQExpBuffer (& query );
3640
+
3648
3641
for (int p = 1 ; p <= partitions ; p ++ )
3649
3642
{
3650
- char query [256 ];
3651
-
3652
3643
if (partition_method == PART_RANGE )
3653
3644
{
3654
3645
int64 part_size = (naccounts * (int64 ) scale + partitions - 1 ) / partitions ;
3655
- char minvalue [32 ],
3656
- maxvalue [32 ];
3646
+
3647
+ printfPQExpBuffer (& query ,
3648
+ "create%s table pgbench_accounts_%d\n"
3649
+ " partition of pgbench_accounts\n"
3650
+ " for values from (" ,
3651
+ unlogged_tables ? " unlogged" : "" , p );
3657
3652
3658
3653
/*
3659
3654
* For RANGE, we use open-ended partitions at the beginning and
@@ -3662,34 +3657,39 @@ createPartitions(PGconn *con)
3662
3657
* scale, it is more generic and the performance is better.
3663
3658
*/
3664
3659
if (p == 1 )
3665
- sprintf ( minvalue , "minvalue" );
3660
+ appendPQExpBufferStr ( & query , "minvalue" );
3666
3661
else
3667
- sprintf (minvalue , INT64_FORMAT , (p - 1 ) * part_size + 1 );
3662
+ appendPQExpBuffer (& query , INT64_FORMAT , (p - 1 ) * part_size + 1 );
3663
+
3664
+ appendPQExpBufferStr (& query , ") to (" );
3668
3665
3669
3666
if (p < partitions )
3670
- sprintf ( maxvalue , INT64_FORMAT , p * part_size + 1 );
3667
+ appendPQExpBuffer ( & query , INT64_FORMAT , p * part_size + 1 );
3671
3668
else
3672
- sprintf (maxvalue , "maxvalue" );
3673
-
3674
- snprintf (query , sizeof (query ),
3675
- "create%s table pgbench_accounts_%d\n"
3676
- " partition of pgbench_accounts\n"
3677
- " for values from (%s) to (%s)%s\n" ,
3678
- unlogged_tables ? " unlogged" : "" , p ,
3679
- minvalue , maxvalue , ff );
3669
+ appendPQExpBufferStr (& query , "maxvalue" );
3670
+
3671
+ appendPQExpBufferChar (& query , ')' );
3680
3672
}
3681
3673
else if (partition_method == PART_HASH )
3682
- snprintf ( query , sizeof ( query ) ,
3683
- "create%s table pgbench_accounts_%d\n"
3684
- " partition of pgbench_accounts\n"
3685
- " for values with (modulus %d, remainder %d)%s\n " ,
3686
- unlogged_tables ? " unlogged" : "" , p ,
3687
- partitions , p - 1 , ff );
3674
+ printfPQExpBuffer ( & query ,
3675
+ "create%s table pgbench_accounts_%d\n"
3676
+ " partition of pgbench_accounts\n"
3677
+ " for values with (modulus %d, remainder %d)" ,
3678
+ unlogged_tables ? " unlogged" : "" , p ,
3679
+ partitions , p - 1 );
3688
3680
else /* cannot get there */
3689
3681
Assert (0 );
3690
3682
3691
- executeStatement (con , query );
3683
+ /*
3684
+ * Per ddlinfo in initCreateTables, fillfactor is needed on table
3685
+ * pgbench_accounts.
3686
+ */
3687
+ appendPQExpBuffer (& query , " with (fillfactor=%d)" , fillfactor );
3688
+
3689
+ executeStatement (con , query .data );
3692
3690
}
3691
+
3692
+ termPQExpBuffer (& query );
3693
3693
}
3694
3694
3695
3695
/*
@@ -3743,63 +3743,50 @@ initCreateTables(PGconn *con)
3743
3743
}
3744
3744
};
3745
3745
int i ;
3746
+ PQExpBufferData query ;
3746
3747
3747
3748
fprintf (stderr , "creating tables...\n" );
3748
3749
3750
+ initPQExpBuffer (& query );
3751
+
3749
3752
for (i = 0 ; i < lengthof (DDLs ); i ++ )
3750
3753
{
3751
- char opts [256 ];
3752
- char buffer [256 ];
3753
3754
const struct ddlinfo * ddl = & DDLs [i ];
3754
- const char * cols ;
3755
3755
3756
3756
/* Construct new create table statement. */
3757
- opts [0 ] = '\0' ;
3757
+ printfPQExpBuffer (& query , "create%s table %s(%s)" ,
3758
+ unlogged_tables ? " unlogged" : "" ,
3759
+ ddl -> table ,
3760
+ (scale >= SCALE_32BIT_THRESHOLD ) ? ddl -> bigcols : ddl -> smcols );
3758
3761
3759
3762
/* Partition pgbench_accounts table */
3760
3763
if (partition_method != PART_NONE && strcmp (ddl -> table , "pgbench_accounts" ) == 0 )
3761
- snprintf ( opts + strlen ( opts ), sizeof ( opts ) - strlen ( opts ) ,
3762
- " partition by %s (aid)" , PARTITION_METHOD [partition_method ]);
3764
+ appendPQExpBuffer ( & query ,
3765
+ " partition by %s (aid)" , PARTITION_METHOD [partition_method ]);
3763
3766
else if (ddl -> declare_fillfactor )
3767
+ {
3764
3768
/* fillfactor is only expected on actual tables */
3765
- append_fillfactor (opts , sizeof (opts ));
3769
+ appendPQExpBuffer (& query , " with (fillfactor=%d)" , fillfactor );
3770
+ }
3766
3771
3767
3772
if (tablespace != NULL )
3768
3773
{
3769
3774
char * escape_tablespace ;
3770
3775
3771
- escape_tablespace = PQescapeIdentifier (con , tablespace ,
3772
- strlen (tablespace ));
3773
- snprintf (opts + strlen (opts ), sizeof (opts ) - strlen (opts ),
3774
- " tablespace %s" , escape_tablespace );
3776
+ escape_tablespace = PQescapeIdentifier (con , tablespace , strlen (tablespace ));
3777
+ appendPQExpBuffer (& query , " tablespace %s" , escape_tablespace );
3775
3778
PQfreemem (escape_tablespace );
3776
3779
}
3777
3780
3778
- cols = (scale >= SCALE_32BIT_THRESHOLD ) ? ddl -> bigcols : ddl -> smcols ;
3779
-
3780
- snprintf (buffer , sizeof (buffer ), "create%s table %s(%s)%s" ,
3781
- unlogged_tables ? " unlogged" : "" ,
3782
- ddl -> table , cols , opts );
3783
-
3784
- executeStatement (con , buffer );
3781
+ executeStatement (con , query .data );
3785
3782
}
3786
3783
3784
+ termPQExpBuffer (& query );
3785
+
3787
3786
if (partition_method != PART_NONE )
3788
3787
createPartitions (con );
3789
3788
}
3790
3789
3791
- /*
3792
- * add fillfactor percent option.
3793
- *
3794
- * XXX - As default is 100, it could be removed in this case.
3795
- */
3796
- static void
3797
- append_fillfactor (char * opts , int len )
3798
- {
3799
- snprintf (opts + strlen (opts ), len - strlen (opts ),
3800
- " with (fillfactor=%d)" , fillfactor );
3801
- }
3802
-
3803
3790
/*
3804
3791
* Truncate away any old data, in one command in case there are foreign keys
3805
3792
*/
@@ -3819,7 +3806,7 @@ initTruncateTables(PGconn *con)
3819
3806
static void
3820
3807
initGenerateDataClientSide (PGconn * con )
3821
3808
{
3822
- char sql [ 256 ] ;
3809
+ PQExpBufferData sql ;
3823
3810
PGresult * res ;
3824
3811
int i ;
3825
3812
int64 k ;
@@ -3845,26 +3832,28 @@ initGenerateDataClientSide(PGconn *con)
3845
3832
/* truncate away any old data */
3846
3833
initTruncateTables (con );
3847
3834
3835
+ initPQExpBuffer (& sql );
3836
+
3848
3837
/*
3849
3838
* fill branches, tellers, accounts in that order in case foreign keys
3850
3839
* already exist
3851
3840
*/
3852
3841
for (i = 0 ; i < nbranches * scale ; i ++ )
3853
3842
{
3854
3843
/* "filler" column defaults to NULL */
3855
- snprintf ( sql , sizeof ( sql ) ,
3856
- "insert into pgbench_branches(bid,bbalance) values(%d,0)" ,
3857
- i + 1 );
3858
- executeStatement (con , sql );
3844
+ printfPQExpBuffer ( & sql ,
3845
+ "insert into pgbench_branches(bid,bbalance) values(%d,0)" ,
3846
+ i + 1 );
3847
+ executeStatement (con , sql . data );
3859
3848
}
3860
3849
3861
3850
for (i = 0 ; i < ntellers * scale ; i ++ )
3862
3851
{
3863
3852
/* "filler" column defaults to NULL */
3864
- snprintf ( sql , sizeof ( sql ) ,
3865
- "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
3866
- i + 1 , i / ntellers + 1 );
3867
- executeStatement (con , sql );
3853
+ printfPQExpBuffer ( & sql ,
3854
+ "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)" ,
3855
+ i + 1 , i / ntellers + 1 );
3856
+ executeStatement (con , sql . data );
3868
3857
}
3869
3858
3870
3859
/*
@@ -3885,10 +3874,10 @@ initGenerateDataClientSide(PGconn *con)
3885
3874
int64 j = k + 1 ;
3886
3875
3887
3876
/* "filler" column defaults to blank padded empty string */
3888
- snprintf ( sql , sizeof ( sql ) ,
3889
- INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" ,
3890
- j , k / naccounts + 1 , 0 );
3891
- if (PQputline (con , sql ))
3877
+ printfPQExpBuffer ( & sql ,
3878
+ INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n" ,
3879
+ j , k / naccounts + 1 , 0 );
3880
+ if (PQputline (con , sql . data ))
3892
3881
{
3893
3882
pg_log_fatal ("PQputline failed" );
3894
3883
exit (1 );
@@ -3950,6 +3939,8 @@ initGenerateDataClientSide(PGconn *con)
3950
3939
exit (1 );
3951
3940
}
3952
3941
3942
+ termPQExpBuffer (& sql );
3943
+
3953
3944
executeStatement (con , "commit" );
3954
3945
}
3955
3946
@@ -3963,7 +3954,7 @@ initGenerateDataClientSide(PGconn *con)
3963
3954
static void
3964
3955
initGenerateDataServerSide (PGconn * con )
3965
3956
{
3966
- char sql [ 256 ] ;
3957
+ PQExpBufferData sql ;
3967
3958
3968
3959
fprintf (stderr , "generating data (server-side)...\n" );
3969
3960
@@ -3976,24 +3967,28 @@ initGenerateDataServerSide(PGconn *con)
3976
3967
/* truncate away any old data */
3977
3968
initTruncateTables (con );
3978
3969
3979
- snprintf (sql , sizeof (sql ),
3980
- "insert into pgbench_branches(bid,bbalance) "
3981
- "select bid, 0 "
3982
- "from generate_series(1, %d) as bid" , nbranches * scale );
3983
- executeStatement (con , sql );
3984
-
3985
- snprintf (sql , sizeof (sql ),
3986
- "insert into pgbench_tellers(tid,bid,tbalance) "
3987
- "select tid, (tid - 1) / %d + 1, 0 "
3988
- "from generate_series(1, %d) as tid" , ntellers , ntellers * scale );
3989
- executeStatement (con , sql );
3990
-
3991
- snprintf (sql , sizeof (sql ),
3992
- "insert into pgbench_accounts(aid,bid,abalance,filler) "
3993
- "select aid, (aid - 1) / %d + 1, 0, '' "
3994
- "from generate_series(1, " INT64_FORMAT ") as aid" ,
3995
- naccounts , (int64 ) naccounts * scale );
3996
- executeStatement (con , sql );
3970
+ initPQExpBuffer (& sql );
3971
+
3972
+ printfPQExpBuffer (& sql ,
3973
+ "insert into pgbench_branches(bid,bbalance) "
3974
+ "select bid, 0 "
3975
+ "from generate_series(1, %d) as bid" , nbranches * scale );
3976
+ executeStatement (con , sql .data );
3977
+
3978
+ printfPQExpBuffer (& sql ,
3979
+ "insert into pgbench_tellers(tid,bid,tbalance) "
3980
+ "select tid, (tid - 1) / %d + 1, 0 "
3981
+ "from generate_series(1, %d) as tid" , ntellers , ntellers * scale );
3982
+ executeStatement (con , sql .data );
3983
+
3984
+ printfPQExpBuffer (& sql ,
3985
+ "insert into pgbench_accounts(aid,bid,abalance,filler) "
3986
+ "select aid, (aid - 1) / %d + 1, 0, '' "
3987
+ "from generate_series(1, " INT64_FORMAT ") as aid" ,
3988
+ naccounts , (int64 ) naccounts * scale );
3989
+ executeStatement (con , sql .data );
3990
+
3991
+ termPQExpBuffer (& sql );
3997
3992
3998
3993
executeStatement (con , "commit" );
3999
3994
}
@@ -4023,27 +4018,30 @@ initCreatePKeys(PGconn *con)
4023
4018
"alter table pgbench_accounts add primary key (aid)"
4024
4019
};
4025
4020
int i ;
4021
+ PQExpBufferData query ;
4026
4022
4027
4023
fprintf (stderr , "creating primary keys...\n" );
4024
+ initPQExpBuffer (& query );
4025
+
4028
4026
for (i = 0 ; i < lengthof (DDLINDEXes ); i ++ )
4029
4027
{
4030
- char buffer [256 ];
4031
-
4032
- strlcpy (buffer , DDLINDEXes [i ], sizeof (buffer ));
4028
+ resetPQExpBuffer (& query );
4029
+ appendPQExpBufferStr (& query , DDLINDEXes [i ]);
4033
4030
4034
4031
if (index_tablespace != NULL )
4035
4032
{
4036
4033
char * escape_tablespace ;
4037
4034
4038
4035
escape_tablespace = PQescapeIdentifier (con , index_tablespace ,
4039
4036
strlen (index_tablespace ));
4040
- snprintf (buffer + strlen (buffer ), sizeof (buffer ) - strlen (buffer ),
4041
- " using index tablespace %s" , escape_tablespace );
4037
+ appendPQExpBuffer (& query , " using index tablespace %s" , escape_tablespace );
4042
4038
PQfreemem (escape_tablespace );
4043
4039
}
4044
4040
4045
- executeStatement (con , buffer );
4041
+ executeStatement (con , query . data );
4046
4042
}
4043
+
4044
+ termPQExpBuffer (& query );
4047
4045
}
4048
4046
4049
4047
/*
0 commit comments