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 1tVFGk-006il0-UY for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 19:29:39 +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 1tVFGj-006yW0-UA for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 19:29:37 +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 1tVFGj-006yVs-HL for pgsql-hackers@lists.postgresql.org; Tue, 07 Jan 2025 19:29:37 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tVFGg-000OYG-0p for pgsql-hackers@postgresql.org; Tue, 07 Jan 2025 19:29:36 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-3002c324e7eso179925211fa.3 for ; Tue, 07 Jan 2025 11:29:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736278172; x=1736882972; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=e55VMC9Xxk/6pK0ZtpHWETbXAfmwUev0RI3SCchE4U8=; b=ldsoNqm/zzfpEb/Wl+Zc2ZsQEDP5SYHRmdOrG49kwRypux+uWTUbo9MgquDU+qdKSC CwF5fHhPum0UtF48r8Bac0XRLYxTbi/ObI5g2OpMZ5dbtdW3lqDn2zxv204fddDxN4ef zua915NHxveKCQJUnooWKLRiYM3hO/1jt7v+qjKACq1+YjMVx1Z+17lYuEVvkijzwGXT 04pom1cqkGM9X8bzcohVnTur7SeugsC/SzROhQnJQ7N7ZBBQ/R8ly1WUumk4krpIyT8V jA3iC706EY8MWpKFBrMWF3x0ZA1hz6Jvl969JeruIHHawNzqBWnSMoLHx1k2a5RxxnaM gu9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736278172; x=1736882972; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e55VMC9Xxk/6pK0ZtpHWETbXAfmwUev0RI3SCchE4U8=; b=Axlm7Ck7p29NL0QwANqM40sEPb41ApKnjdPHv99+H7D+vdN3ZaEEwe46CZDXoPwmTX /gHedWaqZR0PDcLHXqcQrCruzJasJJ3Grkb3q32BZ14cjCMOJGQFCDfvXtuT84ouZkaf D2aUaCOhjFNz2N6qQN2kL6CdnN6J9wLyZRw64LOpsmu6W8UQ1WAEasfRv2o5PKB0K1Hm RnUpBNIrt1GqtJIiX9NnfEquExETRDrWHIZ7uDeOPbtNJVN8eyo7V2lmRZnolYtuRk82 7mI9KIG26Tk1p+rpcU4rnwwEIjWJndBBpN568foKKnIr4hCFchepcJB2FyJgHDW/lreN X/XQ== X-Forwarded-Encrypted: i=1; AJvYcCVL1DtXMvd+2zb6CVX70SOgmzMzHrnHlpwXhdYAoxw5WVb9H4NdqGJoegh7Lke/9di3vrFqV0CE81I2Yjkg@postgresql.org X-Gm-Message-State: AOJu0YwwiD3BiCq+8/h+3ymXQnKBuX6inOZcEIHmhAWk1d8q8IUa0gPt t1ud7MRu5XqPcZe6EKEYoNJ+0Mf4OxsnBeVi2RffjOsUJodDhn00vhzDTVnQSbgrVOFjlL2Ib3u NQH6KG67QH89ItAEkXWfi1WGj12s= X-Gm-Gg: ASbGncs+cOY7VjkdJj4AWewSWztKJwqX4P9MMRD/2XGf5fI4wYloJAPnCF0XKjWtrOA APL9C99UMsz9R+u597+6eSwu27LuoZiyZt6V/ X-Google-Smtp-Source: AGHT+IHpZ5j+serGhLVdmbIrMLh0qJcI7Qk6ZKKysjiU/VMj1NQndflks1z9rl/6pC+pz0IlFNA6D5IkQlpbqkM0hSA= X-Received: by 2002:a2e:a7ca:0:b0:300:26bc:4311 with SMTP id 38308e7fff4ca-30468569292mr163938381fa.18.1736278171611; Tue, 07 Jan 2025 11:29:31 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: <9D8A5BC8-A3A5-4DA9-9382-F30D343247D1@yandex-team.ru> From: Sami Imseih Date: Tue, 7 Jan 2025 13:29:20 -0600 X-Gm-Features: AbW1kvYoI2s1c3lZ1mnVfMWz0UIzSXEGbVPiVYUdArdswAA5pFh4aCbJqHAzsxA Message-ID: Subject: Re: Sample rate added to pg_stat_statements To: "Andrey M. Borodin" Cc: Ilia Evdokimov , Alexander Korotkov , Michael Paquier , Greg Sabino Mullane , pgsql-hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk >> 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