Coder Social home page Coder Social logo

pg_follower's Introduction

pg_follower - Capture changes and follow

The pg_follower extension provides a logical replication feature for PostgreSQL.

Note that this extension was created for educational purposes. Please do not use it in the production stage.

Workflow result

Prerequisite

The pg_follower extension must be installed on both upstream and downstream. Since this extension intensely uses the logical decoding mechanism, the wal_level must be set to logical on the upstream.

Usage

This section describes the primary usage of pg_follower.

At first, you must install pg_follower extension on both node, e.g.:

upstream=# CREATE EXTENSION pg_follower ;
CREATE EXTENSION

Then, start_follow can be called on the downstream, with a connection string toward the upstream:

downstream=# SELECT * FROM start_follow('user=postgres port=5431');
 start_follow
--------------

(1 row)

This function kicks the background worker, which receives and applies changes from the upstream.

$ ps aux | grep postgres
...
hayato     80919  0.0  0.2 203692 11888 ?        Ss   03:06   0:00 postgres: pg_follower worker
hayato     80920  0.0  0.4 211184 16092 ?        Ss   03:06   0:00 postgres: walsender postgres postgres [local] START_REPLICATION

After that, the downstream can follow changes done on the upstream. Assuming a table is created and 20 tuples are inserted upstream.

upstream=# CREATE TABLE foo (id int);
CREATE TABLE
upstream=# INSERT INTO foo VALUES (generate_series(1, 20));
INSERT 0 20

After a specific time, we can see the table is also created on the downstream, and tuples exist.

downstream=# SELECT * FROM foo ;
 id
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
(20 rows)

Supported feature

For now, only INSERT, CREATE TABLE, DROP TABLE, and TRUNCATE statements can be replicated. Any constraints and parameters for the CREATE TABLE would be ignored. Also, an ERROR would be raised if below clauses are used:

  • UNLOGGED
  • TEMPORARY
  • PARTITION OF
  • PARTITION BY
  • INHERITS
  • OF type_name

Internals

The pg_follower extension contains a logical decoding output plugin, a background worker, and an event trigger.

logical decoding output plugin

The logical decoding plugin outputs a mimic of raw SQL statements from reorder-buffer changes.

background worker

The worker connects to the upstream via the libpqwalreceiver shared library. The connection string is passed from the kick function. Then, the worker creates a temporary replication slot with the output plugin described above and requests stream changes.

When the worker receives messages (it would be a usual SQL statement) from the upstream, it opens a transaction and executes them via SPI.

event trigger

The event trigger will fire when DDL commands end. In the trigger function, the parse-tree is checked and de-parsed into an SQL statement. The result would be written to WAL record as logical decoding messages.

TODO

  • Add support for table/column constraints
  • Add support for UPDATE statement
  • Add support for DELETE statement

pg_follower's People

Contributors

huutfj avatar

Watchers

 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.