How to reduce replication lag with IO concurrency in PostgreSQL 15?

PostgreSQL 15 introduced a new feature called “asynchronous commit”, which can help reduce replication lag by allowing multiple transactions to be committed concurrently. This feature works by allowing the server to continue processing new transactions while it waits for synchronous replication feedback from standby servers.

To take advantage of this feature, you need to enable the max_parallel_workers_per_gather and max_parallel_workers settings in your postgresql.conf file, and set them to a value greater than 0. These settings control the number of parallel workers that can be used for various tasks in PostgreSQL, including replication.

For example, you can set max_parallel_workers_per_gather to the number of CPU cores on your system, and max_parallel_workers to a value slightly lower than that. This will allow PostgreSQL to use multiple parallel workers for replication, which can help reduce replication lag.

Here’s an example configuration snippet for postgresql.conf:

max_parallel_workers_per_gather = 4
max_parallel_workers = 3

You can adjust these values based on your specific hardware and workload.

Note that enabling asynchronous commit can increase the risk of data loss in the event of a server crash or other failure, since committed transactions may not be immediately written to disk on the standby server. You should carefully evaluate the trade-offs between reducing replication lag and ensuring data safety in your specific environment.

Additionally, you should monitor the replication lag regularly to ensure that it stays within acceptable limits, and adjust your configuration as necessary to maintain good performance. The pg_stat_replication view can be used to monitor replication lag, and tools like pgAdmin or other monitoring tools can also be used for this purpose.

About Shiv Iyer 446 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.