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 1tVHk4-00715c-3b for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 22:08:04 +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 1tVHk3-008yrj-8H for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 22:08:02 +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 1tVHk2-008yrQ-Ta for pgsql-hackers@lists.postgresql.org; Tue, 07 Jan 2025 22:08:02 +0000 Received: from forward101a.mail.yandex.net ([178.154.239.84]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tVHjz-000Q1E-0U for pgsql-hackers@postgresql.org; Tue, 07 Jan 2025 22:08:02 +0000 Received: from mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net [IPv6:2a02:6b8:c0f:171e:0:640:d7a4:0]) by forward101a.mail.yandex.net (Yandex) with ESMTPS id 6031360C60; Wed, 8 Jan 2025 01:07:58 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id v7uhSFvOmGk0-cv60Gb1N; Wed, 08 Jan 2025 01:07:57 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1736287678; bh=KYNipKHhkBXzFkUzKTf39zOBX5piJtmwN8GYI7K975I=; h=From:In-Reply-To:Cc:Date:References:To:Subject:Message-ID; b=fmaQCsyVptahzQEKi0G4NmJ+aTwsIVtQ4IXl9m0tDwEosKwdpoTn1oJ7MEoyV4MEo DbxLOqpauMjP+2FMB1Xg/kC1ugVr+oq1ttenE6cQzosFMC6ykUcStQF9DpXNHSYyJV dPuMPnV3emJRZl1MFTslY6+K9FUH2RvKfOBkxZvg= Authentication-Results: mail-nwsmtp-smtp-production-main-31.vla.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Message-ID: <4fe22ea2-d366-42f4-92bb-de35d11aa766@tantorlabs.com> Date: Wed, 8 Jan 2025 01:07:57 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Sample rate added to pg_stat_statements To: Sami Imseih , "Andrey M. Borodin" Cc: Alexander Korotkov , Michael Paquier , Greg Sabino Mullane , pgsql-hackers References: <6707FCA9-FD1A-4609-A1A6-142456C14E0C@yandex-team.ru> <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> Content-Language: en-US From: Ilia Evdokimov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 07.01.2025 22:29, Sami Imseih wrote: >>> You are right. This is absolutely unexpected for users. Thank you for >>> the review. >>> >>> To fix this, I suggest storing a random number in the [0, 1) range in a >>> separate variable, which will be used for comparisons in any place. We >>> will compare 'sample_rate' and random value not only in >>> pgss_post_parse_analyze(), but also in pgss_ProcessUtility() and >>> pgss_planner(). >>> >>> I attached patch with test and fixes. > I still think this is not the correct approach. It seems that post_parse_analyze > should not have to deal with checking for a sample rate. This is because > post_parse_analyze, which is the only hook with access to jstate, is > only responsible > for storing a normalized query text on disk and creating a not-yet > user visible entry > in the hash. i.e. pgss_store will never increment counters when called from > pgss_post_parse_analyze. > > /* Increment the counts, except when jstate is not NULL */ > if (!jstate) > { > > What I think may be a better idea is to add something similar > to auto_explain.c, but it should only be added to the top of > pgss_planner, pgss_ExecutorStart and pgss_ProcessUtility. > > if (nesting_level == 0) > { > if (!IsParallelWorker()) > current_query_sampled = pg_prng_double(&pg_global_prng_state) < > pgss_sample_rate; > else > current_query_sampled = false; > } > > This is needed for ExecutorStart and not ExecutorEnd because > ExecutorStart is where the instrumentation is initialized with > queryDesc->totaltime. The above code block could be > turned into a macro and reused in the routines mentioned. > > However, it seems with this change, we can see a much > higher likelihood of non-normalized query texts being stored. > This is because jstate is only available during post_parse_analyze. > Therefore, if the first time you are sampling the statement is in ExecutorEnd, > then you will end up storing a non-normalized version of the query text, > see below example with the attached v8. > > postgres=# set pg_stat_statements.sample_rate = 0; > SET > postgres=# select pg_stat_statements_reset(); > pg_stat_statements_reset > ------------------------------- > 2025-01-07 13:02:11.807952-06 > (1 row) > > postgres=# SELECT 1 \parse stmt > > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# SELECT query, calls FROM pg_stat_statements; > query | calls > -------+------- > (0 rows) > > postgres=# set pg_stat_statements.sample_rate = 1; > SET > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# SELECT query, calls FROM pg_stat_statements; > query | calls > -------+------- > (0 rows) > > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# SELECT query, calls FROM pg_stat_statements; > query | calls > ---------------------------------------------+------- > SELECT 1 | 1 > SELECT query, calls FROM pg_stat_statements | 1 > (2 rows) > > postgres=# \bind_named stmt \g > ?column? > ---------- > 1 > (1 row) > > postgres=# SELECT query, calls FROM pg_stat_statements; > query | calls > ---------------------------------------------+------- > SELECT 1 | 2 > SELECT query, calls FROM pg_stat_statements | 2 > (2 rows) > > One idea is to make jstate available to all hooks, and completely > remove reliance on post_parse_analyze in pg_stat_statements. > I can't find the thread now, but I know this has come up in past discussions > when troubleshooting gaps in query normalization. My concern is this > feature will greatly increase the likelihood of non-normalized query texts. > > Also, with regards to the benchmarks, it seems > sampling will be beneficial for workloads that are touching a small number > of entries with high concurrency. This is why we see benefit for > a standard pgbench workload. > Samping becomes less beneficial when there is a large set of queries > being updated. > I still think this is a good approach for workloads that touch a small set > of entries. > > Regards, > > Sami > > Wow, thank you for pointing this out. Your solution looks interesting, but I'd like to explore other ways to solve the issue besides making it available to all hooks. If I don't find anything better, I'll go with yours. -- Best regards, Ilia Evdokimov, Tantor Labs LLC.