@@ -129,6 +129,11 @@ intforeign_keys = 0;
129
129
*/
130
130
int unlogged_tables = 0 ;
131
131
132
+ /*
133
+ * log sampling rate (1.0 = log everything, 0.0 = option not given)
134
+ */
135
+ double sample_rate = 0.0 ;
136
+
132
137
/*
133
138
* tablespace selection
134
139
*/
@@ -370,6 +375,8 @@ usage(void)
370
375
" -f FILENAME read transaction script from FILENAME\n"
371
376
" -j NUM number of threads (default: 1)\n"
372
377
" -l write transaction times to log file\n"
378
+ " --sampling-rate NUM\n"
379
+ " fraction of transactions to log (e.g. 0.01 for 1%% sample)\n"
373
380
" -M simple|extended|prepared\n"
374
381
" protocol for submitting queries to server (default: simple)\n"
375
382
" -n do not run VACUUM before tests\n"
@@ -883,21 +890,30 @@ doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile)
883
890
instr_time diff ;
884
891
double usec ;
885
892
886
- INSTR_TIME_SET_CURRENT (now );
887
- diff = now ;
888
- INSTR_TIME_SUBTRACT (diff ,st -> txn_begin );
889
- usec = (double )INSTR_TIME_GET_MICROSEC (diff );
893
+ /*
894
+ * write the log entry if this row belongs to the random sample,
895
+ * or no sampling rate was given which means log everything.
896
+ */
897
+ if (sample_rate == 0.0 ||
898
+ pg_erand48 (thread -> random_state ) <=sample_rate )
899
+ {
900
+
901
+ INSTR_TIME_SET_CURRENT (now );
902
+ diff = now ;
903
+ INSTR_TIME_SUBTRACT (diff ,st -> txn_begin );
904
+ usec = (double )INSTR_TIME_GET_MICROSEC (diff );
890
905
891
906
#ifndef WIN32
892
- /* This is more than we really ought to know about instr_time */
893
- fprintf (logfile ,"%d %d %.0f %d %ld %ld\n" ,
894
- st -> id ,st -> cnt ,usec ,st -> use_file ,
895
- (long )now .tv_sec , (long )now .tv_usec );
907
+ /* This is more than we really ought to know about instr_time */
908
+ fprintf (logfile ,"%d %d %.0f %d %ld %ld\n" ,
909
+ st -> id ,st -> cnt ,usec ,st -> use_file ,
910
+ (long )now .tv_sec , (long )now .tv_usec );
896
911
#else
897
- /* On Windows, instr_time doesn't provide a timestamp anyway */
898
- fprintf (logfile ,"%d %d %.0f %d 0 0\n" ,
899
- st -> id ,st -> cnt ,usec ,st -> use_file );
912
+ /* On Windows, instr_time doesn't provide a timestamp anyway */
913
+ fprintf (logfile ,"%d %d %.0f %d 0 0\n" ,
914
+ st -> id ,st -> cnt ,usec ,st -> use_file );
900
915
#endif
916
+ }
901
917
}
902
918
903
919
if (commands [st -> state ]-> type == SQL_COMMAND )
@@ -1926,6 +1942,7 @@ main(int argc, char **argv)
1926
1942
{"index-tablespace" ,required_argument ,NULL ,3 },
1927
1943
{"tablespace" ,required_argument ,NULL ,2 },
1928
1944
{"unlogged-tables" ,no_argument ,& unlogged_tables ,1 },
1945
+ {"sampling-rate" ,required_argument ,NULL ,4 },
1929
1946
{NULL ,0 ,NULL ,0 }
1930
1947
};
1931
1948
@@ -2131,6 +2148,14 @@ main(int argc, char **argv)
2131
2148
case 3 :/* index-tablespace */
2132
2149
index_tablespace = optarg ;
2133
2150
break ;
2151
+ case 4 :
2152
+ sample_rate = atof (optarg );
2153
+ if (sample_rate <=0.0 || sample_rate > 1.0 )
2154
+ {
2155
+ fprintf (stderr ,"invalid sampling rate: %f\n" ,sample_rate );
2156
+ exit (1 );
2157
+ }
2158
+ break ;
2134
2159
default :
2135
2160
fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
2136
2161
exit (1 );
@@ -2166,6 +2191,13 @@ main(int argc, char **argv)
2166
2191
exit (1 );
2167
2192
}
2168
2193
2194
+ /* --sampling-rate may be used only with -l */
2195
+ if (sample_rate > 0.0 && !use_log )
2196
+ {
2197
+ fprintf (stderr ,"log sampling rate is allowed only when logging transactions (-l) \n" );
2198
+ exit (1 );
2199
+ }
2200
+
2169
2201
/*
2170
2202
* is_latencies only works with multiple threads in thread-based
2171
2203
* implementations, not fork-based ones, because it supposes that the