split this module into js-pg and native-pg
It would be nice if the C++ binding and pure javascript implementation were in different modules.
The module pg can maintain 100% back compat and just depend on js-pg and native-pg
The main advantage is that users can install js-pg directly and not have to compile any C++. Removing a binary dependency from my application that's not necessary would be very nice.
@brianc has a module without the native bindings, check out node-postgres-pure if you're interested.
Sweet! problem solved. Maybe mention it on the README somewhere?
@Raynos I totally agree with you 100%
I made pg.js as a stop-gap right now. node-postgres is now a 3+ year old project and originated before the "do one small thing in each module" mode was really in cement. Because of that there's some coupling between the JavaScript bindings, native bindings, and some of the utility "inner modules" within node-postgres like the type converters, result set parser, and client pool. I have on my list of things to do to split it up until native & JS can be teased apart properly while still making it decently easy to understand the code base & contribute fixes. In the mean time, pg.js exists by just using this repo as a submodule & publishing all of this module except the native bindings. It's kinda ugly but it works. I'll swing by & ping you when I tease the two pieces apart in a more elegant way. In the mean time, pg.js is what I use when I don't want to compile and what I recommend to folks who have issues compiling on windows. :)
The one thing I've run into causing me a few problems is it's kinda hard to add peerDependency on node-postgres because sometimes I wanna use pg.js but don't want to force it as a peer dependency for folks who use native bindings. Any thoughts on a way to mitigate that?
@brianc mitigate it by never using peer dependencies. Peer dependencies are a terrible idea.
What you really want is
var connect = require("pg/connect")
var query = require("pg/query")
module.exports = myModule;
function myModule(database) {
connect(database, function (err) {
query(database, "SELECT * FROM LULZ", function (err, data) {})
})
}
Whenever you would peer depend on pg instead you should just accept in a very minimal db interface as an argument (whose interface has to be locked and that module "effectively" peer depends on the interface without using peer dependencies).
Then when you want to connect or run a query you should import a properly versioned local dependencies of a querying implementation from whatever library you want, either pg.js or pg-native or some custom query engine someone wrote thats compatible with the bare minimal database interface.
In fact since pg is probably cross process, instead of a bare minimal database interface maybe you just pass in the tcp connection pool as an argument.
Obvouisly this requires rewriting everything :D
See https://github.com/rvagg/node-levelup/issues/212
TL:DR;
Peer depend on data structures. Never peer depend on functions or methods. Always require functions and methods at concrete versions.
I'm not sure how to peer depend on data structures using npms peerDependencies infrastructure.
When would one use pure JS since it is much slower and compilation is usually trivial? I am genuinely curious.
I'm curious, has anyone done any benchmarking to see the relative performance of the two bindings?
@rpedela binary dependencies are a pain in the ass for small modules. Also if you don't dedupe the pq dependency then you have to install it multiple times. You also need to npm rebuild on the box. It's just annoying.
I've done some non-scientific benchmarks recently and for the tests I ran the pure JavaScript were only around 10% slower than the native bindings. They were pretty bastardized benchmarks though.
Also to add more reasons why you'd use the pure javascript bindings...certain things like cursors & streaming data from one database to another aren't easily supported since libpq sits on top of the binary protocol and parses it for you, while the JavaScript bindings have direct access to the protocol. I'm sure it's still doable in C/C++, but I don't have the time or patience to code it myself. These modules, for example, require the JavaScript bindings. https://github.com/brianc/node-pg-cursor https://github.com/brianc/node-pg-copy-streams https://github.com/brianc/node-pg-query-stream
on a related note, maybe I'll try to do nicer benchmarks to compare at some point. Most of my benchmarks compare 1 version of node-postgres to the previous version so I can make sure performance doesn't regress.
Using the protocol and/or pure JS, are you able to stream result sets as bytes (not rows)? If so, that would be the killer feature for me to use the pure JS. I know it is not possible with libpq.
I am not too familiar with the protocol, but a quick glance at DataRow message format suggests it might be possible. I am also wondering if you could stream a large INSERT as bytes to the server. Any thoughts @brianc? http://www.postgresql.org/docs/9.3/static/protocol-message-formats.html
@rpedela that is pretty much exactly what this module provides:
https://github.com/brianc/node-pg-copy-streams
You get raw binary buffers where your columns are separated by a specific character you define with this syntax: http://www.postgresql.org/docs/9.2/static/sql-copy.html
I've been using that module to copy "chunks" from our production data set to local databases extremely quickly. I can give you more pointers on how to use it if you'd like, just open up any questions you have on it in particular as an issue on that repo. :)
I thought that was only for COPY. Can it do INSERT and SELECT as well? More specifically a complicated INSERT and/or complicated SELECT?
@rpedela:
One can not only COPY from a table but also from a (SELECT-) Query.
And yes, you can INSERT too, see here.
Yeah I forgot that you can use COPY with SELECT so that is great, thanks for the reminder. However all the other limitations of COPY are still there. COPY into the database is fine if you are inserting static data, but what about dynamic (computed) data? What about streaming the output of the RETURNING clause for INSERT and UPDATE byte by byte? If it is available in one of @brianc modules, then awesome but I don't see it.
The whole point is to reduce the memory consumption of the client by being able to stream SQL queries as bytes and stream result sets as bytes regardless of the particular query. It is also means predictable memory usage patterns on the client regardless of the data size which is what I care most about for my particular use case.
I am really just asking if it is possible, but if it is already implemented and I just missed it then even better!
To answer my own question, I read the protocol docs last night. It seems the answer is yes you can write a protocol client to stream bytes for SQL queries and result sets. However for queries, you need to provide the size in bytes of the query string. I was hoping you could stream an INSERT with an unknown size, but that does not seem possible at the protocol level. You can do it at a higher level though.
A common use case for a byte streaming protocol client would be a REST API which retrieves or computes some data from a database then returns the results as JSON. Thanks to JSON support, it is easy to have Postgres do the work of conversion for you with row_to_json(). Regardless of the size of result set, you could just stream the result set as 8K chunks (or whatever chunk size you want) and send it directly to the REST API client over HTTP. This makes it faster because the database does all the work, and the amount of memory you need per query = overhead + stream buffer. This is advantageous to single row streaming because a single row could be a few KB, a few MB, or a few GB (in my use case). Having such variable row sizes makes it really hard to determine memory requirements of the REST API server based on number of concurrent queries.
Oh yeah I see where you're coming from. That could be accomplished without too much changing to the internals of the parser, and could even be done in such a way to maintain full backwards compatibility and have the actual "streamy-ness" of the result be handled in a separate module.
The main meat of the change is that currently the parser stores up buffers until it has enough to read an entire message from the backend server. What it would need to do is peek at the message 'type' byte (which is the 1st byte anyway) and then if it's a data row D type, instead of waiting for the whole thing just immediately pass it to some method like "handleDataRowBuffers" or something on the query object. The standard query object could just concat all the buffers until it reaches the expected length and then pass it back to the connection as 'parseDataRow' - but you can pass anything that responds to #submit to node-postgres query method and so you could pass a different object that knows how to respond to 'handleDataRowBuffers' in a streamy type of way. I'm not sure if what I typed makes any sense....it's been a pretty long day. There are still a bunch of implementation details I'm sure I skipped over but either way...it's doable. Having the time to implement & test it? Not any time too soon unfortunately.
Yeah it makes some sense. :)
Not really asking you to implement it, but it would definitely be cool if you or someone else did! I will likely revisit this at some point in the future and implement it myself if no one else has done it yet.
This pull request splits out native bindings and makes them an optional install, much in the way redis uses hiredis if it's installed...
https://github.com/brianc/node-postgres/pull/657
Going to close this issue now. :wink: