You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -78,12 +78,12 @@ Here's some sample code you can use to try out logical replication.
78
78
79
79
3. Connect to the subscriber database. Create a table with the same schema as on the publisher.
80
80
```SQL
81
-
CREATETABLEbasic(id SERIAL, name varchar(40));
81
+
CREATETABLEbasic(id INTEGERNOT NULLPRIMARY KEY, a TEXT);
82
82
```
83
83
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.
85
85
```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;
87
87
```
88
88
89
89
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.
100
100
101
101
Visit the PostgreSQL documentation to understand more about [logical replication](https://www.postgresql.org/docs/current/logical-replication.html).
102
102
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
+
CREATETABLEbasic (id INTEGERNOT NULLPRIMARY 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:
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:
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 SELECT3, '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
+
103
145
### pglogical extension
104
146
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.
106
148
107
149
1. Install pglogical extension in the database in both the provider and the subscriber database servers.
108
150
```SQL
@@ -187,7 +229,7 @@ In the example below, we use the SQL interface with the wal2json plugin.
187
229
SELECT data FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, 'pretty-print', '1');
188
230
```
189
231
190
-
The output will look like:
232
+
The output looks like:
191
233
```
192
234
{
193
235
"change": [
@@ -230,9 +272,9 @@ Visit the PostgreSQL documentation to understand more about [logical decoding](h
230
272
231
273
232
274
## 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.
234
276
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.
0 commit comments