Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jHSTF-0003bo-Fq for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2020 13:22:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jHSTE-0005JT-8s for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2020 13:22:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jHSTD-0005JM-Qk for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2020 13:22:52 +0000 Received: from mail-lf1-x144.google.com ([2a00:1450:4864:20::144]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jHSTB-0002X9-5s for pgsql-hackers@postgresql.org; Thu, 26 Mar 2020 13:22:50 +0000 Received: by mail-lf1-x144.google.com with SMTP id z23so4791947lfh.8 for ; Thu, 26 Mar 2020 06:22:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=CBaPP7pQYoDZU/3jg3OylfOKfpUYS4KHp+34A51/VlE=; b=UY98U7h4/dqlDWBSmC4qzhbvJeT3neaDYAcMRzcy2ow4/87EHKwzLR3dE6t2VwbPq3 A0mh3iesn43/O/ZvP0DufToNaxpNw9yjJYZSWUX48e9pKo9DEzh34Hsft7pnPwGKOdk8 LqNgnF4wKWKDkJJHccdkb+8IwxBYwWIPyQNFUE6Vay4txFVq7oUebGEB/hZuaQReESV8 CLXKZacFoGn5LLkSXjzyIX1R93dymowprn0yI7jYYMuvTEviH5xYIj2DS22pM2z2VjD0 9NrOFcmqHnoM1DwL4bS2iC4glYMN1oI7KqS+SqKlsgCk0O1GhL4tQgmrOYBG9Vv1X7PU 8ECA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=CBaPP7pQYoDZU/3jg3OylfOKfpUYS4KHp+34A51/VlE=; b=Sjq1ce7a5Sk4EX1aa1b1YSU3ey/TQRcO4jZjVc3nQHh9+IGgvcvhYgEAnys7q58MBi lbXmmfloqa7sk9mb7yE6VRtuQgrMqWJ9OazAAyJvRVqitSGl/jxPHYAEOr6df8b1nsUj bmjD4dHZMPBFYHFA/y+PG+0Nro8mHGOaFiY9S8bRy+5w6OzekKmXn0/seI5JkdbwWs28 j2yQ9loG/SNKURY8COEBcJ2RDbScCVk+ekiOJH42bUllveMiHKGs99E5bhmdUziiQh1T qv2td8bibYU2n1eU1IHoTUWZLBqYzoiCcV5leTLTtVSL8CJFrJYXZJqQz8lsLy8arfar WmgQ== X-Gm-Message-State: ANhLgQ1kVuvLhuA0leCVgdNCKvToIMH3LN5NH40AGx/R+M0lL1OCLgsH 4Mim+4z0raZ2J0fcqDhounc= X-Google-Smtp-Source: ADFU+vs4/06U1VPRT2rmTdSGPm3d4rGkwlun7xs4vKDId5Ph46pr0NVHPGG3tF7/db50bQOOBlsiew== X-Received: by 2002:a05:6512:1041:: with SMTP id c1mr5793219lfb.14.1585228967001; Thu, 26 Mar 2020 06:22:47 -0700 (PDT) Received: from nol (82-64-124-11.subs.proxad.net. [82.64.124.11]) by smtp.gmail.com with ESMTPSA id k23sm1438996ljk.40.2020.03.26.06.22.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 06:22:46 -0700 (PDT) Date: Thu, 26 Mar 2020 14:22:42 +0100 From: Julien Rouhaud To: Fujii Masao Cc: Sergei Kornilov , "imai.yoshikazu@fujitsu.com" , legrand legrand , "pgsql-hackers@postgresql.org" Subject: Re: Planning counters in pg_stat_statements (using pgss_store) Message-ID: <20200326132242.GA80836@nol> References: <1584180240397-0.post@n3.nabble.com> <20200314172733.mg7qpyumlyythm25@nol> <20200316214912.iakenhp7vyd37hmg@nol> <6300601584711975@vla4-87a00c2d2b1b.qloud-c.yandex.net> <20200320193004.rqgf3iim4fugq3sm@nol> <20200325134553.GA14054@nol> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Mar 26, 2020 at 08:08:35PM +0900, Fujii Masao wrote: > > On 2020/03/25 22:45, Julien Rouhaud wrote: > > On Wed, Mar 25, 2020 at 10:09:37PM +0900, Fujii Masao wrote: > > > + /* > > > + * We can't process the query if no query_text is provided, as pgss_store > > > + * needs it. We also ignore query without queryid, as it would be treated > > > + * as a utility statement, which may not be the case. > > > + */ > > > > > > Could you tell me why the planning stats are not tracked when executing > > > utility statements? In some utility statements like REFRESH MATERIALIZED VIEW, > > > the planner would work. > > > > I explained that in [1]. The problem is that the underlying statement doesn't > > get the proper stmt_location and stmt_len, so you eventually end up with two > > different entries. > > It's not problematic to have two different entries in that case. Right? I will unnecessarily bloat the entries, and makes users life harder too. This example is quite easy to deal with, but if the application is sending multi-query statements, you'll just end up with a mess impossible to properly handle. > The actual problem is that the statements reported in those entries are > very similar? For example, when "create table test as select 1;" is executed, > it's strange to get the following two entries, as you explained. > > create table test as select 1; > create table test as select 1 > > But it seems valid to get the following two entries in that case? > > select 1 > create table test as select 1 > > The former is the nested statement and the latter is the top statement. I think that there should only be 1 entry, the utility command. It seems easy to correlate the planning time to the underlying query, but I'm not entirely sure that the execution counters won't be impacted by the fact is being run in a utilty statements. Also, for now current pgss behavior is to always merge underlying optimisable statements in the utility command, and it seems a bit late in this release cycle to revisit that. I'd be happy to work on improving that for the next release, among other things. For instance the total lack of normalization for utility commands [2] is also something that has been bothering me for a long time. In some workloads, you can end up with the entries almost entirely filled with 1-time-execution commands, just because it's using random identifiers, so you have no other choice than to disable track_utility, although it would have been useful for other commands. > Here are other comments. > > - if (jstate) > + if (kind == PGSS_JUMBLE) > > Why is PGSS_JUMBLE necessary? ISTM that we can still use jstate here, instead. > > If it's ok to remove PGSS_JUMBLE, we can define PGSS_NUM_KIND(=2) instead > and replace 2 in, e.g., total_time[2] with PGSS_NUM_KIND. Thought? Yes, we could be using jstate here. I originally used that to avoid passing PGSS_EXEC (or the other one) as a way to say "ignore this information as there's the jstate which says it's yet another meaning". If that's not an issue, I can change that as PGSS_NUM_KIND will clearly improve the explicit "2" all over the place. > + total_time > + double precision > + > + > + Total time spend planning and executing the statement, in milliseconds > + > + > > pg_stat_statements view has this column but the function not. > We should make both have the column or not at all, for consistency? > I'm not sure if it's good thing to expose the sum of total_plan_time > and total_exec_time as total_time. If some users want that, they can > easily calculate it from total_plan_time and total_exec_time by using > their own logic. I think we originally added it as a way to avoid too much compatibility break, and also because it seems like a field most users will be interested in anyway. Now that I'm thinking about it again, I indeed think it was a mistake to have that in view part only. Not mainly for consistency, but for users who would be interested in the total_time field while not wanting to pay the overhead of retrieving the query text if they don't need it. So I'll change that! > + nested_level++; > + PG_TRY(); > > In old thread [1], Tom Lane commented the usage of nested_level > in the planner hook. There seems no reply to that so far. What's > your opinion about that comment? > > [1] https://www.postgresql.org/message-id/28980.1515803777@sss.pgh.pa.us Oh thanks, I didn't noticed this part of the discussion. I agree with Tom's concern, and I think that having a specific nesting level variable for the planner is the best workaround, so I'll implement that. [2] https://twitter.com/fujii_masao/status/1242978261572837377