Skip to content

Commit 1adb6cc

Browse files
[DPE-7626] Add logical replication documentation (#1050)
* Add logical replication documentation Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix Terraform provider link Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix logical replication section in how-to index Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Replace relate with integrate Signed-off-by: Marcelo Henrique Neppel <[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 Signed-off-by: Marcelo Henrique Neppel <[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]> * Change executing state to idle Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Fix lexer issue Signed-off-by: Marcelo Henrique Neppel <[email protected]> * Document replication to multiple clusters Signed-off-by: Marcelo Henrique Neppel <[email protected]> --------- Signed-off-by: Marcelo Henrique Neppel <[email protected]>
1 parent 7403d8e commit 1adb6cc

File tree

5 files changed

+228
-1
lines changed

5 files changed

+228
-1
lines changed

docs/how-to/deploy/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ juju deploy postgresql-k8s --channel 16/stable --trust
2121
> See also: [`juju deploy` command](https://canonical-juju.readthedocs-hosted.com/en/latest/user/reference/juju-cli/list-of-juju-cli-commands/deploy/)
2222
2323
**To deploy with `terraform juju`**, follow the guide [How to deploy using Terraform].
24-
> See also: [Terraform Provider for Juju documentation](https://canonical-terraform-provider-juju.readthedocs-hosted.com/en/latest/)
24+
> See also: [Terraform Provider for Juju documentation](https://canonical-terraform-provider-juju.readthedocs-hosted.com/latest/)
2525
2626
If you are not sure where to start or would like a more guided walkthrough for setting up your environment, see the [](/tutorial/index).
2727

docs/how-to/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Other deployment scenarios and configurations:
4848
* [Integrate with a client app]
4949
* [Remove or recover a cluster]
5050

51+
## Logical replication
52+
* [Logical replication]
53+
* [Set up two clusters]
54+
* [Re-enable logical replication]
55+
5156
## Development
5257

5358
This section is for charm developers looking to support PostgreSQL integrations with their charm.
@@ -91,6 +96,10 @@ This section is for charm developers looking to support PostgreSQL integrations
9196
[Integrate with a client app]: /how-to/cross-regional-async-replication/integrate-with-a-client-app
9297
[Remove or recover a cluster]: /how-to/cross-regional-async-replication/remove-or-recover-a-cluster
9398

99+
[Logical replication]: /how-to/logical-replication/index
100+
[Set up two clusters]: /how-to/logical-replication/set-up-clusters
101+
[Re-enable logical replication]: /how-to/logical-replication/re-enable
102+
94103
[Integrate with your charm]: /how-to/development/integrate-with-your-charm
95104
[Migrate data via pg_dump]: /how-to/development/migrate-data-via-pg-dump
96105
[Migrate data via backup/restore]: /how-to/development/migrate-data-via-backup-restore
@@ -114,4 +123,5 @@ Back up and restore <back-up-and-restore/index>
114123
Monitoring (COS) <monitoring-cos/index>
115124
Upgrade <upgrade/index>
116125
Cross-regional async replication <cross-regional-async-replication/index>
126+
Logical replication <logical-replication/index>
117127
Development <development/index>
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_id_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 10.1.176.92 Logical replication setup is invalid. Check logs
21+
```
22+
23+
Output from Juju debug logs:
24+
25+
```text
26+
unit-postgresql2-0: 15:42:34 ERROR unit.postgresql2/0.juju-log 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_id_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 630 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-k8s --channel 16/edge --trust postgresql1
10+
juju deploy postgresql-k8s --channel 16/edge --trust 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: postgresql1-primary.dev.svc.cluster.local:5432
42+
password: NTgtJkVfUHLiYDk5
43+
read-only-endpoints: postgresql1-primary.dev.svc.cluster.local:5432
44+
read-only-uris: postgresql://relation_id_8:[email protected]:5432/testdb
45+
tls: "False"
46+
tls-ca: ""
47+
uris: postgresql://relation_id_8:[email protected]:5432/testdb
48+
username: relation_id_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_id_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: postgresql2-primary.dev.svc.cluster.local:5432
76+
password: nfWkAiEtSA3iA7t2
77+
read-only-endpoints: postgresql2-primary.dev.svc.cluster.local:5432
78+
read-only-uris: postgresql://relation_id_9:[email protected]:5432/testdb
79+
tls: "False"
80+
tls-ca: ""
81+
uris: postgresql://relation_id_9:[email protected]:5432/testdb
82+
username: relation_id_9
83+
version: "16.9"
84+
```
85+
86+
Then create the same table on the second cluster:
87+
```sh
88+
psql postgresql://relation_id_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_id_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_id_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_id_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_id_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)