Giter Site home page Giter Site logo

suessflorian / postgres-query-federation Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 16 KB

A use case for cross database queries in Postgres utilising foreign data; https://www.postgresql.org/docs/13/ddl-foreign-data.html

Go 100.00%
postgressql golang foreign-data-wrapper

postgres-query-federation's Introduction

Postgres query federation via foreign tables

Serves as an example of foreign data federation in Postgres as per SQL/MED.

Postgres is shipped with the postgres_fdw module. Providing the ability to create foreign data wrappers that are used to establish servers that populate locally queryable foreign tables.

Consider a separate database to that of any of the tenants' databases;

CREATE SERVER tenant_a_fdw_target
  FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS
      (DBNAME 'tenant_a', HOST 'our-rds-cluster.region.rds.amazonaws.com', SSLMODE 'require');

CREATE FOREIGN TABLE tenant_a_entities(
  id uuid,
  last_updated timestamp
) SERVER tenant_a_fdw_target OPTIONS( TABLE_NAME 'entities');

Creates a locally querable construct called foreign table.

-- \dE to list foreign tables

-- for example, staleness monitoring of entities
SELECT
  id,
  EXTRACT(EPOCH FROM (current_timestamp - min(last_updated))) AS time_since_refreshed
FROM ${tenant}_entities group;

Use case

Simply put; cross data source queries, data source being anything queryable.

This is not limited to postgres databases, could be anything that has an associated Postgres foreign data wrapper. Exhaustive list here.

Note; there is currently no such wrapper for a GraphQL data source (as of Oct 1st 2021).

Example here

Grafana is a very popular monitoring and observability platform that namely supports the ingestion of an increasing list of datasources. However, Grafana defines a datasources via a single connection. Problems:

  • In a tenant per database architecture, the tenant count would linearly affect the quantity of data sources required for full monitoring coverage.
  • This is especially a problem with regard to multi-service architectures (for example, micro-services), as service boundaries are quite commonly define database boundaries. This suggests service count could also linearly affect the quantity of data sources required for full monitoring coverage.

Consequently creating quite a bit of management overhead:

  • Grafana advices read only data source role permissions to help restrict unintended data exposure and/or accidental data damages.
  • Grafana handling an increasing amount database connections, effecting dashboard loading times significantly.
  • Grafana alert management will also be forced per data source.
  • Motivates the need for tenant implementation automation (extra work required for setting up a tenant).

Solution

This setup here will simulate the utilisation of query federation to aggregate cross database metrics resembling that of a typical multi-tenanted architecture.

docker compose up --detach
go run .

Navigate to http://localhost:8000/d/federated/example and you'll see a repeat panel reacting to a tenant variable that itself is based off a tenant listing query.

Feel free to also verify only a single datasource is configured.

postgres-query-federation's People

Contributors

suessflorian avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.