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 1uhmPp-003zo4-J9 for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Aug 2025 09:51:06 +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 1uhmOq-006uFq-6b for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Aug 2025 09:50:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uhmOp-006uFi-H5 for pgsql-hackers@lists.postgresql.org; Fri, 01 Aug 2025 09:50:03 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uhmOl-000AU5-0k for pgsql-hackers@lists.postgresql.org; Fri, 01 Aug 2025 09:50:02 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-3b783ea502eso1329725f8f.1 for ; Fri, 01 Aug 2025 02:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754041799; x=1754646599; darn=lists.postgresql.org; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:to:from:date:from:to:cc:subject:date:message-id :reply-to; bh=mUzx4zOwJmXs+qKX73W4ou67Pbocr6cp2TZiOwFVLnk=; b=D5Y5alqGj8BIwhn6pXUAp0EbsKCpESDHxHmaalveblCD68Qzq//o+7AJPGqClkpQSG W8FdVaum6t9ZnYmBmJumP+t9RSfQ3dV5Tm/P0ScArLiM7yQXkF5wEEKA0gHuAwKtz2hg wL9gd9vzLMGUXzjSGCptyrYJKtxvX/+gUhgaroY1yP1UKWdWDD42tMLDAV945cA16c7x E4crF8papSlDK6cGqPRkXXSS33dch4fUOQc5YKV/4nOn+nuFgLz6Rbbqf0wWEEMHpvsv Mxf4+PCoCqborCYJju8Dw0yGonuKiUERveQS1RKgtTrZWP9zelBmxNxAvdd/iSuaPV1E i6FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754041799; x=1754646599; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mUzx4zOwJmXs+qKX73W4ou67Pbocr6cp2TZiOwFVLnk=; b=s1ivH3cAX+tEQaTLMpz5q/HINlZ+ktjhg/1YTN8nYwQTVBkneB9DlMSISyLnc9SgSa jjwis7atIpYbmGFiYIANx8JbRzSuEAGFv48znLH+PAm9xR90NowcgKogIyYs/m1qcMDE T0BnMh6znBlnWqFywpPrhjZfF6Ln3nEGCJK5WYy3iJ1p5oG3PD5mwrF5K5EoJ4l1K4WZ KUixLf4OgDKkLZIFZ8N4CytXBoWAbO6IwOfhqMJM8XMG6pFGSr0eRLxdcY39C5LgZMtK sWovqHfvbd4xHdRLeUiYuiQfrZS0MmJbqcvec+M35ZMlIwEUnKXxnZ1GnMXkrKdWpV3P VcsA== X-Gm-Message-State: AOJu0YwoweSZvaUUUFyXUxMRoLRcEUauCKPrzZ2fTlV3rPpPOVJaLT/j ZNAOTlDsepx28oilahONj1I3vlmdH4U+td9VBrid0kTv47XFbfMpzlMsWgPdbg== X-Gm-Gg: ASbGncu+IVbKOnkd0pdw2W79a3G/6LA1y3I5MZKL1FFfH5KvqDS0NOuRXZtdXWjBzbz EoxpYJdve04dv9i+sTgbGuID2ac9YOGysUFxFDkD/ewyHPVYWygwF0CYG14w2qwshDK90Car6HT zRkrZaOxlekoX0oucYWsZzRhyNmbIlBRptQNpLRKqg6iU/2yL6l+aLmN1I6/w1luGNMt7q9bjlB F1Kl3fJ+TX8Te3z9o8gA5TzLAroGs9Enr730vwtvnWYAojZv0rvSzPz97ncIHmauugXQJFzjkvo U6wvHkjuoBu9EztfA+1jaHj4FpGMOTZkUDGKBxegPGXhqTuxfBjJEFSbLedDR852KEvVPnlxN5f OD45dA5E8FbaWX8SKS4UYeGv/j1PG4Sq3h1IJkWTuW6PXpR/pZsyty0IJID7FLyZnUolvj74okz BXBbtrzIttribEX4e5k3QKcz77wgYb8+q2dLqxumNShZP+tijOI1FGPf4uupLMdB1p X-Google-Smtp-Source: AGHT+IEULgiVe8QVc1LnjzXDpDFlePX6WkY6dUrpXB9U/3NoU/ZRPJgLZcCNDklGLZ2hj506IhuAYQ== X-Received: by 2002:a05:6000:2409:b0:3b8:d25e:f480 with SMTP id ffacd0b85a97d-3b8d25ef4d2mr2062904f8f.29.1754041798276; Fri, 01 Aug 2025 02:49:58 -0700 (PDT) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-181-182.eu-west-3.compute.amazonaws.com. [15.237.181.182]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b79c469093sm5400801f8f.51.2025.08.01.02.49.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Aug 2025 02:49:57 -0700 (PDT) Date: Fri, 1 Aug 2025 09:49:56 +0000 From: Bertrand Drouvot To: pgsql-hackers@lists.postgresql.org Subject: Adding locks statistics Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Th7cMauVQntl/Hn1" Content-Disposition: inline Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Th7cMauVQntl/Hn1 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hi hackers, Please find attached a patch to add a new view (namely pg_stat_lock) that provides lock statistics. It’s output is like the following: postgres=# select * from pg_stat_lock; locktype | requests | waits | timeouts | deadlock_timeouts | deadlocks | fastpath | stats_reset ------------------+----------+-------+----------+-------------------+-----------+----------+------------------------------- relation | 612775 | 1 | 0 | 0 | 0 | 531115 | 2025-08-01 09:18:26.476275+00 extend | 3128 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 frozenid | 11 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 page | 1 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 tuple | 3613 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 transactionid | 6130 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 virtualxid | 15390 | 0 | 0 | 0 | 0 | 15390 | 2025-08-01 09:18:26.476275+00 spectoken | 12 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 object | 8393 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 userlock | 0 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 advisory | 44 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 applytransaction | 0 | 0 | 0 | 0 | 0 | 0 | 2025-08-01 09:18:26.476275+00 It means that it provides historical trends of locks usage per lock type. It can be used for example for: 1. checking if "waits" is close to "requests". Then it means you usually have to wait before acquiring the lock, which means you may have a concurrency issue. 2. lock_timeout and deadlock_timeout tuning (lock_timeout is visible only in the logs if log_min_error_statement is set appropriately). 3. checking the "requests"/"fastpath" ratio to see if "max_locks_per_transaction" needs tuning (see c4d5cb71d2). If any points need more details, it might be a good idea to start sampling pg_locks. The patch is made of 2 sub-patches: 0001 - It adds a new stat kind PGSTAT_KIND_LOCK for the lock statistics. This new statistic kind is a fixed one because its key is the lock type so that we know its size is LOCKTAG_LAST_TYPE + 1. This statistic kind records the following counters: - requests: Number of requests for this lock type. - waits: Number of times requests for this lock type had to wait. - timeouts: Number of times requests for this lock type had to wait longer than lock_timeout. - deadlock_timeouts: Number of times requests for this lock type had to wait longer than deadlock_timeout. - deadlocks: Number of times a deadlock occurred on this lock type. - fastpath: Number of times this lock type was taken via fast path. No extra details is added (like the ones, i.e relation oid, database oid, we can find in pg_locks). The idea is to provide an idea on what the locking behaviour looks like. Those new counters are incremented outside of the wait events code path, as suggested in [1]. There are no major design choices, it relies on the current statistics machinery. 0002 - It adds the pg_stat_lock view It also adds documentation and some tests. Remarks: - maybe we could add some metrics related to the lock duration (we have some hints thanks to the timeout ounters though) - if this is merged, a next step could be to record those metrics per backend [1]: https://www.postgresql.org/message-id/CA%2BTgmobptuUWo7X5zcQrWKh22qeAn4eL%2B%3Dwtb8_ajCOR%2B7_tcw%40mail.gmail.com Looking forward to your feedback, Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --Th7cMauVQntl/Hn1 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Add-lock-statistics.patch"