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 1155120 : pub fn wal_stream_connection_config(
13 1155120 : TenantTimelineId {
14 1155120 : tenant_id,
15 1155120 : timeline_id,
16 1155120 : }: TenantTimelineId,
17 1155120 : listen_pg_addr_str: &str,
18 1155120 : auth_token: Option<&str>,
19 1155120 : availability_zone: Option<&str>,
20 1155120 : ) -> anyhow::Result<PgConnectionConfig> {
21 1155120 : let (host, port) =
22 1155120 : parse_host_port(listen_pg_addr_str).context("Unable to parse listen_pg_addr_str")?;
23 1155120 : let port = port.unwrap_or(5432);
24 1155120 : let mut connstr = PgConnectionConfig::new_host_port(host, port)
25 1155120 : .extend_options([
26 1155120 : "-c".to_owned(),
27 1155120 : format!("timeline_id={}", timeline_id),
28 1155120 : format!("tenant_id={}", tenant_id),
29 1155120 : ])
30 1155120 : .set_password(auth_token.map(|s| s.to_owned()));
31 :
32 1155120 : if let Some(availability_zone) = availability_zone {
33 855 : connstr = connstr.extend_options([format!("availability_zone={}", availability_zone)]);
34 1154265 : }
35 :
36 1155120 : Ok(connstr)
37 1155120 : }
|