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 1w7TGb-005Lj7-0K for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 07:12:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7TFa-008Koj-1t for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 07:10:59 +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 1w7TFa-008Kob-0r for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2026 07:10:58 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7TFX-0000000285n-3hY6 for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2026 07:10:58 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-486fd27754bso52647955e9.3 for ; Tue, 31 Mar 2026 00:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774941054; x=1775545854; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Yz445BPqv9qZVnU2WfBLOgYO4xV5J4P2RLhr2I6gqGI=; b=b7t2LQeCVWDEFWEJbnzc89bHarSJxWxV5E+pqs1mWphPX5lz6YVK3taeM/STlZYI7M Uxiy/vBTvQ7eVg60la7HOPD/uwl1xV5O6VgBWCwhGo315FFr8xcpwSWCjOUOtd485ViV y8Yq7hP6tze2ijSBVlyU0aJFQsypFA7QgKcsbctNPV2uDIBe3ISDgPF4pR0TWst5NWxc QPqz3aBd7z4/a26XVbMaNxP97bAaTSdV/tIVn/FUyphvBCYxv7O/xjJBy6onKBJgrXJ8 cXm2cSZGC02tgWM68NEnugIm9Rb4e8LCo70sBiu09SO+jGHytrlTfeO8MjJDkCBG8Zx5 gq8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774941054; x=1775545854; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Yz445BPqv9qZVnU2WfBLOgYO4xV5J4P2RLhr2I6gqGI=; b=TF1XhQb3yi8oLtnxAvrmJikb4Vd+cSiayUlr4LnBU1pFeO0MSvriz/IgThI6ReqtNo Pu/iswchMviYVtftu3BduyNKi+iSoQ3K8XO9NEFbkcQCMoa+wveacFZImAvlP0SFNxOF bmv8U06zgD7gXu8A6vruMO98itCEr7VQmDdPzna0cAB87wAF5Iyp9/nUK/oGW/k6fYq2 W1H/AxC6nxgXL93pNXOLQz+8MxC96ESPBYY1h975JGPmSqCIJ++tfBzbxptFXz9nRoTw ZVQt7+2Cuf9LY9lf+ePUGcE8+WvwhV2PqoPJMHaA6NB2mtxwnWxV/F/2dO7EGB8uwkLn 6tkw== X-Forwarded-Encrypted: i=1; AJvYcCXfFTvEK8GBgHhXaAOV7jAc6WyGS9FnS8vKim1FPbW+phtC5Ca43fHaEEv8OdmjlmqXnAvgyPxSU/jGQAFM@lists.postgresql.org X-Gm-Message-State: AOJu0YwAnpugl8wF6lvecY7oLEzuDCO+N2kYRineIgObiURC7yH3i1iS 8Lj0aBU01thXpu9iDC6YA3lozshjmmsTwuUxVuq0t71so4ulVAHHxlAZ X-Gm-Gg: ATEYQzzvq2FChlXx56lSxgtMUhm1TPMGYV0yOLA0Z3nAe4929JAu1FG9CILlyHNVi2H ihpphJk3cZpGh21TBnRvfd4+EMtHPd527AIBdT7c/3vTDh8wcqkAPq8Z/mTIM3kK2YzqTxZClX+ 0hsOXsHNNPavIwyUuwT9839ruRv4bESJlgAh96c5vNILtGMANfvkaCkca3AwzaSeZV3XOGlfOd7 Bl4oQfC0iS/UrOEICHf9peldqu3fjstDfwy9T8Gv2EO3TLGjbNOfaaZrLe1CE4Iqo3oq/0vyBba e41htYz6gP1UKkvoIz5+FwnQWWD6EOlU9qJG8vtpD9cvcsoRkJdF+B0CDNS3vEElb9FTg1o9VxV cIIYR5ZKAsZUbv5azGz1dzZzlvk6TkEc2WWLrMWTthHy0s3krIB0pwuHHI7aW3ZHROYxSQLZf9S EIAnHTvRyKAMZQ2VTs1LRs8E8sYzcr+tCVwqUtXchuZIwQXpOzp37todzvm2zCK/BRzdwHDXDdX 4Bh7v4h9jHXqt7re6aKVUbY+rm5HgbEaIlem157qRTPJBUont43wrgdRA== X-Received: by 2002:a05:600c:8485:b0:487:36c:f5ab with SMTP id 5b1f17b1804b1-48727d73464mr292929995e9.10.1774941053566; Tue, 31 Mar 2026 00:10:53 -0700 (PDT) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43cf257cbc3sm24674634f8f.35.2026.03.31.00.10.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 00:10:52 -0700 (PDT) Date: Tue, 31 Mar 2026 07:10:51 +0000 From: Bertrand Drouvot To: Tomas Vondra Cc: Michael Paquier , Andres Freund , Jeff Davis , Greg Sabino Mullane , pgsql-hackers@lists.postgresql.org Subject: Re: Adding locks statistics Message-ID: References: <1af63e6d-16d5-4d5b-9b03-11472ef1adf9@vondra.me> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="m+Gg8weB9LNUiXXr" Content-Disposition: inline In-Reply-To: <1af63e6d-16d5-4d5b-9b03-11472ef1adf9@vondra.me> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --m+Gg8weB9LNUiXXr Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Mon, Mar 30, 2026 at 06:11:17PM +0200, Tomas Vondra wrote: > Hi, > > Isn't pgstat_lock_flush_cb a bit broken with nowait=true? It'll skip > flushing stats for that particular lock type, but then it'll happily > reset the pending stats anyway, forgetting the stats. > > AFAIK it should keep the pending stats, and flush them sometime lager, > when the lock is not contended. That's what the other flush callbacks > do, at least. This probably means it needs to reset the entries one by > one, not the whole struct at once. Oh right, it's currently misbehaving, thanks for the warning! > TBH I'm rather skeptical about having one lock per entry. Sure, it > allows two backends to write different entries concurrently. But is it > actually worth it? With nowait=true it might even be cheaper to try with > a single lock, and AFAICS that's the case where it matters. > > I wouldn't be surprised if this behaved quite poorly with contended > cases, because the backends will be accessing the locks in exactly the > same order and synchronize. So if one lock is contended, won't that > "synchronize" the backends, making the other locks contended too? > > Has anyone tested it actually improves the behavior? I only quickly > skimmed the thread, I might have missed it ... > I just did a few tests, with a per entry lock version fixed (to avoid the bug mentioned above) and with a single lock. The test is this one: Setup: deadlock_timeout set to 1ms. CREATE TABLE t1(id int primary key, v int); CREATE TABLE t2(id int primary key, v int); INSERT INTO t1 SELECT i, 0 FROM generate_series(1,100) i; INSERT INTO t2 SELECT i, 0 FROM generate_series(1,100) i; test.sql: \set id1 random(1, 100) \set id2 random(1, 100) BEGIN; SELECT pg_advisory_xact_lock(:id1); UPDATE t1 SET v=v+1 WHERE id=:id1; UPDATE t2 SET v=v+1 WHERE id=:id2; END; Launched that way: pgbench -c 32 -j 32 -T60 -f test.sql One run produces, something like: postgres=# select locktype, waits, wait_time from pg_stat_lock where waits > 0; locktype | waits | wait_time ---------------+--------+----------- tuple | 5058 | 5092 transactionid | 78287 | 79269 advisory | 105005 | 177253 (3 rows) With one lock per entry, the avg (the test has been run 5 times) tps is 12099. With one single lock, the avg (the test has been run 5 times) tps is 12118. The difference looks like noise so that one lock per entry does not show improved performance. Also both kind of tests produce this perf profile: 0.00% 0.00% postgres postgres [.] pgstat_lock_flush_cb So, I'm tempted to say that one lock per entry adds complexity without observable performance benefit. Also one single lock matches more naturally the intent of the nowait path and I would also not be surprised if one lock per entry behaves worse in some cases. So, PFA a patch to move to a single lock instead. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --m+Gg8weB9LNUiXXr Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-lock-statistics-replace-per-lock-type-locks-with-.patch"