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 1w7aU9-005TXP-38 for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 14:54:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7aU7-00AqYf-0t for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2026 14:54:27 +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 1w7aU6-00AqYX-3D for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2026 14:54:27 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7aU4-00000002BpR-1doW for pgsql-hackers@postgresql.org; Tue, 31 Mar 2026 14:54:26 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-48700b1ba53so53319335e9.1 for ; Tue, 31 Mar 2026 07:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774968859; x=1775573659; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=lfkrr0NlLhdxVDFr8BVlfO/3ghkHQc2oETdqWZt2KG4=; b=tFF4g25nmyc3iAYabkzG90wUEkmzdPTBqyC27jnfsJV6z76RJClYvNKNVVFfHj5L/8 iF60JEZvhhbpYL8Hxmlp/axOSwT2h7FpzACyRwUkee7el89XjLebCcjHhJDAiOXsN0TA x6HKkDnBom3Iaql4LOhk6pTGcz2VEcpzSxb09lGIFZiE9x7Gz8gGcRgs/SejBmspVlzY uMmlTbPozbrcfaaDxrbgGqJNMbcOEkmStH5Dk+K8aYrJwAfUtO1qF2TDLZmQ8lXPUxQp gMk5eYwWcVjIOGIQeRTPVKY2vtk1YYln/+VgRIgLJvOUjSkTqN2HAdc7wr6QkFoHAHvq 8GRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774968859; x=1775573659; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lfkrr0NlLhdxVDFr8BVlfO/3ghkHQc2oETdqWZt2KG4=; b=UlXeF8Xv8krWGIcYvoFJ7qvIQ1KD1MvUd8BJG7arcgIwvGb6rBUpGgFlvxJZp3ZJf3 3Pe5xY7p0v5Tbww0smxV1WCu7T908a9uSij9RDYx4wYYIwQnEc6c/zv71XkXjL2ca09L x37BCbUhhI2G5NTyfqK8ps3j4660aAYcEaBFJVgqBZGfZ29T9P2z63KJxs2v4qvQj+bP po1gUAWSNjy6rpjdURj9QsmfDu8WsqSkJJdcl/MoY5jBOtUPvFDOj73l/l6nWaf1WtDZ H3nCcLwSmkEG827R73LOhXKRotICLnqZ3bGqKofjjT3vztDoMkYPwK9ruD1iuBPTIsU4 aGIQ== X-Forwarded-Encrypted: i=1; AJvYcCUp+iG3K1NH+Nos8cjjAVFfzcVbFBvahV+pi/ucAN4z3Uze5Uymf6FtW2D11fd1q/dm2umA3Na3XVN1dF/0@postgresql.org X-Gm-Message-State: AOJu0Yybl24meaXSPL53dsZ8kNENuCr9pzmrBcBVszBojS2dlJx38mHz AZBtwr+ML4YjEyelPi+K+7CsJiIIGY8+02nHs/7Kcj5//L1yvWJnzMFa X-Gm-Gg: ATEYQzyD4WAggsI++niNF7+1fPtWa6ZNLhktEdKuTnPZqPVNlguA/oRdT7ANPlmdrPR mNNboHlDHCShPGKg/VhloArJ/ycwOOa0KRhCP8Ma/4Wbusm6QU5dCA1rr4Cpx59f0F3mvyn9uP5 h0mBWv3mCGMH17gaIWVCDVQl671/lkQD9Wt2qYWwXV5m2V7T+/95R67+ayzlGlbqMw5LrhzlJnw Wxvtf+BTiv5b8Uaw+1zL9ZV5//HDj34kj34XJWmKQ8lfLM+WfuGimt/jt892z7zqhRRz+qW9SOa qFiKoIm3KLSqVhYAqysl7Nn2DoysLlOAF0duuGoslloHd3cAHhd8lk6gFDhytX6Tam4HNH8UwL3 UjWlDGpslCctWnmN555YYURIb6hTZdKaMIPwKMzC3ZtoC2RaAipAKLsQC3dtmpBioiCXP7eEJ9p Rmh6j7WvXPMW8V++/QiQNS X-Received: by 2002:a05:600c:8489:b0:485:39b2:a47c with SMTP id 5b1f17b1804b1-4872809fdb3mr286310055e9.25.1774968858488; Tue, 31 Mar 2026 07:54:18 -0700 (PDT) Received: from [172.31.5.233] ([147.161.235.32]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4887e80a616sm70457815e9.2.2026.03.31.07.54.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Mar 2026 07:54:17 -0700 (PDT) Message-ID: <8895dcb7-015c-4f25-9c14-0c44cbc8c0fa@gmail.com> Date: Tue, 31 Mar 2026 16:54:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Add pg_stat_vfdcache view for VFD cache statistics To: KAZAR Ayoub Cc: Jakub Wartak , Pg Hackers , "tomas@vondra.me" References: Content-Language: en-US From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi! On 29.03.2026 21:46, KAZAR Ayoub wrote: >>>>> I've looked at struct vfd and some simple changes to the struct would >>>>> already cut memory consumption in half. I can look into that. >>>>> >>>>> Thoughts? >>>> >>>> Looking forward to this. >>> >>> I try to come up with something the next days. >>>> What also bothers me in that space is if a backend allocates 100K >>> entries >>>> in VFD cache, that cache is never shrank ever again, >>>> the cache only grows (if it needs more than its lifetime maximum) until >>> the >>>> backend dies, although this is useful as entries are reused if free >>> instead >>>> of >>>> allocating entries, whether a spike in files openings effects a long >>> living >>>> backend to keep holding a useless amount of >>>> cache size it will need in the future, i don't imagine this to be common >>>> though, what do you think about this issue from your experience ? >>> >>> Currently the cache is directly mapped by the VFD index. That means we >>> could only resize down to the maximum used VFD index. >>> >>> Being able to resize independently of the maximum VFD index would >>> require changing to a hash map like simplehash.h. I can take a look how >>> invasive such a change would be. That would actually be doable without too much code churn. It would, however, add some more overhead to each cache entry: 1 byte for the simplehash.h status and a 4 byte for the hash, if we want to avoid rehashing on each access. Probably we can get away without storing the hash. We would then have to monitor the cache size and recreate the hash table when the size has shrunk by enough. Another alternative is pallocing vfd entries so that we can freely move them around in the vfd cache array. That would mean an ABI change (File would be an 8-byte pointer instead of a 4-byte integer) but give us much more flexibility for possible improvements. For example, apart from compacting without hash map, this would allow us to allocate variable amounts of memory per entry to, e.g. - store the file name inline of the struct as variable length array (instead of a pstrdup() pointer) and - depending on type allocate more or less memory. This is because non-temporary files don't use the ResOwner and the file size. >> I've implemented the recommended global stats view on vfd cache, the >> implementation should be also straightforward as it follows the same >> cumulative shared statistics infrastructure like pgstat_bgwriter and others >> do. >> >> Attached is v2 patch also contains what David suggested for global cache >> size and entries in the view. I'll review the patch the next days. On quick inspection I saw that you compute the size of a struct vfd by using sizeof(). That works, except for the filename which is stored as a pointer to a pstrdup() piece of memory. I guess you can just say: sizeof(VfdCache[i]) + GetMemoryChunkSpace(VfdCache[i].fileName) -- David Geier