Updated on 2024-06-03 GMT+08:00

Background Writer

This section describes background writer parameters. The background writer thread is used to write dirty data (new or modified data) in shared buffers to disks. This mechanism ensures that database processes seldom or never need to wait for a write action to occur when handling user queries.

It also mitigates performance deterioration caused by checkpoints because the background writer continues to write dirty pages to disks and only several pages need to be written to disks at each checkpoint. This mechanism, however, increases the overall net I/O load because while a repeatedly-dirtied page may otherwise be written only once per checkpoint interval, the background writer may write it several times as it is dirtied in the same interval. In most cases, continuous light loads are preferred, instead of periodic load peaks. The parameters discussed in this section can be set based on actual requirements.

bgwriter_delay

Parameter description: Specifies the interval at which the background writer writes dirty shared buffers. Each time, the background writer initiates write operations for some dirty buffers. In full checkpoint mode, the bgwriter_lru_maxpages parameter is used to control the amount of data to be written each time, and the process is restarted after the period of hibernation specified by bgwriter_delay (in ms). In incremental checkpoint mode, the number of target idle buffer pages is calculated based on the value of candidate_buf_percent_target. If the number of idle buffer pages is insufficient, a batch of pages are flushed to disks at the interval specified by bgwriter_delay (in ms). The number of flushed pages is calculated based on the target difference percentage. The maximum number of flushed pages is limited by max_io_capacity.

In many systems, the effective resolution of the sleep time is 10 milliseconds. Therefore, setting this parameter to a value that is not a multiple of 10 has the same effect as setting it to a value multiple of 10.

Parameter type: integer.

Unit: millisecond

Value range: 10 to 10000

Default value: 2s (that is, 2000 ms)

Setting method: This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Setting suggestion:

Incremental checkpoint mode: If the data volume is multiple times the value of shared_buffers, the value of bgwriter_delay cannot be greater than 2s. If the data volume is smaller than the value of shared_buffers, the value of bgwriter_delay can be increased to save I/O resources.

Full checkpoint mode: Retain the default value. If the disk capability is poor, increase the value.

candidate_buf_percent_target

Parameter description: Specifies the expected percentage of available buffers in the candidate buffer chain to shared_buffer when incremental checkpoints are enabled. If the actual percentage is less than the value of this parameter, the background writer thread starts to flush dirty pages that meet the conditions to disks.

Parameter type: double-precision floating point

Unit: none

Value range: 0.1 to 0.85

Default value: 0.3

Setting method: This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Setting suggestion: This parameter directly affects the number of pages flushed by the background writer thread. If the data volume is greater than the value of shared_buffers and the parameter is set to an excessively small value, the performance is affected. If the data volume is twice or more of the value of shared_buffers, the value of this parameter cannot be smaller than the default value. In other scenarios, to reduce I/Os, set this parameter to a smaller value.

bgwriter_lru_maxpages

Parameter description: Specifies the number of dirty buffers the background writer can write in each round.

This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Value range: an integer ranging from 0 to 1000

When this parameter is set to 0, the background writer is disabled. This setting does not affect checkpoints.

Default value: 100

bgwriter_lru_multiplier

Parameter description: Specifies the coefficient used to estimate the number of dirty buffers the background writer can write in the next round.

The number of dirty buffers written in each round depends on the number of buffers used by server processes during recent rounds. The estimated number of buffers required in the next round is calculated using the following formula: Average number of recently used buffers x bgwriter_lru_multiplier. The background writer writes dirty buffers until sufficient, clean, and reusable buffers are available. The number of buffers the background writer writes in each round is always equal to or less than the value of bgwriter_lru_maxpages.

Therefore, the value 1.0 of bgwriter_lru_multiplier represents a just-in-time policy of writing exactly the number of dirty buffers predicted to be required. Larger values provide some cushion against spikes in demand, whereas smaller values intentionally leave more writes to be done by server processes.

Smaller values of bgwriter_lru_maxpages and bgwriter_lru_multiplier reduce the extra I/O load caused by the background writer, but make it more likely that server processes will have to issue writes for themselves, delaying response to queries.

This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Value range: a floating-point number ranging from 0 to 10

Default value: 2

pagewriter_thread_num

Parameter description: Specifies the number of threads for background page flushing after the incremental checkpoint is enabled. Dirty pages are flushed in sequence to disks, updating recovery points.

This is a POSTMASTER parameter. Set it based on instructions provided in Table 1.

Value range: an integer ranging from 1 to 16

Default value: 4

dirty_page_percent_max

Parameter description: Specifies the percentage of dirty pages to shared_buffers after the incremental checkpoint is enabled. When the value of this parameter is reached, the background page flush thread flushes dirty pages based on the maximum value of max_io_capacity.

Parameter type: floating-point.

Unit: none

Value range: 0.1 to 1

Default value: 0.9

Setting method: This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

pagewriter_sleep

Parameter description: Specifies the interval for the page writer thread to flush dirty pages to disks after the incremental checkpoint is enabled. When the ratio of dirty pages to shared_buffers reaches dirty_page_percent_max, the number of pages in each batch is calculated based on the value of max_io_capacity. In other cases, the number of pages in each batch decreases proportionally.

Parameter type: integer.

Unit: millisecond

Value range: 0 to 3600000

Default value: 2000ms (that is, 2s)

Setting method: This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Setting suggestion: Set this parameter to the default value for CNs. Do not set this parameter to a value greater than 2s for DNs. If dirty pages are generated quickly, you are advised to set this parameter to a value ranging from 100 ms to 500 ms. If this parameter is set to a large value, redo points are updated slowly, affecting Xlog recycling.

max_io_capacity

Parameter description: Specifies the maximum I/O per second for the background writer to flush pages in batches.

Parameter type: integer.

Unit: kB

Value range: 30720 to 10485760

Default value: 512000kB (that is, 500 MB)

Setting method: This is a SIGHUP parameter. Set it based on instructions provided in Table 1. If the value of the parameter does not contain a unit, the default unit is kB. The unit can also be MB or GB.

Parameter description: Set this parameter based on the service scenario and the disk I/O capability. If the RTO is short or the data volume is many times larger than the shared memory and the service access data volume is random, the value of this parameter cannot be too small. A small value of max_io_capacity reduces the number of pages flushed by the background writer. If a large number of pages are eliminated due to service triggering, the services are affected.

enable_consider_usecount

Parameter description: Specifies whether the backend thread considers the page popularity during page replacement. You are advised to enable this parameter in large-capacity scenarios.

This is a SIGHUP parameter. Set it based on instructions provided in Table 1.

Value range: Boolean

  • on/true: The page popularity is considered.
  • off/false: The page popularity is not considered.

Default value: off

dw_file_num

Parameter description: Specifies the number of doublewrite files to be written in batches. The value is related to pagewriter_thread_num and cannot be greater than that of pagewriter_thread_num. If the value is too large, it will be corrected to the value of pagewriter_thread_num.

This is a POSTMASTER parameter. Set it based on instructions provided in Table 1.

Value range: an integer ranging from 1 to 16

Default value: 1

dw_file_size

Parameter description: Specifies the size of each doublewrite file.

This is a POSTMASTER parameter. Set it based on instructions provided in Table 1.

Value range: an integer, in the range [32,256]

Default value: 256