.copyFrom issues with large documents
It is not clear you have to listen to client.on('drain',function(){}); rather than stream.on('drain',function(){}) before importing large files.
Is there a way to change the buffer size and make that higher? Right now the buffer size is very low for my application.
I'll need exact steps to reproduce and/or more code examples of the issue. I can't follow exactly what's going on.
I am doing a copyFrom with a large CSV file.
var stream = client.copyFrom('COPY "' + tablename + '" FROM STDIN WITH CSV'); stream.write(largeCSVFileString);
This will error out unless you batch the csv file and listen to client.on('drain', function(error) {}); This "stream" relies on a buffer of size 16kB I believe, and we should allow for this value to be user input.
I’m running into the same issue.
The main problem is that there is a buffer somewhere that fills up when streaming data using copyFrom. Once that happens, the stream hangs.
I don’t know how big the buffer is, but I can never import more than a few hundred records (of ~100,000 I am trying to load), before it hangs.
Regarding the drain event: I haven’t tried this, but based on @BrandonCopley’s comment and some testing, it looks like I could work around the problem by listening for the drain event on the client (note: not the stream). To do this, you stream a small chunk of data—a few hundred records, in my case—wait for the drain event, then send the next chunk. While this is faster than doing individual INSERTs, it is a lot more complicated than a simple stream.
I had to use an ugly workaround: keep track of how much data has been written to the stream. After a certain amount, stop writing, wait a moment using setTimeout, then resume.
Some problems with CopyFromStream:
- There is no usable result from
write(). It’s supposed to returntrueorfalse, but mostly returnsundefined. So there’s no way to tell when you should stop sending data. - Even if you do stop sending data, the stream doesn’t emit
drainto indicate when it is safe to continue. - The stream can emit
drain, but that only happens once. (If the connection is not yet made, the data is buffered. Once the connection is ready, it is written anddrainis emitted. There is no other flow control in the class.)
There must be some underlying buffering going on, at a lower level. The stream needs to return true or false based on that buffering; and it needs to emit drain based on that buffering.
A bulk copy with a limit of 128K* isn’t very bulky. :grin:
* That’s the observed limit on my system. Apparently others see different limits.
I'm having the same issue with not getting usable results from write(). It mostly returns undefined as natesilva mentioned.
I'll try to get an example to reproduce this, but in the meantime, perhaps it's because the code isn't using Node 0.10 streams?
I think I figured out why copyFrom's write() sometimes returns undefined instead of true/false.
In connection.js line 293 sendCopyFromChunk, a return is missing. This causes the function CopyFromStream.prototype._sendIfConnectionReady in copystream.js line 57 to return undefined when the condition is triggered.
From what it looks like, drain isn't being emitted properly because the code doesn't have backpressure handling when copystream.js's line 57 receives false from this._connection.sendCopyFromChunk. This missing backpressure handling is the cause for this bug to appear only when there's a bulk copy of large documents since there's no stream backpressure when bulk copy isn't dealing with large documents.
Another thing about sendCopyFromChunk in connection.js line 293 is that even when it returns false to dataSent (in copystream.js's _sendIfConnectionReady), this return value of false is merely advisory (see: http://nodejs.org/docs/latest/api/stream.html#stream_writable_write_chunk_encoding_callback), so a fix should listen for connection stream's drain somewhere, and trigger copy stream's drain emit.
I would fix this if I could but unfortunately I'm still relatively new to Node streams and the node-postgres code base :(
I think the bug is related to the stream module, since listening on stream's drain event produced the same issue: event being emitted only once. I ended up with the workaround mentioned above: run more copyFrom of little chunks.
Hay - sorry for not being more attentive to this issue - the original COPY implementation was written by a contributor and has some problems in it's design. I'm going to leave this issue open as a reminder to myself to deprecate those APIs. In an effort to keep node-postgres focused, I've re-implemented everything related to COPY TO / COPY FROM in an external module. The module only works with a very recent version of node-postgres and the pure JavaScript driver, but it's very fast, coping 10's of thousands of rows a second on my machine.
I've been using it in a production ETL environment for the past 2 months with good results. Check it out and open up issues over there if you still have problems. I consider the built in COPY TO / COPY FROM to be deprecated & will update node-postgres to print warnings to that shortly.
https://github.com/brianc/node-pg-copy-streams
Thank you guy! It works like a charm!
Thanks Brian. I’ll take a look at it for my next project.
You can take a look at this too for an example of how to use it:
https://github.com/brianc/node-pg-copy-table/blob/master/index.js
Check the tests in that repo too. I'm going to open source that little module at some point w/ a readme and stuff, just too busy to do anything more than throw it up on github at the moment. :) I use to pull ranges of production tables down locally so I can test out changes on a large set of data but not fill my entire hard-drive up with our database.