Line data Source code
1 : //! Postgres client connection code common to other crates (safekeeper and
2 : //! pageserver) which depends on tenant/timeline ids and thus not fitting into
3 : //! postgres_connection crate.
4 :
5 : use anyhow::Context;
6 : use postgres_connection::{parse_host_port, PgConnectionConfig};
7 :
8 : use crate::id::TenantTimelineId;
9 :
10 : /// Create client config for fetching WAL from safekeeper on particular timeline.
11 : /// listen_pg_addr_str is in form host:\[port\].
12 78 : pub fn wal_stream_connection_config(
13 78 : TenantTimelineId {
14 78 : tenant_id,
15 78 : timeline_id,
16 78 : }: TenantTimelineId,
17 78 : listen_pg_addr_str: &str,
18 78 : auth_token: Option<&str>,
19 78 : availability_zone: Option<&str>,
20 78 : ) -> anyhow::Result<PgConnectionConfig> {
21 78 : let (host, port) =
22 78 : parse_host_port(listen_pg_addr_str).context("Unable to parse listen_pg_addr_str")?;
23 78 : let port = port.unwrap_or(5432);
24 78 : let mut connstr = PgConnectionConfig::new_host_port(host, port)
25 78 : .extend_options([
26 78 : "-c".to_owned(),
27 78 : format!("timeline_id={}", timeline_id),
28 78 : format!("tenant_id={}", tenant_id),
29 78 : ])
30 78 : .set_password(auth_token.map(|s| s.to_owned()));
31 :
32 78 : if let Some(availability_zone) = availability_zone {
33 12 : connstr = connstr.extend_options([format!("availability_zone={}", availability_zone)]);
34 66 : }
35 :
36 78 : Ok(connstr)
37 78 : }
|