Redis
Redis is an open-source in-memory storage, used as a distributed, in-memory key–value database, cache and message broker, with optional durability.
The Redis Wrapper allows you to read data from Redis within your Postgres database.
Warning
Restoring a logical backup of a database with a materialized view using a foreign table can fail. For this reason, either do not use foreign tables in materialized views or use them in databases with physical backups enabled.
Supported Redis Data Types
All Redis value will be stored as text
or jsonb
column in Postgres, below are the supported Redis data types:
Redis Type | Foreign Table Type (src_type) |
---|---|
List | list |
Set | set |
Hash | hash |
Sorted Set | zset |
Stream | stream |
Multiple List | multi_list |
Multiple Set | multi_set |
Multiple Hash | multi_hash |
Multiple Sorted Set | multi_zset |
See below for more descriptions for the Multiple *
types and src_type
foreign table option.
Preparation
Before you get started, make sure the wrappers
extension is installed on your database:
1 |
|
and then create the foreign data wrapper:
1 2 3 |
|
Secure your credentials (optional)
By default, Postgres stores FDW credentials inside pg_catalog.pg_foreign_server
in plain text. Anyone with access to this table will be able to view these credentials. Wrappers is designed to work with Vault, which provides an additional level of security for storing credentials. We recommend using Vault to store your credentials.
1 2 3 4 5 6 7 |
|
Connecting to Redis
We need to provide Postgres with the credentials to connect to Redis. We can do this using the create server
command:
1 2 3 4 5 |
|
1 2 3 4 5 |
|
The connection URL format is:
1 |
|
Creating Foreign Tables
The Redis Wrapper supports data reads from Redis.
Integration | Select | Insert | Update | Delete | Truncate |
---|---|---|---|---|---|
Redis | ✅ | ❌ | ❌ | ❌ | ❌ |
For example:
1 2 3 4 5 6 7 8 |
|
The foreign table columns names and types must be fixed for each source type, as listed below:
src_type | Column name | Column type |
---|---|---|
list, set, zset | element |
text |
hash | key |
text |
value |
text | |
stream | id |
text |
items |
jsonb | |
multi_* | key |
text |
items |
jsonb |
See below for the full list of src_type
and descriptions.
Foreign table options
The full list of foreign table options are below:
src_type
- Foreign table source type in Redis, required.
This can be one of below types,
Source type | Description |
---|---|
list | Single list |
set | Single set |
hash | Single hash |
zset | Single sorted set |
stream | Stream |
multi_list | Multiple lists, specified by src_key pattern |
multi_set | Multiple sets, specified by src_key pattern |
multi_hash | Multiple hashes, specified by src_key pattern |
multi_zset | Multiple sorted sets, specified by src_key pattern |
src_key
- Source object key in Redis, required.
This key can be a pattern for multi_*
type of foreign table. For other types, this key must return exact one value. For example,
Source Type | src_key examples |
---|---|
list, set, hash, zset, stream | my_list , list:001 , hash_foo , zset:1000 and etc. |
multi_list, multi_set, multi_hash, multi_zset | my_list:* , set:* , zset:* and etc. |
Query Pushdown Support
This FDW doesn't supports pushdown.
Examples
Some examples on how to use Redis foreign tables.
Let's prepare some source data in Redis CLI first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Basic example
This example will create foreign tables inside your Postgres database and query their data:
- List
1 2 3 4 5 6 7 8 9 10 |
|
Query result:
1 2 3 4 5 6 |
|
- Set
1 2 3 4 5 6 7 8 9 10 |
|
Query result:
1 2 3 4 5 6 |
|
- Hash
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|
- Sorted set
1 2 3 4 5 6 7 8 9 10 |
|
Query result:
1 2 3 4 5 6 |
|
- Stream
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|
Query multiple objects example
This example will create several foreign tables using pattern in key and query multiple objects from Redis:
- List
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|
- Set
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|
- Hash
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|
- Sorted set
1 2 3 4 5 6 7 8 9 10 11 |
|
Query result:
1 2 3 4 5 |
|