Skip to content
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

prepare statement w/o values #24

Closed
dvv opened this issue Apr 19, 2011 · 5 comments
Closed

prepare statement w/o values #24

dvv opened this issue Apr 19, 2011 · 5 comments

Comments

@dvv
Copy link

dvv commented Apr 19, 2011

Hi!

How do I prepare a statement w/o binding values and executing it, just for future use as named query? The absense of values key in configuration parameters causes unhandled error event so far

TIA,
--Vladimir

@brianc
Copy link
Owner

brianc commented Apr 19, 2011

dang! I'll take a look at this after work today.

@brianc
Copy link
Owner

brianc commented Apr 19, 2011

I cannot reproduce. Can I get a code sample, version of pg you're using, version of node you're using, and operating system?

@dvv
Copy link
Author

dvv commented Apr 19, 2011

Sure. Please, take a look at https://gist.github.com/929626

@brianc
Copy link
Owner

brianc commented Apr 19, 2011

Okey dokey...thanks for the awesome gist. I see the issue.

With node-postgres the first time you issue a named query it is parsed, bound, and executed all at once. Every subsequent query issued on the same connection with the same name will automatically skip the "parse" step and only rebind and execute the already planned query.

Currently node-postgres does not support a way to create a named, prepared query and not execute the query. This feature is supported within libpq and the client/server protocol (used by the pure javascript bindings), but I've not directly exposed it in the API. I thought it would add complexity to the API without any real benefit. Since named statements are bound to the client in which they are created, if the client is disconnected and reconnected or a different client is returned from the client pool, the named statement will no longer work (it requires a re-parsing).

Here's an example of how it works currently:

https://github.com/brianc/node-postgres/blob/master/test/integration/client/prepared-statement-tests.js#L18

@dvv
Copy link
Author

dvv commented Apr 20, 2011

In fact, I've found the current behavior quite well-suited. I can perfectly live with this "lazy-preparation" way. Thanks. Closing

@dvv dvv closed this as completed Apr 20, 2011
brianc pushed a commit that referenced this issue Dec 27, 2019
* Revert "When connection fail, emit the error. (#28)"

This reverts commit 6a7edab.

The callback passed to `Pool.prototype.connect` should be responsible for handling connection errors. The `error` event is documented to be:

> Emitted whenever an idle client in the pool encounters an error.

This isn’t the case of an idle client in the pool; it never makes it into the pool.

It also breaks tests on pg’s master because of nonspecific dependencies.

* Don’t create promises when callbacks are provided

It’s incorrect to do so. One consequence is that a rejected promise will be unhandled, which is currently annoying, but also dangerous in the future:

> DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The way callbacks are used currently also causes #24 (hiding of errors thrown synchronously from the callback). One fix for that would be to call them asynchronously from inside the `new Promise()` executor:

    process.nextTick(cb, error);

I don’t think it’s worth implementing, though, since it would still be backwards-incompatible – just less obvious about it.

Also fixes a bug where the `Pool.prototype.connect` callback would be called twice if there was an error.

* Use Node-0.10-compatible `process.nextTick`
brianc pushed a commit that referenced this issue Apr 28, 2020
Only publish the required files
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

No branches or pull requests

2 participants