Skip to content

Support for logical streaming replication #1254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

Support for logical streaming replication #1254

wants to merge 6 commits into from

Conversation

kibae
Copy link
Contributor

@kibae kibae commented Apr 2, 2017

Hello,

Please check this code.

  1. database connection for replication mode
  2. WalStream class includes data event for logical replication stream
  3. includes sample code test/test-walstream.js
'use strict';

const WalStream = require(__dirname + '/../lib').WalStream;

var lastLsn = null;

var walStream = new WalStream({});
function proc() {
  walStream.getChanges('test_slot', lastLsn, {
    includeXids: false, //default: false
    includeTimestamp: false, //default: false
    skipEmptyXacts: true, //default: true
  }, function(err) {
    if (err) {
      console.log('Logical replication initialize error', err);
      setTimeout(proc, 1000);
    }
  });
}

walStream.on('data', function(msg) {
  lastLsn = msg.lsn || lastLsn;

  //DO SOMETHING. eg) replicate to other dbms(pgsql, mysql, ...)
  console.log('log recv', msg);
}).on('error', function(err) {
  console.log('Error #2', err);
  setTimeout(proc, 1000);
});

proc();

//If want to stop replication
//walStream.stop();

@@ -82,6 +83,9 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
if(this.database) {
params.push("dbname='" + this.database + "'");
}
if(this.replication) {
params.push("replication='" + (this.database === true ? "true" : this.replication) + "'");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this.database === true intended to read this.replication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented pg_recvlogical's access method the same way.
The replication value can have a value of true or database.

https://github.com/pipelinedb/pipelinedb/blob/bdcd1fcd37584a3ad5427bb1a1b27a1af7521f0c/src/bin/pg_basebackup/streamutil.c#L105

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it’s intended to have:

{
    database: true,
    replication: 'truthy value',
}

resulting in:

[
    "dbname='true'",
    "replication='true'",
]

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Replication" can have "database", "true" and "false" values.

https://github.com/postgres/postgres/blob/19dc233c32f2900e57b8da4f41c0f662ab42e080/src/backend/postmaster/postmaster.c#L2088

For a clean commit log, I will just clean up the connection related code and request a new pull.

@kibae
Copy link
Contributor Author

kibae commented Apr 16, 2017

Let me know if you have a problem with my code.

@brianc
Copy link
Owner

brianc commented Apr 19, 2017

Hey @kibae - this is pretty awesome! Sorry for the delay. I'll merge this now!

What I'm thinking of doing is pulling out the Walstream code into it's own module & only adding the things to node-postgres itself to support the replication. That way the pg-walstream can live on its own. Would you be cool with me doing that or do you want to do that - I'm fine either way? I understand we'll still need the changes inside node-postgres core to support the replication setup & backend-events, but I try to keep things as slim as possible within node-postgres itself - having a separate installable module for the wal-stream will allow it to mature faster on its own.

Does that make sense?

@brianc
Copy link
Owner

brianc commented Apr 19, 2017

I'll wait for @charmander's approval - looks like there might be some ⁉️ about the code still.

@kibae
Copy link
Contributor Author

kibae commented Apr 20, 2017

@brianc Oh, I see.
For a clean commit log, I will just clean up the connection related code and request a new pull.
And I'll separate project for logical replication :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants