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 1wtRFl-001FsC-0E for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Aug 2026 14:45:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wtRFk-00HZLK-1L for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Aug 2026 14:45:23 +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 1wtRFk-00HZLB-0R for pgsql-hackers@lists.postgresql.org; Mon, 10 Aug 2026 14:45:23 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wtRFg-00000001MiK-45Aa for pgsql-hackers@lists.postgresql.org; Mon, 10 Aug 2026 14:45:23 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4996f1ee4a4so8600735e9.2 for ; Mon, 10 Aug 2026 07:45:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786373120; x=1786977920; darn=lists.postgresql.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=caI9zVlucRWu7b0sVDsMtgl2cLZ8o4iMESz6/ivWIw0=; b=o5XAAUeLXpBGLYvjc1WC4GwKqa3UF5/G7E5IYdfgAykkxA5flO041b5XIUgTSTbNrE B+WTFJQt18+ZN65rp9v1l1yQsp2ABjGw0Jmm6J8VvEZRZheuDKHCgdurfs25IsVcWF+s qu+dwhw5uedAt/J3fzkTQXzpBHU7yQ8XwDI+Y+Vlm6TgYhOb7lORLUwZIf+dMjNMC2kJ AM2ei8qWQZfZH64r1LDn12Xb83tii5NLM2/7EBU99aLEGnRdOsjxNGd2nJjeZXkJ+dPp 8qDUqU2vNgIfXFNVPNPvIDJFXbFO2eY4zBLSXr2bUNRLMLKHw59ZTVdXGy961ChQB5HV zBRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786373120; x=1786977920; h=in-reply-to:content-disposition:content-type: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 :content-type; bh=caI9zVlucRWu7b0sVDsMtgl2cLZ8o4iMESz6/ivWIw0=; b=YRVmhSOA+ZpQDlpHEsdQabkAGOzTIhNu6b7rNY2wiHCF+AhQ7TSxaRzrSQRIPQoAAQ VvQt5zx+fD1u4Uzsm9iY5v4Ozg8FtupiApSgSN/I6NSpmFSNLbIGmGEy+FlGZp/V3M1T DnFKZrNpG0n7LEyxOGvXdU4Ikv64zOebpOyF79vW6LahY4/Pjyj41hlthGpzUf4GHH8p CTmOX349kFCbDpJ+bAFQ3lsfNymG831tIjcpaGNILQe4kWcBrEYKFWlXLBflzTHBnkgY +3J9ST6J7GUa40osYHEdONqyrROpF/L1jK23/pHNJFO+uTuBVFlIkHu1rgXuuW2cKU/U dcdQ== X-Gm-Message-State: AOJu0Yxm2UNQyh33sbejJh12mKu92a3iZnryKgqvR9STQO22Vy+X0lTJ 4Ak9ul4Z3BKMBl80PkPJ5XPsZ1OwVRHifcgG3y8cRQSkL6xLeKxs5Ry0 X-Gm-Gg: AR+sD12huEspYpWayh8CdWbTLVEaxHhkxoUjY1cBDSsNgwRrLpIe2H3NP4p6ajO2qcN gjyxBKUKbXEdliXU3/NCNTEF0Dm95xgoUOZVzNgvn7b8EkSMnYXObrrrXujowFhB/R4+M7wu07v GZ961DmCXAVd3xArd1Faw5GQMKGOL8NNUEFH98awJGijAMCNT3mAfKZf5+YsBKlo7qZAI8bIZtm /vl+nV61PgxrAXlLZ57LgO1i4YMDNM7VGh5NRX5IV0AopTRL5fUScap1P8Y4zAohdIQdxsY4Bb9 ghqO5cIaoaA9KKn2uwzXiDzBRa90i+7vFvcBr80jW5F9Kqh2uKCxGtLWHifjyT3psKIkLKRXGc7 1buTVNgfW/+JPsJEuSJ8XZRp3yLbH0LLoqv9D1gH6NqRwSpepaixqpP7kZ4AZjsJV+60nrJX2o2 hABNlOLh4+54F/598Kc7w8f43Gc9938nP7VjmVmX9lw9kSG+U7UfYjJ89UhzJXlm8yiiUj4HnyY /pBQFBkSkHFO+EW+FVooWmxtqbQIbhRdx3Dc2Zv805DeX4K X-Received: by 2002:a05:600c:3585:b0:493:e451:a9e1 with SMTP id 5b1f17b1804b1-49972740d07mr26928855e9.2.1786373119357; Mon, 10 Aug 2026 07:45:19 -0700 (PDT) Received: from bdtpg (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4995e9e7171sm299535005e9.1.2026.08.10.07.45.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2026 07:45:18 -0700 (PDT) Date: Mon, 10 Aug 2026 14:45:16 +0000 From: Bertrand Drouvot To: Michael Paquier Cc: pgsql-hackers@lists.postgresql.org, Andres Freund , Sami Imseih Subject: Re: Redesign per-backend 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 10, 2026 at 01:38:31PM +0900, Michael Paquier wrote: > Accessing an array indexed by procnumber should be slightly cheaper > than a hash lookup when grabbing the stats of an individual backend, > as this is just a BackendPidGetProc() -> GetNumberFromPGProc() to get > a location. Right, an array would make fetching an individual backend slightly cheaper. My point was only that routine flushes use cached entry pointers and therefore avoid hash lookups. > I can see that: > > +pgstat_per_backend_snapshot(PgStat_Kind kind, dshash_table *hash, void *snap) > [...] > + while ((entry = dshash_seq_next(&hstat)) != NULL) > + { > + LWLockAcquire(&entry->lock, LW_SHARED); > > That's a sequential scan combined with potentially hundreds of LWLocks > acquired and released successivelly. That looks expensive here for a > single IO/lock/WAL data scan. That's the level of locking required > because a mutex cannot be hold while doing external calls, and here we > have one per_backend_acc_cb callback and one > pgstat_cache_per_backend_entry(). Not sure I like much this costly > locking level. I'm concerned by this cost. Yeah, I benchmarked this against unpatched master (-O2 and assertions disabled). Each sessions generated and flushed WAL/IO statistics, then remained connected and idle. Then queried pg_stat_wal, pg_stat_lock and pg_stat_io: Mean latency in ms: 400 backends 10000 backends master v1 master v1 WAL 0.017 0.029 0.017 0.354 Lock 0.018 0.033 0.018 0.575 IO 0.069 0.143 0.069 2.847 Those are warmed, continuously repeated queries. Now the impact: - the extra timing is only when querying the corresponding global view - the shared entry lock conflicts only with exclusive operations on the same backend's entry for that statistics kind. The usual statistics flush uses LWLockConditionalAcquire(), so it leaves counters pending rather than waiting. Forced flushes, resets, and backend exit processing may wait, but other backends can continue flushing their own entries. FWIW, this kind of scan is not new: - pg_locks walks the PGPROC slots and takes each live process's fpInfoLock in shared mode. - pg_stat_activity also performs a scan, although it uses a lockless changecount and retry protocol rather than taking one LWLock per backend. - the current statistics implementation with stats_fetch_consistency = snapshot also scans the shared statistics dshash and takes each entry's content LWLock in shared mode while copying it. For comparison, select count(*) FROM pg_stat_activity took about 57 ms and select count(*) FROM pg_locks took about 4ms, both with the same 10000 connections. Given that the cost is still sub millisecond at hundreds of connections and a few milliseconds at 10000 and given the impact mentioned above, I don't think this is a practical blocker though. What do you think? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com