Planet PostgreSQL -
Tristen Raab: PostgreSQL Load Balancing Made Easy: A Deep Dive into pgpool
Introduction This blog is aimed at beginners trying to learn the basics of PostgreSQL and PGPool but already have some experience under their belt. For this tutorial, we will assume you have PostgreSQL correctly installed on Ubuntu. All of these steps were done using PostgreSQL 16 (development version) and PGPool 4.4.3 on Ubuntu 23.04. We’ll go over an example of setting up multiple PostgreSQL servers, one primary and two replicas, and how they can be balanced using PGPool. Setting Up Our overall architecture will be PGPool sitting in front of 3 PostgreSQL instances, 1 primary and 2 replicas. It will look something like this: To get this setup we first have to create our primary database: $ mkdir data/ $ initdb data/ Set up the primary as you would to allow for replication (edit the correct postgresql.conf and pg_hba.conf files). If you want to learn more about setting up database replication in PostgreSQL, see my previous blog here. Once set up, start the database: $ pg_ctl -D data/ -l logfile start Follow these steps again to create both replicas and set them up to replicate the primary database. Lastly, we need to start PGPool in order to pool our databases together. We’re going to use the following configuration file, pgpool.conf: listen_addresses = 'localhost' port = 9999 pcp_listen_addresses = 'localhost' pcp_port = 9898 load_balance_mode = on #Backend 0 backend_hostname0 = 'localhost' backend_port0 = 5432 backend_weight0 = 0.2 backend_data_directory0 = '/home/tristen/pg/data' backend_flag0 = 'ALWAYS_PRIMARY' backend_application_name0 = 'primary' #Backend 1 backend_hostname1 = 'localhost' backend_port1 = 5433 backend_data_directory1 = '/home/tristen/pg/data1' backend_weight1 = 0.4 backend_flag1 = 'ALLOW_TO_FAILOVER' backend_application_name1 = 'server1' #Backend 2 backend_hostname2 = 'localhost' backend_port2 = 5434 backend_data_directory2 = '/home/tristen/pg/data2' backend_weight2 = 0.4 backend_flag2 = 'ALLOW_TO_FAILOVER' backend_application_name2 = 'server[...]
AI生成摘要 本文介绍了如何使用PGPool动态地添加和删除PostgreSQL池中的副本服务器。首先介绍了如何设置PGPool来平衡一个主数据库和两个副本,然后介绍了如何在运行时动态地添加和删除副本,并演示了如何自动化这个过程。最后,强调了能够在零停机时间下更改副本对于需要高可用性的任何应用程序的重要性。