18 Commits

Author SHA1 Message Date
Rafa de la Torre
fd3cc95573 Remove unused var buffer_sent 2018-06-11 12:17:39 +02:00
Rafa de la Torre
922627daaf Small refactor 2018-06-11 12:14:28 +02:00
Rafa de la Torre
61bc713e0c Improve performance of COPY TO #56
Under some circumstances, the COPY TO streamming can be CPU-bound,
particularly when PG holds the resultset in memory buffers and the size
of the rows << chunk (64 KB in my linux box).

This commits improves the situation by creating a buffer of `chunk`
size and fitting in as many rows as it can before pushing them. This
results in more balanced read and writes (in terms of size and in bigger
chunks) as well as more frequent calls to the callback, thus freeing the
main loop for other events to be processed, and therefore avoiding
starvation.
2018-06-08 15:04:42 +02:00
jeromew
e15feb199a README.md: copy-from error and vacuum #26 2016-08-23 12:10:25 +02:00
jeromew
191a4ec16a Fix documentation of copy-from completion 2016-08-23 11:32:10 +02:00
jeromew
399bff7ed7 Bump version 2016-08-22 16:27:04 +00:00
jeromew
8174e10fb5 Merge pull request #54 from jeromew/upstream-end
Test: `end` event should not be triggered 2 times on copy-from
2016-08-22 17:47:29 +02:00
jeromew
f29aef3bba Merge pull request #53 from jeromew/upstream-frontier
Bugfix - wrong tests on chunk frontiers
2016-08-22 17:47:01 +02:00
jeromew
b2e108571e Issue #54: We should probably delay the _flush cb() to CommandComplete 2016-07-30 00:08:54 +00:00
jeromew
7003f6070f Fix issue #54: end is being triggered 2 times 2016-07-29 23:51:41 +00:00
jeromew
ade7ab95a0 Test: end event should not be triggered 2 times on copy-from 2016-07-29 23:47:39 +00:00
jeromew
9ccda04036 Bugfix - Chunk frontiers were not correctly tested 2016-07-28 23:05:50 +00:00
jeromew
a5e532f20b Bugfix - wrong tests on chunk frontiers 2016-07-28 22:55:02 +00:00
jeromew
2a4db2920e Fix NoticeResponse test and handle other messages (#52) 2016-07-28 16:13:22 -05:00
jeromew
f155899570 Add a test for NoticeResponse handling (#51) 2016-07-28 13:15:59 -05:00
brianc
18be125596 Bump version 2016-07-26 14:40:01 -05:00
jeromew
ae5b344395 Refactor message format codes handling (#45) 2016-07-26 14:39:48 -05:00
jeromew
ee84aba89f Use end stream option instead of not calling _flush callback (#44) 2016-07-26 14:39:33 -05:00
7 changed files with 181 additions and 57 deletions

View File

@@ -42,10 +42,15 @@ pg.connect(function(err, client, done) {
var stream = client.query(copyFrom('COPY my_table FROM STDIN')); var stream = client.query(copyFrom('COPY my_table FROM STDIN'));
var fileStream = fs.createReadStream('some_file.tsv') var fileStream = fs.createReadStream('some_file.tsv')
fileStream.on('error', done); fileStream.on('error', done);
fileStream.pipe(stream).on('finish', done).on('error', done); stream.on('error', done);
stream.on('end', done);
fileStream.pipe(stream);
}); });
``` ```
*Important*: Even if `pg-copy-streams.from` is used as a Writable (via `pipe`), you should not listen for the 'finish' event and expect that the COPY command has already been correctly acknowledged by the database. Internally, a duplex stream is used to pipe the data into the database connection and the COPY command should be considered complete only when the 'end' event is triggered.
## install ## install
```sh ```sh
@@ -56,7 +61,10 @@ $ npm install pg-copy-streams
This module __only__ works with the pure JavaScript bindings. If you're using `require('pg').native` please make sure to use normal `require('pg')` or `require('pg.js')` when you're using copy streams. This module __only__ works with the pure JavaScript bindings. If you're using `require('pg').native` please make sure to use normal `require('pg')` or `require('pg.js')` when you're using copy streams.
Before you set out on this magical piping journey, you _really_ should read this: http://www.postgresql.org/docs/9.3/static/sql-copy.html, and you might want to take a look at the [tests](https://github.com/brianc/node-pg-copy-streams/tree/master/test) to get an idea of how things work. Before you set out on this magical piping journey, you _really_ should read this: http://www.postgresql.org/docs/current/static/sql-copy.html, and you might want to take a look at the [tests](https://github.com/brianc/node-pg-copy-streams/tree/master/test) to get an idea of how things work.
Take note of the following warning in the PostgreSQL documentation:
> COPY stops operation at the first error. This should not lead to problems in the event of a COPY TO, but the target table will already have received earlier rows in a COPY FROM. These rows will not be visible or accessible, but they still occupy disk space. This might amount to a considerable amount of wasted disk space if the failure happened well into a large copy operation. You might wish to invoke VACUUM to recover the wasted space.
## contributing ## contributing

View File

@@ -4,11 +4,12 @@ module.exports = function(txt, options) {
var Transform = require('stream').Transform var Transform = require('stream').Transform
var util = require('util') var util = require('util')
var code = require('./message-formats')
var CopyStreamQuery = function(text, options) { var CopyStreamQuery = function(text, options) {
Transform.call(this, options) Transform.call(this, options)
this.text = text this.text = text
this._copyOutResponse = null this._gotCopyOutResponse = false
this.rowCount = 0 this.rowCount = 0
} }
@@ -23,63 +24,89 @@ CopyStreamQuery.prototype.submit = function(connection) {
connection.stream.pipe(this) connection.stream.pipe(this)
} }
var code = {
E: 69, //Error
H: 72, //CopyOutResponse
d: 0x64, //CopyData
c: 0x63 //CopyDone
}
CopyStreamQuery.prototype._detach = function() { CopyStreamQuery.prototype._detach = function() {
this.connection.stream.unpipe(this) this.connection.stream.unpipe(this)
// Unpipe can drop us out of flowing mode // Unpipe can drop us out of flowing mode
this.connection.stream.resume() this.connection.stream.resume()
} }
CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { CopyStreamQuery.prototype._transform = function(chunk, enc, cb) {
var offset = 0 var offset = 0
var Byte1Len = 1;
var Int32Len = 4;
if(this._remainder && chunk) { if(this._remainder && chunk) {
chunk = Buffer.concat([this._remainder, chunk]) chunk = Buffer.concat([this._remainder, chunk])
} }
if(!this._copyOutResponse) {
this._copyOutResponse = true var length;
if(chunk[0] == code.E) { var messageCode;
this._detach() var needPush = false;
this.push(null)
return cb(); var buffer = Buffer.alloc(chunk.length);
var buffer_offset = 0;
this.pushBufferIfneeded = function() {
if (needPush && buffer_offset > 0) {
this.push(buffer.slice(0, buffer_offset))
buffer_offset = 0;
} }
if(chunk[0] != code.H) {
this.emit('error', new Error('Expected copy out response'))
}
var length = chunk.readUInt32BE(1)
offset = 1
offset += length
} }
while((chunk.length - offset) > 5) {
while((chunk.length - offset) >= (Byte1Len + Int32Len)) {
var messageCode = chunk[offset] var messageCode = chunk[offset]
//complete or error
if(messageCode == code.c || messageCode == code.E) { //console.log('PostgreSQL message ' + String.fromCharCode(messageCode))
this._detach() switch(messageCode) {
this.push(null)
return cb(); // detect COPY start
case code.CopyOutResponse:
if (!this._gotCopyOutResponse) {
this._gotCopyOutResponse = true
} else {
this.emit('error', new Error('Unexpected CopyOutResponse message (H)'))
}
break;
// meaningful row
case code.CopyData:
needPush = true;
break;
// standard interspersed messages. discard
case code.ParameterStatus:
case code.NoticeResponse:
case code.NotificationResponse:
break;
case code.ErrorResponse:
case code.CopyDone:
this.pushBufferIfneeded();
this._detach()
this.push(null)
return cb();
break;
default:
this.emit('error', new Error('Unexpected PostgreSQL message ' + String.fromCharCode(messageCode)))
} }
//something bad happened
if(messageCode != code.d) { length = chunk.readUInt32BE(offset+Byte1Len)
return this.emit('error', new Error('expected "d" (copydata message)')) if(chunk.length >= (offset + Byte1Len + length)) {
} offset += Byte1Len + Int32Len
var length = chunk.readUInt32BE(offset + 1) - 4 //subtract length of UInt32 if (needPush) {
//can we read the next row? var row = chunk.slice(offset, offset + length - Int32Len)
if(chunk.length > (offset + length + 5)) { this.rowCount++
offset += 5 row.copy(buffer, buffer_offset);
var slice = chunk.slice(offset, offset + length) buffer_offset += row.length;
offset += length }
this.push(slice) offset += (length - Int32Len)
this.rowCount++
} else { } else {
// we need more chunks for a complete message
break; break;
} }
} }
this.pushBufferIfneeded();
if(chunk.length - offset) { if(chunk.length - offset) {
var slice = chunk.slice(offset) var slice = chunk.slice(offset)
this._remainder = slice this._remainder = slice

View File

@@ -10,6 +10,7 @@ module.exports = {
var Transform = require('stream').Transform var Transform = require('stream').Transform
var util = require('util') var util = require('util')
var code = require('./message-formats')
var CopyStreamQuery = function(text, options) { var CopyStreamQuery = function(text, options) {
Transform.call(this, options) Transform.call(this, options)
@@ -26,27 +27,23 @@ CopyStreamQuery.prototype.submit = function(connection) {
connection.query(this.text) connection.query(this.text)
} }
var code = {
H: 72, //CopyOutResponse
d: 0x64, //CopyData
c: 0x63 //CopyDone
}
var copyDataBuffer = Buffer([code.d]) var copyDataBuffer = Buffer([code.CopyData])
CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { CopyStreamQuery.prototype._transform = function(chunk, enc, cb) {
var Int32Len = 4;
this.push(copyDataBuffer) this.push(copyDataBuffer)
var lenBuffer = Buffer(4) var lenBuffer = Buffer(Int32Len)
lenBuffer.writeUInt32BE(chunk.length + 4, 0) lenBuffer.writeUInt32BE(chunk.length + Int32Len, 0)
this.push(lenBuffer) this.push(lenBuffer)
this.push(chunk) this.push(chunk)
cb() cb()
} }
CopyStreamQuery.prototype._flush = function(cb) { CopyStreamQuery.prototype._flush = function(cb) {
var finBuffer = Buffer([code.c, 0, 0, 0, 4]) var Int32Len = 4;
var finBuffer = Buffer([code.CopyDone, 0, 0, 0, Int32Len])
this.push(finBuffer) this.push(finBuffer)
//never call this callback, do not close underlying stream this.cb_flush = cb
//cb()
} }
CopyStreamQuery.prototype.handleError = function(e) { CopyStreamQuery.prototype.handleError = function(e) {
@@ -54,7 +51,7 @@ CopyStreamQuery.prototype.handleError = function(e) {
} }
CopyStreamQuery.prototype.handleCopyInResponse = function(connection) { CopyStreamQuery.prototype.handleCopyInResponse = function(connection) {
this.pipe(connection.stream) this.pipe(connection.stream, { end: false })
} }
CopyStreamQuery.prototype.handleCommandComplete = function(msg) { CopyStreamQuery.prototype.handleCommandComplete = function(msg) {
@@ -65,8 +62,13 @@ CopyStreamQuery.prototype.handleCommandComplete = function(msg) {
this.rowCount = parseInt(match[1], 10) this.rowCount = parseInt(match[1], 10)
} }
this.unpipe() // we delay the _flush cb so that the 'end' event is
this.emit('end') // triggered after CommandComplete
this.cb_flush()
// unpipe from connection
this.unpipe(this.connection)
this.connection = null
} }
CopyStreamQuery.prototype.handleReadyForQuery = function() { CopyStreamQuery.prototype.handleReadyForQuery = function() {

25
message-formats.js Normal file
View File

@@ -0,0 +1,25 @@
/**
* The COPY feature uses the following protocol codes.
* The codes for the most recent protocol version are documented on
* https://www.postgresql.org/docs/current/static/protocol-message-formats.html
*
* The protocol flow itself is described on
* https://www.postgresql.org/docs/current/static/protocol-flow.html
*/
module.exports = {
ErrorResponse: 0x45, // E
CopyInResponse: 0x47, // G
CopyOutResponse: 0x48, // H
CopyBothResponse: 0x57, // W
CopyDone: 0x63, // c
CopyData: 0x64, // d
CopyFail: 0x66, // f
// It is possible for NoticeResponse and ParameterStatus messages to be interspersed between CopyData messages;
// frontends must handle these cases, and should be prepared for other asynchronous message types as well
// (see Section 50.2.6).
// Otherwise, any message type other than CopyData or CopyDone may be treated as terminating copy-out mode.
NotificationResponse: 0x41, // A
NoticeResponse: 0x4E, // N
ParameterStatus: 0x53 // S
}

View File

@@ -1,6 +1,6 @@
{ {
"name": "pg-copy-streams", "name": "pg-copy-streams",
"version": "1.1.0", "version": "1.2.0",
"description": "Low-Level COPY TO and COPY FROM streams for PostgreSQL in JavaScript using", "description": "Low-Level COPY TO and COPY FROM streams for PostgreSQL in JavaScript using",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -39,6 +39,7 @@ var testRange = function(top) {
fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) { fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) {
assert.ifError(err) assert.ifError(err)
assert.equal(res.rows[0].count, top, 'expected ' + top + ' rows but got ' + res.rows[0].count) assert.equal(res.rows[0].count, top, 'expected ' + top + ' rows but got ' + res.rows[0].count)
assert.equal(stream.rowCount, top, 'expected ' + top + ' rows but db count is ' + stream.rowCount)
//console.log('found ', res.rows.length, 'rows') //console.log('found ', res.rows.length, 'rows')
countDone() countDone()
var firstRowDone = gonna('have correct result') var firstRowDone = gonna('have correct result')
@@ -54,3 +55,19 @@ var testRange = function(top) {
} }
testRange(1000) testRange(1000)
var testSingleEnd = function() {
var fromClient = client()
fromClient.query('CREATE TEMP TABLE numbers(num int)')
var txt = 'COPY numbers FROM STDIN';
var stream = fromClient.query(copy(txt))
var count = 0;
stream.on('end', function() {
count++;
assert(count==1, '`end` Event was triggered ' + count + ' times');
if (count == 1) fromClient.end();
})
stream.end(Buffer('1\n'))
}
testSingleEnd()

View File

@@ -7,6 +7,7 @@ var concat = require('concat-stream')
var pg = require('pg') var pg = require('pg')
var copy = require('../').to var copy = require('../').to
var code = require('../message-formats')
var client = function() { var client = function() {
var client = new pg.Client() var client = new pg.Client()
@@ -22,6 +23,18 @@ var testConstruction = function() {
testConstruction() testConstruction()
var testComparators = function() {
var copy1 = copy();
copy1.pipe(concat(function(buf) {
assert(copy1._gotCopyOutResponse, 'should have received CopyOutResponse')
assert(!copy1._remainder, 'Message with no additional data (len=Int4Len+0) should not leave a remainder')
}))
copy1.end(new Buffer([code.CopyOutResponse, 0x00, 0x00, 0x00, 0x04]));
}
testComparators();
var testRange = function(top) { var testRange = function(top) {
var fromClient = client() var fromClient = client()
var txt = 'COPY (SELECT * from generate_series(0, ' + (top - 1) + ')) TO STDOUT' var txt = 'COPY (SELECT * from generate_series(0, ' + (top - 1) + ')) TO STDOUT'
@@ -73,5 +86,37 @@ var testInternalPostgresError = function() {
queryClient.end() queryClient.end()
}) })
} }
testInternalPostgresError() testInternalPostgresError()
var testNoticeResponse = function() {
// we use a special trick to generate a warning
// on the copy stream.
var queryClient = client()
var set = '';
set += 'SET SESSION client_min_messages = WARNING;'
set += 'SET SESSION standard_conforming_strings = off;'
set += 'SET SESSION escape_string_warning = on;'
queryClient.query(set, function(err, res) {
assert.equal(err, null, 'testNoticeResponse - could not SET parameters')
var runStream = function(callback) {
var txt = "COPY (SELECT '\\\n') TO STDOUT"
var stream = queryClient.query(copy(txt))
stream.on('data', function(data) {
})
stream.on('error', callback)
// make sure stream is pulled from
stream.pipe(concat(callback.bind(null,null)))
}
runStream(function(err) {
assert.equal(err, null, err)
queryClient.end()
})
})
}
testNoticeResponse();