> ## Documentation Index
> Fetch the complete documentation index at: https://docs.powersync.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Postgres Maintenance

> Manage Postgres replication slots and WAL lag for reliable PowerSync replication.

## Logical Replication Slots

Postgres logical replication slots are used to keep track of [replication](/architecture/powersync-service#replication-from-the-source-database) progress (recorded as a [LSN](https://www.postgresql.org/docs/current/datatype-pg-lsn.html)).

Every time a new version of [Sync Streams or Sync Rules](/sync/overview) are deployed, PowerSync creates a new replication slot, then switches over and deletes the old replication slot when the reprocessing of the new Sync Streams/Rules version is done.

The replication slots can be viewed using this query:

```sql theme={null}
select slot_name, confirmed_flush_lsn, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) as lag from pg_replication_slots;
```

Example output:

| slot\_name             | confirmed\_flush\_lsn | active | lag      |
| ---------------------- | --------------------- | ------ | -------- |
| powersync\_1\_c3c8cf21 | 0/70D8240             | 1      | 56 bytes |
| powersync\_2\_e62d7e0f | 0/70D8240             | 1      | 56 bytes |

In some cases, a replication slot may remain without being used. In this case, the slot prevents Postgres from deleting older WAL entries. One such example is when a PowerSync instance has been deprovisioned.

While this is desired behavior for slot replication downtime, it could result in excessive disk usage if the slot is not used anymore.

Inactive slots can be dropped using:

```bash theme={null}
select slot_name, pg_drop_replication_slot(slot_name) from pg_replication_slots where active = false;
```

Postgres prevents active slots from being dropped. If it does happen (e.g. while a PowerSync instance is disconnected), PowerSync would automatically re-create the slot, and restart replication.

### Maximum Replication Slots

Postgres is configured with a maximum number of replication slots per server. Since each PowerSync instance uses one replication slot for replication and an additional one while deploying a new Sync Streams/Rules version, the maximum number of PowerSync instances connected to one Postgres server is equal to the maximum number of replication slots, minus 1.

If other clients are also using replication slots, this number is reduced further.

The maximum number of slots can be configured by setting `max_replication_slots` (not all hosting providers expose this), and checked using:

```sql theme={null}
select current_setting('max_replication_slots')
```

If this number is exceeded, you'll see an error such as "all replication slots are in use".
