-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3c01b1
Support for logical/streaming replication mode
1437fc3
- Support for Logical Replication Stream
35f35eb
WalStream test/sample code
1ff0219
WalStream test/sample code
fd67ef1
Merge branch 'master' of https://github.com/kibae/node-postgres
0aed4d0
Update test-walstream.js
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) 2017 Kibae Shin ([email protected]) | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* README.md file in the root directory of this source tree. | ||
*/ | ||
var EventEmitter = require('events').EventEmitter; | ||
var Client = require('./client'); | ||
var util = require('util'); | ||
|
||
var WalStream = function(config) { | ||
EventEmitter.call(this); | ||
|
||
var c = config || {}; | ||
c.replication = 'database'; | ||
|
||
var self = this; | ||
|
||
var client; | ||
var stoped = false; | ||
this.getChanges = function(slotName, uptoLsn, option, cb /*(err)*/) { | ||
option = option || {}; | ||
/* | ||
* includeXids : include xid on BEGIN and COMMIT, default false | ||
* includeTimestamp : include timestamp on COMMIT, default false | ||
* skipEmptyXacts : skip empty transaction like DDL, default true | ||
*/ | ||
|
||
stoped = false; | ||
client = new Client(config); | ||
|
||
client.on('error', function(err) { | ||
self.emit('error', err); | ||
}); | ||
|
||
client.connect(function(err) { | ||
//error handling | ||
if (err) { | ||
self.emit('error', err); | ||
return; | ||
} | ||
|
||
var sql = 'START_REPLICATION SLOT ' + slotName + ' LOGICAL ' + (uptoLsn ? uptoLsn : '0/00000000'); | ||
var opts = [ | ||
'"include-xids" \'' + (option.includeXids === true ? 'on' : 'off') + '\'', | ||
'"include-timestamp" \'' + (option.includeTimestamp === true ? 'on' : 'off') + '\'', | ||
'"skip-empty-xacts" \'' + (option.skipEmptyXacts !== false ? 'on' : 'off') + '\'', | ||
]; | ||
sql += ' (' + (opts.join(' , ')) + ')'; | ||
|
||
client.query(sql, function(err) { | ||
if (err) { | ||
if (!stoped && cb) { | ||
cb(err); | ||
cb = null; | ||
} | ||
} | ||
cb = null; | ||
}); | ||
|
||
client.connection.once('replicationStart', function() { | ||
//start | ||
self.emit('start', self); | ||
client.connection.on('copyData', function(msg) { | ||
if (msg.chunk[0] != 0x77) { | ||
return; | ||
} | ||
|
||
var lsn = (msg.chunk.readUInt32BE(1).toString(16).toUpperCase()) + '/' + (msg.chunk.readUInt32BE(5).toString(16).toUpperCase()); | ||
self.emit('data', { | ||
lsn: lsn, | ||
log: msg.chunk.slice(25), | ||
}); | ||
}); | ||
}); | ||
}); | ||
return self; | ||
}; | ||
|
||
this.stop = function() { | ||
stoped = true; | ||
if (client) { | ||
client.end(); | ||
client = null; | ||
} | ||
}; | ||
}; | ||
|
||
util.inherits(WalStream, EventEmitter); | ||
|
||
module.exports = WalStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 readthis.replication
?There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry. That's pipelinedb link..
please check this out :)
https://github.com/postgres/postgres/blob/839da3c7d9b76b178910ce2999f7cbbdf6e91f70/src/bin/pg_basebackup/streamutil.c#L110
There was a problem hiding this comment.
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:
resulting in:
?
There was a problem hiding this comment.
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.