Quite possible. Thus the reason I was looking for clarification on how
the module was intended to work - if it doesn't work in the way that I
want it to, I need to look elsewhere for a solution. My main reason for
posting this thread was that I was expecting it to work one way, but
testing showed it working another way, so I was trying to find out if
that was intentional or user error. Apparently it's intentional, so
there we go - in it's current form at least, my needs are beyond what
the psycopg2 pool provides. Fair enough.
> I suggest looking into a dedicated connection pooler like
> PgBouncer. You'll find that it's much more feature-rich and
> configurable than psycopg2.pool. It's production-ready, unlike your
> prototype. And since it's a proxy, it can take connections from
> multiple client apps and tune the pool to your overall load rather
> than on an app-by-app basis (and thus risk overloading the backend if
> multiple apps unexpectedly peak together).
Very true, and I've looked into that (as well as the related, but more
basic, PgPool product), but it seems to me that any external proxy
product like these would defeat *my* purpose for using a pool in the
first place: avoiding the overhead of making/breaking many connections
quickly. That is, all you have really done is gone from connecting to
Postgres to connecting to PgBouncer. You are still making and breaking
just as many connections. Unless connecting to PgBouncer is
significantly cheaper than connecting to Postgres? This may well be the
case, but I haven't yet seen anything to support that. Haven't seen
anything to refute that either, however :)
Of course, there may be many other features provided by such tools that
would make them worthwhile, even for my use case. However, my primary
goal in using a pool was avoiding the connection overhead with each
request, so if a tool doesn't do that, then it isn't the right tool for
me :)
>
> As for why psycopg2.pool is the way it is, maybe most users don't have
> your situation of serving multiple apps with loads varying on
> different cycles. Most are probably only serving a single app, or if
> serving multiple apps then they likely have common user bases with
> similar peak times. You can't dynamically adjust the amount of RAM in
> your server, so saving resources like RAM at below-peak times only
> matters if you're going to do something else with it. In the scenarios
> I described there isn't much else to do with it, so I can understand
> if saving RAM isn't a priority.
True, but you would still have to deal with the minconn "magic number",
unless you just adjusted it so high from the start that even if your
load/use grows over time you never have to mess with it. Even in the
single app use case, where you don't care about RAM usage (since there
is nothing else trying to use the RAM) in order to get maximum benefit
from a pool you'd have to keep an eye on your usage and make sure it
never (or rarely) exceeds whatever arbitrary value you have set for
minconn. Not a big deal, especially if you tune it high to begin with,
but it is one more thing.
Honestly, some of that is just personal issues. I have problems with
code that is inefficient by design (even if there is nothing to be
gained from efficiency), or that makes assumptions about things when it
could be dynamic. I have often coded in such a way that a given value
can be adjusted dynamically, even when the people giving me the specs
say it will never change. More than once that has enabled me to respond
to a "feature request" or change order by saying "it already does that".
On the other hand, I am probably a poster child for "premature
optimization", and often have to stop myself from optimizing code just
because I can, when in reality it is not worth my time. By the same
token, the idea of wasting resources - even when, as you state, there is
nothing else to do with them - just rubs me the wrong way. As such, I
readily acknowledge that some of my requests/statements stem from my own
personal desires, and not from any actual lack/need in the product.