Configuration
You can define different types of instances & clusters.
- Identity: Parameters used for describing a PostgreSQL cluster
- Primary: Define a single instance cluster.
- Replica: Define a basic HA cluster with one primary & one replica.
- Offline: Define a dedicated instance for OLAP/ETL/Interactive queries
- Sync Standby: Enable synchronous commit to ensure no data loss.
- Quorum Commit: Use quorum sync commit for an even higher consistency level.
- Standby Cluster: Clone an existing cluster and follow it
- Delayed Cluster: Clone an existing cluster for emergency data recovery
- Citus Cluster: Define a Citus distributed database cluster
- Major Version: Create postgres cluster with different major version
Primary
Let’s start with the simplest case, singleton meta:
Use the following command to create a primary database instance on the 10.10.10.11 node.
Replica
To add a physical replica, you can assign a new instance to pg-test with pg_role set to replica
You can create an entire cluster or append a replica to the existing cluster:
Offline
The offline instance is a dedicated replica to serve slow queries, ETL, OLAP traffic and interactive queries, etc…
To add an offline instance, assign a new instance with pg_role set to offline.
Offline instance works like common replica instances, but it is used as a backup server in pg-test-replica service.
That is to say, offline and primary instance serves only when all replica instances are down.
You can have ad hoc access control offline with pg_default_hba_rules and pg_hba_rules.
It will apply to the offline instance and any instances with pg_offline_query flag.
Sync Standby
Pigsty uses asynchronous stream replication by default. Which may have a small replication lag. (10KB / 10ms).
A small window of data loss may occur when the primary fails (can be controlled with pg_rpo.), but it is acceptable for most scenarios.
But in some critical scenarios (e.g. financial transactions), data loss is totally unacceptable or read-your-write consistency is required. In this case, you can enable synchronous commit to ensure that.
To enable sync standby mode, you can simply use crit.yml template in pg_conf
To enable sync standby on existing clusters, config the cluster and enable synchronous_mode:
If synchronous_mode: true, the synchronous_standby_names parameter will be managed by patroni.
It will choose a sync standby from all available replicas and write its name to the primary’s configuration file.
Quorum Commit
When sync standby is enabled, PostgreSQL will pick one replica as the standby instance, and all other replicas as candidates. Primary will wait until the standby instance flushes to disk before a commit is confirmed, and the standby instance will always have the latest data without any lags.
However, you can achieve an even higher/lower consistency level with the quorum commit (trade-off with availability).
For example, to have all 2 replicas to confirm a commit:
If you have more replicas and wish to have more sync standby, increase synchronous_node_count accordingly.
Beware of adjust synchronous_node_count accordingly when you append or remove replicas.
The postgres synchronous_standby_names parameter will be managed by patroni:
Example: Multiple Sync Standby
And we can see that the two replicas are selected as sync standby now.
The classic quorum commit is to use majority of replicas to confirm a commit.
Example: Enable Quorum Commit
After applying the configuration, we can see that all replicas are no longer sync standby, but just normal replicas.
After that, when we can check pg_stat_replication.sync_state, it becomes quorum instead of sync or async.
Standby Cluster
You can clone an existing cluster and create a standby cluster, which can be used for migration, horizontal split, multi-az deployment, or disaster recovery.
A standby cluster’s definition is just the same as any other normal cluster, except there’s a pg_upstream defined on the primary instance.
For example, you have a pg-test cluster, to create a standby cluster pg-test2, the inventory may look like this:
And pg-test2-1, the primary of pg-test2 will be a replica of pg-test and serve as a Standby Leader in pg-test2.
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.
Example: Change Replication Upstream
You can change the replication upstream of the standby cluster when necessary (e.g. upstream failover).
To do so, just change the standby_cluster.host to the new upstream IP address and apply.
Example: Promote Standby Cluster
You can promote the standby cluster to a standalone cluster at any time.
To do so, you have to config the cluster and wipe the entire standby_cluster section then apply.
Example: Cascade Replica
If the pg_upstream is specified for replica rather than primary, the replica will be configured as a cascade replica with the given upstream ip instead of the cluster primary
Delayed Cluster
A delayed cluster is a special type of standby cluster, which is used to recover “drop-by-accident” ASAP.
For example, if you wish to have a cluster pg-testdelay which has the same data as 1-day ago pg-test cluster:
You can also configure a replication delay on the existing standby cluster.
When some tuples & tables are dropped by accident, you can advance this delayed cluster to a proper time point and select data from it.
It takes more resources, but can be much faster and have less impact than PITR
Citus Cluster
Pigsty has native citus support. Check files/pigsty/citus.yml & prod.yml for example.
To define a citus cluster, you have to specify the following parameters:
pg_modehas to be set tocitusinstead of defaultpgsqlpg_shard&pg_grouphas to be defined on each sharding clusterpatroni_citus_dbhas to be defined to specify the database to be managedpg_dbsu_passwordhas to be set to a non-empty string plain password if you want to use thepg_dbsupostgresrather than defaultpg_admin_usernameto perform admin commands
Besides, extra hba rules that allow ssl access from local & other data nodes are required. Which may looks like this
And you can create distributed table & reference table on the coordinator node. Any data node can be used as the coordinator node since citus 11.2.
Major Version
Pigsty works on PostgreSQL 10+. While the pre-packaged packages only includes 12 - 16 for now.
| version | Comment | Packages |
|---|---|---|
| 16 | Default stable major version with the full v2.7 extension set | Core, L1,L2,L3 |
| 15 | Previous stable major version; adjust repository packages | Core, L1,L2 |
| 14 | The old stable major version, ith L1 extension support only | Core, L1 |
| 13 | Older major version, with L1 extension support only | Core, L1 |
| 12 | Older major version, with L1 extension support only | Core, L1 |
- Core:
postgresql*, available on PG 12 - 16 - L1 extensions:
wal2json,pg_repack,passwordcheck_cracklib(PG 12 - 16) - L2 extensions:
postgis,citus,timescaledb,pgvector(PG15, PG16) - L3 extensions: Other miscellaneous extensions in the default v2.7 PG16 repository
Since the default v2.7 repository focuses on PG16, you may have to change pg_extensions and pg_libs to fit your needs.
Here are some example cluster definition with different major versions.
Beware that these extensions are just not included in Pigsty’s default repo. You can have these extensions on older pg version with proper configuration.