Skip to content

Commit 799e220

Browse files
committed
Log all statements from a sample of transactions
This is useful to obtain a view of the different transaction types in an application, regardless of the durations of the statements each runs. Author: Adrien Nayrat Reviewed-by: Masahiko Sawada, Hayato Kuroda, Andres Freund
1 parent d8c0bd9 commit 799e220

File tree

7 files changed

+60
-3
lines changed

7 files changed

+60
-3
lines changed

doc/src/sgml/config.sgml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5871,6 +5871,32 @@ local0.* /var/log/postgresql
58715871
</listitem>
58725872
</varlistentry>
58735873

5874+
<varlistentry id="guc-log-transaction-sample-rate" xreflabel="log_transaction_sample_rate">
5875+
<term><varname>log_transaction_sample_rate</varname> (<type>real</type>)
5876+
<indexterm>
5877+
<primary><varname>log_transaction_sample_rate</varname> configuration parameter</primary>
5878+
</indexterm>
5879+
</term>
5880+
<listitem>
5881+
<para>
5882+
Set the fraction of transactions whose statements are all logged,
5883+
in addition to statements logged for other reasons. It applies to
5884+
each new transaction regardless of its statements' durations.
5885+
The default is <literal>0</literal>, meaning not to log statements
5886+
from any additional transaction. Setting this to <literal>1</literal>
5887+
logs all statements for all transactions.
5888+
<varname>log_transaction_sample_rate</varname> is helpful to track a
5889+
sample of transaction.
5890+
</para>
5891+
<note>
5892+
<para>
5893+
Like all statement-logging options, this option can add significant
5894+
overhead.
5895+
</para>
5896+
</note>
5897+
</listitem>
5898+
</varlistentry>
5899+
58745900
</variablelist>
58755901

58765902
<para>

src/backend/access/transam/xact.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ static char *prepareGID;
264264
*/
265265
static bool forceSyncCommit = false;
266266

267+
/* Flag for logging statements in a transaction. */
268+
bool xact_is_sampled = false;
269+
267270
/*
268271
* Private context for transaction-abort work --- we reserve space for this
269272
* at startup to ensure that AbortTransaction and AbortSubTransaction can work
@@ -1903,6 +1906,11 @@ StartTransaction(void)
19031906
s->state = TRANS_START;
19041907
s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
19051908

1909+
/* Determine if statements are logged in this transaction */
1910+
xact_is_sampled = log_xact_sample_rate != 0 &&
1911+
(log_xact_sample_rate == 1 ||
1912+
random() <= log_xact_sample_rate * MAX_RANDOM_VALUE);
1913+
19061914
/*
19071915
* initialize current transaction state fields
19081916
*

src/backend/tcop/postgres.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,8 @@ check_log_statement(List *stmt_list)
21942194
* check_log_duration
21952195
* Determine whether current command's duration should be logged.
21962196
* If log_statement_sample_rate < 1.0, log only a sample.
2197+
* We also check if this statement in this transaction must be logged
2198+
* (regardless of its duration).
21972199
*
21982200
* Returns:
21992201
* 0 if no logging is needed
@@ -2209,7 +2211,7 @@ check_log_statement(List *stmt_list)
22092211
int
22102212
check_log_duration(char *msec_str, bool was_logged)
22112213
{
2212-
if (log_duration || log_min_duration_statement >= 0)
2214+
if (log_duration || log_min_duration_statement >= 0 || xact_is_sampled)
22132215
{
22142216
long secs;
22152217
int usecs;
@@ -2243,11 +2245,11 @@ check_log_duration(char *msec_str, bool was_logged)
22432245
(log_statement_sample_rate == 1 ||
22442246
random() <= log_statement_sample_rate * MAX_RANDOM_VALUE);
22452247

2246-
if ((exceeded && in_sample) || log_duration)
2248+
if ((exceeded && in_sample) || log_duration || xact_is_sampled)
22472249
{
22482250
snprintf(msec_str, 32, "%ld.%03d",
22492251
secs * 1000 + msecs, usecs % 1000);
2250-
if (exceeded && !was_logged)
2252+
if ((exceeded || xact_is_sampled) && !was_logged)
22512253
return 2;
22522254
else
22532255
return 1;

src/backend/utils/misc/guc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ int client_min_messages = NOTICE;
510510
int log_min_duration_statement = -1;
511511
int log_temp_files = -1;
512512
double log_statement_sample_rate = 1.0;
513+
double log_xact_sample_rate = 0;
513514
int trace_recovery_messages = LOG;
514515

515516
int temp_file_limit = -1;
@@ -3386,6 +3387,18 @@ static struct config_real ConfigureNamesReal[] =
33863387
NULL, NULL, NULL
33873388
},
33883389

3390+
{
3391+
{"log_transaction_sample_rate", PGC_SUSET, LOGGING_WHEN,
3392+
gettext_noop("Set the fraction of transactions to log for new transactions."),
3393+
gettext_noop("Logs all statements from a fraction of transactions. "
3394+
"Use a value between 0.0 (never log) and 1.0 (log all "
3395+
"statements for all transactions).")
3396+
},
3397+
&log_xact_sample_rate,
3398+
0.0, 0.0, 1.0,
3399+
NULL, NULL, NULL
3400+
},
3401+
33893402
/* End-of-list marker */
33903403
{
33913404
{NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL, NULL

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@
495495
# log_min_duration_statement. 1.0 logs all statements,
496496
# 0 never logs.
497497

498+
#log_transaction_sample_rate = 0.0 # Fraction of transactions whose statements
499+
# are logged regardless of their duration. 1.0 logs all
500+
# statements from all transactions, 0.0 never logs.
501+
498502
# - What to Log -
499503

500504
#debug_print_parse = off

src/include/access/xact.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extern PGDLLIMPORT int XactIsoLevel;
5555
extern bool DefaultXactReadOnly;
5656
extern bool XactReadOnly;
5757

58+
/* flag for logging statements in this transaction */
59+
extern bool xact_is_sampled;
60+
5861
/*
5962
* Xact is deferrable -- only meaningful (currently) for read only
6063
* SERIALIZABLE transactions

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ extern PGDLLIMPORT int client_min_messages;
252252
extern int log_min_duration_statement;
253253
extern int log_temp_files;
254254
extern double log_statement_sample_rate;
255+
extern double log_xact_sample_rate;
255256

256257
extern int temp_file_limit;
257258

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy