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 1tuf1Z-00AWJA-9I for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Mar 2025 22:03:02 +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 1tuf1U-006QMq-9f for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Mar 2025 22:02:56 +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 1tuf1T-006QMi-Pm for pgsql-hackers@lists.postgresql.org; Tue, 18 Mar 2025 22:02:55 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tuf1Q-003dlM-10 for pgsql-hackers@lists.postgresql.org; Tue, 18 Mar 2025 22:02:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1742335372; bh=BMRvl9rPORnZols5o5L1l2cbDmNqaS9LSd1aWzexsRg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:Message-ID:From; b=ZXNujt9tKD18gPUXKsDtOB14jNEnt1waVyFe+biWVP4xDjs9BeLXamADwEmV598/7 4H0OIPMz72jO6Ie0fn6wHIKMtvE7H91M2w/CLNsKJ8QeVX9QwVDaa4tEUraEZMumd4 TvOT9HstWXkGK48lKrCovw0VeQtDV275z2tk+hFMI4IPptzxHSjaiuQ50Hl0pCAddM k++MGxXoO++hZXoKeRurLJ6TyPphUvRJb0QCJxZ5pxyJltn2zPVB5dSW/sn8l9UYlW tstrKLgpd5eFt2hYbNZ8eexjs4NVCh+wYzJ7IBQrFj9ScNlStP11xfCGY4L3IMRrYS qgH4i3zsZoaqg== Received: from mail.postgrespro.ru (webmail-slave-mstn.l.postgrespro.ru [192.168.2.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.imamov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 3D92C60136; Wed, 19 Mar 2025 01:02:52 +0300 (MSK) MIME-Version: 1.0 Date: Wed, 19 Mar 2025 01:02:52 +0300 From: Aidar Imamov To: Nazir Bilal Yavuz Cc: Andres Freund , PostgreSQL Hackers , koshy44@gmail.com Subject: Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions In-Reply-To: References: Message-ID: <568288111d505a81ebb2a89cea9eacb2@postgrespro.ru> X-Sender: a.imamov@postgrespro.ru Organization: PostgresPro Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/03/18 19:55:00 #27799428 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi! This is my first time trying out as a patch reviewer, and I don't claim to be an expert. I am very interested in the topic of buffer cache and would like to learn more about it. I have reviewed the patches after they were edited and I would like to share my thoughts on some points. pg_buffercache_evict_all(): > for (int buf = 1; buf < NBuffers; buf++) Mb it would be more correct to use <= NBuffers? I also did not fully understand what A. Freund was referring to. However, we cannot avoid blocking the buffer header, as we need to ensure that the buffer is not being pinned by anyone else. Perhaps it would be beneficial to call LockBufHdr() outside and check if BUT_STATE_GET_REFCOUNT == 0 before calling EvictUnpinnedBuffer(). This would help to prevent unnecessary calls to EvictUnpinnedBuffer() itself, ResourceOwnerEnlarge() and ReservePrivateRefCountEntry(). pg_buffercache_mark_dirty_all(): > for (int buf = 1; buf < NBuffers; buf++) Mb it would be more correct to use <= NBuffers? pg_buffercache_evict_relation(): > errmsg("must be superuser to use pg_buffercache_evict function") '_relation' postfix got lost here > /* Open relation. */ > rel = relation_open(relOid, AccessShareLock); If I understand correctly, this function is meant to replicate the behavior of VACUUM FULL, but only for dirty relation pages. In this case, wouldn't it be necessary to obtain an exclusive access lock? > for (int buf = 1; buf < NBuffers; buf++) Mb it would be more correct to use <= NBuffers? > /* > * No need to call UnlockBufHdr() if BufTagMatchesRelFileLocator() > * returns true, EvictUnpinnedBuffer() will take care of it. > */ > buf_state = LockBufHdr(bufHdr); > if (BufTagMatchesRelFileLocator(&bufHdr->tag, &rel->rd_locator)) > { > if (EvictUnpinnedBuffer(buf, buf_state, &flushed)) > buffers_evicted++; > if (flushed) > buffers_flushed++; > } If you have previously acquired the exclusive access lock to the relation, you may want to consider replacing the order of the BufTagMatchesRelFileLocator() and LockBufHdr() calls for improved efficiency (as mentioned in the comment on the BufTagMatchesRelFileLocator() call in the DropRelationBuffers() function in bufmgr.c). And it maybe useful also to add BUT_STATE_GET_REFCOUNT == 0 precheck before calling EvictUnpinnedBuffer()? regards, Aidar Imamov