StrategyGetBuffer questions - Mailing list pgsql-hackers

From Merlin Moncure
Subject StrategyGetBuffer questions
Date
Msg-id CAHyXU0x47D4n6EdPyNyadShXQQXKoheLV2cbRgr_2NGrC8KRRQ@mail.gmail.com
Whole thread Raw
Responses Re: StrategyGetBuffer questions  (Jeff Janes <jeff.janes@gmail.com>)
List pgsql-hackers
In this sprawling thread on scaling issues [1], the topic meandered
into StrategyGetBuffer() -- in particular the clock sweep loop.  I'm
wondering:

*) If there shouldn't be a a bound in terms of how many candidate
buffers you're allowed to skip for having a non-zero usage count.
Whenever an unpinned usage_count>0 buffer is found, trycounter is
reset (!) so that the code operates from point of view as it had just
entered the loop.  There is an implicit assumption that this is rare,
but how rare is it?

*) Shouldn't StrategyGetBuffer() bias down usage_count if it finds
itself examining too many unpinned buffers per sweep?

*) Since the purpose of usage_count is to act on advisory basis to
keep recently/frequently accessed buffers from being discarded, is it
really necessary to rigorously guard the count with a spinlock?  If a
++ or -- operation on the value gets missed here or there, how big of
a deal is it really?

Right now the code is going:    LockBufHdr(buf);    if (buf->refcount == 0)    {        if (buf->usage_count > 0)
{            buf->usage_count--;            trycounter = NBuffers;        }        else        {            /* Found a
usablebuffer */            if (strategy != NULL)                AddBufferToRing(strategy, buf);            return buf;
     }    }
 

What if the spinlock was deferred to *after* the initial examination
of the pin refcount.  Naturally, if we decided the buffer was
interesting we'd need to lock the header and retest refcount == 0 to
make sure nobody got to the buffer before us.  In the unlikely event
it was set we'd immediately unlock the header and loop.

That way we get to decrement usage_count and loop without having to
have ever acquired a spinlock on the header.  Point being: this would
tighten the clock sweep loop considerably if it ever happens that
there were a lot of buffers with usage_count > 0 to wade through
before getting a candidate, at the cost of usage_count being 100%
deterministic. Is that harmless though?  If it was, or it could be
reasonably be put in, would this positively impact cases that are
bound by the free list lock?

merlin

http://postgresql.1045698.n5.nabble.com/High-SYS-CPU-need-advise-td5732045.html



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: foreign key locks
Next
From: Merlin Moncure
Date:
Subject: Re: Do we need so many hint bits?