Skip to content

Commit 7defc85

Browse files
[DPE-7624] Add logical replication documentation (#1084)
* Add logical replication documentation Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Add toctree Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix Juju setup guide link Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Replace relate with integrate Co-authored-by: Andreia <[email protected]> * Move clusters deployment commands to complete guide Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Replace relate with integrate in the remaining places Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Avoid duplicating guides list Co-authored-by: Andreia <[email protected]> * Document what happens regarding the data when the relation is broken Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Document blocked status regarding table not being dropped and re-created Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Document what happens when the logical_replication_subscription_request config option is changed Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Document replication in the other direction, while keeping the replication in the original direction Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix second cluster unit IP Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix lexer issue Co-authored-by: Andreia <[email protected]> * Document replication to multiple clusters Signed-off-by: Marcelo Henrique Neppel <[email protected]> --------- Signed-off-by: Marcelo Henrique Neppel <[email protected]> Co-authored-by: Andreia <[email protected]>
1 parent 6610871 commit 7defc85

File tree

5 files changed

+228
-1
lines changed

5 files changed

+228
-1
lines changed

docs/how-to/deploy/tls-vip-access.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The basic requirements to follow along with this example setup are the following
1414

1515
* A fully deployed and running Juju machine environment
1616
* See the [PostgreSQL Tutorial](/tutorial/index) for a quick setup with Multipass
17-
* See the official [Juju deployment guide](https://juju.is/docs/juju/tutorial#deploy) for more details
17+
* See the official [Juju setup guide](https://juju.is/docs/juju/tutorial#set-up-juju) for more details
1818
* A spare virtual IP address for [`hacluster`](https://discourse.charmhub.io/t/pgbouncer-how-to-externally-access/15741)
1919
* See the PgBouncer guide: [How to use a VIP to connect to PgBouncer](https://charmhub.io/pgbouncer/docs/h-external-access?channel=1/stable)
2020
* DNS record pointing to VIP above (`my-tls-example-db.local` is used as an example here)

docs/how-to/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Other deployment scenarios and configurations:
5454
* [Remove or recover a cluster]
5555
* [Enable plugins/extensions]
5656

57+
## Logical replication
58+
* [Logical replication]
59+
* [Set up two clusters]
60+
* [Re-enable logical replication]
61+
5762
## Development
5863

5964
This section is for charm developers looking to support PostgreSQL integrations with their charm.
@@ -102,6 +107,10 @@ This section is for charm developers looking to support PostgreSQL integrations
102107
[Remove or recover a cluster]: /how-to/cross-regional-async-replication/remove-or-recover-a-cluster
103108
[Enable plugins/extensions]: /how-to/enable-plugins-extensions/index
104109

110+
[Logical replication]: /how-to/logical-replication/index
111+
[Set up two clusters]: /how-to/logical-replication/set-up-clusters
112+
[Re-enable logical replication]: /how-to/logical-replication/re-enable
113+
105114
[Integrate with your charm]: /how-to/development/integrate-with-your-charm
106115
[Migrate data via pg_dump]: /how-to/development/migrate-data-via-pg-dump
107116
[Migrate data via backup/restore]: /how-to/development/migrate-data-via-backup-restore
@@ -126,5 +135,6 @@ Back up and restore <back-up-and-restore/index>
126135
Monitoring (COS) <monitoring-cos/index>
127136
Upgrade <upgrade/index>
128137
Cross-regional async replication <cross-regional-async-replication/index>
138+
Logical replication <logical-replication/index>
129139
Development <development/index>
130140
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Logical replication
2+
3+
Logical replication is a feature that allows replicating a subset of one PostgreSQL cluster data to another PostgreSQL cluster.
4+
5+
Under the hood, it uses the publication and subscriptions mechanisms from the [PostgreSQL logical replication](https://www.postgresql.org/docs/16/logical-replication.html) feature.
6+
7+
## Prerequisites
8+
* Juju `v.3.6.8+`
9+
* Make sure your machine(s) fulfil the [](/reference/system-requirements)
10+
11+
## Guides
12+
13+
```{toctree}
14+
:titlesonly:
15+
16+
Set up two clusters <set-up-clusters>
17+
Re-enable logical replication <re-enable>
18+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# How to re-enable logical replication
2+
3+
If the relation between the PostgreSQL clusters is broken, you can re-enable logical replication by following these steps.
4+
5+
Drop and re-create the table on the second cluster:
6+
```sh
7+
psql postgresql://relation-9:[email protected]:5432/testdb
8+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
9+
Type "help" for help.
10+
11+
testdb=> drop table asd; create table asd (message int);
12+
DROP TABLE
13+
CREATE TABLE
14+
```
15+
16+
If the table is not dropped and re-created, the second cluster will get into a blocked state like in the following example:
17+
Output from `juju status`:
18+
19+
```text
20+
postgresql2/0* blocked idle 1 10.166.227.109 5432/tcp Logical replication setup is invalid. Check logs
21+
```
22+
23+
Output from Juju debug logs:
24+
25+
```text
26+
unit-postgresql2-0: 11:55:43 ERROR unit.postgresql2/0.juju-log logical-replication:11: relations.logical_replication:Logical replication validation: table public.asd in database testdb isn't empty
27+
```
28+
29+
Then, integrate the clusters again:
30+
```sh
31+
juju integrate postgresql1:logical-replication-offer postgresql2:logical-replication
32+
```
33+
34+
And you'll be able to see the data replicated from the first cluster to the second:
35+
```sh
36+
psql postgresql://relation-9:[email protected]:5432/testdb
37+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
38+
Type "help" for help.
39+
40+
testdb=> select * from asd;
41+
message
42+
---------
43+
123
44+
(1 row)
45+
```
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# How to set up clusters for logical replication
2+
3+
```{caution}
4+
This feature is only available for revision 863 or higher, which is not yet in the stable track.
5+
```
6+
7+
Start by deploying two PostgreSQL clusters:
8+
```sh
9+
juju deploy postgresql --channel 16/edge postgresql1
10+
juju deploy postgresql --channel 16/edge postgresql2
11+
```
12+
13+
For testing purposes, you can deploy two applications of the [data integrator charm](https://charmhub.io/data-integrator) and then integrate them to the two PostgreSQL clusters you want to replicate data between.
14+
```sh
15+
juju deploy data-integrator di1 --config database-name=testdb
16+
juju deploy data-integrator di2 --config database-name=testdb
17+
18+
juju integrate postgresql1 di1
19+
juju integrate postgresql2 di2
20+
```
21+
22+
Then, integrate both PostgreSQL clusters:
23+
```sh
24+
juju integrate postgresql1:logical-replication-offer postgresql2:logical-replication
25+
```
26+
27+
This will create a publication on the first cluster and a subscription on the second cluster, allowing data to be replicated from the first to the second.
28+
29+
Request the credentials for the first PostgreSQL cluster.
30+
```sh
31+
juju run di1/leader get-credentials
32+
```
33+
34+
The output example:
35+
```yaml
36+
postgresql:
37+
data: '{"database": "testdb", "external-node-connectivity": "true", "provided-secrets":
38+
"[\"mtls-cert\"]", "requested-secrets": "[\"username\", \"password\", \"tls\",
39+
\"tls-ca\", \"uris\", \"read-only-uris\"]"}'
40+
database: testdb
41+
endpoints: 10.166.227.78:5432
42+
password: G7Qu77SU0qeadnhn
43+
read-only-endpoints: 10.166.227.78:5432
44+
read-only-uris: postgresql://relation-8:[email protected]:5432/testdb
45+
tls: "False"
46+
tls-ca: ""
47+
uris: postgresql://relation-8:[email protected]:5432/testdb
48+
username: relation-8
49+
version: "16.9"
50+
```
51+
52+
Then create a table and insert some data into it on the first cluster:
53+
```sh
54+
psql postgresql://relation-8:[email protected]:5432/testdb
55+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
56+
Type "help" for help.
57+
58+
testdb=> create table asd (message int); insert into asd values (123);
59+
CREATE TABLE
60+
INSERT 0 1
61+
```
62+
63+
After that, you need to create the same table on the second cluster so that the data can be replicated. Start by getting the credentials for the second cluster:
64+
```sh
65+
juju run di2/leader get-credentials
66+
```
67+
68+
The output example:
69+
```yaml
70+
postgresql:
71+
data: '{"database": "testdb", "external-node-connectivity": "true", "provided-secrets":
72+
"[\"mtls-cert\"]", "requested-secrets": "[\"username\", \"password\", \"tls\",
73+
\"tls-ca\", \"uris\", \"read-only-uris\"]"}'
74+
database: testdb
75+
endpoints: 10.166.227.109:5432
76+
password: FHZbyAPGQjbDpj65
77+
read-only-endpoints: 10.166.227.109:5432
78+
read-only-uris: postgresql://relation-9:[email protected]:5432/testdb
79+
tls: "False"
80+
tls-ca: ""
81+
uris: postgresql://relation-9:[email protected]:5432/testdb
82+
username: relation-9
83+
version: "16.9"
84+
```
85+
86+
Then create the same table on the second cluster:
87+
```sh
88+
psql postgresql://relation-9:[email protected]:5432/testdb
89+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
90+
Type "help" for help.
91+
92+
testdb=> create table asd (message int);
93+
CREATE TABLE
94+
```
95+
96+
Configure the replication of that specific database and table (remember to specify the table schema; it's the `public` schema in this example):
97+
```sh
98+
juju config postgresql2 logical_replication_subscription_request='{"testdb": ["public.asd"]}'
99+
```
100+
101+
After a few seconds, you can check that the data has been replicated:
102+
```sh
103+
psql postgresql://relation-9:[email protected]:5432/testdb
104+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
105+
Type "help" for help.
106+
107+
testdb=> select * from asd;
108+
message
109+
---------
110+
123
111+
(1 row)
112+
```
113+
114+
You can then add more data to the table in the first cluster, and it will be replicated to the second cluster automatically.
115+
116+
It's also possible to replicate tables in the other direction, from the second cluster to the first, while keeping the replication from the first cluster to the second. To do that, you need to also integrate the clusters in the opposite direction:
117+
```sh
118+
psql postgresql://relation-9:[email protected]:5432/testdb
119+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
120+
Type "help" for help.
121+
122+
testdb=> create table asd2 (message int); insert into asd2 values (123);
123+
CREATE TABLE
124+
INSERT 0 1
125+
testdb=> \q
126+
127+
psql postgresql://relation-8:[email protected]:5432/testdb
128+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
129+
Type "help" for help.
130+
131+
testdb=> create table asd2 (message int);
132+
CREATE TABLE
133+
testdb=> \q
134+
135+
juju integrate postgresql1:logical-replication postgresql2:logical-replication-offer
136+
137+
juju config postgresql1 logical_replication_subscription_request='{"testdb": ["public.asd2"]}'
138+
139+
psql postgresql://relation-8:[email protected]:5432/testdb
140+
psql (16.9 (Ubuntu 16.9-0ubuntu0.24.04.1))
141+
Type "help" for help.
142+
143+
testdb=> select * from asd2;
144+
message
145+
---------
146+
123
147+
(1 row)
148+
```
149+
150+
And the same table, or even different tables, can be replicated to multiple clusters at the same time. For example, you can replicate the `asd` table from the first cluster to both a second and a third clusters, or you can replicate it only to the second cluster and replicate a different table to the third cluster.
151+
152+
If the relation between the PostgreSQL clusters is broken, the data will be kept in both clusters, but the replication will stop. You can re-enable logical replication by following the steps from [](/how-to/logical-replication/re-enable).
153+
154+
The same will happen for that specific table if you change the table in the `logical_replication_subscription_request` config option to a different table or remove it completely. If one or more tables other than the current one are specified, the replication will continue for those tables, but the current table will not be replicated any more.

0 commit comments

Comments
 (0)