1

As per my understanding

  1. checkpoint write all dirty buffer(data) periodically into disk and
  2. background writer writes some specific dirty buffer(data) into disk

It looks both do almost same work.

But what are the specific dirty buffer(data) writes into disk? How frequently checkpoint and bgwriter it is calling?

I want to know what are the difference between them.

Thanks in advance

  • 1
    Like most things this information is found in the docs [Background writer](https://www.postgresql.org/docs/current/runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-BACKGROUND-WRITER) – Adrian Klaver Mar 19 '22 at 02:14
  • If you want the full details see the source at `~/backend/postmaster/bgwriter.c `. – Adrian Klaver Mar 19 '22 at 02:20
  • 1
    I go through that document but What are the relation between checkpointer and bgwriter? Within checkpoint when bgwriter run? – Sheikh Wasiu Al Hasib Mar 20 '22 at 06:43
  • 1
    Take a look at these two posts: [Writer and WAL](https://www.cybertec-postgresql.com/en/postgresql-writer-and-wal-writer-processes-explained/) and [What is a checkpoint](https://www.cybertec-postgresql.com/en/postgresql-writer-and-wal-writer-processes-explained/). Basically the background writer runs between checkpoints to off load some of the work from the checkpoint process. – Adrian Klaver Mar 20 '22 at 15:29

1 Answers1

0

It looks both do almost same work.

Looking at the source code link given by Adrian, you can see these words in the comments for the background writer:

As of Postgres 9.2 the bgwriter no longer handles checkpoints.

...which means in the past, the background writer and checkpointer tasks were handled by one component, which explains the similarity that probably led you to ask this question. The two components were split on 1/Nov/2011 in this commit and you can learn more about the checkpointer here.

From my own understanding, they are doing the same task from different perspectives. The task is making sure we use a limited amount of resources:

  • For the background writer, that resource is RAM and it writes dirty buffers to disk so the buffers can be reused to store other data hence limiting the amount of RAM required.
  • For the checkpointer, that resource is DISK and it writes all dirty buffers to disk so it can add a checkpoint record to the WAL, which allows all segments of the WAL prior to that record to be removed/recycled hence limiting the amount of DISK required to store the WAL files. You can confirm this in the docs which say ...after a checkpoint, log segments preceding the one containing the redo record are no longer needed and can be recycled or removed.

It may be helpful to read more about the WAL (Write-Ahead Log) in general.

Eric Mutta
  • 1,144
  • 1
  • 11
  • 15