# Storage

> The tables behind each Redis data type and how keys, expiry and streams are stored.

---

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

---

All durable Redis state lives in ordinary WAL-logged PostgreSQL tables inside schema `redis`. The Go gateway holds no persistent data of its own.

## Keyspace and binary identity {#keys}

Every Redis key has one row in `redis._keyspace`:

```sql
CREATE TABLE redis._keyspace (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    db integer NOT NULL CHECK (db BETWEEN 0 AND 15),
    digest bytea NOT NULL CHECK (octet_length(digest) = 32),
    key bytea NOT NULL,
    kind "char" NOT NULL DEFAULT 's',
    value bytea,
    expires bigint
) WITH (fillfactor = 85, autovacuum_vacuum_scale_factor = 0.02);

CREATE INDEX ON redis._keyspace (db, digest);
CREATE INDEX ON redis._keyspace (expires) WHERE expires IS NOT NULL;
```

- **Digest-indexed lookup:** Unbounded binary keys (`bytea`, up to 512 MiB) never enter a PostgreSQL B-tree index directly. Instead, `pg_redis` indexes `(db, digest)` using a 32-byte SHA-256 digest, locks the digest bucket, and compares the full `key` bytes to resolve hash collisions safely.
- **Collection child rows:** Hash fields (`redis._hash`), Set members (`redis._set`), ZSet members (`redis._zset`), Stream groups (`redis._stream_group`) and Stream consumers (`redis._stream_consumer`) use the same 32-byte `digest` + full `bytea` comparison rule, referencing `redis._keyspace(id)` with `ON DELETE CASCADE`.
- **Sorted sets:** `redis._zset` stores `score double precision` (rejecting `NaN`) and breaks ties by comparing full member bytes.
- **Lists and length cache:** `redis._list` orders elements by `(kid, pos, id)` without putting element `value` bytes in a B-tree. Schema `0.54` adds `redis._list_length(kid, length)`, maintained by statement-level transition-table triggers on `redis._list` so `LLEN`, `LPUSH` and `RPUSH` read one row instead of counting every element.

## Table inventory (schema 0.54) {#tables}

| Category | Tables | Purpose |
| --- | --- | --- |
| Keyspace and Strings | `redis._keyspace` | Logical DB (`0`–`15`), 32-byte digest, binary key, type tag (`s`, `h`, `l`, `S`, `z`, `x`), inline payload (`value` for Strings, Bitmaps, HLL) and millisecond deadline (`expires`) |
| Hashes | `redis._hash` | `(kid, digest, field, value)` rows per hash field |
| Lists | `redis._list`, `redis._list_length` | `(kid, pos, value)` element rows and statement-trigger-maintained per-list length cache |
| Sets | `redis._set` | `(kid, digest, member)` rows per set member |
| Sorted sets ( and Geo) | `redis._zset` | `(kid, digest, member, score)` rows; Geo keys store 52-bit geohash integers as ZSet scores |
| Streams | `redis._stream`, `redis._stream_entry`, `redis._stream_node`, `redis._stream_radix`, `redis._stream_group`, `redis._stream_consumer`, `redis._stream_pel`, `redis._stream_config` | Stream metadata (`last_id`, `max_deleted_id`, `entries_added`), live entries, listpack nodes, radix tree prefixes, consumer groups, consumers and Pending Entries List (PEL) |
| Object metadata | `redis._object_encoding`, `redis._object_heat`, `redis._object_shared_heat`, `redis._object_reference`, `redis._object_config` | Encoding state, optional listpack/quicklist layout (`pg_redis.list_layout_tracking`), optional LRU/LFU heat (`pg_redis.object_heat_tracking`) and shared-integer tracking |
| Transactions | `redis._transaction_session`, `redis._watch`, `redis._exec_context`, `redis._mutation_stats` | `WATCH` key versions, `EXEC` context and per-key mutation counters |
| Blocking commands | `redis._blocked`, `redis._block_keys`, `redis._ready` | Blocked waiter registrations, watched keys per waiter and committed handoff replies |
| Pub/Sub | `redis._pubsub_session`, `redis._pubsub_subscription`, `redis._pubsub_message`, `redis._notify_config` | Subscriber sessions, channel/pattern/shard bindings, message ring pages and keyspace-event configuration |
| Client tracking | `redis._tracking_client`, `redis._tracking_key`, `redis._tracking_prefix`, `redis._tracking_pending`, `redis._tracking_config` | Client-side caching sessions, tracked key digests, `BCAST` prefixes and queued invalidations |
| Auth and Redis ACL | `redis._acl_user`, `redis._acl_session`, `redis._acl_log`, `redis._acl_config`, `redis._acl_service`, `redis._auth_ticket`, `redis._auth_key_ticket` | Redis ACL users, rules, security log and session tickets |
| Diagnostics and control | `redis._monitor_client`, `redis._client_pause_control`, `redis._debug_control`, `redis._output_config`, `redis._slowlog_config`, `redis._encoding_config`, `redis._hll_config`, `redis._runtime_identity` | `MONITOR` observers, `CLIENT PAUSE` deadlines, `DEBUG PAUSE-CRON` / `SET-ACTIVE-EXPIRE` state, output buffer limits, `SLOWLOG` settings and installation identity |
{.wide}

## Encoding history and Streams {#encoding}

`redis._object_encoding` stores per-key encoding metadata (`int`, `embstr`, `raw`, `listpack`, `hashtable`, `intset`, `skiplist`, `stream`), and `COPY` copies this representation state alongside the key. Deleting a key cascades to its child rows and encoding metadata; transaction and savepoint rollback restore both business values and encoding state.

Streams persist both live entries (`redis._stream_entry`) and physical listpack/radix structures (`redis._stream_node`, `redis._stream_radix`) so `XTRIM` approximate trimming and `XINFO STREAM FULL` reflect the recorded structure.

## Runtime statistics versus durable rows {#statistics}

Keyspace modifications and dirty counters participate in PostgreSQL WAL transactions. Runtime command call counters (`INFO commandstats`, `INFO stats`) live in bounded shared memory partitioned by database OID and extension installation identity:

- Rolling back a SQL transaction undoes all table writes and queued notifications, while the in-memory call counter records that the command handler ran.
- Blocking command completion counters update only after the transaction that fixes the blocking result commits; repeated `blocking_poll` calls do not increment completion counts again.

---

Backlinks:

- [Architecture](/docs/redis/architecture/)
- [Data types](/docs/redis/reference/data-types/)
