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

Commit92f03fe

Browse files
committed
Allow setting sample ratio for auto_explain
New configuration parameter auto_explain.sample_ratio makes itpossible to log just a fraction of the queries meeting the configuredthreshold, to reduce the amount of logging.Author: Craig Ringer and Julien RouhaudReview: Petr Jelinek
1 parent69ab7b9 commit92f03fe

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

‎contrib/auto_explain/auto_explain.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static bool auto_explain_log_triggers = false;
2929
staticboolauto_explain_log_timing= true;
3030
staticintauto_explain_log_format=EXPLAIN_FORMAT_TEXT;
3131
staticboolauto_explain_log_nested_statements= false;
32+
staticdoubleauto_explain_sample_ratio=1;
3233

3334
staticconststructconfig_enum_entryformat_options[]= {
3435
{"text",EXPLAIN_FORMAT_TEXT, false},
@@ -47,6 +48,9 @@ static ExecutorRun_hook_type prev_ExecutorRun = NULL;
4748
staticExecutorFinish_hook_typeprev_ExecutorFinish=NULL;
4849
staticExecutorEnd_hook_typeprev_ExecutorEnd=NULL;
4950

51+
/* Is the current query sampled, per backend */
52+
staticboolcurrent_query_sampled= true;
53+
5054
#defineauto_explain_enabled() \
5155
(auto_explain_log_min_duration >= 0 && \
5256
(nesting_level == 0 || auto_explain_log_nested_statements))
@@ -159,6 +163,19 @@ _PG_init(void)
159163
NULL,
160164
NULL);
161165

166+
DefineCustomRealVariable("auto_explain.sample_ratio",
167+
"Fraction of queries to process.",
168+
NULL,
169+
&auto_explain_sample_ratio,
170+
1.0,
171+
0.0,
172+
1.0,
173+
PGC_SUSET,
174+
0,
175+
NULL,
176+
NULL,
177+
NULL);
178+
162179
EmitWarningsOnPlaceholders("auto_explain");
163180

164181
/* Install hooks. */
@@ -191,7 +208,15 @@ _PG_fini(void)
191208
staticvoid
192209
explain_ExecutorStart(QueryDesc*queryDesc,inteflags)
193210
{
194-
if (auto_explain_enabled())
211+
/*
212+
* For ratio sampling, randomly choose top-level statement. Either
213+
* all nested statements will be explained or none will.
214+
*/
215+
if (auto_explain_log_min_duration >=0&&nesting_level==0)
216+
current_query_sampled= (random()<auto_explain_sample_ratio*
217+
MAX_RANDOM_VALUE);
218+
219+
if (auto_explain_enabled()&&current_query_sampled)
195220
{
196221
/* Enable per-node instrumentation iff log_analyze is required. */
197222
if (auto_explain_log_analyze&& (eflags&EXEC_FLAG_EXPLAIN_ONLY)==0)
@@ -210,7 +235,7 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags)
210235
else
211236
standard_ExecutorStart(queryDesc,eflags);
212237

213-
if (auto_explain_enabled())
238+
if (auto_explain_enabled()&&current_query_sampled)
214239
{
215240
/*
216241
* Set up to track total elapsed time in ExecutorRun. Make sure the
@@ -280,7 +305,7 @@ explain_ExecutorFinish(QueryDesc *queryDesc)
280305
staticvoid
281306
explain_ExecutorEnd(QueryDesc*queryDesc)
282307
{
283-
if (queryDesc->totaltime&&auto_explain_enabled())
308+
if (queryDesc->totaltime&&auto_explain_enabled()&&current_query_sampled)
284309
{
285310
doublemsec;
286311

‎doc/src/sgml/auto-explain.sgml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ LOAD 'auto_explain';
203203
</para>
204204
</listitem>
205205
</varlistentry>
206+
207+
<varlistentry>
208+
<term>
209+
<varname>auto_explain.sample_ratio</varname> (<type>real</type>)
210+
<indexterm>
211+
<primary><varname>auto_explain.sample_ratio</> configuration parameter</primary>
212+
</indexterm>
213+
</term>
214+
<listitem>
215+
<para>
216+
<varname>auto_explain.sample_ratio</varname> (<type>floating point</type>)
217+
causes auto_explain to only explain a fraction of the statements in each
218+
session. The default is 1, meaning explain all the queries. In case
219+
of nested statements, either all will be explained or none. Only
220+
superusers can change this setting.
221+
</para>
222+
</listitem>
223+
</varlistentry>
206224
</variablelist>
207225

208226
<para>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp