# Upgrade and recovery

> Extension upgrades, backups and recovery with PostgreSQL tools.

---

LLMS index: [llms.txt](/llms.txt)

---

`pgredis` stores its catalog schema and durable data inside PostgreSQL 18. Extension upgrades use `ALTER EXTENSION`, background cleanup runs inside a PostgreSQL background worker, and backup and recovery use standard PostgreSQL physical or logical tools.

## Extension upgrades {#upgrade}

The extension ships sequential SQL migration scripts from `0.1` through `0.54` (`extension/pg_redis--*.sql`). To upgrade an existing database to the current `0.54` schema (ABI `3`):

{.steps}
1. ### Install the updated binary and extension files

   Install the matching `pgredis` gateway binary, `pg_redis.so` library, `pg_redis.control`, and `pg_redis--*.sql` files on the PostgreSQL host. Stop the PostgreSQL instance before replacing a loaded `pg_redis.so` file, then start PostgreSQL and the new gateway.

2. ### Run the catalog update in PostgreSQL

   Connect to each database where `pg_redis` is installed and update the extension as its administrator:

   ```sql
   ALTER EXTENSION pg_redis UPDATE TO '0.54';
   SELECT extversion FROM pg_extension WHERE extname = 'pg_redis';
   SELECT redis._abi_version();
   ```

   Verify that `extversion` is `0.54` and `redis._abi_version()` returns `3`.

3. ### Reconcile prerequisite checks if upgrading from older schemas

   Two migration steps refuse to overwrite ambiguous state automatically:
   - **`0.49 → 0.50` (`redis._acl_service`):** If a bound ACL service role was dropped or renamed in `pg_roles` so its legacy `role_oid` and `role_name` disagree, the migration raises SQLSTATE `55000` (`ACL service bindings require explicit reconciliation before update to 0.50`). Reconcile or unbind the stale entry before retrying.
   - **`0.50 → 0.51` (`stream_group_ready` trigger):** The migration verifies the SHA-256 hash of `redis._stream_group_ready()` before adding a `WHEN (OLD.kind='x')` filter, raising `55000` if the trigger or function was manually customized.

## Background maintenance worker {#maintenance}

While read paths filter out expired keys immediately (`expires_at <= clock_timestamp()`), background reclamation of expired keys and stale session leases runs in the `pg_redis` background worker. Enable it in `postgresql.conf`:

```ini
shared_preload_libraries = 'pg_redis'
pg_redis.maintenance_database = 'postgres'
pg_redis.maintenance_role = 'postgres'
```

Restart PostgreSQL after changing `shared_preload_libraries`. The worker periodically deletes expired rows from `redis._keyspace`, reclaims expired leases in `_pubsub_session`, `_blocked`, `_transaction_session`, and `_acl_session`, and compacts tracking and mutation-counter tables. `DEBUG SET-ACTIVE-EXPIRE 0` and `DEBUG PAUSE-CRON 1` pause periodic maintenance in the current database for deterministic testing; a new postmaster epoch resets both controls automatically.

## Backup and recovery {#backup}

- **Physical backup and PITR:** `pg_basebackup`, WAL archiving, and point-in-time recovery preserve all keyspace tables, typed value tables, Streams metadata, and extension configuration tables together with ordinary SQL tables.
- **Logical backup (`pg_dump` / `pg_restore`):** Use custom format (`pg_dump -Fc`) or plain SQL dumps. Keep PostgreSQL role DDL quiescent during dump and restore when migrating `redis._acl_service` bindings across clusters.

What a logical dump includes and excludes:

{.wide}
| Category | Tables | Logical dump behavior |
| --- | --- | --- |
| Keyspace and values | `_keyspace`, `_string`, `_hash`, `_list`, `_set`, `_zset`, `_stream_*`, `_hll`, `_bitmap` | Restored in full; `expires_at` retains its absolute `timestamptz` deadline. |
| Extension configuration | `_acl_user`, `_acl_config`, `_acl_service`, `_slowlog_config`, `_tracking_config`, `_output_config` | Registered via `pg_extension_config_dump` and restored across clusters. |
| Derived caches | `_list_length` | Excluded from dumps; statement triggers on `redis._list` rebuild it automatically as rows are copied back during `pg_restore`. |
| Transient session state | `_pubsub_session`, `_pubsub_event`, `_blocked`, `_transaction_session`, `_watch`, `_acl_session`, `_tracking_*`, `_monitor_client` | Excluded from dumps; reconnecting clients establish fresh sessions. |
{.wide .expand}

## Failure handling and timeouts {#timeouts}

- **Command and statement timeouts:** Set `-command-timeout` on the gateway and `statement_timeout` / `lock_timeout` on the PostgreSQL role to bound worst-case query duration.
- **Inspecting active backends:** Query `pg_stat_activity` for queries executing `redis._dispatch` or `redis.command` when diagnosing lock waits or slow commands.
- **Handling `UNKNOWN` write outcomes:** If a network drop or gateway timeout interrupts a write after the SQL statement was sent to PostgreSQL, the transaction may have either committed or rolled back. Non-idempotent callers should inspect the target key or application outbox table before retrying.

---

Backlinks:

- [Operations](/docs/redis/operations/)
- [Data types](/docs/redis/reference/data-types/)
