Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDP8M-000T9C-7m for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Nov 2024 14:23:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tDP8K-00DhED-Tc for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Nov 2024 14:23:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDP8K-00DhDU-9j for pgsql-hackers@lists.postgresql.org; Tue, 19 Nov 2024 14:23:12 +0000 Received: from forward100a.mail.yandex.net ([178.154.239.83]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDP8F-002hmx-FE for pgsql-hackers@postgresql.org; Tue, 19 Nov 2024 14:23:11 +0000 Received: from mail-nwsmtp-smtp-production-main-33.iva.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-33.iva.yp-c.yandex.net [IPv6:2a02:6b8:c0c:8101:0:640:a961:0]) by forward100a.mail.yandex.net (Yandex) with ESMTPS id E595646C81; Tue, 19 Nov 2024 17:23:02 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-33.iva.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id 2NS2KiZOhOs0-D6qcgTch; Tue, 19 Nov 2024 17:23:02 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1732026182; bh=B8TFFjPNo8UT0VjudevtiCtho+LhxpTjGzAGLQEyqG4=; h=In-Reply-To:Cc:Date:References:To:Subject:Message-ID:From; b=J7/kNaHMNWJaRqB1+0WXLlICEaQarTRigiUv7i+SodnGCjRRSFDfhnwWDaOxIGCRd 96El31Lj7ro68IqqLuYaHnAsfGf1uHxTr673dXhr91N+arwwZFYEG/u1PCnuMnWfXU Z/dg+NxG4VcD+yJynyb3CgoiFSCoBPxw24QH8UxQ= Authentication-Results: mail-nwsmtp-smtp-production-main-33.iva.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Content-Type: multipart/mixed; boundary="------------QGR02bJISSJrqkO0hXgWyEnQ" Message-ID: Date: Tue, 19 Nov 2024 17:23:01 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Sample rate added to pg_stat_statements To: "Andrey M. Borodin" Cc: pgsql-hackers References: <6707FCA9-FD1A-4609-A1A6-142456C14E0C@yandex-team.ru> Content-Language: en-US From: Ilia Evdokimov In-Reply-To: <6707FCA9-FD1A-4609-A1A6-142456C14E0C@yandex-team.ru> 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. --------------QGR02bJISSJrqkO0hXgWyEnQ Content-Type: multipart/alternative; boundary="------------LOGJwgHrKWJQ6q6oVU07IVLQ" --------------LOGJwgHrKWJQ6q6oVU07IVLQ Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 19.11.2024 15:11, Andrey M. Borodin wrote: > >> On 18 Nov 2024, at 23:33, Ilia Evdokimov wrote: >> >> Hi hackers, >> >> Under high-load scenarios with a significant number of transactions per second, pg_stat_statements introduces substantial overhead due to the collection and storage of statistics. Currently, we are sometimes forced to disable pg_stat_statements or adjust the size of the statistics using pg_stat_statements.max, which is not always optimal. One potential solution to this issue could be query sampling in pg_stat_statements. >> >> A similar approach has been implemented in extensions like auto_explain and pg_store_plans, and it has proven very useful in high-load systems. However, this approach has its trade-offs, as it sacrifices statistical accuracy for improved performance. This patch introduces a new configuration parameter, pg_stat_statements.sample_rate for the pg_stat_statements extension. The patch provides the ability to control the sampling of query statistics in pg_stat_statements. >> >> This patch serves as a proof of concept (POC), and I would like to hear your thoughts on whether such an approach is viable and applicable. > +1 for the idea. I heard a lot of complaints about that pgss is costly. Most of them were using it wrong though. But at least it could give an easy way to rule out performance impact of pgss. Thank you for review. >> On 19 Nov 2024, at 15:09, Ilia Evdokimov wrote: >> >> I believe we should also include this check in the pgss_ExecutorEnd() function because sampling in pgss_ExecutorEnd() ensures that a query not initially sampled in pgss_ExecutorStart() can still be logged if it meets the pg_stat_statements.sample_rate criteria. This approach adds flexibility by allowing critical queries to be captured while maintaining efficient sampling. > Is there a reason why pgss_ProcessUtility is excluded? > > > Best regards, Andrey Borodin. > Ah, you’re right! Moreover, this check is needed not only in pgss_ProcessUtility() but in all places where pgss_enabled() is called. Therefore, it’s better to move the sampling check directly into pgss_enabled(). However, another issue arises with the initialization of 'current_query_sample' variable that contains whether query is sampling or not. Initializing it in pgss_ExecutorStart()||is not sufficient, because pgss_post_parse_analyze() or pgss_ProcessUtility() might be called earlier. This could lead to different values of 'current_query_sample' being used in these functions, which is undesirable. Run the regression tests for pg_stat_statements with initializing in pgss_ExecutorStart(), and you'll see this. To avoid this, we need to find a function that is called earlier than all the others. In my opinion, pgss_post_parse_analyze() is a good candidate for this purpose. If you have objections or better suggestions, feel free to share them. I attached the patch with fixes. -- Best regards, Ilia Evdokimov, Tantor Labs LLC. --------------LOGJwgHrKWJQ6q6oVU07IVLQ Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

On 19.11.2024 15:11, Andrey M. Borodin wrote:


On 18 Nov 2024, at 23:33, Ilia Evdokimov <ilya.evdokimov@tantorlabs.com> wrote:

Hi hackers,

Under high-load scenarios with a significant number of transactions per second, pg_stat_statements introduces substantial overhead due to the collection and storage of statistics. Currently, we are sometimes forced to disable pg_stat_statements or adjust the size of the statistics using pg_stat_statements.max, which is not always optimal. One potential solution to this issue could be query sampling in pg_stat_statements.

A similar approach has been implemented in extensions like auto_explain and pg_store_plans, and it has proven very useful in high-load systems. However, this approach has its trade-offs, as it sacrifices statistical accuracy for improved performance. This patch introduces a new configuration parameter, pg_stat_statements.sample_rate for the pg_stat_statements extension. The patch provides the ability to control the sampling of query statistics in pg_stat_statements.

This patch serves as a proof of concept (POC), and I would like to hear your thoughts on whether such an approach is viable and applicable.
+1 for the idea. I heard a lot of complaints about that pgss is costly. Most of them were using it wrong though. But at least it could give an easy way to rule out performance impact of pgss.
Thank you for review.

      
On 19 Nov 2024, at 15:09, Ilia Evdokimov <ilya.evdokimov@tantorlabs.com> wrote:

I believe we should also include this check in the pgss_ExecutorEnd() function because sampling in pgss_ExecutorEnd() ensures that a query not initially sampled in pgss_ExecutorStart() can still be logged if it meets the pg_stat_statements.sample_rate criteria. This approach adds flexibility by allowing critical queries to be captured while maintaining efficient sampling.
Is there a reason why pgss_ProcessUtility is excluded?


Best regards, Andrey Borodin.


Ah, you’re right! Moreover, this check is needed not only in pgss_ProcessUtility() but in all places where pgss_enabled() is called. Therefore, it’s better to move the sampling check directly into pgss_enabled().

However, another issue arises with the initialization of 'current_query_sample' variable that contains whether query is sampling or not. Initializing it in pgss_ExecutorStart() is not sufficient, because pgss_post_parse_analyze() or pgss_ProcessUtility() might be called earlier. This could lead to different values of 'current_query_sample' being used in these functions, which is undesirable. Run the regression tests for pg_stat_statements with initializing in pgss_ExecutorStart(), and you'll see this.

To avoid this, we need to find a function that is called earlier than all the others. In my opinion, pgss_post_parse_analyze() is a good candidate for this purpose. If you have objections or better suggestions, feel free to share them. I attached the patch with fixes.

--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC.

--------------LOGJwgHrKWJQ6q6oVU07IVLQ-- --------------QGR02bJISSJrqkO0hXgWyEnQ Content-Type: text/x-patch; charset=UTF-8; name="v3-0001-Allow-setting-sample-ratio-for-pg_stat_statements.patch" Content-Disposition: attachment; filename*0="v3-0001-Allow-setting-sample-ratio-for-pg_stat_statements.pa"; filename*1="tch" Content-Transfer-Encoding: base64 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