* check_log_duration * Determine whether current command's duration should be logged * We also check if this statement in this transaction must be logged * (regardless of its duration). * * Returns: * 0 if no logging is needed * 1 if just the duration should be logged * 2 if duration and query details should be logged * * If logging is needed, the duration in msec is formatted into
| 3188 | * essentially prevents 2 from being returned). |
| 3189 | */ |
| 3190 | int |
| 3191 | check_log_duration(char *msec_str, bool was_logged) |
| 3192 | { |
| 3193 | if (log_duration || log_min_duration_sample >= 0 || |
| 3194 | log_min_duration_statement >= 0 || xact_is_sampled) |
| 3195 | { |
| 3196 | long secs; |
| 3197 | int usecs; |
| 3198 | int msecs; |
| 3199 | bool exceeded_duration; |
| 3200 | bool exceeded_sample_duration; |
| 3201 | bool in_sample = false; |
| 3202 | |
| 3203 | TimestampDifference(GetCurrentStatementStartTimestamp(), |
| 3204 | GetCurrentTimestamp(), |
| 3205 | &secs, &usecs); |
| 3206 | msecs = usecs / 1000; |
| 3207 | |
| 3208 | /* |
| 3209 | * This odd-looking test for log_min_duration_* being exceeded is |
| 3210 | * designed to avoid integer overflow with very long durations: don't |
| 3211 | * compute secs * 1000 until we've verified it will fit in int. |
| 3212 | */ |
| 3213 | exceeded_duration = (log_min_duration_statement == 0 || |
| 3214 | (log_min_duration_statement > 0 && |
| 3215 | (secs > log_min_duration_statement / 1000 || |
| 3216 | secs * 1000 + msecs >= log_min_duration_statement))); |
| 3217 | |
| 3218 | exceeded_sample_duration = (log_min_duration_sample == 0 || |
| 3219 | (log_min_duration_sample > 0 && |
| 3220 | (secs > log_min_duration_sample / 1000 || |
| 3221 | secs * 1000 + msecs >= log_min_duration_sample))); |
| 3222 | |
| 3223 | /* |
| 3224 | * Do not log if log_statement_sample_rate = 0. Log a sample if |
| 3225 | * log_statement_sample_rate <= 1 and avoid unnecessary random() call |
| 3226 | * if log_statement_sample_rate = 1. |
| 3227 | */ |
| 3228 | if (exceeded_sample_duration) |
| 3229 | in_sample = log_statement_sample_rate != 0 && |
| 3230 | (log_statement_sample_rate == 1 || |
| 3231 | random() <= log_statement_sample_rate * MAX_RANDOM_VALUE); |
| 3232 | |
| 3233 | if (exceeded_duration || in_sample || log_duration || xact_is_sampled) |
| 3234 | { |
| 3235 | snprintf(msec_str, 32, "%ld.%03d", |
| 3236 | secs * 1000 + msecs, usecs % 1000); |
| 3237 | if ((exceeded_duration || in_sample || xact_is_sampled) && !was_logged) |
| 3238 | return 2; |
| 3239 | else |
| 3240 | return 1; |
| 3241 | } |
| 3242 | } |
| 3243 | |
| 3244 | return 0; |
| 3245 | } |
| 3246 | |
| 3247 | /* |
no test coverage detected