PostgreSQL Deployment
This article describes several ways to deploy a PostgreSQL cluster using Pigsty: PGSQL-related playbook and config. Please refer to the related doc.
- Identity Parameters: Introduces the identity parameters required to define a standard PostgreSQL HA cluster.
- Singleton Deployment: Defines a single instance PostgreSQL cluster.
- Primary-Replica Cluster: Defines a standard availability cluster with one primary & one replica.
- Sync-Standby: Define a highly consistent cluster with sync standby and RPO = 0.
- Quorum Commit: Defines a cluster with higher data consistency: most replicas return commits on the successful side.
- Offline Replica: Dedicated instances for hosting OLAP analysis, ETL, and interactive personal queries individually.
- Standby Cluster: Produces real-time online clones of existing clusters for offsite disaster recovery or delayed.
- Delayed Cluster: For responding to software/human failures such as mistaken table and database deletion, faster than PITR.
- Cascade Instance: Used to build cascade within a cluster for many replica scenarios (20+) to reduce primary pressure.
- Citus Deployment: Deploy Citus distributed database cluster.
- MatrixDB Deployment: Deploy Greenplum7/PostgreSQL12 compatible chronological data warehouse.
Identity
The Core Identity Parameters are information that must be provided when defining a PostgreSQL cluster.
| Name | Attribute | Description | Example |
|---|---|---|---|
pg_cluster |
MUST, cluster level | Cluster name | pg-test |
pg_role |
MUST, instance level | Instance Role | primary, replica |
pg_seq |
MUST, instance level | Instance number | 1, 2, 3,... |
The content of the identity parameter follows the entity naming pattern. Where pg_cluster, pg_role, and pg_seq belong to the core identity parameters, the minimum set of mandatory parameters required to define the database cluster and core identity parameters must be explicitly specified.
-
pg_clusteridentities the name of the cluster configured at the cluster level and serves as the top-level namespace for cluster resources. -
pg_roleidentities the role of the instance in the cluster, configured at the instance level, with optional values including:primary: the only primary in the cluster, that provides writing services.replica: the ordinary replica in the cluster, takes regular production read-only traffic.offline: an offline replica in the cluster, takes ETL/SAGA/personal user/interactive/analytical queries.standby: a standby replica in the cluster, with synchronous replication and no replication latency (reserved).delayed: a delayed replica in the cluster, explicitly specifying replication delay, used to perform backtracking queries and data salvage (reserved).
-
pg_seqis used to identify the instance within the cluster. Usually, an integer incrementing from 0 or 1 will not be changed once assigned. -
pg_shardis used to identify the upper-level shard cluster to which the cluster belongs, and only needs to be set if the cluster belongs to a horizontal sharding cluster. -
pg_sindexis used to identify the cluster’s slice cluster number and only needs to be set if the cluster belongs to a horizontal sharding cluster. -
pg_instanceis the derived identity parameter that uniquely identifies a database instance, with the following composition rules{{ pg_cluster }}-{{ pg_seq }}. Sincepg_seqis unique within the cluster, this identity is globally unique.
Sharding Cluster
pg_shard and pg_sindex define particular sharded clusters and are optional, currently reserved for Citus and Greenplum.
Suppose a user has a horizontal sharding sharded database cluster with the name test. This cluster consists of four separate clusters: pg-test1, pg-test2, pg-test3, and pg-test-4. The user can bind the identity of pg_shard: test to each database cluster and pg_sindex: 1|2|3|4 to each database cluster separately.
With this definition, you can easily observe the cross-sectional metrics comparison of four horizontal sharding clusters from the PGSQL Shard monitoring dashboard. The same functionality works for Citus and MatrixDB clusters as well.
Singleton
Let’s start with the simplest case.
Use the following command to create a primary database instance on the 10.10.10.11 node.
M-S Replication
Pigsty natively supports M-S replication, e.g., to declare a typical one primary & one replica HA database cluster.
Use bin/createpg pg-test to create the cluster. If you have already finished deploying 10.10.10.11 in step 1 singleton deployment, you can also use bin/createpg 10.10.10.12 to expand the cluster.
Sync Standby
Under normal circumstances, PostgreSQL’s replication latency is a few tens of KB/10ms, which is negligible for regular business.
When the primary fails, data that has not yet completed replication will be lost! Replication latency can be a problem when dealing with critical and sophisticated business queries. Or, in a replica, immediately read-your-write after the primary writes, which can also be very sensitive to replication latency.
Sync standbys can solve such problems. A simple way to configure a sync standby is to use the pg_conf = crit template, which automatically enables synchronous replication.
After the cluster is created, you can also execute pg edit-config <cluster.name> on the meta node, edit the cluster configuration file, change the value of the synchronous_mode to true and apply it.
Quorum Commit
By default, synchronous replication picks an instance from all candidate replicas as a sync standby. Any primary transaction is only considered successfully committed and returned when replicated to the replica and flushed to the disk. A quorum commit can be used if more persistent data is expected. For example, in a 1primary & 3 replicas cluster, at least two replicas successfully flush to disk before a commit is confirmed.
When using quorum commit, you need to modify the synchronous_standby_names in PostgreSQL and the value of synchronous_node_count in Patroni. Assuming that the three replicas are pg-test-2, pg-test-3, and pg-test-4, the following should be configured.
synchronous_standby_names = ANY 2 (pg-test-2, pg-test-3, pg-test-4)synchronous_node_count : 2
Execute pg edit-config pg-test and modify the config as follows.
After the application, the configuration takes effect, and two Sync Standby appear. When the cluster has Failover or expansion and contraction, please adjust these parameters to avoid service unavailability.
Offline Replica
Data analysis/ETL/personal interactive queries should be placed on the offline replica when the high online business request load.
Use bin/createpg pg-test to create the cluster. If you have already completed singleton deployment and primary-replica-cluster, you can use bin/createpg 10.10.10.13 to expand the cluster and add an offline replica to the cluster.
Offline replicas do not host the replica service by default, and the offline instance will only host read-only traffic if all instances in the replica service are unavailable. If you have only one primary & one replica, or only one primary, you can set the pg_offline_query flag for an offline instance that also hosts the offline service to be used as a quasi-offline instance.
Standby Cluster
You can make a clone of an existing cluster using the Standby Cluster method, which allows for a smooth migration from a current database to a Pigsty cluster.
Just make sure that the pg_upstream parameter is configured on the primary of the backup cluster to pull backups from the original upstream automatically.
Promote Standby Cluster
When you want to promote the standby cluster to a standalone cluster, edit the Patroni configuration file of the new cluster to remove all standby_cluster configurations, and the Standby Leader in the standby cluster will be elevated to a standalone primary.
Remove the following config: the entire standby_cluster definition section.
Change Replication Upstream
When a Failover primary change occurs in the source cluster, you need to adjust the replication source of the standby cluster. Execute pg edit-config <cluster> and change the source address in standby_cluster to the new primary, and the application will take effect. Note that replica replication from the source cluster is feasible, and a Failover in the source cluster will not affect the replication of the standby cluster. However, the new cluster cannot create replication slots on the read-only replica, and there may be related error reports and a risk of replication interruption. It is recommended to adjust the upstream replication source of the standby cluster in time.
Modify the IP of the replication upstream in standby,_cluster.host, and the application will take effect (no need to reboot, Reload).
Delayed Cluster
HA and M-S replication can solve the problems caused by machine hardware failure, but cannot solve the failure caused by software bugs and human operations. A cold standby is usually required for accidental data deletion, but another way is to prepare a delayed cluster.
You can use the function standby cluster to create a delayed. For example, now you want to specify a delayed for the pg-test cluster: pg-testdelay, which is the state of pg-test 1 hour ago.
After creation, edit the Patroni config file for the delayed cluster using pg edit-config pg-testdelay in the meta node and change standby_cluster.recovery_min_apply_delay to the delay value you expect.
Cascade Instance
When creating a cluster, if the pg_upstream parameter is specified for one of the replicas in the cluster (defined as another replica in the cluster), the instance will attempt to build logical replication from that specified replica.
Citus Deployment
Citus is a distributed extension plugin for PostgreSQL. By default, Pigsty installs Citus but does not enable it. pigsty-citus.yml provides a config file case for deploying a Citus cluster. To allow Citus to, you need to modify the following parameters.
max_prepared_transaction: Modify to a value greater thanmax_connections, e.g. 800.pg_libs: Must containcitusand be placed in the top position.- You need to include the
citusextension plugin in the business database (but you can also manually install it viaCREATE EXTENSION).
Citus cluster sample config
Next, you need to refer to the Citus Multi-Node Deployment Guide, and on the Coordinator node, execute the following command to add a data node.
After successfully adding data nodes, you can use the following command to create sample data tables on the coordinator and distribute them to each data node.
For more information about Citus, please refer to the Citus official doc.
MatrixDB Deployment
Greenplum is a distributed data warehouse based on the PostgreSQL ecosystem, and MatrixDB is a branch of Greenplum based on Greenplum 7, using the PostgreSQL 12 kernel. Greenplum 7 has not yet been officially released, so Pigsty is currently using MatrixDB as a replacement for Greenplum.
MatrixDB is based on the PostgreSQL ecosystem, so most PostgreSQL playbooks and tasks can be reused on MatrixDB. There are only two additional parameters specific to MatrixDB.
gp_role: Define the identity of the Greenplum cluster,master,orsegment.pg_instances: Define the Segment instance, which is used to deploy the Segment monitoring instance.
For details, please refer to MatrixDB Deployment.