Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o5mpP-0001cE-D3 for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jun 2022 11:22:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o5mpO-00043q-4x for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Jun 2022 11:22:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o5mpM-00043h-Hf for pgsql-hackers@lists.postgresql.org; Mon, 27 Jun 2022 11:22:49 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o5mpK-0001B2-4e for pgsql-hackers@lists.postgresql.org; Mon, 27 Jun 2022 11:22:48 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 126DE5C0116; Mon, 27 Jun 2022 07:22:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Mon, 27 Jun 2022 07:22:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h=cc :cc:content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1656328964; x=1656415364; bh=iDG/Zm9GgB ix4LuIYyU+Ysdb7fxDK5Uvdml/U3KXs9M=; b=K3dBr0aFdtK/6d1v0WksxZhils 6XaWCxmp5PwIMs9duJf0qfh7PiWPhdZ3l3Xx1jxNE1HFGzkVYITO4myQ3n8QUStU z8h0O50fpqMiwCzFHaiXN22ous8S0HU7Pcp4p7Z9PsL3a/WrOt1MmRZdGT1rPDFs L6D9/5Xwiw0XuYSc7DzUlaDcdZSVyg3j9zKDmc59RQSfkYcYia+9QrY+e8NubUDi CWhCnicMKJ7EVOtJY0rpnahprQ+1rg0q1x8oE82O6nq6UEsCa9p5Faj7D6JSiwqJ meWbnTkUzTKsPcw7pG9fLP9RxE7rVA+2hSai+BQxbpTvaDAIWvDjH/Fn0N7A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1656328964; x=1656415364; bh=iDG/Zm9GgBix4LuIYyU+Ysdb7fxD K5Uvdml/U3KXs9M=; b=xzgxYIgiHXpYXgxJn8QRWTIeRZ7NuKcqu0zPv9JNBQ25 N18g1/VEdOxB80ThSTxA6lSL084iRo27ZEHbPXC9ElJgG+v1wsI5x9p1tv5ECs/T QTb9nglHBWywzawvsWkkXml6NJtytJgbx7RjcgugBLc1g9q+w4YiXI3rQIhalXGq nfGZMlzFr1i230YEpNiiMAOBO2BYsyOhufDEdJaq6PssKhS1a+9R24fA1DBLfoqN Tsn6QOKYGJrzIO1JUXQbfEeWOKRicxzT/hkurAgHugBWxXNXwEpqKpA8QuD1ScBX lgCStwEY7g0RKYLUEnKdXmDgXtKrfDtRqfObHhc7eg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudeghedggedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfeehmdenucfjughrpefhvfevufhfffgjkfgfgggtsehmtderredt reejnecuhfhrohhmpeffrghgfhhinhhnpgfklhhmrghrihgpofgrnhhnshonkhgvrhcuoe hilhhmrghrihesihhlmhgrrhhirdhorhhgqeenucggtffrrghtthgvrhhnpeeugfefudeu tdejueehteejueekhfejtdevheejlefhudetvdeiledtvdeuvdduvdenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehilhhmrghrihesihhlmhgr rhhirdhorhhg X-ME-Proxy: Feedback-ID: i1ff147bf:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 27 Jun 2022 07:22:43 -0400 (EDT) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: Michael Paquier Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Logging query parmeters in auto_explain References: <87ee09mohb.fsf@wibble.ilmari.org> <8735ggn2lf.fsf@wibble.ilmari.org> <87tu8tla5s.fsf@wibble.ilmari.org> Date: Mon, 27 Jun 2022 12:22:42 +0100 In-Reply-To: (Michael Paquier's message of "Tue, 14 Jun 2022 16:50:14 +0900") Message-ID: <87iloml5a5.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Michael Paquier writes: > On Thu, Jun 09, 2022 at 11:55:11PM +0100, Dagfinn Ilmari Mannsåker wrote: >> Done (and more tests added), v3 attached. > > One thing I am wondering is if we'd better mention errdetail_params() > at the top of the initial check done in ExplainQueryParameters(). > That's a minor issue, though. > > +sub query_log > +{ > Perhaps a short description at the top of this routine to explain it > is worth it? Done. I also moved the function to the bottom of the file, to avoid distracting from the actual test queries. > The test still does a set of like() and unlike() after running each > query when the parameter updates are done. One thing I would have > played with is to group the set of logs expected or not expected as > parameters of the centralized routine, but that would reduce the > customization of the test names, so at the end the approach you have > taken for query_log() looks good to me. I did consider passing the tests as a data structure to the function, but that would amount to specifying exactly the same things but as a data structure, and then calling the appropriate function by reference, which just makes things more cluttered. If we were using TAP subtests, it might make a sense to have the function run each set of related tests in a subtest, rather than having multiple subtest calls at the top level, but we don't, so it doesn't. > +$node->stop('fast'); > There is no need for that. The END block of Cluster.pm does that > already. Ah, I was not aware of that. The call was there in the original version, so I had just left it in. Removed. v4 patch attached. - ilmari --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=v4-0001-Log-query-parameters-in-auto_explain.patch From 1c504beecdf2a0e64a7bbd1d0e07b6b33fdc533b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Tue, 31 May 2022 21:12:12 +0100 Subject: [PATCH v4] Log query parameters in auto_explain Add an auto_explain.log_parameter_max_length config setting, similar to the corresponding core setting, that controls the inclusion of query parameters in the logged explain output. Also adjust the tests to only look at the relevant parts of the log for each query, and tho reset the GUCs after each test. --- contrib/auto_explain/auto_explain.c | 15 +++ contrib/auto_explain/t/001_auto_explain.pl | 134 ++++++++++++++++++--- doc/src/sgml/auto-explain.sgml | 19 +++ src/backend/commands/explain.c | 22 ++++ src/include/commands/explain.h | 1 + 5 files changed, 177 insertions(+), 14 deletions(-) diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c index c9a0d947c8..1ba7536879 100644 --- a/contrib/auto_explain/auto_explain.c +++ b/contrib/auto_explain/auto_explain.c @@ -19,12 +19,14 @@ #include "common/pg_prng.h" #include "executor/instrument.h" #include "jit/jit.h" +#include "nodes/params.h" #include "utils/guc.h" PG_MODULE_MAGIC; /* GUC variables */ static int auto_explain_log_min_duration = -1; /* msec or -1 */ +static int auto_explain_log_parameter_max_length = -1; /* bytes or -1 */ static bool auto_explain_log_analyze = false; static bool auto_explain_log_verbose = false; static bool auto_explain_log_buffers = false; @@ -105,6 +107,18 @@ _PG_init(void) NULL, NULL); + DefineCustomIntVariable("auto_explain.log_parameter_max_length", + "Sets the maximum length of query parameters to log.", + "Zero logs no query parameters, -1 logs them in full.", + &auto_explain_log_parameter_max_length, + -1, + -1, INT_MAX, + PGC_SUSET, + GUC_UNIT_BYTE, + NULL, + NULL, + NULL); + DefineCustomBoolVariable("auto_explain.log_analyze", "Use EXPLAIN ANALYZE for plan logging.", NULL, @@ -389,6 +403,7 @@ explain_ExecutorEnd(QueryDesc *queryDesc) ExplainBeginOutput(es); ExplainQueryText(es, queryDesc); + ExplainQueryParameters(es, queryDesc->params, auto_explain_log_parameter_max_length); ExplainPrintPlan(es, queryDesc); if (es->analyze && auto_explain_log_triggers) ExplainPrintTriggers(es, queryDesc); diff --git a/contrib/auto_explain/t/001_auto_explain.pl b/contrib/auto_explain/t/001_auto_explain.pl index 82e4d9d15c..2d04147090 100644 --- a/contrib/auto_explain/t/001_auto_explain.pl +++ b/contrib/auto_explain/t/001_auto_explain.pl @@ -17,41 +17,147 @@ $node->start; # run a couple of queries -$node->safe_psql("postgres", "SELECT * FROM pg_class;"); -$node->safe_psql("postgres", - "SELECT * FROM pg_proc WHERE proname = 'int4pl';"); +my $log_contents = query_log("SELECT * FROM pg_class;"); -# emit some json too -$node->append_conf('postgresql.conf', "auto_explain.log_format = json"); -$node->reload; -$node->safe_psql("postgres", "SELECT * FROM pg_proc;"); -$node->safe_psql("postgres", - "SELECT * FROM pg_class WHERE relname = 'pg_class';"); +like( + $log_contents, + qr/Query Text: SELECT \* FROM pg_class;/, + "query text logged, text mode"); -$node->stop('fast'); - -my $log = $node->logfile(); - -my $log_contents = slurp_file($log); +unlike( + $log_contents, + qr/Query Parameters:/, + "no query parameters logged when none, text mode"); like( $log_contents, qr/Seq Scan on pg_class/, "sequential scan logged, text mode"); +# with parameters +$log_contents = query_log( + q{PREPARE get_proc(name) AS SELECT * FROM pg_proc WHERE proname = $1; EXECUTE get_proc('int4pl');} +); + +like( + $log_contents, + qr/Query Text: PREPARE get_proc\(name\) AS SELECT \* FROM pg_proc WHERE proname = \$1;/, + "prepared query text logged, text mode"); + +like( + $log_contents, + qr/Query Parameters: \$1 = 'int4pl'/, + "query parameters logged, text mode"); + like( $log_contents, qr/Index Scan using pg_proc_proname_args_nsp_index on pg_proc/, "index scan logged, text mode"); +# check that logged parameters can be truncated +$log_contents = query_log( + q{PREPARE get_type(name) AS SELECT * FROM pg_type WHERE typname = $1; EXECUTE get_type('float8');}, + { "auto_explain.log_parameter_max_length" => 3 }); + +like( + $log_contents, + qr/Query Text: PREPARE get_type\(name\) AS SELECT \* FROM pg_type WHERE typname = \$1;/, + "prepared query text logged, text mode"); + +like( + $log_contents, + qr/Query Parameters: \$1 = 'flo\.\.\.'/, + "query parameters truncated, text mode"); + +# check that parameter logging can be disabled +$log_contents = query_log( + q{PREPARE get_type(name) AS SELECT * FROM pg_type WHERE typname = $1; EXECUTE get_type('float8');}, + { "auto_explain.log_parameter_max_length" => 0 }); + +like( + $log_contents, + qr/Query Text: PREPARE get_type\(name\) AS SELECT \* FROM pg_type WHERE typname = \$1;/, + "prepared query text logged, text mode"); + +unlike( + $log_contents, + qr/Query Parameters:/, + "query parameters not logged when disabled, text mode"); + +# emit some json too +$log_contents = query_log("SELECT * FROM pg_proc;", + { "auto_explain.log_format" => "json" }); + +like( + $log_contents, + qr/"Query Text": "SELECT \* FROM pg_proc;"/, + "query text logged, json mode"); + +unlike( + $log_contents, + qr/"Query Parameters":/, + "query parameters not logged when none, json mode"); + like( $log_contents, qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_proc"/s, "sequential scan logged, json mode"); +# and with parameters +$log_contents = query_log( + q{PREPARE get_class(name) AS SELECT * FROM pg_class WHERE relname = $1; EXECUTE get_class('pg_class');}, + { "auto_explain.log_format" => "json" }); + +like( + $log_contents, + qr/"Query Text": "PREPARE get_class\(name\) AS SELECT \* FROM pg_class WHERE relname = \$1;"/, + "prepared query text logged, json mode"); + +like( + $log_contents, + qr/"Query Parameters": "\$1 = 'pg_class'"/, + "query parameters logged, json mode"); + like( $log_contents, qr/"Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/s, "index scan logged, json mode"); done_testing(); + +# Runs the specified query and returns the emitted server log. +# If any parameters are specified, these are set in postgresql.conf, +# and reset after the query is run. +sub query_log +{ + my ($sql, $params) = @_; + $params ||= {}; + + if (keys %$params) + { + for my $key (keys %$params) + { + $node->append_conf('postgresql.conf', "$key = $params->{$key}\n"); + } + $node->reload; + } + + my $log = $node->logfile(); + my $offset = -s $log; + + $node->safe_psql("postgres", $sql); + + my $log_contents = slurp_file($log, $offset); + + if (keys %$params) + { + for my $key (keys %$params) + { + $node->adjust_conf('postgresql.conf', $key, undef); + } + $node->reload; + } + + return $log_contents; +} + diff --git a/doc/src/sgml/auto-explain.sgml b/doc/src/sgml/auto-explain.sgml index 30e35a714a..a033c9c685 100644 --- a/doc/src/sgml/auto-explain.sgml +++ b/doc/src/sgml/auto-explain.sgml @@ -63,6 +63,25 @@ + + + auto_explain.log_parameter_max_length (integer) + + auto_explain.log_parameter_max_length configuration parameter + + + + + auto_explain.log_parameter_max_length controls the + logging of query parameter values. A value of-1 (the + default) logs the parameter values in full. 0 disables + logging of parameter values. A value greater than zero truncates each + parameter value to that many bytes. Only superusers can change this + setting. + + + + auto_explain.log_analyze (boolean) diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 5d1f7089da..8584e3585d 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -972,6 +972,28 @@ ExplainQueryText(ExplainState *es, QueryDesc *queryDesc) ExplainPropertyText("Query Text", queryDesc->sourceText, es); } +/* + * ExplainQueryParameters - + * add a "Query Parameters" node that describes the parameters of the query, + * if any + * + * The caller should have set up the options fields of *es, as well as + * initializing the output buffer es->str. + * + */ +void +ExplainQueryParameters(ExplainState *es, ParamListInfo params, int maxlen) +{ + char *str; + + if (params == NULL || params->numParams == 0 || maxlen == 0) + return; + + str = BuildParamLogString(params, NULL, maxlen); + if (str && str[0] != '\0') + ExplainPropertyText("Query Parameters", str, es); +} + /* * report_triggers - * report execution stats for a single relation's triggers diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h index 666977fb1f..9ebde089ae 100644 --- a/src/include/commands/explain.h +++ b/src/include/commands/explain.h @@ -99,6 +99,7 @@ extern void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc); extern void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc); extern void ExplainQueryText(ExplainState *es, QueryDesc *queryDesc); +extern void ExplainQueryParameters(ExplainState *es, ParamListInfo params, int maxlen); extern void ExplainBeginOutput(ExplainState *es); extern void ExplainEndOutput(ExplainState *es); -- 2.30.2 --=-=-=--