Why BgWriterDelay is fixed?

Started by 高健over 13 years ago5 messagesgeneral
Jump to latest
#1高健
luckyjackgao@gmail.com

In src/backend/postmaster/bgwriter.c , I can find the following source
code(PostgreSQL9.2):

/*
* GUC parameters
*/
int BgWriterDelay = 200;

...
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay /* ms */ );
...
if (rc == WL_TIMEOUT && can_hibernate && prev_hibernate)
{
/* Ask for notification at next buffer allocation */
StrategyNotifyBgWriter(&MyProc->procLatch);
/* Sleep ... */
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay * HIBERNATE_FACTOR);
/* Reset the notification request in case we timed out */
StrategyNotifyBgWriter(NULL);
}

But I also found the following in postgresql.conf:
#bgwriter_delay = 200ms # 10-10000ms between rounds
It is now comment .
But according to the fixed code of BgWriterDelay = 200, even when I update
bgwriter_delay in postgresql.conf to a different value(eg 300ms),
how can it ovewrite the fixed 200ms in bgwriter.c ?

I also want to know, if it is not a bug, then what is the reason?

#2Xiong He
iihero@qq.com
In reply to: 高健 (#1)
Re: Why BgWriterDelay is fixed?

You can check the code in guc.c,&nbsp; search "bgwriter_delay",&nbsp; &amp;BgWriterDelay<br>In the global user configuration, it can change the value of BgWriterDelay.<br>Since the BgWriterDelay declared in bgwriter.h as extern.&nbsp; It can be changed in the global namespace.<br><br><div><div style="color:#909090;font-family:Arial Narrow;font-size:12px">------------------</div><div style="font-size:14px;font-family:Verdana;color:#000;"><div><div style="font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px; ">Thanks&amp;Regards,</div><div style="font-family: 'lucida Grande', Verdana; font-size: 12px; line-height: 18px; ">Xiong He<br><br></div></div></div></div><div>&nbsp;</div><div><includetail><div><br></div><div><br></div><div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------&nbsp;Original&nbsp;------------------</div><div style="font-size: 12px;background:#efefef;padding:8px;"><div><b>From: </b>&nbsp;"高健"&lt;luckyjackgao@gmail.com&gt;;</div><div><b>Date: </b>&nbsp;Mon, Oct 29, 2012 03:17 PM</div><div><b>To: </b>&nbsp;"pgsql-general"&lt;pgsql-general@postgresql.org&gt;; <wbr></div><div></div><div><b>Subject: </b>&nbsp;[GENERAL] Why BgWriterDelay is fixed?</div></div><div><br></div>In&nbsp;src/backend/postmaster/bgwriter.c , I can find the following source code(PostgreSQL9.2):<div><br></div><div><div><br></div><div>/*</div><div>&nbsp;* GUC parameters</div><div>&nbsp;*/</div><div>int<span class="Apple-tab-span" style="white-space:pre"> </span>BgWriterDelay = 200;</div>
</div><div><br></div><div>...</div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>rc = WaitLatch(&amp;MyProc-&gt;procLatch,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> &nbsp; WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> &nbsp; BgWriterDelay /* ms */ );</div></div><div>...</div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if (rc == WL_TIMEOUT &amp;&amp; can_hibernate &amp;&amp; prev_hibernate)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>/* Ask for notification at next buffer allocation */</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>StrategyNotifyBgWriter(&amp;MyProc-&gt;procLatch);</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>/* Sleep ... */</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>rc = WaitLatch(&amp;MyProc-&gt;procLatch,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> &nbsp; WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> &nbsp; BgWriterDelay * HIBERNATE_FACTOR);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>/* Reset the notification request in case we timed out */</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>StrategyNotifyBgWriter(NULL);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div></div><div><br></div><div>But I also found &nbsp;the following in postgresql.conf:</div>
<div><div>#bgwriter_delay = 200ms &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 10-10000ms between rounds</div></div><div>It is now comment .</div><div>But according to the fixed code of &nbsp;BgWriterDelay = 200, even when I update bgwriter_delay in postgresql.conf to a different value(eg 300ms),&nbsp;</div>
<div>how can it ovewrite the fixed &nbsp;200ms in bgwriter.c ?</div><div><br></div><div>I also want to know, &nbsp;if it is not a bug, then what is the reason?</div></includetail></div>

#3Jeff Janes
jeff.janes@gmail.com
In reply to: 高健 (#1)
Re: Why BgWriterDelay is fixed?

On Mon, Oct 29, 2012 at 12:17 AM, 高健 <luckyjackgao@gmail.com> wrote:

In src/backend/postmaster/bgwriter.c , I can find the following source
code(PostgreSQL9.2):

/*
* GUC parameters
*/
int BgWriterDelay = 200;

The value hard coded into the C code is the starting value, or
default. It is not a constant.

Indeed, I don't think that value is even used. I think that upon
start-up, that value gets set to the default listed in the guc.c file
(which is also 200), and so the value listed in the bgwriter.c file is
merely a mnemonic to remind people editing the file what the default
value is.

But according to the fixed code of BgWriterDelay = 200, even when I update
bgwriter_delay in postgresql.conf to a different value(eg 300ms),
how can it ovewrite the fixed 200ms in bgwriter.c ?

Doing so is the job of the "grand unified configuration" machinery, in
src/backend/utils/misc/guc.c

Cheers,

Jeff

In reply to: 高健 (#1)
Re: Why BgWriterDelay is fixed?

On 29 October 2012 07:17, 高健 <luckyjackgao@gmail.com> wrote:

But I also found the following in postgresql.conf:
#bgwriter_delay = 200ms # 10-10000ms between rounds
It is now comment .
But according to the fixed code of BgWriterDelay = 200, even when I update
bgwriter_delay in postgresql.conf to a different value(eg 300ms),
how can it ovewrite the fixed 200ms in bgwriter.c ?

That value is just a default, that we initialise the variable to as a
sort of a reminder. We do this all over the place. Setting
bgwriter_delay downwards will alter the frequency of iterations.

The WaitLatch() thing is purely concerned with keeping the number of
wake-ups low. As long as your database server does not idle, the logic
is effectively exactly the same as what you'll see in 9.1, where the
event loop is much simpler, and merely sleeps exactly bgwriter_delay
ms every iteration.

--
Peter Geoghegan http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jeff Janes (#3)
Re: Why BgWriterDelay is fixed?

Jeff Janes <jeff.janes@gmail.com> writes:

The value hard coded into the C code is the starting value, or
default. It is not a constant.

Indeed, I don't think that value is even used. I think that upon
start-up, that value gets set to the default listed in the guc.c file
(which is also 200), and so the value listed in the bgwriter.c file is
merely a mnemonic to remind people editing the file what the default
value is.

It's partly that, and mostly to ensure that the variable has some valid
value even before the configuration file has been read. The latter
consideration might or might not be important for BgWriterDelay in
particular; but it is important for some GUC variables, so we tend to
follow the coding pattern of initializing GUC variables to their
defaults whenever practical.

regards, tom lane