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 1oaEEZ-0000e2-N9 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Sep 2022 10:42:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oaEEY-0005kE-08 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Sep 2022 10:42:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oaEEX-0005k5-9c for pgsql-hackers@lists.postgresql.org; Mon, 19 Sep 2022 10:42:37 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oaEEO-0002s1-1T for pgsql-hackers@lists.postgresql.org; Mon, 19 Sep 2022 10:42:36 +0000 Received: from [192.168.8.225] (unknown [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 2323821C45E2; Mon, 19 Sep 2022 13:42:25 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1663584145; bh=zaL4AX98zOc4vp4qwKgp4fPD4SBNdYLvBn4QqGk/pYI=; h=Date:To:From:Subject; b=bLR6Ntndz4vtg07XCjheenBbmGU1Ranm2uJtTTyr7tCv1fUw9FLHDymqwgPFR3v8h zw6e5XFs+OlqgwUqG3Ekw45wG1Kj0m8mKGyBi0I4AcY4ls64Z8D/QcqthfdP14DeL2 o1M6IFSApkx9R3bcLJmb6uPZXVQgH7fP2DmaXTh0= Content-Type: multipart/mixed; boundary="------------Y4a1ORitzvUUbBW01Ds0f070" Message-ID: <6ee42413-41db-91af-4391-614f6af39000@postgrespro.ru> Date: Mon, 19 Sep 2022 13:42:24 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Content-Language: en-US To: torikoshia , pgsql-hackers@lists.postgresql.org From: "a.rybakina" Subject: Re: RFC: Logging plan of the running query List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------Y4a1ORitzvUUbBW01Ds0f070 Content-Type: multipart/alternative; boundary="------------UvcgS3YrAPwqVyoXWg0Ymlxb" --------------UvcgS3YrAPwqVyoXWg0Ymlxb Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, I'm sorry,if this message is duplicated previous this one, but the previous message is sent incorrectly. I sent it from email address lena.ribackina@yandex.ru. I liked this idea and after reviewing code I noticed some moments and I'd rather ask you some questions. Firstly, I suggest some editing in the comment of commit. I think, it is turned out the more laconic and the same clear. I wrote it below since I can't think of any other way to add it. ``` Currently, we have to wait for finishing of the query execution to check its plan. This is not so convenient in investigation long-running queries on production environments where we cannot use debuggers. To improve this situation there is proposed the patch containing the pg_log_query_plan() function which request to log plan of the specified backend process. By default, only superusers are allowed to request log of the plan otherwise allowing any users to issue this request could create cause lots of log messages and it can lead to denial of service. At the next requesting CHECK_FOR_INTERRUPTS(), the target backend logs its plan at LOG_SERVER_ONLY level and therefore this plan will appear in the server log only, not to be sent to the client. ``` Secondly, I have question about deleting USE_ASSERT_CHECKING in lock.h? It supposed to have been checked in another placed of the code by matching values. I am worry about skipping errors due to untesting with assert option in the places where it (GetLockMethodLocalHash) participates and we won't able to get core file in segfault cases. I might not understand something, then can you please explain to me? Thirdly, I have incomprehension of the point why save_ActiveQueryDesc is declared in the pquery.h? I am seemed to save_ActiveQueryDesc to be used in an once time in the ExecutorRun function and  its declaration superfluous. I added it in the attached patch. Fourthly, it seems to me there are not enough explanatory comments in the code. I also added them in the attached patch. Lastly, I have incomprehension about handling signals since have been unused it before. Could another signal disabled calling this signal to log query plan? I noticed this signal to be checked the latest in procsignal_sigusr1_handler function. Regards, -- Alena Rybakina Postgres Professional > 19.09.2022, 11:01, "torikoshia" : > > On 2022-05-16 17:02, torikoshia wrote: > >  On 2022-03-09 19:04, torikoshia wrote: > >  On 2022-02-08 01:13, Fujii Masao wrote: > >  AbortSubTransaction() should reset ActiveQueryDesc to >  save_ActiveQueryDesc that ExecutorRun() set, instead > of NULL? >  Otherwise ActiveQueryDesc of top-level statement will > be unavailable >  after subtransaction is aborted in the nested statements. > > >  I once agreed above suggestion and made v20 patch making >  save_ActiveQueryDesc a global variable, but it caused > segfault when >  calling pg_log_query_plan() after FreeQueryDesc(). > >  OTOH, doing some kind of reset of ActiveQueryDesc seems > necessary >  since it also caused segfault when running > pg_log_query_plan() during >  installcheck. > >  There may be a better way, but resetting ActiveQueryDesc > to NULL seems >  safe and simple. >  Of course it makes pg_log_query_plan() useless after a > subtransaction >  is aborted. >  However, if it does not often happen that people want to > know the >  running query's plan whose subtransaction is aborted, > resetting >  ActiveQueryDesc to NULL would be acceptable. > >  Attached is a patch that sets ActiveQueryDesc to NULL when a >  subtransaction is aborted. > >  How do you think? > > Attached new patch to fix patch apply failures again. > > -- > Regards, > > -- > Atsushi Torikoshi > NTT DATA CORPORATION > --------------UvcgS3YrAPwqVyoXWg0Ymlxb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi,

I'm sorry,if this message is duplicated previous this one, but the previous message is sent incorrectly. I sent it from email address lena.ribackina@yandex.ru.

I liked this idea and after reviewing code I noticed some moments and I'd rather ask you some questions.

Firstly, I suggest some editing in the comment of commit. I think, it is turned out the more laconic and the same clear. I wrote it below since I can't think of any other way to add it.

```
Currently, we have to wait for finishing of the query execution to check its plan.
This is not so convenient in investigation long-running queries on production
environments where we cannot use debuggers.

To improve this situation there is proposed the patch containing the pg_log_query_plan()
function which request to log plan of the specified backend process.

By default, only superusers are allowed to request log of the plan otherwise
allowing any users to issue this request could create cause lots of log messages
and it can lead to denial of service.

At the next requesting CHECK_FOR_INTERRUPTS(), the target backend logs its plan at
LOG_SERVER_ONLY level and therefore this plan will appear in the server log only,
not to be sent to the client.
```

Secondly, I have question about deleting USE_ASSERT_CHECKING in lock.h?
It supposed to have been checked in another placed of the code by matching values. I am worry about skipping errors due to untesting with assert option in the places where it (GetLockMethodLocalHash) participates and we won't able to get core file in segfault cases. I might not understand something, then can you please explain to me?

Thirdly, I have incomprehension of the point why save_ActiveQueryDesc is declared in the pquery.h? I am seemed to save_ActiveQueryDesc to be used in an once time in the ExecutorRun function and  its declaration superfluous. I added it in the attached patch.

Fourthly, it seems to me there are not enough explanatory comments in the code. I also added them in the attached patch.

Lastly, I have incomprehension about handling signals since have been unused it before. Could another signal disabled calling this signal to log query plan? I noticed this signal to be checked the latest in procsignal_sigusr1_handler function.

Regards,

--
Alena Rybakina
Postgres Professional
19.09.2022, 11:01, "torikoshia" <torikoshia@oss.nttdata.com>:

On 2022-05-16 17:02, torikoshia wrote:

 On 2022-03-09 19:04, torikoshia wrote:
 On 2022-02-08 01:13, Fujii Masao wrote:
 AbortSubTransaction() should reset ActiveQueryDesc to
 save_ActiveQueryDesc that ExecutorRun() set, instead of NULL?
 Otherwise ActiveQueryDesc of top-level statement will be unavailable
 after subtransaction is aborted in the nested statements.
 
 I once agreed above suggestion and made v20 patch making
 save_ActiveQueryDesc a global variable, but it caused segfault when
 calling pg_log_query_plan() after FreeQueryDesc().
 
 OTOH, doing some kind of reset of ActiveQueryDesc seems necessary
 since it also caused segfault when running pg_log_query_plan() during
 installcheck.
 
 There may be a better way, but resetting ActiveQueryDesc to NULL seems
 safe and simple.
 Of course it makes pg_log_query_plan() useless after a subtransaction
 is aborted.
 However, if it does not often happen that people want to know the
 running query's plan whose subtransaction is aborted, resetting
 ActiveQueryDesc to NULL would be acceptable.
 
 Attached is a patch that sets ActiveQueryDesc to NULL when a
 subtransaction is aborted.
 
 How do you think?

Attached new patch to fix patch apply failures again.
 

--
Regards,

--
Atsushi Torikoshi
NTT DATA CORPORATION
--------------UvcgS3YrAPwqVyoXWg0Ymlxb-- --------------Y4a1ORitzvUUbBW01Ds0f070 Content-Type: text/x-patch; charset=UTF-8; name="diff_query_plan.patch" Content-Disposition: attachment; filename="diff_query_plan.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2V4ZWN1dG9yL2V4ZWNNYWluLmMgYi9zcmMvYmFj a2VuZC9leGVjdXRvci9leGVjTWFpbi5jCmluZGV4IGE4MmFjODc0NTdlLi42NWI2OTJiMGRk ZiAxMDA2NDQKLS0tIGEvc3JjL2JhY2tlbmQvZXhlY3V0b3IvZXhlY01haW4uYworKysgYi9z cmMvYmFja2VuZC9leGVjdXRvci9leGVjTWFpbi5jCkBAIC0zMDYsNiArMzA2LDEyIEBAIEV4 ZWN1dG9yUnVuKFF1ZXJ5RGVzYyAqcXVlcnlEZXNjLAogewogCVF1ZXJ5RGVzYyAqc2F2ZV9B Y3RpdmVRdWVyeURlc2M7CiAKKwkvKgorCSAqIFNhdmUgdmFsdWUgb2YgQWN0aXZlUXVlcnlE ZXNjIGJlZm9yZSBoYXZpbmcgY2FsbGVkCisJICogRXhlY3V0b3JSdW5faG9vayBmdW5jdGlv biBkdWUgdG8gaGF2aW5nIHJlc2V0IGJ5CisJICogQWJvcnRTdWJUcmFuc2FjdGlvbi4KKwkg Ki8KKwogCXNhdmVfQWN0aXZlUXVlcnlEZXNjID0gQWN0aXZlUXVlcnlEZXNjOwogCUFjdGl2 ZVF1ZXJ5RGVzYyA9IHF1ZXJ5RGVzYzsKIApAQCAtMzE0LDYgKzMyMCw3IEBAIEV4ZWN1dG9y UnVuKFF1ZXJ5RGVzYyAqcXVlcnlEZXNjLAogCWVsc2UKIAkJc3RhbmRhcmRfRXhlY3V0b3JS dW4ocXVlcnlEZXNjLCBkaXJlY3Rpb24s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