client backend VS background writer

Viewed 31

I know the difference between background writer, checkpointer, walwriter, and parallel_worker:

But I am confused with client backend. I use opensnoop and trace to find that client backend usually writes (pwrite(2)) something to both data-file and wal-file.

PID                     COMM               FD ERR PATH
18365 (client backend)  postgres           51   0 base/16384/16403
18365 (client backend)  postgres           52   0 pg_wal/0000000100000001000000E1

Question

  1. What is client backend responsible for?
  2. At most one client backend per connection?
1 Answers

The client backend is the server process that communicates with the client and performs SQL statements on the client's behalf. There is one client backend process for each database session.

However, the client backend process normally should not have to write to the data file: the backend writes table data only to shared buffers (the cache), and the "checkpointer" and "background writer" process do most of the actual writing to disk. The design idea here is that the client backend should not be bothered with these potentially slow tasks, but be free to process SQL statements as quickly as possible. WAL is written by the backend process when a transaction is committed, otherwise the WAL writer process takes care of that. So it is normal that you see these writes by a backend.

If the background writer is not fast enough to cope with the workload, it can happen that the backend process has to do some of the writing. This should not happen regularly, but only during spikes in data modification activity. Often, that is an indication that you can improve the performance of your SQL statements if you increase the size of shared buffers.

Related