Skip to content

Commit cebf51a

Browse files
fix(rdb): replication as pub (#5780)
1 parent 451e28d commit cebf51a

File tree

5 files changed

+187
-7
lines changed

5 files changed

+187
-7
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: Setting up logical replication as a publisher in PostgreSQL
3+
description: Learn how to set up and use logical replication as a publisher in PostgreSQL
4+
tags: postgresql logical replication publisher publication database
5+
dates:
6+
validation: 2025-08-25
7+
posted: 2025-02-20
8+
---
9+
import Requirements from '@macros/iam/requirements.mdx'
10+
11+
The logical replication of databases as a publisher is available with [PostgreSQL 17](/managed-databases-for-postgresql-and-mysql/reference-content/pg-version-updates#postgresql-17), which is supported in Scaleway's Managed Databases for PostgreSQL.
12+
13+
The feature allows you to replicate data from a managed PostgreSQL to another Database Instance (a Managed PostgreSQL in another region, for example).
14+
15+
Replication slots can be created with the `failover` flag, which ensures that replication slots survive failovers. When a secondary node is promoted to primary, the replication slot is automatically available on the new main node.
16+
17+
<Requirements />
18+
19+
- A Scaleway account logged into the [console](https://console.scaleway.com)
20+
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
21+
- A [Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/create-a-database/) running PostgreSQL 17
22+
23+
## Configuring advanced settings and user rights
24+
25+
As the service uses physical replication internally, some advanced settings and parameters must be enabled to allow the logical replication to work properly with our system, for standalone and High Availability clusters.
26+
27+
Follow the steps in the [How to configure advanced settings](/managed-databases-for-postgresql-and-mysql/how-to/configure-advanced-settings) documentation page to configure the following settings:
28+
29+
To enable logical replication, you must:
30+
31+
- Enable the `rdb.enable_logical_replication` setting. This automatically changes the `wal_level` settings to `logical`.
32+
- Set `hot_standby_feedback` to `ON`
33+
- Set `sync_replication_slots` to `ON`- [replication slots](https://www.postgresql.org/docs/17/logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS) is an important part of the logical replication. Each subscription receives changes via one replication slot created on the "publishing" node. You have one replication slot available per logical database.
34+
35+
<Message type="important">
36+
As the service uses physical replication to provide High Availability, the `hot_standby_feedback` and `sync_replication_slots` parameters must be enabled to allow the logical replication to remain active after the promotion of a secondary node, without disturbing the HA setup. It is essential to ensure these settings are enabled as even a Database Instance in standalone mode can be replaced with temporary replicas.
37+
</Message>
38+
39+
To use replication, users must also have the corresponding rights.
40+
41+
Follow the steps in the [How to manage users](/managed-databases-for-postgresql-and-mysql/how-to/manage-users) documentation page to attribute user rights.
42+
43+
To create publications and replication slots, the `replication_user` must:
44+
45+
- Either be an member of admin user role (`_rdb_admin`)
46+
- Or be granted the following rights from a role member of the`_rdb_admin` role:
47+
- `ALTER replication_user with REPLICATION`
48+
- `GRANT CREATE ON DATABASE my_database TO my_replication_user;`
49+
50+
## Setting up logical replication
51+
52+
We use an example scenario to show how logical replication can be set up.
53+
54+
1. Create the table and publication on the publisher database.
55+
```sql
56+
CREATE TABLE mytable (id INTEGER PRIMARY KEY, val TEXT);
57+
CREATE PUBLICATION pub FOR TABLE mytable;
58+
```
59+
2. Insert a sample row into the table. This ensures that there is data to test the replication with.
60+
```sql
61+
INSERT INTO mytable VALUES (1, 'foo');
62+
```
63+
3. Create the same table structure on the subscriber database. Leave the table empty, as data will be copied into it upon subscription.
64+
```sql
65+
CREATE TABLE mytable (id INTEGER PRIMARY KEY, val TEXT);
66+
```
67+
4. Create a subscription. Two methods are available:
68+
- **Direct subscription with failover** - use this method if your environment supports the failover option in `CREATE SUBSCRIPTION`
69+
- **Creating a replication slot manually on the publisher** -
70+
71+
<Tabs>
72+
<TabsTab label="Set up direct subscription">
73+
```sql
74+
CREATE SUBSCRIPTION sub CONNECTION 'host=myhost port=myport user=myuser dbname=mydb password=mypassword' PUBLICATION pub WITH (failover);
75+
```
76+
</TabsTab>
77+
<TabsTab label="Create replication slot manually">
78+
Some environments do not allow `CREATE SUBSCRIPTION` to auto-create slots due to permissions or configuration. In this case, you can create a slot with failover manually.
79+
1. Create a replication slot on the publisher database.
80+
```sql
81+
SELECT pg_create_logical_replication_slot('sub', 'pgoutput', false, false, true);
82+
```
83+
<Message type="note">
84+
The last `true` in the command above enables the `failover` flag.
85+
</Message>
86+
2. Create the subscription without creating a slot.
87+
```sql
88+
CREATE SUBSCRIPTION sub CONNECTION 'host=myhost port=myport user=myuser dbname=mydb password=mypassword' PUBLICATION pub WITH (enabled=true, create_slot=false, slot_name='sub');
89+
```
90+
<Message type="note">
91+
Refer to the official [PostgreSQL documentation](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-REPLICATION) for more information about `pg_create_logical_replication_slot()`.
92+
</Message>
93+
</TabsTab>
94+
</Tabs>
95+
96+
5. Query the publisher table in the subscriber to test the replication.
97+
```sql
98+
SELECT * FROM mytable;
99+
```
100+
101+
<Message type="note">
102+
Refer to the official [PostgreSQL documentation](https://www.postgresql.org/docs/current/logical-replication.html) for more information about logical replication.
103+
</Message>
104+
105+
## Limitations
106+
107+
- 100 replication slots are available for each Database Instance.
108+
- 25 out of the 100 slots are reserved for Scaleway's internal systems.
109+
- During the synchronization phase, 2 or more slots might be needed for a subscription.
110+
<Message type="important">
111+
- If you need more than 50 replication slots, [create a support ticket](https://console.scaleway.com/support).
112+
- Exceeding this limit may cause the Instance's internal replication to fail. High Availability and cluster operations may also become degraded or not functional. In extreme cases, service availability may be affected.
113+
</Message>
114+
115+
<Message type="tip">
116+
Run the following command to get a list of your replication slots:
117+
```sql
118+
SELECT * FROM pg_replication_slots where slot_type = 'logical';
119+
```
120+
And the following command to check publications:
121+
```sql
122+
SELECT * from pg_publication;
123+
```
124+
</Message>
125+
126+
- If a logical replication is not active (not consumed by a subscription or other tool), PostgreSQL WAL files will accumulate in the primary node, filling up storage. In the worst case scenario, your database can be set to read only mode (check disk full section) or unavailable.
127+
<Message type="tip">
128+
- We recommend you delete any replication slots that are no longer used.
129+
- If a logical replication is inactive for 24h, it will be automatically removed.
130+
</Message>

pages/managed-databases-for-postgresql-and-mysql/api-cli/logical-replication-as-subscriber.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dates:
66
validation: 2025-08-25
77
posted: 2025-02-20
88
---
9+
import Requirements from '@macros/iam/requirements.mdx'
910

1011
The logical replication of databases as a subscriber is available with [PostgreSQL 16](/managed-databases-for-postgresql-and-mysql/reference-content/pg-version-updates#postgresql-16), which is now supported in Scaleway's Managed Databases for PostgreSQL.
1112

@@ -23,6 +24,11 @@ By setting up the new database as a subscriber to the original database you can
2324
- **Flexible migration windows** - You can choose when to switch your application to the new database, without having to worry about data consistency. Logical replication ensures that the new database is always up-to-date with the latest changes in the original database.
2425
- **Easy rollback** - If you encounter issues during migration, such as data corruption and application issues, you can point your application back to the original database. Since the subscriber database constantly keeps a copy of the data in sync with the publisher, you can avoid data loss or inconsistencies.
2526

27+
<Requirements />
28+
29+
- A Scaleway account logged into the [console](https://console.scaleway.com)
30+
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
31+
- A [Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/create-a-database/) running PostgreSQL 16 or a higher version
2632

2733
## How to set up the subscription
2834

pages/managed-databases-for-postgresql-and-mysql/how-to/create-a-database.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Compared to traditional database management, which requires customers to provide
6868

6969
You can restore the data contained in a snapshot to a Database Instance. When you do so, a new Database Instance is created and billed to your account.
7070

71+
<Message type="important">
72+
Keep in mind that replication slots cannot be restored when using PostgreSQL.
73+
</Message>
74+
75+
7176
1. Go to the **Snapshots** tab of your Database Instance of choice.
7277
2. Click the <Icon name="more" /> next to the name of the snapshot.
7378
3. Click **Create Database Instance from snapshot**. A pop-up appears.

pages/managed-databases-for-postgresql-and-mysql/menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ export const managedDatabasesForPostgresqlAndMysqlMenu = {
165165
label: 'Setting up logical replication as a subscriber',
166166
slug: 'logical-replication-as-subscriber',
167167
},
168+
{
169+
label: 'Setting up logical replication as a publisher',
170+
slug: 'logical-replication-as-publisher',
171+
},
168172
{
169173
label:
170174
'Connecting Managed Databases to Kubernetes clusters',

pages/managed-databases-for-postgresql-and-mysql/reference-content/pg-version-updates.mdx

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,46 @@ dates:
88

99
This page lists updates for PostreSQL versions and their corresponding features that are supported at Scaleway.
1010

11+
## PostgreSQL 17
12+
13+
Find below the features and updates available with PostgreSQL 17.
14+
15+
### Roles
16+
17+
The `RDB` admin role can now assign the following predefined roles:
18+
19+
| Role | Description |
20+
| :--- | :--- |
21+
| `pg_maintain` | Allows executing `VACUUM`, `ANALYZE`, `CLUSTER`, `REFRESH MATERIALIZED VIEW`, `REINDEX`, and `LOCK TABLE` on all relations, simulating `MAINTAIN` rights on those objects, even without having it explicitly. |
22+
23+
### Features
24+
25+
New features are available with PostgreSQL 17:
26+
27+
- The logical [replication of databases as a publisher](/managed-databases-for-postgresql-and-mysql/api-cli/logical-replication-as-publisher) is now supported.
28+
29+
### Extensions
30+
31+
- **PostGIS** - 3.14 and [3.5](https://postgis.net/tags/3.5/)
32+
- **Timescale** - [2.2](https://github.com/timescale/timescaledb/releases/tag/2.22.1)
33+
- **pgRouting** - [3.7.3](https://github.com/pgRouting/pgrouting/releases/tag/v3.7.3)
34+
- **pgvector** - [0.8.1](https://github.com/pgvector/pgvector/releases/tag/v0.8.1)
35+
- **H3 PG** - [4.2.3](https://github.com/zachasme/h3-pg/releases/tag/v4.2.3)
36+
37+
### Advanced settings
38+
39+
These are the new advanced settings available with PostgreSQL 17:
40+
41+
- `rdb.enable_logical_replication`
42+
- `sync_replication_slots`
43+
44+
Refer to the official [Server configuration](https://www.postgresql.org/docs/17/runtime-config-compatible.html) PostgreSQL documentation for more information.
45+
1146
## PostgreSQL 16
1247

1348
Find below the new features and updates available with PostgreSQL 16.
1449

15-
## Roles
50+
### Roles
1651

1752
The `RDB` admin role can now assign the following predefined roles:
1853

@@ -32,15 +67,15 @@ The `RDB` admin role can now assign the following predefined roles:
3267
Refer to the official [Predefined Roles](https://www.postgresql.org/docs/16/predefined-roles.html) PostgreSQL documentation for a more detailed description of the roles above.
3368
</Message>
3469

35-
## Features
70+
### Features
3671

3772
New features are available with PostgreSQL 16:
3873

3974
- The logical replication of databases as a subscriber is now supported. Refer to the [Setting up logical replication as a subscriber in PostgreSQL](/managed-databases-for-postgresql-and-mysql/api-cli/logical-replication-as-subscriber) documentation page for more information.
4075
- Passwords are now encrypted using the `SCRAM-SHA-256` setting. If you upgrade your engine and are currently using MD5, you also have to migrate to SCRAM-SHA-256. Refer to the official [Password Authentication](https://www.postgresql.org/docs/current/auth-password.html#AUTH-PASSWORD) PostgreSQL documentation to learn how to do so.
4176
- Support of the Timescale [pre-restore](https://docs.timescale.com/api/latest/administration/#timescaledb_pre_restore) and [post-restore](https://docs.timescale.com/api/latest/administration/#timescaledb_post_restore) features. They allow you to restore the database using `pg_restore`.
4277

43-
## Extensions
78+
### Extensions
4479

4580
The following extensions were also upgraded.
4681

@@ -51,11 +86,11 @@ The following extensions were also upgraded.
5186
- **pgvector** - [0.8.0](https://github.com/pgvector/pgvector/releases/tag/v0.8.0)
5287
- **H3 PG** - [4.1.4](https://github.com/zachasme/h3-pg/releases/tag/v4.1.4)
5388

54-
## Advanced settings
89+
### Advanced settings
5590

5691
These are the new advanced settings available with PostgreSQL 16:
5792

58-
### Autovaccum
93+
#### Autovaccum
5994

6095
- `autovacuum_vacuum_insert_scale_factor`
6196
- `autovacuum_vacuum_insert_threshold`
@@ -64,7 +99,7 @@ These are the new advanced settings available with PostgreSQL 16:
6499

65100
Refer to the official [Autovaccum](https://www.postgresql.org/docs/16/routine-vacuuming.html#AUTOVACUUM) PostgreSQL documentation for more information.
66101

67-
### Error reporting and logging
102+
#### Error reporting and logging
68103

69104
- `cron.timezone`
70105
- `log_checkpoints`
@@ -75,7 +110,7 @@ Refer to the official [Autovaccum](https://www.postgresql.org/docs/16/routine-va
75110

76111
Refer to the official [Error reporting and logging](https://www.postgresql.org/docs/16/runtime-config-logging.html) PostgreSQL documentation for more information.
77112

78-
### PG stats parameters
113+
#### PG stats parameters
79114

80115
- `pg_stat_statements.max`
81116
- `pg_stat_statements.track`

0 commit comments

Comments
 (0)