Re: calculating an aspect of shared buffer state from a background worker

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Berry <berrydigital(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: calculating an aspect of shared buffer state from a background worker
Date: 2014-03-10 14:33:25
Message-ID: 32356.1394462005@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Robert Berry <berrydigital(at)gmail(dot)com> writes:
> I'm looking at doing a calculation to determine the number of free buffers
> available. A n example ratio that is based on some data structures in
> freelist.c as follows:

> (StrategyControl->lastFreeBuffer - StrategyControl->firstFreeBuffer) /
> (double) NBuffers

> Is there a way to get access to the StrategyControl pointer in the context
> of a background worker?

The BufferStrategyControl struct is in shared memory, so you can certainly
get at it. One way would be to modify freelist.c to export its static
pointer variable. Alternatively, you could call ShmemInitStruct an extra
time to look up the struct for yourself, and then save it in your own
static variable.

Having said that, though, I'm pretty dubious of the premise. I trust you
realize that the above calculation is entirely wrong; firstFreeBuffer and
lastFreeBuffer are list head and tail pointers, and have no numerical
relation to the list length. The only way to determine the list length
accurately would be to chase down the whole list, which you'd have to hold
the BufFreelistLock while doing, which'd be disastrous for performance if
the list was long. (If you're okay with modifying the backend code you
could dodge this by teaching freelist.c to maintain a counter, I guess.)

An even bigger issue is that it's not clear that the length of the free
list is actually a useful number to have; in steady-state usage it
frequently is always zero. Buffers only get put back on the freelist if
they're invalidated, eg by dropping the relation they belonged to. Normal
usage tends to allocate buffers by reclaiming ones whose usage_count has
reached zero in the clock sweep algorithm. So a better picture of the
availability of buffers would require scanning the buffer pool to see how
many there are of each usage_count level.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2014-03-10 14:45:50 Re: Little confusing things about client_min_messages.
Previous Message Amit Kapila 2014-03-10 14:31:20 Re: Performance Improvement by reducing WAL for Update Operation