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 1ullMv-005BDY-09 for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Aug 2025 09:32:33 +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 1ullMt-006aDY-FO for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Aug 2025 09:32:31 +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 1ullMt-006aD3-5y for pgsql-hackers@lists.postgresql.org; Tue, 12 Aug 2025 09:32:31 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ullMr-000AyG-09 for pgsql-hackers@lists.postgresql.org; Tue, 12 Aug 2025 09:32:31 +0000 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-3b7823559a5so2488231f8f.0 for ; Tue, 12 Aug 2025 02:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754991148; x=1755595948; 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=SKnGNYXMSjPueV6Ol7mcG5p22M+mGHYU0ZI4EpyReT0=; b=l0WTx3eeRhRwX++Lwr/OI2jZl4fBVA2ayb0Bo+56FYCMG+J96h5JVCXm7ygmOUBc9D g83GWrndE8EUSeFmwBHLPKVukHt7X5jdoNlwcz00s2x73wgbG/3laU6xMd+o9egIFBud uQ2ZVKfaXKDbbOUo3O+4QZsJ27A5sfjNbeEUeS/4IBywvk44tfdv/km3yj/+02XrD7ka 0N/ypthxUQ/o47Ru47N9SBXZd3hFSl8uQHNL0ucAArhB3kuVnruMhQrmFzZ561UhIB6N eSxOabrR16gMZILdc1p7UJ/+keFw2oUWI0iV+5NzxkTz7lt1IQumXqH8/S4jb2gc+ZaD Nx2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754991148; x=1755595948; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SKnGNYXMSjPueV6Ol7mcG5p22M+mGHYU0ZI4EpyReT0=; b=iy57TR1BIfgHKYgBd2uGlf4KrKfYLxa+rYPLf6jYuhtNzoflYKqZP7uNmeqyuWGXUA bBceUe+SUhHPq8XM3/aiBhgRV3dvIC4ogDm4cI2ZH0Wk8B2DdGFnXIDZJUs5ilyYE5XD qkkX7g45Ecft/CHIwb+rbIiXJ9vgYiQapmvrAIH85a58Lm/fee5wTqNu8zC1EMqR7fwn 0Nves8LJB61o9MlJ/tK6jj32u4IhfgIlbf/C+kto77G7/XqQKenCwcslzeqLE+HRTwMQ oSpScnzsSqaUj4Fpsb0LQsEkKaXEcT0RlrD6Fq2OFGSfl9r38F0lpdW30yANfLQnNxib vgSg== X-Gm-Message-State: AOJu0Yw984F/cQddI1R2PYHoJ+HZ8McHLJJazN0VRv3CtWSgLwr7eq1K uDHuouxFtV+h+oBwIkDmPCidcZ4z4t+K63NYZxJyxo/8NUi2lk5a266X X-Gm-Gg: ASbGnct8hNzU6hvclSC1xJeKcNjewBCQdNwTbmZnumiOzH+89CGsGnY4Ex3e+n87cdZ 0VemF+UF4rl2h2TDOr3pNDsWPVQP0lG17n6oIh1c1757+oCYmuOaQiF7lgrh4oxdXR4u06ILuee ts+UgRis0v+MDUDF1aGAj0ESsY1xblSiTZK0nV3QANkThm0cZBcHnHsSwLrWsdbiaAlHUPNggye fJOvliOyyr95XK4Cet+Fyk9SlaA8K+1tznoS/9hj8jdroVffPRyZm27rQqLWNhxUpiuC5bbJIbI XTrrU8F8B2UyhcdsAKuAOk/W+wdrSL4VoNcLvifC9YDtOUKZqPEZgNo4wnzYXhB4YqiaU/ZY6Ss YBj5pnTRHLY00Q8sbr84TdxqaHTILc/cnOhNR4zkTNFXHPmWcsD/rqnHztZVxuD2jj+/yKYllHe FR3V1xoW5ZV6GiI6vNs5w1/wOhevOtuyxtwyj4Y+Xj2hM4JCvduzgLOQ== X-Google-Smtp-Source: AGHT+IEIsXpzJyMAW5vhUSFTkf07CKn3xNQ7oR/Z3Kpdi+NSZgw/hgwOeyw9FT8vwls7Tk+T0RzBzA== X-Received: by 2002:a05:6000:310f:b0:3b3:9c75:acc6 with SMTP id ffacd0b85a97d-3b91101542cmr2172324f8f.59.1754991147508; Tue, 12 Aug 2025 02:32:27 -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 5b1f17b1804b1-459ebede65asm240691125e9.8.2025.08.12.02.32.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Aug 2025 02:32:27 -0700 (PDT) Date: Tue, 12 Aug 2025 09:32:25 +0000 From: Bertrand Drouvot To: Greg Sabino Mullane Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Adding locks statistics Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Mon, Aug 11, 2025 at 01:54:38PM -0400, Greg Sabino Mullane wrote: > Great idea. +1. Here is a quick overall review to get things started. Thanks for looking at it! > Meta: > patch did not apply via "git apply". Also has carriage returns (e.g. DOS > file), and some errant whitespace. Interesting, I'm not seeing those issues on my side when applying with "git am". > Name: > I think the name would read better as pg_stat_locks, especially as it > returns multiple rows. I considered pg_stat_locks, but chose the singular form to be consistent with pg_stat_database, pg_stat_subscription, and friends. > Docs: seem good. Needs a section on how to reset via > SELECT pg_stat_reset_shared('lock'); That's already included in v1: " @@ -5055,6 +5200,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage pg_stat_io view. + + + lock: Reset all the counters shown in the + pg_stat_lock view. + + " Do you think that this needs any additions or changes? > Also plural better here ('locks') I think having pg_stat_ and XXX being the same option in " pg_stat_reset_shared()" makes sense. It's how it has been done so far, that's why I went with 'lock'. > Code: > > + * Copyright (c) 2021-2025, PostgreSQL Global Development Group > > If a new file, we can simply say 2025? I'm not sure that matters that much, see [1] and we can look at some files added in 2025 for examples: src/backend/storage/aio/aio_io.c src/backend/access/nbtree/nbtpreprocesskeys.c where 2025 is not "alone". > + LWLock locks[LOCKTAG_LAST_TYPE + 1]; > + PgStat_Lock stats; > > Adding a lock to the system that counts locks! :) (just found amusing, not > a comment) ;-) > -#define PGSTAT_KIND_SLRU 11 > -#define PGSTAT_KIND_WAL 12 > +#define PGSTAT_KIND_LOCK 11 > +#define PGSTAT_KIND_SLRU 12 > +#define PGSTAT_KIND_WAL 13 > > Why not just add LOCK as #13? Because it looks like that they are ordered by alphabetical order. > What about the overhead of maintaining this system? I know it's overall > very lightweight, but from my testing, the relation locktype in particular > is very, very busy. The counter increments do call a function generated that way: " #define PGSTAT_COUNT_LOCK_FUNC(stat) \ void \ CppConcat(pgstat_count_lock_,stat)(uint8 locktag_type) \ { \ Assert(locktag_type <= LOCKTAG_LAST_TYPE); \ PendingLockStats.stats[locktag_type].stat++; \ have_lockstats = true; \ pgstat_report_fixed = true; \ } " So, it's pretty lightweight as you said (given that PendingLockStats is not shared and just local to the backend that increments the counter). There could be some contention when the pending stats are flushed but that's the same for all the stats kinds. We could do better, though, and avoid the function calls by creating macros instead of functions. That would mean PendingLockStats to be visible to the outside world but: - that's not the direction that has been taken recently (for example, see the "kept here to avoid exposing PendingBackendStats to the outside world" comment in pgstat_backend.c). - I'm not sure that's worth for this particular case and code paths. That said, if you (and/or others) have strong concerns about performance penalties, I could study this area more in depth. > Busier than I realized until I saw it in this useful > view! Thanks! Did you observe some noticeable performance penalties? [1]: https://www.postgresql.org/message-id/202501211750.xf5j6thdkppy%40alvherre.pgsql Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com