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 1tY8uu-004Wlp-2y for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 19:19:05 +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 1tY8us-00FuAq-RR for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 19:19:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tY8us-00FuAi-BB for pgsql-hackers@lists.postgresql.org; Wed, 15 Jan 2025 19:19:03 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tY8uq-000bV1-0g for pgsql-hackers@postgresql.org; Wed, 15 Jan 2025 19:19:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1736968738; bh=8KSou9tlRB3NCl3KcQ/7y40n0efUazGQwiAbQdxtOVA=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=pHgTS9n38Dg+Kdvq9FknMS9uk/RQKEBTgFcIeOSLjurz4opbQ9PKXh9v1D/ijDyAx KJnH4uHsS1r8BT1ei/Lcjk30HnTahTLpXAxbp/0bi7FZKHtTzdUjzGgk8FQ1SVPlar WdV5GahijWX8aWKLtfGvoUEnA/YLHI34+wCaiAVpYf/qQ199zZ3PCjVb2/w+ct1z5X rELU2QXZqca90njslTcWoVeFOCBabgZLvXz1fqHXJCBl+nZAmo7dDrEqb0zQDjQTRq lCJn4dwoyugzdU97jbeRhCnuEyJnzDa2XdMwMZQLvnoWRKHrJx+pfu7J4Gzy2gjnI7 BOUhKEEkHGO4A== Received: from [10.0.85.1] (ec2-35-158-208-124.eu-central-1.compute.amazonaws.com [35.158.208.124]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.rybakina@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id B9AD060DD1; Wed, 15 Jan 2025 22:18:57 +0300 (MSK) Content-Type: multipart/alternative; boundary="------------rxAxpSKe0DX55xg1VTXG0AIz" Message-ID: Date: Wed, 15 Jan 2025 22:18:56 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Sample rate added to pg_stat_statements To: Ilia Evdokimov Cc: "Andrey M. Borodin" , Sami Imseih , Alexander Korotkov , Michael Paquier , Greg Sabino Mullane , pgsql-hackers References: <320a31e6-4c91-438b-ab17-8a1d72384727@tantorlabs.com> <52e5e3aa-066a-4daf-8d65-77c34a2f0d98@tantorlabs.com> <6482A576-4465-4E46-A42C-F1767E8DDE30@yandex-team.ru> <20994d10-ab44-4c6b-91bc-c4675cd2e58e@tantorlabs.com> <9D8A5BC8-A3A5-4DA9-9382-F30D343247D1@yandex-team.ru> <6cf6270b-e8fa-4d99-b74a-17e781cdf309@tantorlabs.com> <3a27c830-b81f-4560-9774-a87d9b922fbd@tantorlabs.com> Content-Language: en-US From: Alena Rybakina In-Reply-To: X-KSMG-AntiPhishing: NotDetected, bases: 2025/01/15 16:40:00 X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/01/15 17:39:00 #27036931 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 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. --------------rxAxpSKe0DX55xg1VTXG0AIz Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 15.01.2025 12:47, Ilia Evdokimov wrote: > On 06.01.2025 18:57, Andrey M. Borodin wrote: >> 1. This code seems a little different from your patch. It is trying to avoid engaging PRNG. I'm not sure it's a good idea, but still. Also, it uses "<=", not "<". >> >> xact_is_sampled = log_xact_sample_rate != 0 && >> (log_xact_sample_rate == 1 || >> pg_prng_double(&pg_global_prng_state) <= log_xact_sample_rate); > > Sorry for the delayed reply. Andrey was right about this > suggestion—first, it makes the code more readable for others, and > second, it avoids engaging the PRNG on edge values of 0.0 and 1.0. > I’ve attached patch v11 with these changes. > Patch looks fine. Thank you! On 14.01.2025 15:00, Ilia Evdokimov wrote: > > Alena, Sami – I apologize for not including you in the previous email. > If you're interested in this approach, I'm open to any suggestions. > > [0]: > https://www.postgresql.org/message-id/1b13d748-5e98-479c-9222-3253a734a038%40tantorlabs.com > > This is a difficult question. I tend to agree with Alexander Korotkov's proposal to add a filter that saves information about queries whose statistical information satisfies the conditions of the configured filter. However, I don’t believe query execution time is a sufficient metric for this purpose. It is too unstable and influenced by many external factors, such as system load. For instance, various background processes like vacuum, checkpointer, or background writer mechanisms could be running simultaneously. Additionally, a query may take a long time to execute because another large query is consuming most of the system's resources at the same time. In such cases, the long execution time of the triggered query may not indicate anything remarkable. Furthermore, statistics for resource-intensive queries may appear normal if the query simply processed a large volume of data, such as when it involves a Cartesian product or a full join. Therefore, I think the idea of using filters is more promising, especially if multiple filters are implemented. For example, we could add filters for buffer usage (pages read or modified), differences in cardinality (predicted vs. actual), username, application name, and other criteria. These filters would help reduce the volume of queries tracked by the pg_stat_statements extension. However, there might be challenges in keeping this state up to date, given the volatile and unstable nature of the system. Your proposed parameter could also reduce the volume of queries for which statistics need to be saved, but it is too unpredictable for analysis. This unpredictability makes it unclear how the data would be interpreted in the future. For instance, a query that genuinely impacted performance might not be processed by pg_stat_statements simply due to randomness, while a small, insignificant query could be selected instead. Analyzing such statistical information might lead to misleading conclusions. -- Regards, Alena Rybakina Postgres Professional --------------rxAxpSKe0DX55xg1VTXG0AIz Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 15.01.2025 12:47, Ilia Evdokimov wrote:
On 06.01.2025 18:57, Andrey M. Borodin wrote:
1. This code seems a little different from your patch. It is trying to avoid engaging PRNG. I'm not sure it's a good idea, but still. Also, it uses "<=", not "<".

    xact_is_sampled = log_xact_sample_rate != 0 &&
(log_xact_sample_rate == 1 ||
pg_prng_double(&pg_global_prng_state) <= log_xact_sample_rate);

Sorry for the delayed reply. Andrey was right about this suggestion—first, it makes the code more readable for others, and second, it avoids engaging the PRNG on edge values of 0.0 and 1.0. I’ve attached patch v11 with these changes.

Patch looks fine. Thank you!

On 14.01.2025 15:00, Ilia Evdokimov wrote:

Alena, Sami – I apologize for not including you in the previous email. If you're interested in this approach, I'm open to any suggestions.

[0]: https://www.postgresql.org/message-id/1b13d748-5e98-479c-9222-3253a734a038%40tantorlabs.com

This is a difficult question. I tend to agree with Alexander Korotkov's proposal to add a filter that saves information about queries whose statistical information satisfies the conditions of the configured filter. However, I don’t believe query execution time is a sufficient metric for this purpose. It is too unstable and influenced by many external factors, such as system load. For instance, various background processes like vacuum, checkpointer, or background writer mechanisms could be running simultaneously.

Additionally, a query may take a long time to execute because another large query is consuming most of the system's resources at the same time. In such cases, the long execution time of the triggered query may not indicate anything remarkable. Furthermore, statistics for resource-intensive queries may appear normal if the query simply processed a large volume of data, such as when it involves a Cartesian product or a full join.

Therefore, I think the idea of using filters is more promising, especially if multiple filters are implemented. For example, we could add filters for buffer usage (pages read or modified), differences in cardinality (predicted vs. actual), username, application name, and other criteria. These filters would help reduce the volume of queries tracked by the pg_stat_statements extension. However, there might be challenges in keeping this state up to date, given the volatile and unstable nature of the system.

Your proposed parameter could also reduce the volume of queries for which statistics need to be saved, but it is too unpredictable for analysis. This unpredictability makes it unclear how the data would be interpreted in the future. For instance, a query that genuinely impacted performance might not be processed by pg_stat_statements simply due to randomness, while a small, insignificant query could be selected instead. Analyzing such statistical information might lead to misleading conclusions.

-- 
Regards,
Alena Rybakina
Postgres Professional
--------------rxAxpSKe0DX55xg1VTXG0AIz--