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.96) (envelope-from ) id 1wVFgF-001p6U-2e for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Jun 2026 21:32:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wVFgE-008rO7-16 for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Jun 2026 21:32:46 +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.96) (envelope-from ) id 1wVFgD-008rNy-2Q for pgsql-hackers@lists.postgresql.org; Thu, 04 Jun 2026 21:32:46 +0000 Received: from fhigh-a2-smtp.messagingengine.com ([103.168.172.153]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wVFgA-00000001J6d-2ttC for pgsql-hackers@lists.postgresql.org; Thu, 04 Jun 2026 21:32:45 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 1EE6B14000E3; Thu, 4 Jun 2026 17:32:40 -0400 (EDT) Received: from phl-imap-15 ([10.202.2.104]) by phl-compute-05.internal (MEProxy); Thu, 04 Jun 2026 17:32:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=partin.io; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1780608760; x=1780695160; bh=VJSevfdOgHkOLEe7k4T4hmKUvRGjsb4B5es1aqnH+tk=; b= SIUrIYxPcalCbQq4mNBk4ZjgIiIFTxlYsy1zFZyonyMQREH5CTno48FzFD5I+6B/ kEdtuMopQaJ9eHS8uqivNo/uVuUj8cULhSf713tehW4q7RhkhuLO39LbJ4R6g4FB nLLkn6JwBjlkF9A41Ut/GTqKyy59zGxGlM82nUXR3EXekhLFJeeatS3KIipQwQcV UGN6VwdLBOCGVzclo8DyXEtNZzUuAPvGIRG0SgechaXsyVNqgya0PI5vEjAVd/LW 5qT9uYdagYm/ax+IG4RRhDFn2gBh/X2rqbfy/e5F14XYZJrCmA5VerhMrsNmEOPB vcz7Y46Qi1sXEjA2ytr9zA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1780608760; x= 1780695160; bh=VJSevfdOgHkOLEe7k4T4hmKUvRGjsb4B5es1aqnH+tk=; b=Q XiZETs1ciPhPkW7pt0QZr8Kj4eib6qQpdhK/tLEZ/89/8lNDskkrtF33nKtDEg5o mPuCcSFzpFAqkSP1VXZCCOV4V06Aj5Lxqx8ebpf+cXWm3HFLgE89njHjqPvXkrQN k88pI/nygRSaRySZJBfyhK3FF7mbCIYVcF8fyHTuQuTtSKCwoKgNASQ5VP+rmNtt EQ02s8xRuYvWXoULwDbO/Y1ZNx8+WWC3wGPXeLWAgVZe/y2bN6hYyje2WzaD8veZ iXjdVXmiq0MH3NkBzGBjdxYyrU/z13W/hbcwmTXsU/Kbpu1Hq4e9oW29vDNQJk9g xh5MTn7gQI7+yq+xJFOew== X-ME-Sender: X-ME-Proxy-Cause: dmFkZTEGSrR1WHoGU+V8AI1xuixQT0X+T9sfgtqMbzWmC4JJZVeibkHMZwtNXs+3ZCHUNc jWrg/Xwe1OjuUQI/waNTR2v9JMoBUTW5HUh6Wiaz1SDG/xqKYpTlfisdyG8JpOdcs2/LO6 LUX7Z3OrcybEcpgxUCMFeeHLs+2lrmkIHbQtu4Bs7ON0WwdJn2IR8kc5BQWAXAxkySB29n T/SF6td56suz6dRZ/DDhgDz8y2zJvKpZiUaN4TiqedfV73sSd10bCphJrqiAYj4/iUbvnU FpEq1rk4DGa0v3Xi6WlUHx5cHqR3AUeeyaF8ADdxLLLIOpyGnvFwI6aYVURv/UikQydIIG 3S2N3Emejz1o6eYYwMLJYYmpVLFJGWQ9wOvrr1qc2ani513H0a6o4dwgqOlDCBXnG9Ht4A DDLRnsu+0AYO18wSnmZhI9Blfprwd5EFdEWdwmvBlGI4i++tdduWGKTJvIzkzqA3BseAuQ Jy7NOsm1WHoDSImgANOlCJhqVDIjCv0bDx1r1Dmran7ZvgrEo2dYIuBVhHDuMR3GM3uOMI Jc4m5Shs/TfufVBqejXf3ImOKYcO9viCCT+XXJMSbSPeEuLMKdWwHfPIR3+kvM+H6YQNsi GIa7V/xGTqIe+L2+SEeCmfhiSo+ZgByv6kGew3mOOcJ2Cv+rS9ROGZom7YPw X-ME-Proxy: Feedback-ID: idd01497b:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 96969780076; Thu, 4 Jun 2026 17:32:39 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 04 Jun 2026 21:32:39 +0000 Message-Id: Cc: "pgsql-hackers" Subject: Re: Add per-backend lock statistics To: "Bertrand Drouvot" From: "Tristan Partin" X-Mailer: aerc 0.21.0 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed Jun 3, 2026 at 1:59 PM UTC, Bertrand Drouvot wrote: > Hi hackers, > > Now that we have global lock statistics since 4019f725f5d, it could be us= eful > to have the same kind of information on a per-backend basis. > > Indeed, pg_stat_lock gives us cluster-wide aggregates: total waits, total= wait > time, total fast-path exceeded across all backends since last reset. > > When we see high numbers, we can't answer: > > - Which backend is affected the most? > - Is it one backend affected or many? > - Is a specific application or connection pool suffering? > - After a specific workload/application is improved, did its lock behavio= r > improve? > > With per-backend lock stats, we could: > > 1/ Isolate problematic sessions. We can correlate locks behavior with spe= cific > PIDs visible in pg_stat_activity: identify the exact application_name or = user > experiencing lock waits. > > 2/ Debug live contention. During an incident, we could pinpoint which bac= kends > are experiencing fast-path exhaustion or lock waits without having to res= et > global stats and lose history. > > 3/ Define workload characterization. Different backend types may have ver= y > different lock profiles. Per-backend stats would let us see this directly= . > > 4/ Compare before/after per session. We could measure a single backend's = lock > behavior across a specific operation, which is impossible with global cou= nters > that include metrics from all other backends. > > IO and WAL stats already have per-backend counterparts (pg_stat_get_backe= nd_io(), > pg_stat_get_backend_wal()). Lock stats are the same class of operational = data: > having them only at the global level is an inconsistency that limits obse= rvability. The motivation makes sense to me. > As far the technical implementation: > > This data can be retrieved with a new system function called > pg_stat_get_backend_lock(), that returns one tuple per lock type based on= the PID > provided in input. > > pgstat_flush_backend() gains a new flag value, able to control the flush = of the > lock stats. > > This patch relies mostly on the infrastructure provided by 9aea73fc61d4, = that > has introduced backend statistics. > > The overhead (2 functions calls and counters increments) on the hot path = (normal > lock acquisition) is zero: counters are only incremented on paths that ar= e already > "slow" (post deadlock timeout waits, fast-path slot exhaustion) and does = not add > that much memory per-backend: PgStat_PendingLock is 288 bytes. > > The patch is made of 2 sub-patches: > > 0001: Refactor pg_stat_get_lock() to use a helper function > +static void > +pg_stat_lock_build_tuples(ReturnSetInfo *rsinfo, > + PgStat_LockEntry *lock_stats, > + TimestampTz stat_reset_timestamp) I think that the alignment of the second and third arguments could be=20 off by one. They should line up with the capital R in ReturnSetInfo. > - values[i] =3D TimestampTzGetDatum(lock_stats->stat_reset_timestam= p); > + if (stat_reset_timestamp !=3D 0) > + values[i] =3D TimestampTzGetDatum(stat_reset_timestamp); > + else > + nulls[i] =3D true; It's not super clear to me why this changed in the first patch. Perhaps=20 it is meant to be in the second patch? I see in the second patch that we=20 use the stat_reset_timestamp from the backend stats instead of the lock=20 stats in pg_stat_get_backend_lock(). The motivation makes sense. It=20 might be cleaner to move the change into patch 2. > 0002: Add per-backend lock statistics > + Returns lock statistics about the backend with the specified > + process ID. The output fields are exactly the same as the ones i= n the > + pg_stat_lock view. It probably makes sense to link to pg_stat_lock here. Other than the few comments I had, this patchset looks good. It follows=20 patterns that were already established with the per-backend IO and WAL=20 stats. --=20 Tristan Partin PostgreSQL Contributors Team AWS (https://aws.amazon.com)