On 25/08/2015 14:45, Michael Paquier wrote: > On Fri, Jul 17, 2015 at 2:53 PM, Craig Ringer <cr...@2ndquadrant.com> wrote: >> On 7 July 2015 at 21:37, Julien Rouhaud <julien.rouh...@dalibo.com> wrote: >> >>> Well, I obviously missed that pg_srand48() is only used if the system >>> lacks random/srandom, sorry for the noise. So yes, random() must be >>> used instead of pg_lrand48(). >>> >>> I'm attaching a new version of the patch fixing this issue just in case. >> >> Thanks for picking this up. I've been trying to find time to come back >> to it but been swamped in priority work. > > For now I am marking that as returned with feedback. >
PFA v3 of the patch, rebased on current head. It fixes the last issue (sample a percentage of queries). I'm adding it to the next commitfest. -- Julien Rouhaud http://dalibo.com - http://dalibo.org
diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c index 0950e18..4edb08e 100644 --- a/contrib/auto_explain/auto_explain.c +++ b/contrib/auto_explain/auto_explain.c @@ -29,6 +29,7 @@ static bool auto_explain_log_triggers = false; static bool auto_explain_log_timing = true; static int auto_explain_log_format = EXPLAIN_FORMAT_TEXT; static bool auto_explain_log_nested_statements = false; +static double auto_explain_sample_ratio = 1; static const struct config_enum_entry format_options[] = { {"text", EXPLAIN_FORMAT_TEXT, false}, @@ -47,9 +48,14 @@ static ExecutorRun_hook_type prev_ExecutorRun = NULL; static ExecutorFinish_hook_type prev_ExecutorFinish = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd = NULL; +/* Is the current query sampled, per backend */ +static bool current_query_sampled = true; + #define auto_explain_enabled() \ (auto_explain_log_min_duration >= 0 && \ - (nesting_level == 0 || auto_explain_log_nested_statements)) + (nesting_level == 0 || auto_explain_log_nested_statements) && \ + current_query_sampled) + void _PG_init(void); void _PG_fini(void); @@ -62,6 +68,7 @@ static void explain_ExecutorFinish(QueryDesc *queryDesc); static void explain_ExecutorEnd(QueryDesc *queryDesc); + /* * Module load callback */ @@ -159,6 +166,19 @@ _PG_init(void) NULL, NULL); + DefineCustomRealVariable("auto_explain.sample_ratio", + "Percentage of queries to process.", + NULL, + &auto_explain_sample_ratio, + 1.0, + 0.0, + 1.0, + PGC_SUSET, + 0, + NULL, + NULL, + NULL); + EmitWarningsOnPlaceholders("auto_explain"); /* Install hooks. */ @@ -191,6 +211,14 @@ _PG_fini(void) static void explain_ExecutorStart(QueryDesc *queryDesc, int eflags) { + /* + * For ratio sampling, randomly choose top-level statement. Either + * all nested statements will be explained or none will. + */ + if (auto_explain_log_min_duration >= 0 && nesting_level == 0) + current_query_sampled = (random() < auto_explain_sample_ratio * + MAX_RANDOM_VALUE); + if (auto_explain_enabled()) { /* Enable per-node instrumentation iff log_analyze is required. */ diff --git a/doc/src/sgml/auto-explain.sgml b/doc/src/sgml/auto-explain.sgml index d527208..9d40e65 100644 --- a/doc/src/sgml/auto-explain.sgml +++ b/doc/src/sgml/auto-explain.sgml @@ -203,6 +203,23 @@ LOAD 'auto_explain'; </para> </listitem> </varlistentry> + + <varlistentry> + <term> + <varname>auto_explain.sample_ratio</varname> (<type>real</type>) + <indexterm> + <primary><varname>auto_explain.sample_ratio</> configuration parameter</primary> + </indexterm> + </term> + <listitem> + <para> + <varname>auto_explain.sample_ratio</varname> causes auto_explain to only + explain X percent statements in each session. In case of nested + statements, either all will be explained or none. Only superusers can + change this setting. + </para> + </listitem> + </varlistentry> </variablelist> <para>
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers