pgspider / dynamodb_fdw

DynamoDB Foreign Data Wrapper for PostgreSQL
Other
28 stars 5 forks source link

DynamoDB Foreign Data Wrapper for PostgreSQL

This is a foreign data wrapper (FDW) to connect PostgreSQL to DynamoDB.

PostgreSQL + DynamoDB

Contents

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

Features

Common features

Pushdowning

Common pushdowning

Comparison operators

No PostgreSQL Remark
1 = Equal to
2 <> or != Not Equal to
3 > Greater than
4 < Less than
5 >= Greater than or equal to
6 <= Less than or equal to

Logical operators

No PostgreSQL Remark
1 AND TRUE if all the conditions separated by AND are TRUE
2 BETWEEN TRUE if the operand is within the range of comparisons
3 IN Only support IN with a list of value. Return TRUE if the operand is equal to one of a list of expressions.
4 IS Only support IS NULL. Return TRUE if the operand is NULL.
5 NOT Reverses the value of a given Boolean expression
6 OR TRUE if any of the conditions separated by OR are TRUE

Dereference operators

No PostgreSQL Remark
1 -> Extracts JSON object field with the given key. This mapping will be used when right operand is an attribute name (which is represented as a text constant).
2 ->> Extracts JSON object field with the given key, as text.
3 -> Extract nested element of List type. This mapping will be used when right operand is a number.

Notes about features

Also see Limitations.

Supported platforms

dynamodb_fdw was developed on Linux, and should run on any reasonably POSIX-compliant system.

dynamodb_fdw is designed to be compatible with PostgreSQL 13 ~ 16.0.

Installation

Prerequisites

  1. AWS C++ SDK DynamoDB FDW uses the APIs provided by AWS C++ SDK to connect and execute query on DynamoDB. It requires gcc version 4.9.0 and above to be able to use and compile. It also requires 3rd party libraries: libcurl, openssl, libuuid, pulseaudio-libs.
  2. Java Runtime Environment (JRE) version 8.x or newer If using DynamoDB local, JRE 8.x or newer is required.

Source installation

This section describle how to install required library on CentOS 7.

  1. AWS C++ SDK Download and follow the Amazon developer guide.

    https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/setup-linux.html
  2. Java Runtime Environment (JRE) version 8.x or newer

    $ sudo yum install java-1.8.0-openjdk-devel.x86_64
  3. Build and install PostgreSQL: from PostgreSQL directory

    ./configure
    $ make
    $ make install
  4. Build and install DynamoDB FDW: clone source code under PostgreSQL/contrib/

    $ make
    $ make install

Usage

CREATE SERVER options

dynamodb_fdw accepts the following options via the CREATE SERVER command:

CREATE USER MAPPING options

dynamodb_fdw accepts the following options via the CREATE USER MAPPING command:

CREATE FOREIGN TABLE options

dynamodb_fdw accepts the following table-level options via the CREATE FOREIGN TABLE command.

The following column-level options are available:

IMPORT FOREIGN SCHEMA options

dynamodb_fdw don't support IMPORT FOREIGN SCHEMA and accepts no custom options.

TRUNCATE support

dynamodb_fdw don't support the foreign data wrapper TRUNCATE API, available from PostgreSQL 14.

Functions

As well as the standard dynamodb_fdw_handler() and dynamodb_fdw_validator() functions, dynamodb_fdw provides the following user-callable utility functions: Functions from this FDW in PostgreSQL catalog are yet not described.

Data type mapping

No PostgreSQL DynamoDB Remark
1 boolean Boolean N/A
2 bytea Binary PartiQL of DynamoDB does not have any way to represent binary data. Therefore, DynamoDB FDW only supports selecting Binary column. DynamoDB FDW does not support Binary column in WHERE clause.
3 JSON/JSONB Map N/A
4 NULL Null N/A
5 smallint, integer, bigint, numeric, real, double precision Number N/A
6 smallint[], integer[], bigint[], numeric[], real[], double precision[] Number Set N/A
7 text character varying(n)[], varchar(n)[], character(n)[], char(n) [], text[] String Set N/A
8 text character varying(n), varchar(n), character(n), char(n), text String N/A

Identifier case handling

PostgreSQL folds identifiers to lower case by default. Rules and problems with DynamoDB identifiers yet not tested and described.

Generated columns

Behaviour within generated columns yet not tested and described.

For more details on generated columns see:

Character set handling

Yet not described.

Examples

Install the extension:

Once for a database you need, as PostgreSQL superuser.

    CREATE EXTENSION dynamodb_fdw;

Create a foreign server with appropriate configuration:

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

    CREATE SERVER dynamodb_svr
    FOREIGN DATA WRAPPER dynamodb_fdw
    OPTIONS (
      endpoint 'http://localhost:8000'
    );

Grant usage on foreign server to non-superuser in PostgreSQL:

Once for a 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 dynamodb_svr TO pguser;

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

Create an appropriate user mapping:

    CREATE USER MAPPING
    FOR pgser
    SERVER dynamodb_svr 
        OPTIONS(
      username 'username',
      password 'password'
    );

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

Create a foreign table referencing the dynamodb table:

    CREATE FOREIGN TABLE frtbl (
      c1 int,
      c2 text,
      c3 jsonb
    )
    SERVER dynamodb_svr
    OPTIONS (
      table_name 'table1'
    );

Query the foreign table.

    SELECT *
    FROM frtbl;

Limitations

Contributing

Opening issues and pull requests on GitHub are welcome.

Useful links

Source code

Reference FDW realisation, postgres_fdw

General FDW Documentation

Other FDWs

License

Copyright and license information can be found in the file LICENSE.