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 1tkl0l-006XCP-Ar for pgsql-hackers@arkaria.postgresql.org; Wed, 19 Feb 2025 14:25:15 +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 1tkl0i-0091hh-8I for pgsql-hackers@arkaria.postgresql.org; Wed, 19 Feb 2025 14:25:12 +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 1tkl0h-0091hZ-T4 for pgsql-hackers@lists.postgresql.org; Wed, 19 Feb 2025 14:25:12 +0000 Received: from forward103a.mail.yandex.net ([178.154.239.86]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tkl0d-001m8C-35 for pgsql-hackers@postgresql.org; Wed, 19 Feb 2025 14:25:11 +0000 Received: from mail-nwsmtp-smtp-production-main-54.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-54.vla.yp-c.yandex.net [IPv6:2a02:6b8:c15:2c8f:0:640:f9cc:0]) by forward103a.mail.yandex.net (Yandex) with ESMTPS id 4EA5160FA9; Wed, 19 Feb 2025 17:25:06 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-54.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id 2PXroO0Oda60-DlUhA9Wr; Wed, 19 Feb 2025 17:25:05 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1739975105; bh=Xt4qYIMpOKOKc6R1YgMNc6F7//+KFT2G/6V8EtLwLPs=; h=In-Reply-To:Cc:Date:References:To:From:Subject:Message-ID; b=xABaFwZtJROj47sGScRIMuLD9f7BFVL8ZqEGu/LiSuOI/CXvdK6vhx7tWL1RYvR+B FIpE5QBvmsyjEgWL7UQWj0MWvpzfMaOFGdoM3e61Kb6/+nunAE5xaL2DXfWmiMlem3 LzAHI+x+zUplQzep8c9cTqMu7MCunAxp1qnO7RrA= Authentication-Results: mail-nwsmtp-smtp-production-main-54.vla.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Message-ID: <7b9df88f-3015-464b-881e-4c6a7b3a5c03@tantorlabs.com> Date: Wed, 19 Feb 2025 17:24:58 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Sample rate added to pg_stat_statements From: Ilia Evdokimov To: Alexander Korotkov , Michael Paquier , Greg Sabino Mullane , Andrey Borodin , Alena Rybakina , Sami Imseih Cc: pgsql-hackers References: <3a27c830-b81f-4560-9774-a87d9b922fbd@tantorlabs.com> <7AD765EF-14DE-4B3E-8282-E82A9B44DBAA@yandex-team.ru> <0c7770bd-9750-4200-a472-bdb262583c2f@tantorlabs.com> <18631d46-1741-4edc-b116-8d9631cdf919@tantorlabs.com> <4f98ec33-c627-4eb4-84f3-9994a0982277@tantorlabs.com> <623db69f-b40a-4d50-b1bd-bb073fcc4336@tantorlabs.com> <3546e2a7-5831-4c0a-996b-358d4ec483a6@tantorlabs.com> <6bf3059f-7ebe-4640-8870-1220f83b88ed@tantorlabs.com> Content-Language: en-US In-Reply-To: <6bf3059f-7ebe-4640-8870-1220f83b88ed@tantorlabs.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 14.02.2025 16:17, Ilia Evdokimov wrote: > Hi hackers, > > I've decided to explore a slightly different approach to reducing > spinlock contention—by introducing a simple execution time threshold. > If a query’s execution time exceeds this threshold, it is recorded in > pg_stat_statements; otherwise, it is ignored. As Alexander [0] pointed > out, this helps retain valuable queries for further analysis. A > similar mechanism is already present in auto_explain and > pg_store_plans. When pg_stat_statements.track_min_duration = -1, > disable tracking. If pg_stat_statements.track_min_duration = -1, all > statements are tracked. > > I benchmarked this approach using -M prepared -S on my machine with 48 > CPUs. However, I couldn’t reproduce spinlock contention because the > machine isn’t large enough to create sufficient concurrency. > Nevertheless, I’m sharing my results for reference and checking > correct results of threshold. > > Here’s the benchmarking procedure I followed: >     createdb pgbench >     pgbench -i -s 3000 pgbench >     psql -c 'SELECT pg_stat_statements_reset()' >     pgbench -c 46 -j 46 -T 120 -M prepared -S --progress=10 pgbench > > select query, calls, min_exec_time, max_exec_time, mean_exec_time, > stddev_exec_time from pg_stat_statements where query = 'SELECT > abalance FROM pgbench_accounts WHERE aid = $1'; > > track_min_duration |     calls | min_exec_time | max_exec_time |     > mean_exec_time | stddev_exec_time >                  0 | 111282955 |       0.00365 |      15.56946 | > 0.015042374707317802 | 0.06067634978916631 >                  5 |       458 |       5.00627 |      15.699129 |    > 5.962879746724887 | 1.1432124887616204 >                 10 |        14 |      10.538461 |      16.113204 |   > 12.415218999999999 | 1.5598854455354354 >                 20 |         - |              - |              - |     >                - | - >                 -1 |         - |              - |              - |     >                - | - > > I’d greatly appreciate any feedback on this alternative approach, as > well as benchmarking on a pretty large machine to see its impact at > scale. > > [0]: > https://www.postgresql.org/message-id/CAPpHfdsTKAQqC3A48-MGQhrhfEamXZPb64w%3Dutk7thQcOMNr7Q%40mail.gmail.com > > -- > Best regards, > Ilia Evdokimov, > Tantor Labs LLC. Hi hackers, I rebased this patch to v18 to fix an inaccurate additional description of the GUC parameter. The -1 value should be described first, followed by 0. Does anyone have other suggestions on how we could sample queries in pg_stat_statements to reduce spin-lock contention on entries? -- Best regards, Ilia Evdokimov, Tantor Labs LLC.