sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.43k stars 436 forks source link

Possibility to share the type cache across clients #1039

Open rudolphfroger opened 1 year ago

rudolphfroger commented 1 year ago

It would be great if one would be able to share the type cache, for example by providing a shared instance of the cache when creating a new connection. For larger scale usage of PostgreSQL the usage of external connection poolers like PgBouncer can bring certain benefits. Often this also means using quite short lived connections to a local PgBouncer instance. Every new client has a fresh type cache so that leads to unnecessary overhead of looking up type information.

Would you be open to add such functionality? Me or one of my colleagues could help by starting a PR.

sfackler commented 1 year ago

Is the bouncer sitting in front of a cluster of Postgresi? If so, I don't know if I'd expect to be able to guarantee that the OIDs of types defined by extensions would be identical across the entire cluster.

rudolphfroger commented 1 year ago

The bouncer is in front of a single PostgreSQL instance. Such a feature should go with a warning that sharing the cache across connections is only safe when the connection are made to the same database and server instance.

sfackler commented 1 year ago

If there's only a single instance, it seems like an application-level connection pool would work? I may be missing something about what value the bouncer provides.

rudolphfroger commented 1 year ago

Our application uses multiple processes so an external connection pooler helps to reduce the number of idle connections. But even without a pooler it may be beneficial to share the type cache between connections within an application-level connection pool.