Skip to content
Open
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
8 changes: 8 additions & 0 deletions pgdog-config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,13 @@ pub struct General {
/// https://docs.pgdog.dev/configuration/pgdog.toml/general/#cutover_save_config
#[serde(default)]
pub cutover_save_config: bool,

/// Append the client host address and port to the application_name set on connection start.
/// This helps identify the source of queries in pg_stat_activity.
///
/// _Default:_ `false`
#[serde(default)]
pub application_name_add_host: bool,
}

impl Default for General {
Expand Down Expand Up @@ -827,6 +834,7 @@ impl Default for General {
cutover_timeout: Self::cutover_timeout(),
cutover_timeout_action: Self::cutover_timeout_action(),
cutover_save_config: bool::default(),
application_name_add_host: bool::default(),
unique_id_function: Self::unique_id_function(),
}
}
Expand Down
18 changes: 18 additions & 0 deletions pgdog/src/frontend/client/query_engine/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ impl QueryEngine {
let begin_stmt = self.begin_stmt.take();

// We may need to sync params with the server and that reads from the socket.
// If application_name_add_host is enabled, append client address to application_name.
// This matches PgBouncer behavior: only applied at connection start, not on SET.
if crate::config::config()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to replicate pgbouncer behavior, I would recommend handling it here:

let startup = match Startup::from_stream(&mut stream).await {

The config is already available and you can mutate the params, i.e., if application_name is indeed in the startup params, you can add the peer_addr() there once.

Your current implementation will actually update the application_name with the IP address even if the client uses SET. This is probably how we want it to work actually; I find pgbouncer's implementation arbitrarily limited (probably because they don't parse / handle SET statements).

If we go with this, I would recommend handling this here:

let config = config::config();
and only doing this operation if it hasn't been done before; this avoids unnecessary allocations (format! creates a new string).

.config
.general
.application_name_add_host
{
if let Some(addr) = *context.stream.peer_addr() {
let base = context
.params
.get_default("application_name", "PgDog")
.to_owned();
let with_host = format!("{} ({})", base, addr);
context
.params
.insert("application_name", with_host.as_str());
}
}
timeout(
query_timeout,
self.backend.link_client(
Expand Down
Loading