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

Add setting pg_experimental_connection_cache that is disabled by default #170

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/include/storage/postgres_connection_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class PostgresConnectionPool {
void ReturnConnection(PostgresConnection connection);
void SetMaximumConnections(idx_t new_max);

static void PostgresSetConnectionCache(ClientContext &context, SetScope scope, Value &parameter);

private:
PostgresCatalog &postgres_catalog;
mutex connection_lock;
Expand Down
3 changes: 3 additions & 0 deletions src/postgres_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ static void LoadInternal(DatabaseInstance &db) {
config.AddExtensionOption("pg_array_as_varchar",
"Read Postgres arrays as varchar - enables reading mixed dimensional arrays",
LogicalType::BOOLEAN, Value::BOOLEAN(false));
config.AddExtensionOption("pg_experimental_connection_cache",
"Whether or not to use the connection cache (currently experimental)", LogicalType::BOOLEAN,
Value::BOOLEAN(false), PostgresConnectionPool::PostgresSetConnectionCache);
config.AddExtensionOption("pg_experimental_filter_pushdown",
"Whether or not to use filter pushdown (currently experimental)", LogicalType::BOOLEAN,
Value::BOOLEAN(false));
Expand Down
11 changes: 11 additions & 0 deletions src/storage/postgres_connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "storage/postgres_catalog.hpp"

namespace duckdb {
static bool pg_use_connection_cache = false;

PostgresPoolConnection::PostgresPoolConnection() : pool(nullptr) {
}
Expand Down Expand Up @@ -61,6 +62,13 @@ bool PostgresConnectionPool::TryGetConnection(PostgresPoolConnection &connection
return true;
}

void PostgresConnectionPool::PostgresSetConnectionCache(ClientContext &context, SetScope scope, Value &parameter) {
if (parameter.IsNull()) {
throw BinderException("Cannot be set to NULL");
}
pg_use_connection_cache = BooleanValue::Get(parameter);
}

PostgresPoolConnection PostgresConnectionPool::GetConnection() {
PostgresPoolConnection result;
if (!TryGetConnection(result)) {
Expand All @@ -82,6 +90,9 @@ void PostgresConnectionPool::ReturnConnection(PostgresConnection connection) {
// immediately
return;
}
if (!pg_use_connection_cache) {
return;
}
// check if the underlying connection is still usable
auto pg_con = connection.GetConn();
if (PQstatus(connection.GetConn()) != CONNECTION_OK) {
Expand Down
3 changes: 3 additions & 0 deletions test/sql/storage/attach_connection_pool.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require postgres_scanner

require-env POSTGRES_TEST_DATABASE_AVAILABLE

statement ok
SET pg_experimental_connection_cache=true

statement ok
SET pg_connection_limit=1000

Expand Down
Loading