Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit799e220

Browse files
committed
Log all statements from a sample of transactions
This is useful to obtain a view of the different transaction types in anapplication, regardless of the durations of the statements each runs.Author: Adrien NayratReviewed-by: Masahiko Sawada, Hayato Kuroda, Andres Freund
1 parentd8c0bd9 commit799e220

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
staticboolforceSyncCommit= false;
266266

267+
/* Flag for logging statements in a transaction. */
268+
boolxact_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,boolwas_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
longsecs;
22152217
intusecs;
@@ -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
return2;
22522254
else
22532255
return1;

‎src/backend/utils/misc/guc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ intclient_min_messages = NOTICE;
510510
intlog_min_duration_statement=-1;
511511
intlog_temp_files=-1;
512512
doublelog_statement_sample_rate=1.0;
513+
doublelog_xact_sample_rate=0;
513514
inttrace_recovery_messages=LOG;
514515

515516
inttemp_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
externboolDefaultXactReadOnly;
5656
externboolXactReadOnly;
5757

58+
/* flag for logging statements in this transaction */
59+
externboolxact_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
externintlog_min_duration_statement;
253253
externintlog_temp_files;
254254
externdoublelog_statement_sample_rate;
255+
externdoublelog_xact_sample_rate;
255256

256257
externinttemp_file_limit;
257258

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp