Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qJCe6-0006kA-22 for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jul 2023 12:39:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qJCe4-0004A2-UG for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jul 2023 12:39:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qJCe4-00049t-Gz for pgsql-hackers@lists.postgresql.org; Tue, 11 Jul 2023 12:39:08 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qJCe1-003GN4-U1 for pgsql-hackers@postgresql.org; Tue, 11 Jul 2023 12:39:07 +0000 Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-3a3fbfb616dso1534683b6e.3 for ; Tue, 11 Jul 2023 05:39:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689079145; x=1691671145; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UuyKZt2yLb/zX7W/IoJzbts+OtFEc2f8Hc9NF/QbRho=; b=h2WCMxf+jX12s37gv+5VH/rOUQC7S4P5xiIz2qkgzNbM+mdSce18wrvF/cpwvCI4eE TyCYmW+bFeRkgU1qjIn8AWVhzivqOyFdoRnXRmWVdNnnPiVHaELX+YoFP2FlbnKwyzEa pSJgr+cD6OQL0EDf6KVfaX1zHbwIQDovEpLKzTi/h6zEBd8+S+7jN1qCFt7wZoOdU4Qi qajWkuCjb3uc3ODxT20cL6EMPT1z9Av9WvpZLVev/TWvyhg6fhYRpUjAc1JtCfDyyFeK 57yZRLHe1Q2th/Ment0nbDzjtnu2T+vNp96ml0TK99jYbm6RZ5w0iP7vKmg9tF26lpO5 tQhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689079145; x=1691671145; h=content-transfer-encoding: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=UuyKZt2yLb/zX7W/IoJzbts+OtFEc2f8Hc9NF/QbRho=; b=M72x3Ie2Q2v2XAa2JARRbkgNxNEHqJBgq6+PsoPwje45k/ZQSvijKTNkMxY6NOQA2l GVChNa171rO4rtj1zo/pPkbOWaJzB5jQOsDlqeX7ruhXSiEbwqcbez1SNxsbXSDF7PAP PqTae45O2k6P4Xcdxx2Swev7DLu0+RZRQmmgezKIc4ipWJDsWo+Nfnxs4Y6lZ6pp5gEy 6L94bP6by97kGhAn34Tlxfml5wxFcuADV4PnWmDa7g0eOp5vkwBGYCV6FPXX69w78Np0 1uA0dXwmFOfzRGqFhqsJoUijpzWVCG5aKD/xO/IGYKNA7Em7FJfk8T+IjL3D4KqYgX9s 9+og== X-Gm-Message-State: ABy/qLarY4N4Bmhc1Oj9Pny3/pnSiDLbZpgkSF7QQANo5+dDjKOWa+83 T1aKuMDJAuspQvulhdT819rNe6bCktLmbqiydm0= X-Google-Smtp-Source: APBJJlEzGGI9xSofHDeiXfpEM5Dwf+Rz0YMEDBgSCRpG2GCZtrJpAvSv4iAvt2OEGWF20FzFf6ZIHFLdycLulR2XkIY= X-Received: by 2002:a05:6808:1aa8:b0:3a3:7db0:3a46 with SMTP id bm40-20020a0568081aa800b003a37db03a46mr13345990oib.7.1689079145285; Tue, 11 Jul 2023 05:39:05 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Palak Chaturvedi Date: Tue, 11 Jul 2023 18:08:56 +0530 Message-ID: Subject: Re: Extension Enhancement: Buffer Invalidation in pg_buffercache To: Nitin Jadhav , pgsql-hackers@postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hey Nitin, >Will >there be a scenario where the buffer is dirty and its reference count >is zero? There might be a buffer that has been dirtied but is not pinned or being used currently by a process. So checking the refcount and then dirty buffers helps. >First, The TryInvalidateBuffer() tries to flush the buffer if it is dirty and then tries to invalidate it if it meets the requirement. Instead of directly doing this can we provide an option to the caller to mention whether to invalidate the dirty buffers or not. Yes that can be implemented with a default value of force. Will implement it in the next patch. On Wed, 5 Jul 2023 at 17:53, Nitin Jadhav w= rote: > > +1 for the idea. It's going to be more useful to test and understand > the buffer management of PostgreSQL and it can be used to explicitly > free up the buffers if there are any such requirements. > > I had a quick look over the patch. Following are the comments. > > First, The TryInvalidateBuffer() tries to flush the buffer if it is > dirty and then tries to invalidate it if it meets the requirement. > Instead of directly doing this can we provide an option to the caller > to mention whether to invalidate the dirty buffers or not. For > example, TryInvalidateBuffer(Buffer bufnum, bool force), if the force > is set to FALSE, then ignore invalidating dirty buffers. Otherwise, > flush the dirty buffer and try to invalidate. > > Second, In TryInvalidateBuffer(), it first checks if the reference > count is greater than zero and then checks for dirty buffers. Will > there be a scenario where the buffer is dirty and its reference count > is zero? Can you please provide more information on this or adjust the > code accordingly. > > > +/* > > +Try Invalidating a buffer using bufnum. > > +If the buffer is invalid, the function returns false. > > +The function checks for dirty buffer and flushes the dirty buffer befo= re invalidating. > > +If the buffer is still dirty it returns false. > > +*/ > > +bool > > The star(*) and space are missing here. Please refer to the style of > function comments and change accordingly. > > Thanks & Regards, > Nitin Jadhav > > On Fri, Jun 30, 2023 at 4:17=E2=80=AFPM Palak Chaturvedi > wrote: > > > > I hope this email finds you well. I am excited to share that I have > > extended the functionality of the `pg_buffercache` extension by > > implementing buffer invalidation capability, as requested by some > > PostgreSQL contributors for improved testing scenarios. > > > > This marks my first time submitting a patch to pgsql-hackers, and I am > > eager to receive your expert feedback on the changes made. Your > > insights are invaluable, and any review or comments you provide will > > be greatly appreciated. > > > > The primary objective of this enhancement is to enable explicit buffer > > invalidation within the `pg_buffercache` extension. By doing so, we > > can simulate scenarios where buffers are invalidated and observe the > > resulting behavior in PostgreSQL. > > > > As part of this patch, a new function or mechanism has been introduced > > to facilitate buffer invalidation. I would like to hear your thoughts > > on whether this approach provides a good user interface for this > > functionality. Additionally, I seek your evaluation of the buffer > > locking protocol employed in the extension to ensure its correctness > > and efficiency. > > > > Please note that I plan to add comprehensive documentation once the > > details of this enhancement are agreed upon. This documentation will > > serve as a valuable resource for users and contributors alike. I > > believe that your expertise will help uncover any potential issues and > > opportunities for further improvement. > > > > I have attached the patch file to this email for your convenience. > > Your valuable time and consideration in reviewing this extension are > > sincerely appreciated. > > > > Thank you for your continued support and guidance. I am looking > > forward to your feedback and collaboration in enhancing the PostgreSQL > > ecosystem. > > > > The working of the extension: > > > > 1. Creating the extension pg_buffercache and then call select query on > > a table and note the buffer to be cleared. > > pgbench=3D# create extension pg_buffercache; > > CREATE EXTENSION > > pgbench=3D# select count(*) from pgbench_accounts; > > count > > -------- > > 100000 > > (1 row) > > > > pgbench=3D# SELECT * > > FROM pg_buffercache > > WHERE relfilenode =3D pg_relation_filenode('pgbench_accounts'::regclass= ); > > bufferid | relfilenode | reltablespace | reldatabase | relforknumber > > | relblocknumber | isdirty | usagecount | pinning_backends > > ----------+-------------+---------------+-------------+---------------+= ----------------+---------+------------+------------------ > > 233 | 16397 | 1663 | 16384 | 0 > > | 0 | f | 1 | 0 > > 234 | 16397 | 1663 | 16384 | 0 > > | 1 | f | 1 | 0 > > 235 | 16397 | 1663 | 16384 | 0 > > | 2 | f | 1 | 0 > > 236 | 16397 | 1663 | 16384 | 0 > > | 3 | f | 1 | 0 > > 237 | 16397 | 1663 | 16384 | 0 > > | 4 | f | 1 | 0 > > > > > > 2. Clearing a single buffer by entering the bufferid. > > pgbench=3D# SELECT count(*) > > FROM pg_buffercache > > WHERE relfilenode =3D pg_relation_filenode('pgbench_accounts'::regclass= ); > > count > > ------- > > 1660 > > (1 row) > > > > pgbench=3D# select pg_buffercache_invalidate(233); > > pg_buffercache_invalidate > > --------------------------- > > t > > (1 row) > > > > pgbench=3D# SELECT count(*) > > FROM pg_buffercache > > WHERE relfilenode =3D pg_relation_filenode('pgbench_accounts'::regclass= ); > > count > > ------- > > 1659 > > (1 row) > > > > 3. Clearing the entire buffer for a relation using the function. > > pgbench=3D# SELECT count(*) > > FROM pg_buffercache > > WHERE relfilenode =3D pg_relation_filenode('pgbench_accounts'::regclass= ); > > count > > ------- > > 1659 > > (1 row) > > > > pgbench=3D# select count(pg_buffercache_invalidate(bufferid)) from > > pg_buffercache where relfilenode =3D > > pg_relation_filenode('pgbench_accounts'::regclass); > > count > > ------- > > 1659 > > (1 row) > > > > pgbench=3D# SELECT count(*) > > FROM pg_buffercache > > WHERE relfilenode =3D pg_relation_filenode('pgbench_accounts'::regclass= ); > > count > > ------- > > 0 > > (1 row) > > > > > > Best regards, > > Palak