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 1tkuzr-007pTp-1L for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Feb 2025 01:04:59 +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 1tkuzo-00GJp7-RD for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Feb 2025 01:04:57 +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 1tkuzo-00GJoz-HR for pgsql-hackers@lists.postgresql.org; Thu, 20 Feb 2025 01:04:56 +0000 Received: from mail-lj1-x22c.google.com ([2a00:1450:4864:20::22c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tkuzm-001rwy-2L for pgsql-hackers@postgresql.org; Thu, 20 Feb 2025 01:04:56 +0000 Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-30227c56b11so3244481fa.3 for ; Wed, 19 Feb 2025 17:04:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740013493; x=1740618293; 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=QqJz7JcO53koNuxZhu1Fm6ZG3/aiNuk49GCxmuhYnMk=; b=NiHon+yf1p+iWJ1UHAxJpQo52m5ZXurleyMIl3epDfcImZfH6DIsdesl0lr2O1cGdo oby/p+2YHmjIhd57ym/jnMaPG6i+zoe/4aaA6QC5iE+XLCyDO2o7YArNuVaPV0idKUi4 BLw1B2rYxsCUuUgMu38guE8uOYHs7jR+6QtRKXF+v0J8rx93ADwckXApguiAaNIvt5g/ VtvIqHuhGvpnFErXN1oa9ehsLw2I1i+r/EJfI65Qr+XP8CQ9MNpHVxGVoEAWj2cCxAhg nbD+mD3C3XNb5BwUf7jj1XZThkmb2VEtQ2Yla2XQUMeQeMV7dwdYdJeK0Y3OvC4EHz5R Yd+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740013493; x=1740618293; 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=QqJz7JcO53koNuxZhu1Fm6ZG3/aiNuk49GCxmuhYnMk=; b=guUSAUeHmRTy7iOw7317wTmMFqwresi36mrbmQjHnAlY7b9RyAvUPafE6awr5okDLj 57OiK7P/DvhxeZlZlpuqX7pqc+5nFAD6NQ7o5UBWqzTQ3wR/Q0JxSixBCGjbHGa+TOsL VhmNe8kABc8rxAZ/v43Ekzz2z4uAlamlHRR87cB8V9f2CxefxZDgSTsKiDqr4GU09s7a muOSCd8pEyhUnDhqNRprMZJ3/VtPwAH9SGnmtfoVpclCYIbZVdovz5ih3rs61mAG6Y96 AY9x6P5MDn4pPzkJA9OirM+2yutfnWI7CBuLOiKNwJa67anLCVYC2pTjCesRKjzkD81r VmZg== X-Forwarded-Encrypted: i=1; AJvYcCXFUNsCx2LTv37LoScQlyE4HRoB0+1Qs50eS2hJzHAZE0Vi6QxtaRDGHotXreMZyQpcg7Jog9fqyrQ8XNgO@postgresql.org X-Gm-Message-State: AOJu0YydVIJh8xDlqcpxJKBFzbzYU+n2sbLXlnbLvoxcUcVeD7b6ZJaM gGveVqpsud5+xi/4znZfuVkBDoI9pCX8B5yx0bWc+CnxKPb1X2ycvqwJ8+8gr1LvCCS0Wf+XDuV T+3HK/eFKH06qnkK4ILTC9QKTFGs= X-Gm-Gg: ASbGnctnN4qph2qiYR1wD/XyeH97dJPEhu4vKKCOkPLlX4klUKm/8dDTHU9WupAXqWv 1e5hjQL2bFnZKXETX3UMQMae48+iDGFZXgZD2fzIMOmtTenPRSNyKQpuMZizls+1rlnZsOGo= X-Google-Smtp-Source: AGHT+IEflKcwuxhASC/008gOWfwKWV2tA1o3yjpnOfjTr9VCM/XrVhSgpZqfmQt8zR0hLT5yxHJC8SdBloUVPCXijYQ= X-Received: by 2002:a2e:978e:0:b0:302:1fce:3956 with SMTP id 38308e7fff4ca-30a51fee77amr1637871fa.4.1740013492588; Wed, 19 Feb 2025 17:04:52 -0800 (PST) MIME-Version: 1.0 References: <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> <7b9df88f-3015-464b-881e-4c6a7b3a5c03@tantorlabs.com> <6f305fd1-44c3-4189-81ef-238777277e5e@tantorlabs.com> In-Reply-To: From: Sami Imseih Date: Wed, 19 Feb 2025 19:04:41 -0600 X-Gm-Features: AWEUYZnGEszZGb_IHmsMH5l2djiVl6aTRh2QTQxEwDLM1LVM2K3SFkG1oaC8NyA Message-ID: Subject: Re: Sample rate added to pg_stat_statements To: Ilia Evdokimov Cc: Alexander Korotkov , Michael Paquier , Greg Sabino Mullane , Andrey Borodin , Alena Rybakina , 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 > But instead of blindly reducing the frequency via PRNG, we can take a more thoughtful approach with threshold by execute time: > Find the most frequent query by column 'calls' in pg_stat_statements; > In this query look at info about execution time: min_exec_time, max_exec_time, etc; > Gradually increase the threshold from min_exec_time to max_exec_time, limiting the tracking of this query. > Monitor performance: once the bottleneck is resolved, stop at the current threshold value. This approach allows us to: > Eliminate the spin-lock bottleneck; > Preserve data about slow queries, which may be critical for performance analysis; > Reduce the load on the most frequent queries causing contention, instead of uniformly reducing the frequency for all queries. In my opinion, sample rate is a better fit for pg_stat_statements, since the queries that you care about the most are usually the most frequently executed. Sampling them will still provide enough good data without the risk of not capturing statistics about them at all. Longer running queries will also likely be the least frequent, so they are already not likely contributing to the spinlock contention. Also, the least frequent queries will likely be aged out faster, so pg_stat_statements was never really a good candidate to track those anyways; slow query logging with log_min_duration_statement is a better way to ensure you capture the data. Maybe others may have a different opinion? -- Sami