Reset pooled sessions between uses
When using pooled connections the session configuration in a session will affect operations performed on what the client considers a new connection (but it is not, due to pooling).
For comparison, pg_bouncer default configuration is to issue a "DISCARD ALL" whenever a connection is released into the pool.
This ticket is for supporting a similar mechanism at the node pooled connection level. A "session reset" command could be configurable so that users using pg_bouncer could avoid doing it again.
See https://github.com/Vizzuality/CartoDB-SQL-API/issues/98
First of all, I totally agree if you check a client into the pool while it's still got queued queries or other "state" internally something should happen to clean up that state or at least alert the user somehow they're about to experience unexpected behavior. I'm just not sure what you mean by 'session.' We could make this configurable and off by default for v2.x. Perhaps we can switch it to on by default for v3.x since it probably isn't a breaking change in most cases & might be the more correct behavior? I just need to wrap my head around what you mean by a session.
In node-postgres terms I guess by "session" I mean "client". Each "client" should get a clean and reset environment to work in, as if it was a new connection.
That makes total sense. Sorry to need more info still, but I guess the way I've always used postgres is pretty much "get a client, run some queries, return client" so I don't know how the client's environment can be modified in such a way it needs to be cleaned up or reset. Could you show an example of code where the client can alter it's state and upon it next being checked out it's dirty?
Example:
First client query: SET statement_timeout TO 1
Second client query: WITH c AS ( select x from generate_series(1,9999999) x ) select count(*) from c
The second query will take more than 1 milliseconds and thus timeout, but only if the session (connection, in this case) was "polluted" by the previous client.
Perfect example. Now for another question....how do you reset the session? Send in a DISCARD ALL message?
On Wed, Jul 03, 2013 at 06:19:51AM -0700, Brian C wrote:
Perfect example. Now for another question....how do you reset the session? Send in a
DISCARD ALLmessage?
That's what pg_bouncer uses by default. But I think it'd be nice to allow for overriding the sequence of commands. For example there's a setseed() function that's used to control the pseudo-random sequence returned by random(). That setting affects the session/connection, so one might want to add a setseed(<another_random_source>) to the "session initialization script".
Yeah maybe having a hook on 'before checkout' and 'before checkin' you could optionally implement on the pool.
Aside: I find myself yet again wishing the pooling was handled in it's own module.
Just saw this and would love to see some way to "reset" client's session state upon the client being returned to the pool. For my use case I'm setting session_replication_role to replica for bulk deletions and inserts and would like it to revert to origin (the default) after the client is returned to the pool so the setting does not affect the next query to be called using the same client.
I've got a situation where node is calling a postgres function in a loop (a basic job polling mechanism), and that function uses temp tables, which are dropped within the function, but resources for which don't seem to be released until the node server is stopped.
The node-postgres connections stay open indefinitely and because temp table resources aren't released, eventually the server runs out of memory and falls over.
DISCARD ALL seems to be exactly what we need: since it "Releases all temporary resources associated with the current session".
CORRECTION: DISCARD ALL did nothing when we called it manually after each transaction commit. So it's not needed for this use case.
If temp tables are kept alive for the length of a connection id recommend opening & closing a skngle client in your loop instead of using a pooled client - that way you have control over the lifecycle of everything.
Well, that will solve the immediate problem, but isn't a scalable long term solution.
Is there a way with node-postgres to configure a connection recycle time (TTL)? Ie some time within X-Y minutes connections will be closed and re-opened? See, my server is dying after several hours. If pooled connections had a TTL of between 10 and 20 minutes, they could be recycled in a staggered way that didn't disrupt the application, and problem solved.
I think your best solution is to use the client directly and implement your own pooling behavior. I used the "generic-pool" module. There are likely other generic pool modules that support TTL behavior.
I'm going down that route as we speak! :) Alternatively there's this feature request in generic-pool https://github.com/coopernurse/node-pool/issues/133 which would make it much easier to implement a TTL node-postgres pooling. I might even make a pull request myself if I get the time.
awesome!
+1 for running DISCARD ALL when connections are returned to the pool; or providing a way for the user to do it themselves. (The advantage of doing it yourself is that you can clear out only the parts you want to (e.g. just DEALLOCATE ALL), which is potentially more efficient, however I think it's good to have a safe default such as DISCARD ALL.)
DISCARD ALL works all the way back to (at least) 8.3: https://www.postgresql.org/docs/8.3/static/sql-discard.html
Without this, potential security issues may not be as obvious. For example, if a naïve user were to do SET myapp.bypass_checks = 1 on a connection, and use this to allow triggers or functions to do something different, then it may seem to be working correctly since it's only enabled on one of the pooled connections. If they check a request afterwards which doesn't use this same connection it would seem to be safe; but actually it's more like Russian roulette. (A more experienced user would wrap it in a transaction and use SET LOCAL, but hopefully you catch my meaning.)
As a further example as to why this would be needed, cyanaudit, a well-known audit extension for postgres (that works with triggers), requires a DISCARD ALL (in order to reset the application user id)
https://pgxn.org/dist/cyanaudit/doc/cyanaudit.html
"[...] you must have the pooler issue a DISCARD ALL command to reset the persistent server connection after a client disconnects."
Hi,
At least we should clearly state around the documentation of release() (and in general, around the pool doc) that release does not imply connection reset. I've spend few days assuming it does, and it created a lot of issues. Great work btw, and thank you !
I have a similar situation like Poshest's. We have some business logic and 20+ tables involved in a HUGE query. What we implemented is to break it into a few baby steps using temp tables, works out great except when the session/connection is reused, it will throw relation "temp1" already exists. So our workaround is to name each temp table with a timestamp for unique identifier. However, this duct tape eventually will exhaust all resources because we have no way to drop them. So, a periodic RESET of the pool will solve all issues!