Case study : PostgreSQL Kernel Parameters

In this post, we are going to learn about the operating system kernel parameters, shared memory, and semaphores.

But why?

As we all know, PostgreSQL highly interacts with the operating system parameters for the operations that it does on the database.

Understanding the operating system resource limits is one of the important things for a PostgreSQL DBA.

Before we deep dive into the PostgreSQL usage of kernel parameters, we first understand the basics of inter-process communication.

This tutorial is divided into two sections.

1. Operating system basics.
2. PostgreSQL usage and Case study.

Operating system basics

What is a process and what is Inter-process communication?

A process is a series of actions or steps to achieve something.

A process within a system may be independent or cooperative. If the process is independent it is not affected by the execution of other processes on the other side if it is cooperative, it can be affected by other processes.

Interprocess communication is a mechanism which allows processes to communicate with each other and synchronize their actions.

Linux Kernel provides different types of IPC mechanisms of which PostgreSQL uses System V and POSIX IPC mechanisms.

System V or POSIX IPC refers to a set of mechanisms that allow a User Mode process to do the following:

1. Shared memory allows processes to share parts of their virtual address space.
2. Semaphores allow processes to synchronize execution.
3. Messages allow processes to send formatted data streams to arbitrary processes.

PostgreSQL requires the operating system to provide inter-process communication (IPC) features, specifically shared memory and semaphores.

So, we limit our discussion to shared memory and semaphores.

Shared memory:

Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like systems.

Upon a new process request, a shared memory segment is created by the kernel and mapped to the data segment of the address space of a requesting process.

How shared memory works?

When data from one process needs to be shared with another process, The first process simply writes data into the shared memory segment.

As soon as it is written, the data becomes available to the second process.

POSIX shared memory files are provided from a tmpfs filesystem mounted at /dev/shm.

Semaphores

semaphore acts as a synchronization tool in IPC systems with the help of which we can ensure that a critical section can be accessed by the processes in a mutually exclusive way.

It is simply a variable or abstract data type used to control access to a common resource by multiple processes in interprocess communication.

PostgreSQL usage and Case study:

PostgreSQL Shared Memory:

When a new client connection request comes to the postmaster process, the postmaster process calls fork() to create a child one for handling the incoming request.

postmaster creates child backend process
postmaster creates the backend process

PostgreSQL saves the cache blocks in the memory area called “shared buffer”, and they are shared among multiple back-end processes.

The shared buffer that PostgreSQL instance uses is configured by System V Shared Memory or POSIX.

System V Shared Memory in Linux environment is created using the shmget system call.

For the creation of the System V Shared Memory, a unique key number and size on the host must be specified.

The key number is generated using the following formula.

Shared Memory Key = parameter “port” * 1000 + 1

Since the standard port number waiting for a connection (parameter port) is 5,432, key of the shared memory is 5,432,001 (= 0x52e2c1)

Inadequate settings may lead to the following error.

PostgreSQL Semaphores:

For the locking between each process, Semaphores are utilized. The number of the semaphore set is not changed even if the number of the connecting client increases.

When using System V semaphores, PostgreSQL uses one semaphore per allowed connection (max_connections), allowed autovacuum worker process (autovacuum_max_workers) and allowed background process (max_worker_processes), in sets of 16.

Each such set will also contain a 17th semaphore which contains a “magic number”, to detect collision with semaphore sets used by other applications.

The maximum number of semaphores in the system is set by SEMMNS, which consequently must be at least as high as max_connections plus autovacuum_max_workers plus max_worker_processes, plus one extra for each 16 allowed connections plus workers.

Inadequate settings may lead to the following error.

We can check the shared memory and semaphore usage with the following command.

Conclusion:

It is highly recommended to set the kernel parameters adequately enough in the PostgreSQL environment.

The table here shows about the settings of kernel parameters we need to set on a typical PostgreSQL environment.

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestion/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

 

Leave a Reply