Skip to content

Commit 6c065eb

Browse files
Updated Postgres Best Practices for Replication Slots (#276)
* Added a new section around managing replication slots and also renamed the max_slot_wal_keep_size section to Managing & Monitoring Replication Lag * Small wording changes
1 parent dfeba51 commit 6c065eb

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

resources/production-readiness-guide.mdx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ The easiest way to check for replication issues is to look at the Diagnostics en
253253

254254
## Postgres
255255

256-
### `max_slot_wal_keep_size`
256+
### Managing & Monitoring Replication Lag
257257

258-
This Postgres [configuration parameter](https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-MAX-SLOT-WAL-KEEP-SIZE) limits the size of the Write-Ahead Log (WAL) files that replication slots can hold.
259-
Because PowerSync uses logical replication it's important to consider the size of the `max_slot_wal_keep_size` and monitoring lag of replication slots used by PowerSync in a production environment to ensure lag of replication slots do not exceed the `max_slot_wal_keep_size`.
258+
Because PowerSync relies on Postgres logical replication, it's important to consider the size of the `max_slot_wal_keep_size` and monitoring lag of replication slots used by PowerSync in a production environment to ensure lag of replication slots do not exceed the `max_slot_wal_keep_size`.
259+
260+
<Tip>The `max_slot_wal_keep_size` Postgres [configuration parameter](https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-MAX-SLOT-WAL-KEEP-SIZE) limits the size of the Write-Ahead Log (WAL) files that replication slots can hold.</Tip>
260261

261262
The WAL growth rate is expected to increase substantially during the initial replication of large datasets with high update frequency, particularly for tables included in the PowerSync publication.
262263

@@ -271,10 +272,10 @@ To view the current replication slots that are being used by PowerSync you can r
271272

272273
```
273274
SELECT slot_name,
274-
plugin,
275-
slot_type,
276-
active,
277-
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS replication_lag
275+
plugin,
276+
slot_type,
277+
active,
278+
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS replication_lag
278279
FROM pg_replication_slots;
279280
```
280281

@@ -287,3 +288,32 @@ WHERE name = 'max_slot_wal_keep_size'
287288

288289
It's recommended to check the current replication slot lag and `max_slot_wal_keep_size` when deploying Sync Rules changes to your PowerSync Service instance, especially when you're working with large database volumes.
289290
If you notice that the replication lag is greater than the current `max_slot_wal_keep_size` it's recommended to increase value of the `max_slot_wal_keep_size` on the connected source Postgres database to accommodate for the lag and to ensure the PowerSync Service can complete initial replication without further delays.
291+
292+
### Managing Replication Slots
293+
294+
Under normal operating conditions when new Sync Rules are deployed to a PowerSync Service instance, a new replication slot will also be created and used for replication. The old replication slot from the previous version of the Sync Rules will still remain, until Sync Rules reprocessing is completed, at which point the old replication slot will be removed by the PowerSync Service.
295+
However, in some cases, a replication slot may remain without being used. Usually this happens when a PowerSync Service instance is de-provisioned, stopped intentionally or due to unexpected errors. This results in excessive disk usage due to the continued growth of the WAL.
296+
297+
To check which replication slots used by a PowerSync Service are no longer active, the following query can be executed against the source Postgres database:
298+
```
299+
SELECT slot_name,
300+
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS replication_lag
301+
FROM pg_replication_slots WHERE active = false;
302+
```
303+
304+
If you have inactive replication slots that need to be cleaned up, you can drop them using the following query:
305+
```
306+
SELECT slot_name,
307+
pg_drop_replication_slot(slot_name)
308+
FROM pg_replication_slots where active = false;
309+
```
310+
311+
The alternative to manually checking for inactive replication slots would be to configure the `idle_replication_slot_timeout` configuration parameter on the source Postgres database.
312+
313+
<Note>The `idle_replication_slot_timeout` [configuration parameter](https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-IDLE-REPLICATION-SLOT-TIMEOUT) is only available from PostgresSQL 18 and above.</Note>
314+
315+
The `idle_replication_slot_timeout` will invalidate replication slots that have remained inactive for longer than the value set for the `idle_replication_slot_timeout` parameter.
316+
317+
It's recommended to configure this parameter for source Postgres databases as this will prevent runaway WAL growth for replication slots that are no longer active or used by the PowerSync Service.
318+
319+

0 commit comments

Comments
 (0)