Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions - Mailing list pgsql-hackers

From Robert Haas
Subject Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions
Date
Msg-id CA+Tgmoa-epr=JDyD4rJYc+CCtP6bZPamJuXxtOe2PBt0EXQ9VA@mail.gmail.com
Whole thread Raw
In response to Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions  (Aidar Imamov <a.imamov@postgrespro.ru>)
Responses Re: Add pg_buffercache_evict_all() and pg_buffercache_mark_dirty[_all]() functions
List pgsql-hackers
On Tue, Mar 18, 2025 at 6:03 PM Aidar Imamov <a.imamov@postgrespro.ru> wrote:
> > for (int buf = 1; buf < NBuffers; buf++)
> Mb it would be more correct to use <= NBuffers?

I agree that (int buf = 1; buf < NBuffers; buf++) isn't right because
that iterates one fewer times than the number of buffers. What was
ultimately committed was:

+   for (int buf = 1; buf <= NBuffers; buf++)
+   {
+       BufferDesc *desc = GetBufferDescriptor(buf - 1);

Curiously, there is no other instance of <= NBuffers in the code.
Elsewhere we instead do:

        for (i = 0; i < NBuffers; i++)
        {
                BufferDesc *bufHdr = GetBufferDescriptor(i);

Or in BufferSync:

        for (buf_id = 0; buf_id < NBuffers; buf_id++)
        {
                BufferDesc *bufHdr = GetBufferDescriptor(buf_id);

Nonetheless what was committed seems pretty defensible, because we
have lots of other places that do GetBufferDescriptor(buffer - 1) and
similar. Alternating between 0-based indexing and 1-based indexing
like this seems rather error-prone somehow. :-(

--
Robert Haas
EDB: http://www.enterprisedb.com



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: pgsql: Add function to get memory context stats for processes
Next
From: Robert Haas
Date:
Subject: Re: Reduce "Var IS [NOT] NULL" quals during constant folding