Skip to content

Commit e8792a6

Browse files
Merge pull request #250742 from AlicjaKucharczyk/patch-2
Using Logical Replication Between Databases on the Same Server
2 parents 970d868 + 5b453d5 commit e8792a6

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

articles/postgresql/flexible-server/concepts-logical.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ Here's some sample code you can use to try out logical replication.
6666

6767
1. Connect to the publisher database. Create a table and add some data.
6868
```SQL
69-
CREATE TABLE basic(id SERIAL, name TEXT);
70-
INSERT INTO basic(name) VALUES ('apple');
71-
INSERT INTO basic(name) VALUES ('banana');
69+
CREATE TABLE basic (id INTEGER NOT NULL PRIMARY KEY, a TEXT);
70+
INSERT INTO basic VALUES (1, 'apple');
71+
INSERT INTO basic VALUES (2, 'banana');
7272
```
7373

7474
2. Create a publication for the table.
@@ -78,12 +78,12 @@ Here's some sample code you can use to try out logical replication.
7878

7979
3. Connect to the subscriber database. Create a table with the same schema as on the publisher.
8080
```SQL
81-
CREATE TABLE basic(id SERIAL, name varchar(40));
81+
CREATE TABLE basic (id INTEGER NOT NULL PRIMARY KEY, a TEXT);
8282
```
8383

84-
4. Create a subscription that will connect to the publication you created earlier.
84+
4. Create a subscription that connects to the publication you created earlier.
8585
```SQL
86-
CREATE SUBSCRIPTION sub CONNECTION 'host=<server>.postgres.database.azure.com user=<admin> dbname=<dbname> password=<password>' PUBLICATION pub;
86+
CREATE SUBSCRIPTION sub CONNECTION 'host=<server>.postgres.database.azure.com user=<rep_user> dbname=<dbname> password=<password>' PUBLICATION pub;
8787
```
8888

8989
5. You can now query the table on the subscriber. You'll see that it has received data from the publisher.
@@ -100,9 +100,51 @@ Here's some sample code you can use to try out logical replication.
100100

101101
Visit the PostgreSQL documentation to understand more about [logical replication](https://www.postgresql.org/docs/current/logical-replication.html).
102102

103+
### Using logical replication between databases on the same server
104+
When you're aiming to set up logical replication between different databases residing on the same PostgreSQL server, it's essential to follow certain guidelines to avoid implementation restrictions that are currently present. As of now, creating a subscription that connects to the same database cluster will only succeed if the replication slot isn't created within the same command; otherwise, the `CREATE SUBSCRIPTION` call will hang, on a `LibPQWalReceiverReceive` wait event. This happens due to an existing restriction within Postgres engine, which might be removed in future releases.
105+
106+
To effectively setup logical replication between your "source" and "target" databases on the same server while circumventing this restriction, follow the steps outlined below:
107+
108+
First, create a table named "basic" with an identical schema in both the source and target databases:
109+
110+
```SQL
111+
-- Run this on both source and target databases
112+
CREATE TABLE basic (id INTEGER NOT NULL PRIMARY KEY, a TEXT);
113+
```
114+
115+
Next, in the source database, create a publication for the table and separately create a logical replication slot using the `pg_create_logical_replication_slot` function, which helps to avert the hanging issue that typically occurs when the slot is created in the same command as the subscription. Note that you'll need to use the `pgoutput` plugin:
116+
117+
```SQL
118+
-- Run this on the source database
119+
CREATE PUBLICATION pub FOR TABLE basic;
120+
SELECT pg_create_logical_replication_slot('myslot', 'pgoutput');
121+
```
122+
123+
Thereafter, in your target database, create a subscription to the previously created publication, ensuring that `create_slot` is set to `false` to prevent PostgreSQL from creating a new slot, and correctly specifying the slot name that was created in the previous step. Before running the command, replace the placeholders in the connection string with your actual database credentials:
124+
125+
``` SQL
126+
-- Run this on the target database
127+
CREATE SUBSCRIPTION sub
128+
CONNECTION 'dbname=<source dbname> host=<server>.postgres.database.azure.com port=5432 user=<rep_user> password=<password>'
129+
PUBLICATION pub
130+
WITH (create_slot = false, slot_name='myslot');
131+
```
132+
Having set up the logical replication, you can now test it by inserting a new record into the "basic" table in your source database and then verifying that it replicates to your target database:
133+
``` SQL
134+
-- Run this on the source database
135+
INSERT INTO basic SELECT 3, 'mango';
136+
137+
-- Run this on the target database
138+
TABLE basic;
139+
```
140+
141+
If everything is configured correctly, you should witness the new record from the source database in your target database, confirming the successful setup of logical replication.
142+
143+
144+
103145
### pglogical extension
104146

105-
Here is an example of configuring pglogical at the provider database server and the subscriber. Refer to [pglogical extension documentation](https://github.com/2ndQuadrant/pglogical#usage) for more details. Also make sure you have performed prerequisite tasks listed above.
147+
Here's an example of configuring pglogical at the provider database server and the subscriber. Refer to [pglogical extension documentation](https://github.com/2ndQuadrant/pglogical#usage) for more details. Also make sure you have performed prerequisite tasks listed above.
106148

107149
1. Install pglogical extension in the database in both the provider and the subscriber database servers.
108150
```SQL
@@ -187,7 +229,7 @@ In the example below, we use the SQL interface with the wal2json plugin.
187229
SELECT data FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, 'pretty-print', '1');
188230
```
189231

190-
The output will look like:
232+
The output looks like:
191233
```
192234
{
193235
"change": [
@@ -230,9 +272,9 @@ Visit the PostgreSQL documentation to understand more about [logical decoding](h
230272

231273

232274
## Monitoring
233-
You must monitor logical decoding. Any unused replication slot must be dropped. Slots hold on to Postgres WAL logs and relevant system catalogs until changes have been read. If your subscriber or consumer fails or if it's improperly configured, the unconsumed logs will pile up and fill your storage. Also, unconsumed logs increase the risk of transaction ID wraparound. Both situations can cause the server to become unavailable. Therefore, it's critical that logical replication slots are consumed continuously. If a logical replication slot is no longer used, drop it immediately.
275+
You must monitor logical decoding. Any unused replication slot must be dropped. Slots hold on to Postgres WAL logs and relevant system catalogs until changes have been read. If your subscriber or consumer fails or if it's improperly configured, the unconsumed logs pile up and fill your storage. Also, unconsumed logs increase the risk of transaction ID wraparound. Both situations can cause the server to become unavailable. Therefore, it's critical that logical replication slots are consumed continuously. If a logical replication slot is no longer used, drop it immediately.
234276

235-
The 'active' column in the pg_replication_slots view will indicate whether there's a consumer connected to a slot.
277+
The 'active' column in the pg_replication_slots view indicates whether there's a consumer connected to a slot.
236278
```SQL
237279
SELECT * FROM pg_replication_slots;
238280
```

0 commit comments

Comments
 (0)