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

Implemented PGBouncer like database Proxy functionality using Auth_Query #855

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions pgcat.proxy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is an example of the most basic config
# that will mimic what PgBouncer does in transaction mode with one server.

[general]

log_level = "DEBUG"

host = "0.0.0.0"
port = 6433
admin_username = "pgcat"
admin_password = "pgcat"

[pools.pgml]
auth_query="SELECT usename, passwd FROM pg_shadow WHERE usename='$1'"

# Be sure to grant this user LOGIN on Postgres
auth_query_user = "postgres"
auth_query_password = "postgres"

proxy = true

[pools.pgml.users.0]
username = "postgres"
#password = "postgres"
pool_size = 10
min_pool_size = 1
pool_mode = "session"

[pools.pgml.shards.0]
servers = [
["localhost", 5432, "primary"]
]
database = "postgres"
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::config::{
use crate::constants::*;
use crate::messages::*;
use crate::plugins::PluginOutput;
use crate::pool::{get_pool, ClientServerMap, ConnectionPool};
use crate::pool::{get_or_create_pool, get_pool, ClientServerMap, ConnectionPool};
use crate::query_router::{Command, QueryRouter};
use crate::server::{Server, ServerParameters};
use crate::stats::{ClientStats, ServerStats};
Expand Down Expand Up @@ -557,7 +557,7 @@ where
}
// Authenticate normal user.
else {
let pool = match get_pool(pool_name, username) {
let pool = match get_or_create_pool(pool_name, username).await {
Some(pool) => pool,
None => {
error_response(
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ pub struct Pool {
#[serde(default = "Pool::default_prepared_statements_cache_size")]
pub prepared_statements_cache_size: usize,

#[serde(default = "Pool::default_proxy")]
pub proxy: bool,

pub plugins: Option<Plugins>,
pub shards: BTreeMap<String, Shard>,
pub users: BTreeMap<String, User>,
Expand Down Expand Up @@ -642,6 +645,10 @@ impl Pool {
0
}

pub fn default_proxy() -> bool {
false
}

pub fn validate(&mut self) -> Result<(), Error> {
match self.default_role.as_ref() {
"any" => (),
Expand Down Expand Up @@ -753,6 +760,7 @@ impl Default for Pool {
cleanup_server_connections: true,
log_client_parameter_status_changes: false,
prepared_statements_cache_size: Self::default_prepared_statements_cache_size(),
proxy: Self::default_proxy(),
plugins: None,
shards: BTreeMap::from([(String::from("1"), Shard::default())]),
users: BTreeMap::default(),
Expand Down Expand Up @@ -1228,6 +1236,11 @@ impl Config {
pool_name,
pool_config.pool_mode.to_string()
);
info!(
"[pool: {}] Proxy mode: {}",
pool_name,
pool_config.proxy.to_string()
);
info!(
"[pool: {}] Load Balancing mode: {:?}",
pool_name, pool_config.load_balancing_mode
Expand Down
Loading