GitHub - pg-redis-fdw/redis_fdw: A PostgreSQL foreign data wrapper for Redis (original) (raw)

Redis Foreign Data Wrapper for PostgreSQL

This is a foreign data wrapper (FDW) to connect PostgreSQLto Redis key/value databases. This FDW works with PostgreSQL 10+ and confirmed with some Redis versions near 6.0.

PostgreSQL + Redis

This code was originally experimental, and largely intended as a pet project for Dave to experiment with and learn about FDWs in PostgreSQL. It has now been extended for production use by Andrew.

image

By all means use it, but do so entirely at your own risk! You have been warned!

Contents

  1. Features
  2. Supported platforms
  3. Installation
  4. Usage
  5. Functions
  6. Identifier case handling
  7. Generated columns
  8. Character set handling
  9. Examples
  10. Limitations
  11. Tests
  12. Contributing
  13. Useful links
  14. License and authors

Features

Common features

Pushdowning

Not supported, there is no common calculations in Redis.

Notes about features

Also see Limitations

Supported platforms

redis_fdw was developed on Linux and Mac OS X and should run on any reasonably POSIX-compliant system. Dave has tested the original on Mac OS X 10.6 only, and Andrew on Fedora and Suse. Other *nix's should also work. Neither of us have tested on Windows, but the code should be good on MinGW.

Installation

Package installation

No deb or rpm packages are available.

Source installation

Prerequisites:

Build and install on OS

Ensure pg_config is callable without full path, build and install regis_fdwwith commands below. Use release you need instead of {REL}, for ex.REL_15_STABLE, REL_16_STABLE.

git clone https://github.com/pg-redis-fdw/redis_fdw.git -b {REL}

make USE_PGXS=1 sudo make install USE_PGXS=1

Make necessary changes for your PostgreSQL version if needed. You will need to have the right branch checked out to match the PostgreSQL release you are building against, as the FDW API has changed from release to release.

Usage

CREATE SERVER options

redis_fdw accepts the following options via the CREATE SERVER command:

CREATE USER MAPPING options

redis_fdw accepts the following options via the CREATE USER MAPPINGcommand:

CREATE FOREIGN TABLE options

redis_fdw accepts the following table-level options via theCREATE FOREIGN TABLE command:

You can only have one of tablekeyset and tablekeyprefix, and if you usesingleton_key you can't have either.

Structured items are returned as array text, or, if the value column is a text array as an array of values. In the case of hash objects this array is an array of key, value, key, value ...

Singleton key tables are returned as rows with a single column of text in the case of lists sets and scalars, rows with key and value text columns for hashes, and rows with a value text columns and an optional numeric score column for zsets.

IMPORT FOREIGN SCHEMA options

redis_fdw doesn't support IMPORT FOREIGN SCHEMA and accepts no custom options for this command. There is no formal storing schema in Redis in oppose to RDBMS.

TRUNCATE support

redis_fdw doesn't implements the foreign data wrapper TRUNCATE API, available from PostgreSQL 14.

Functions

As well as the standard redis_fdw_handler() and redis_fdw_validator()functions, redis_fdw provides no user-callable utility functions.

Identifier case handling

PostgreSQL folds identifiers to lower case by default, Redis is case sensetive by default. It's important to be aware of potential issues with table and column names. If there will no proper name quoting in PostgreSQL, access from PostgreSQL foreign tables with mixedcase or uppercase names to mixedcase or uppercase Redis objects can cause unexpected results.

Generated columns

Redis doesn't provide support for generated columns.

For more details on generated columns see:

Character set handling

All strings from Redis are interpreted acording to the PostgreSQL database's server encoding. Redis supports UTF-8 only data. It's not a problem if the PostgreSQL server encoding is UTF-8. Behaviour with non-UTF8 PostgreSQL servers is undefined and untested. It is not recommended to use redis_fdw with non UTF-8 PostgreSQL databases.

Examples

Install the extension:

Once for a database you need, as PostgreSQL superuser.

CREATE EXTENSION redis_fdw;

Create a foreign server with appropriate configuration:

Once for a foreign datasource you need, as PostgreSQL superuser.

CREATE SERVER redis_server
FOREIGN DATA WRAPPER redis_fdw
OPTIONS (
  address '127.0.0.1',
  port '6379'
);

Grant usage on foreign server to normal user in PostgreSQL:

Once for a normal user (non-superuser) in PostgreSQL, as PostgreSQL superuser. It is a good idea to use a superuser only where really necessary, so let's allow a normal user to use the foreign server (this is not required for the example to work, but it's secirity recomedation).

GRANT USAGE ON FOREIGN SERVER redis_server TO pguser;

Where pguser is a sample user for works with foreign server (and foreign tables).

User mapping

CREATE USER MAPPING FOR pguser
SERVER redis_server
OPTIONS (
  password 'secret'
);

Where pguser is a sample user for works with foreign server (and foreign tables).

Create foreign table

All CREATE FOREIGN TABLE SQL commands can be executed as a normal PostgreSQL user if there were correct GRANT USAGE ON FOREIGN SERVER. No need PostgreSQL supersuer for secirity reasons but also works with PostgreSQL supersuer.

Simple table

CREATE FOREIGN TABLE redis_db0 (
  key text,
  val text
)
SERVER redis_server
OPTIONS (
  database '0'
);

Hash table + tablekeyprefix

CREATE FOREIGN TABLE myredishash (
  key text,
  val text[]
)
SERVER redis_server
OPTIONS (
  database '0',
  tabletype 'hash',
  tablekeyprefix 'mytable:'
);

INSERT INTO myredishash (key, val)
VALUES ('mytable:r1','{prop1,val1,prop2,val2}');

UPDATE myredishash
   SET val = '{prop3,val3,prop4,val4}'
 WHERE key = 'mytable:r1';

DELETE from myredishash
 WHERE key = 'mytable:r1';

Hash table + singleton_key

CREATE FOREIGN TABLE myredis_s_hash (
  key text,
  val text
)
SERVER redis_server
OPTIONS (
  database '0',
  tabletype 'hash',
  singleton_key 'mytable'
);

INSERT INTO myredis_s_hash (key, val)
VALUES ('prop1','val1'),('prop2','val2');

UPDATE myredis_s_hash
   SET val = 'val23'
 WHERE key = 'prop1';

DELETE from myredis_s_hash
 WHERE key = 'prop2';

Limitations

SQL commands

Other

Tests

The tests for PostgreSQL assume that you have access to a Redis server on the local machine with no password, and uses PostgreSQL 15 server with_english_ locale. This database must be empty, and that the redis-cli program is in the PATH envireonment variable when tests is run. The test script checks that the database is empty before it tries to populate it, and it cleans up afterwards.

Some tests as psql expected outputs can be found in test/expected directory.

Contributing

Opening issues and pull requests on GitHub are welcome.

Redis selected documentation

Source code

Reference FDW implementation, postgres_fdw

General FDW Documentation

Other FDWs

License and authors