diff --git a/pgdog-config/src/general.rs b/pgdog-config/src/general.rs index ba3f32dc9..8903d965c 100644 --- a/pgdog-config/src/general.rs +++ b/pgdog-config/src/general.rs @@ -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 { @@ -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(), } } diff --git a/pgdog/src/frontend/client/query_engine/connect.rs b/pgdog/src/frontend/client/query_engine/connect.rs index 57bcc6026..ca189f156 100644 --- a/pgdog/src/frontend/client/query_engine/connect.rs +++ b/pgdog/src/frontend/client/query_engine/connect.rs @@ -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() + .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(