55#include < inttypes.h>
66#include < sys/time.h>
77#include < pthread.h>
8+ #include < unistd.h>
89
910#include < string>
1011#include < vector>
1314#include < pqxx/transaction>
1415#include < pqxx/nontransaction>
1516#include < pqxx/pipeline>
17+ #include < pqxx/version>
1618
1719using namespace std ;
1820using namespace pqxx ;
@@ -39,6 +41,8 @@ struct config
3941 int nIndexes;
4042 int nIterations;
4143 int transactionSize;
44+ bool useSystemTime;
45+ bool noPK;
4246 string connection;
4347
4448 config () {
@@ -47,6 +51,8 @@ struct config
4751 nIndexes = 8 ;
4852 nIterations = 10000 ;
4953 transactionSize = 100 ;
54+ useSystemTime = false ;
55+ noPK = false ;
5056 }
5157};
5258
@@ -55,6 +61,7 @@ bool running;
5561int nIndexUpdates;
5662time_t maxIndexUpdateTime;
5763time_t totalIndexUpdateTime;
64+ time_t currTimestamp;
5865
5966#define USEC 1000000
6067
@@ -79,14 +86,30 @@ void exec(transaction_base& txn, char const* sql, ...)
7986void * inserter (void * arg)
8087{
8188 connection con (cfg.connection );
82- con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" );
89+ if (cfg.useSystemTime )
90+ {
91+ #if PQXX_VERSION_MAJOR >= 4
92+ con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" );
93+ #else
94+ con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" );
95+ #endif
96+ } else {
97+ con.prepare (" insert" , " insert into t (select generate_series($1::integer,$2::integer),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000))" );
98+ }
99+
83100 for (int i = 0 ; i < cfg.nIterations ; i++)
84101 {
85102 work txn (con);
86- for (int j = 0 ; j < cfg.transactionSize ; j++)
87- {
88- txn.prepared (" insert" )(getCurrentTime ())(random ())(random ())(random ())(random ())(random ())(random ())(random ())(random ()).exec ();
89- }
103+ if (cfg.useSystemTime )
104+ {
105+ for (int j = 0 ; j < cfg.transactionSize ; j++)
106+ {
107+ txn.prepared (" insert" )(getCurrentTime ())(random ())(random ())(random ())(random ())(random ())(random ())(random ())(random ()).exec ();
108+ }
109+ } else {
110+ currTimestamp = i*cfg.transactionSize ;
111+ txn.prepared (" insert" )(i*cfg.transactionSize )((i+1 )*cfg.transactionSize -1 ).exec ();
112+ }
90113 txn.commit ();
91114 }
92115 return NULL ;
@@ -97,14 +120,16 @@ void* indexUpdater(void* arg)
97120 connection con (cfg.connection );
98121 while (running) {
99122 sleep (cfg.indexUpdateInterval );
123+ printf (" Alter indexes\n " );
100124 time_t now = getCurrentTime ();
101125 {
102126 work txn (con);
103127 for (int i = 0 ; i < cfg.nIndexes ; i++) {
104- exec (txn, " alter index idx%d where pk<%lu" , i, now);
128+ exec (txn, " alter index idx%d where pk<%lu" , i, cfg. useSystemTime ? now : currTimestamp );
105129 }
106130 txn.commit ();
107131 }
132+ printf (" End alter indexes\n " );
108133 nIndexUpdates += 1 ;
109134 time_t elapsed = getCurrentTime () - now;
110135 totalIndexUpdateTime += elapsed;
@@ -122,12 +147,16 @@ void initializeDatabase()
122147 time_t now = getCurrentTime ();
123148 exec (txn, " drop table if exists t" );
124149 exec (txn, " create table t (pk bigint, k1 bigint, k2 bigint, k3 bigint, k4 bigint, k5 bigint, k6 bigint, k7 bigint, k8 bigint)" );
125- exec (txn, " create index pk on t(pk)" );
150+ if (!cfg.noPK ) {
151+ exec (txn, " create index pk on t(pk)" );
152+ }
126153 for (int i = 0 ; i < cfg.nIndexes ; i++) {
127154 if (cfg.indexUpdateInterval == 0 ) {
128155 exec (txn, " create index idx%d on t(k%d)" , i, i+1 );
129- } else {
156+ } else if (cfg. useSystemTime ) {
130157 exec (txn, " create index idx%d on t(k%d) where pk<%ld" , i, i+1 , now);
158+ } else {
159+ exec (txn, " create index idx%d on t(k%d) where pk<%ld" , i, i+1 , 0 );
131160 }
132161 }
133162 txn.commit ();
@@ -162,6 +191,12 @@ int main (int argc, char* argv[])
162191 case ' c' :
163192 cfg.connection = string (argv[++i]);
164193 continue ;
194+ case ' q' :
195+ cfg.useSystemTime = true ;
196+ continue ;
197+ case ' p' :
198+ cfg.noPK = true ;
199+ continue ;
165200 }
166201 }
167202 printf (" Options:\n "
@@ -170,6 +205,8 @@ int main (int argc, char* argv[])
170205 " \t -u N\t index update interval (0)\n "
171206 " \t -n N\t number of iterations (10000)\n "
172207 " \t -i N\t number of indexes (8)\n "
208+ " \t -q\t use system time and libpq\n "
209+ " \t -p\t no primary key\n "
173210 " \t -c STR\t database connection string\n " );
174211 return 1 ;
175212 }
0 commit comments