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 1twh5Q-00GgyH-8J for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Mar 2025 12:39:24 +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 1twh5O-002l5I-V6 for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Mar 2025 12:39:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1twh5O-002l5A-LJ for pgsql-hackers@lists.postgresql.org; Mon, 24 Mar 2025 12:39:22 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1twh5N-000r3y-0X for pgsql-hackers@lists.postgresql.org; Mon, 24 Mar 2025 12:39:21 +0000 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-301302a328bso8092487a91.2 for ; Mon, 24 Mar 2025 05:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742819960; x=1743424760; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=2N8eB6+xwsAn+bxwIXOS+BddlUV3Cz0JPYda11nS+4E=; b=FAmcdiegBPgWWSoEW+LpZRr7tMz6+YOmlZe6kM1J+fi/3xOpl2ke+aO1VLvJWAAmat fWgrLdeIsQWjN7CmAu2p8k9Lqy8C68hwzryW/JAOJtxTYhm/EfBmquUt+0b6QRwzvby3 gOaSqgmLE5AosvVt8xupe+IiO80VGT51BizR0rynaLBfWpXcxoP5b+LxtNz44jzYK6cD bJhMhnzoWgf1DmE26szc1l0HgnInKuJ4RIGe4PXLfsxBW6HrerqWQbYtXlc8PIgU5iTk aziGK6vp+3W0kI9sGRDD8SYUOdMB++9i+/HNGU24Tj1pomebOCL7oQgdNh194qhIYLjE P5+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742819960; x=1743424760; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2N8eB6+xwsAn+bxwIXOS+BddlUV3Cz0JPYda11nS+4E=; b=Nmk29J9IW/+EUhGwanwj5ICTwcdBwe4XNHC6ak994YXaiBmKwYkZSWIfrmvtwLL9Ie q7tF1EbyCumau4V5dW72j8B79ciwVfvcXKUgS5Ox9+/Z0Jnt2jF79rGbV6ML91e4xIgw SCsRQZMzx7Gp6Y9xUHg1rx4PKBKEKUomYXg/RdZ4wHh4+EqidUweCflcj1r62dCg7rQT O62003XBic7i+5Y8KsWhffkeZDfTWpOiydIPcE1LPlt/hWItv75gCWKBperkItWMNBmC Yaff0LbCRC4+rWadUPn+Wvn6hBiLznLkWXuelIrv+FsTNhFZ874C1P7Zg7Gl7/SNkysx Q8hQ== X-Forwarded-Encrypted: i=1; AJvYcCXTfQrwRVSQ7e+MTq9C+1W1/PysgR/LMl47WSEo+Wjn5QM5kUmLbOKuPYf7Usjq84ShuFOLmvSEWBMjEfXJ@lists.postgresql.org X-Gm-Message-State: AOJu0Yxb9SNy371QxfyjjhgQ7lwKebSP0Qh6AJ6rlqbcA1BwFBH6++YA qigED5X9/j7bkPClrbgy08chS78gDDPJcBNR4m37Ms8WDqY7Z7lnt2gdvOcS7rhUc4ZQKlb+O63 3MdjzA/6VXZEqWVA6ybC88EWrSUQ= X-Gm-Gg: ASbGncvyCqkDg8hR1OgRRlLF3F631FcPaGznU8SUsrPUtJs5ASvFqJ0dc5UBuHkIKLu I7PWNjGI1NP7WU8C9jb8lQbw57dZXkSOgvkxls97YkEj3lKwKMaxed93ctbDF43vs2xiPFrm5uv YhVw1nzaiF/dQuxONAoCnpPA8L34o= X-Google-Smtp-Source: AGHT+IEFNRI9zn4Xri3CeHsu/bjXl+BcXobguKNaQ2pJuqAh8Fxozqk/yOCIoqjj5jra2XJssigqCayVUg33O3iVQi0= X-Received: by 2002:a17:90b:1811:b0:2fe:6942:3710 with SMTP id 98e67ed59e1d1-3030fe5a697mr16882938a91.3.1742819959933; Mon, 24 Mar 2025 05:39:19 -0700 (PDT) MIME-Version: 1.0 References: <568288111d505a81ebb2a89cea9eacb2@postgrespro.ru> In-Reply-To: <568288111d505a81ebb2a89cea9eacb2@postgrespro.ru> From: Nazir Bilal Yavuz Date: Mon, 24 Mar 2025 15:39:08 +0300 X-Gm-Features: AQ5f1JpSP1C5RD3PDWhXrHMgsJ7cSZamY35CxZaQhC0MqPmU987Wk7nxMx9Lc0w Message-ID: Subject: Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions To: Aidar Imamov Cc: Andres Freund , PostgreSQL Hackers , koshy44@gmail.com Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Wed, 19 Mar 2025 at 01:02, Aidar Imamov wrote: > > 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. Thank you so much for the review! > pg_buffercache_evict_all(): > > for (int buf = 1; buf < NBuffers; buf++) > Mb it would be more correct to use <= NBuffers? Yes, done. > 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(). I had an off-list talk with Andres and learned that ResourceOwnerEnlarge() and ReservePrivateRefCountEntry() need to be called before acquiring the buffer header lock. I introduced the EvictUnpinnedBuffersFromSharedRelation() function for that [1]. > pg_buffercache_mark_dirty_all(): > > for (int buf = 1; buf < NBuffers; buf++) > Mb it would be more correct to use <= NBuffers? Done. > pg_buffercache_evict_relation(): > > errmsg("must be superuser to use pg_buffercache_evict function") > '_relation' postfix got lost here Done. > > /* 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? I think VACUUM FULL does more things but I agree with you, obtaining an exclusive access lock is the correct way. > > for (int buf = 1; buf < NBuffers; buf++) > Mb it would be more correct to use <= NBuffers? Done. > > > /* > > * 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). I think you are right, we are basically copying FlushRelationBuffers() to the contrib/pg_buffer_cache. Done. > And it maybe useful also to add BUT_STATE_GET_REFCOUNT == 0 precheck > before calling > EvictUnpinnedBuffer()? I think we do not need to do this. I see EvictUnpinnedBuffer() as a central place that checks all conditions. If we add this pre-check then we should not do the same check in the EvictUnpinnedBuffer(), creating this logic would add unnecessary complexity to EvictUnpinnedBuffer(). I addressed these reviews in v3. I will share the patches in my next reply. -- Regards, Nazir Bilal Yavuz Microsoft