Administration
Maintain PostgreSQL cluster in Pigsty?
Here are 12 SOP for common pgsql admin tasks
- Case 1: Create Cluster
- Case 2: Create User
- Case 3: Create Database
- Case 4: Reload Service
- Case 5: Reload HBARule
- Case 6: Config Cluster
- Case 7: Append Replica
- Case 8: Remove Replica
- Case 9: Remove Cluster
- Case 10: Switchover
- Case 11: Backup Cluster
- Case 12: Restore Cluster
Cheatsheet
PGSQL playbooks and shortcuts:
1. bin/pgsql-add <cls> # create pgsql cluster <cls>
2. bin/pgsql-user <cls> <username> # create pg user <username> on <cls>
3. bin/pgsql-db <cls> <dbname> # create pg database <dbname> on <cls>
4. bin/pgsql-svc <cls> [...ip] # reload pg service of cluster <cls>
5. bin/pgsql-hba <cls> [...ip] # reload postgres/pgbouncer HBA rules of cluster <cls>
6. bin/pgsql-add <cls> [...ip] # append replicas for cluster <cls>
7. bin/pgsql-rm <cls> [...ip] # remove replicas from cluster <cls>
8. bin/pgsql-rm <cls> # remove pgsql cluster <cls>
Patroni admin command and shortcuts:
1. pg list <cls> # print cluster info
2. pg edit-config <cls> # edit cluster config
3. pg reload <cls> [ins] # reload cluster config
4. pg restart <cls> [ins] # restart pgsql cluster
5. pg reinit <cls> [ins] # reinit cluster members
6. pg pause <cls> # entering maintenance mode (no auto failover)
7. pg resume <cls> # exiting maintenance mode
8. pg switchover <cls> # switchover on cluster <cls>
9. pg failover <cls> # failover on cluster <cls>
pgBackRest backup & restore command and shortcuts:
1. pb info # print pgbackrest repo info
2. pg-backup # make a backup, incr, or full backup if necessary
3. pg-backup full # make a full backup
4. pg-backup diff # make a differential backup
5. pg-backup incr # make a incremental backup
6. pg-pitr -i # restore to the time of latest backup complete (not often used)
7. pg-pitr --time="2022-12-30 14:44:44+08" # restore to specific time point (in case of drop db, drop table)
8. pg-pitr --name="my-restore-point" # restore TO a named restore point create by pg_create_restore_point
9. pg-pitr --lsn="0/7C82CB8" -X # restore right BEFORE a LSN
10. pg-pitr --xid="1234567" -X -P # restore right BEFORE a specific transaction id, then promote
11. pg-pitr --backup=latest # restore to latest backup set
12. pg-pitr --backup=20221108-105325 # restore to a specific backup set, which can be checked with pgbackrest info
Systemd components quick reference
1. systemctl stop patroni # start stop restart reload
2. systemctl stop pgbouncer # start stop restart reload
3. systemctl stop pg_exporter # start stop restart reload
4. systemctl stop pgbouncer_exporter # start stop restart reload
5. systemctl stop node_exporter # start stop restart
6. systemctl stop haproxy # start stop restart reload
7. systemctl stop vip-manager # start stop restart reload
8. systemctl stop postgres # only when patroni_mode == 'remove'
Create Cluster
To create a new Postgres cluster, define it in the inventory first, then init with:
1. bin/node-add <cls> # init nodes for cluster <cls> # ./node.yml -l <cls>
2. bin/pgsql-add <cls> # init pgsql instances of cluster <cls> # ./pgsql.yml -l <cls>
Create User
To create a new business user on the existing Postgres cluster, add user definition to all.children.<cls>.pg_users, then create the user as follows:
1. bin/pgsql-user <cls> <username> # ./pgsql-user.yml -l <cls> -e username=<username>
Create Database
To create a new database user on the existing Postgres cluster, add database definition to all.children.<cls>.pg_databases, then create the database as follows:
1. bin/pgsql-db <cls> <dbname> # ./pgsql-db.yml -l <cls> -e dbname=<dbname>
Note: If the database has specified an owner, the user should already exist, or you’ll have to Create User first.
Reload Service
Services are exposed access point served by HAProxy.
This task is used when cluster membership has changed, e.g., append/remove replicas, switchover/failover / exposing new service or updating existing service’s config (e.g., LB Weight)
To create new services or reload existing services on entire proxy cluster or specific instances:
1. bin/pgsql-svc <cls> # pgsql.yml -l <cls> -t pg_service -e pg_reload=true
2. bin/pgsql-svc <cls> [ip...] # pgsql.yml -l ip... -t pg_service -e pg_reload=true
Reload HBARule
This task is used when your Postgres/Pgbouncer HBA rules have changed, you may have to reload hba to apply changes.
If you have any role-specific HBA rules, you may have to reload hba after a switchover/failover, too.
To reload postgres & pgbouncer HBA rules on entire cluster or specific instances:
1. bin/pgsql-hba <cls> # pgsql.yml -l <cls> -t pg_hba,pgbouncer_hba,pgbouncer_reload -e pg_reload=true
2. bin/pgsql-hba <cls> [ip...] # pgsql.yml -l ip... -t pg_hba,pgbouncer_hba,pgbouncer_reload -e pg_reload=true
Config Cluster
To change the config of a existing Postgres cluster, you have to initiate control command on admin node with admin user:
1. pg edit-config <cls> # interactive config a cluster with patronictl
Change patroni parameters & postgresql.parameters, save & apply changes with the wizard.
Example: Change Cluster Config with Patroni REST API
You can also use Patroni REST API to change the config in a non-interactive mode, for example:
1. $ curl -s 10.10.10.11:8008/config | jq . # get current config
2. $ curl -u 'postgres:Patroni.API' \
3. -d '{"postgresql":{"parameters": {"log_min_duration_statement":200}}}' \
4. -s -X PATCH http://10.10.10.11:8008/config | jq .
Note: patroni unsafe RestAPI access is limit from infra/admin nodes and protected with an HTTP basic auth username/password and an optional HTTPS mode.
Append Replica
To add a new replica to the existing Postgres cluster, you have to add its definition to the inventory: all.children.<cls>.hosts, then:
1. bin/node-add <ip> # init node <ip> for the new replica
2. bin/pgsql-add <cls> <ip> # init pgsql instances on <ip> for cluster <cls>
It will add node <ip> to pigsty and init it as a replica of the cluster <cls>.
Cluster services will be reloaded to adopt the new member
Example: Add replica to pg-test
For example, if you want to add a pg-test-3 / 10.10.10.13 to the existing cluster pg-test, you’ll have to update the inventory first:
1. pg-test:
2. hosts:
3. 10.10.10.11: { pg_seq: 1, pg_role: primary } # existing member
4. 10.10.10.12: { pg_seq: 2, pg_role: replica } # existing member
5. 10.10.10.13: { pg_seq: 3, pg_role: replica } # <--- new member
6. vars: { pg_cluster: pg-test }
then apply the change as follows:
1. bin/node-add 10.10.10.13 # add node to pigsty
2. bin/pgsql-add pg-test 10.10.10.13 # init new replica on 10.10.10.13 for cluster pg-test
which is similar to cluster init but only works on single instance。
1. [ OK ] init instances 10.10.10.11 to pgsql cluster 'pg-test':
2. [WARN] reminder: add nodes to pigsty, then install additional module 'pgsql'
3. [HINT] $ bin/node-add 10.10.10.11 # run this ahead, except infra nodes
4. [WARN] init instances from cluster:
5. [ OK ] $ ./pgsql.yml -l '10.10.10.11,&pg-test'
6. [WARN] reload pg_service on existing instances:
7. [ OK ] $ ./pgsql.yml -l 'pg-test,!10.10.10.11' -t pg_service
Remove Replica
To remove a replica from the existing PostgreSQL cluster:
1. bin/pgsql-rm <cls> <ip...> # ./pgsql-rm.yml -l <ip>
It will remove instance <ip> from cluster <cls>. Cluster services will be reloaded to kick the removed instance from load balancer.
Example: Remove replica from pg-test
For example, if you want to remove pg-test-3 / 10.10.10.13 from the existing cluster pg-test:
1. bin/pgsql-rm pg-test 10.10.10.13 # remove pgsql instance 10.10.10.13 from pg-test
2. bin/node-rm 10.10.10.13 # remove that node from pigsty (optional)
3. vi pigsty.yml # remove instance definition from inventory
4. bin/pgsql-svc pg-test # refresh pg_service on existing instances to kick removed instance from load balancer
1. [ OK ] remove pgsql instances from 10.10.10.13 of 'pg-test':
2. [WARN] remove instances from cluster:
3. [ OK ] $ ./pgsql-rm.yml -l '10.10.10.13,&pg-test'
And remove instance definition from the inventory:
1. pg-test:
2. hosts:
3. 10.10.10.11: { pg_seq: 1, pg_role: primary }
4. 10.10.10.12: { pg_seq: 2, pg_role: replica }
5. 10.10.10.13: { pg_seq: 3, pg_role: replica } # <--- remove this after execution
6. vars: { pg_cluster: pg-test }
Finally, you can update pg service and kick the removed instance from load balancer:
1. bin/pgsql-svc pg-test # reload pg service on pg-test
Remove Cluster
To remove the entire Postgres cluster, just run:
1. bin/pgsql-rm <cls> # ./pgsql-rm.yml -l <cls>
Example: Force removing a cluster
Note: if pg_safeguard is configured for this cluster (or globally configured to true), pgsql-rm.yml will abort to avoid removing a cluster by accident.
You can use playbook command line args to explicitly overwrite it to force the purge:
1. ./pgsql-rm.yml -l pg-meta -e pg_safeguard=false # force removing pg cluster pg-meta
Switchover
You can perform a PostgreSQL cluster switchover with patroni cmd.
1. pg switchover <cls>
Example: Switchover pg-test
1. $ pg switchover pg-test
2. Master [pg-test-1]:
3. Candidate ['pg-test-2', 'pg-test-3'] []: pg-test-2
4. When should the switchover take place (e.g. 2022-12-26T07:39 ) [now]: now
5. Current cluster topology
6. + Cluster: pg-test (7181325041648035869) -----+----+-----------+-----------------+
7. | Member | Host | Role | State | TL | Lag in MB | Tags |
8. +-----------+-------------+---------+---------+----+-----------+-----------------+
9. | pg-test-1 | 10.10.10.11 | Leader | running | 1 | | clonefrom: true |
10. | | | | | | | conf: tiny.yml |
11. | | | | | | | spec: 1C.2G.50G |
12. | | | | | | | version: '15' |
13. +-----------+-------------+---------+---------+----+-----------+-----------------+
14. | pg-test-2 | 10.10.10.12 | Replica | running | 1 | 0 | clonefrom: true |
15. | | | | | | | conf: tiny.yml |
16. | | | | | | | spec: 1C.2G.50G |
17. | | | | | | | version: '15' |
18. +-----------+-------------+---------+---------+----+-----------+-----------------+
19. | pg-test-3 | 10.10.10.13 | Replica | running | 1 | 0 | clonefrom: true |
20. | | | | | | | conf: tiny.yml |
21. | | | | | | | spec: 1C.2G.50G |
22. | | | | | | | version: '15' |
23. +-----------+-------------+---------+---------+----+-----------+-----------------+
24. Are you sure you want to switchover cluster pg-test, demoting current master pg-test-1? [y/N]: y
25. 2022-12-26 06:39:58.02468 Successfully switched over to "pg-test-2"
26. + Cluster: pg-test (7181325041648035869) -----+----+-----------+-----------------+
27. | Member | Host | Role | State | TL | Lag in MB | Tags |
28. +-----------+-------------+---------+---------+----+-----------+-----------------+
29. | pg-test-1 | 10.10.10.11 | Replica | stopped | | unknown | clonefrom: true |
30. | | | | | | | conf: tiny.yml |
31. | | | | | | | spec: 1C.2G.50G |
32. | | | | | | | version: '15' |
33. +-----------+-------------+---------+---------+----+-----------+-----------------+
34. | pg-test-2 | 10.10.10.12 | Leader | running | 1 | | clonefrom: true |
35. | | | | | | | conf: tiny.yml |
36. | | | | | | | spec: 1C.2G.50G |
37. | | | | | | | version: '15' |
38. +-----------+-------------+---------+---------+----+-----------+-----------------+
39. | pg-test-3 | 10.10.10.13 | Replica | running | 1 | 0 | clonefrom: true |
40. | | | | | | | conf: tiny.yml |
41. | | | | | | | spec: 1C.2G.50G |
42. | | | | | | | version: '15' |
43. +-----------+-------------+---------+---------+----+-----------+-----------------+
To do so with Patroni API (schedule a switchover from 2 to 1 at a specific time):
1. curl -u 'postgres:Patroni.API' \
2. -d '{"leader":"pg-test-2", "candidate": "pg-test-1","scheduled_at":"2022-12-26T14:47+08"}' \
3. -s -X POST http://10.10.10.11:8008/switchover
Backup Cluster
To create a backup with pgBackRest, run as local dbsu:
1. pg-backup # make a postgres base backup
2. pg-backup full # make a full backup
3. pg-backup diff # make a differential backup
4. pg-backup incr # make a incremental backup
5. pb info # check backup information
Check Backup & PITR for details.
Example: Create routine backup crontab
You can add crontab to node_crontab to specify your backup policy.
1. # make a full backup 1 am everyday
2. - '00 01 * * * postgres /pg/bin/pg-backup full'
4. # rotate backup: make a full backup on monday 1am, and an incremental backup during weekdays
5. - '00 01 * * 1 postgres /pg/bin/pg-backup full'
6. - '00 01 * * 2,3,4,5,6,7 postgres /pg/bin/pg-backup'
Restore Cluster
To restore a cluster to a previous time point (PITR), run as local dbsu:
1. pg-pitr -i # restore to the time of latest backup complete (not often used)
2. pg-pitr --time="2022-12-30 14:44:44+08" # restore to specific time point (in case of drop db, drop table)
3. pg-pitr --name="my-restore-point" # restore TO a named restore point create by pg_create_restore_point
4. pg-pitr --lsn="0/7C82CB8" -X # restore right BEFORE a LSN
5. pg-pitr --xid="1234567" -X -P # restore right BEFORE a specific transaction id, then promote
6. pg-pitr --backup=latest # restore to latest backup set
7. pg-pitr --backup=20221108-105325 # restore to a specific backup set, which can be checked with pgbackrest info
And follow the instructions wizard, Check Backup & PITR for details.
Example: PITR with raw pgBackRest Command
1. # restore to the latest available point (e.g. hardware failure)
2. pgbackrest --stanza=pg-meta restore
4. # PITR to specific time point (e.g. drop table by accident)
5. pgbackrest --stanza=pg-meta --type=time --target="2022-11-08 10:58:48" \
6. --target-action=promote restore
8. # restore specific backup point and then promote (or pause|shutdown)
9. pgbackrest --stanza=pg-meta --type=immediate --target-action=promote \
10. --set=20221108-105325F_20221108-105938I restore
Last modified 2023-02-27: add v2.0 images and docs (5b09f12)
