mod client;
pub mod config;
pub mod session;
mod error;
mod owner;
mod postmaster;
use std::ffi::{OsStr, OsString};
use std::io::{self, Read, Write};
use std::os::unix::prelude::OsStringExt;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Output};
use std::{fmt, fs};
use shell_quote::{QuoteExt, Sh};
use crate::runtime::{
self,
strategy::{Strategy, StrategyLike},
Runtime,
};
use crate::version;
pub use client::{ClientError, ServerError};
pub use error::ClusterError;
pub use session::{Finish, Session, Takeover};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum State {
Modified,
Unmodified,
}
use State::{Modified, Unmodified};
pub const DATABASE_TEMPLATE0: &str = "template0";
pub const DATABASE_TEMPLATE1: &str = "template1";
pub const DATABASE_POSTGRES: &str = "postgres";
pub const PORT: u16 = 5432;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ClusterStatus {
Running,
Stopped,
Missing,
}
impl fmt::Display for ClusterStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ClusterStatus::Running => write!(f, "running"),
ClusterStatus::Stopped => write!(f, "stopped"),
ClusterStatus::Missing => write!(f, "missing"),
}
}
}
#[derive(Debug)]
pub struct Cluster {
pub datadir: PathBuf,
pub strategy: Strategy,
}
mod files {
pub(super) const LOCK: &str = "pgdo.lock";
pub(super) const INIT: &str = "pgdo.init";
pub(super) const VERSION_INIT: &str = "PG_VERSION.init";
pub(super) const OWNER: &str = "pgdo.owner";
pub(super) const VERSION: &str = "PG_VERSION";
pub(super) fn is_pgdo(name: &std::ffi::OsStr) -> bool {
match name.to_str() {
Some(LOCK | OWNER | INIT | VERSION_INIT) => true,
Some(name) => name.starts_with("pgdo.owner."),
None => false,
}
}
pub(super) fn is_session(name: &std::ffi::OsStr) -> bool {
matches!(name.to_str(), Some(LOCK | OWNER))
}
}
impl Cluster {
pub fn new<P: AsRef<Path>, S: Into<Strategy>>(
datadir: P,
strategy: S,
) -> Result<Self, ClusterError> {
Ok(Self {
datadir: datadir.as_ref().to_owned(),
strategy: strategy.into(),
})
}
fn runtime(&self) -> Result<Runtime, ClusterError> {
let runtime = match version(self)? {
None => self
.strategy
.fallback()
.ok_or(ClusterError::RuntimeDefaultNotFound)?,
Some(version) if !runtime::is_supported(version) => {
return Err(ClusterError::UnsupportedVersion(version))
}
Some(version) => self
.strategy
.select(&version.into())
.ok_or(ClusterError::RuntimeNotFound(version))?,
};
if runtime.is_supported() {
Ok(runtime)
} else {
Err(ClusterError::UnsupportedRuntime(runtime.version))
}
}
fn ctl(&self) -> Result<Command, ClusterError> {
let mut command = self.runtime()?.execute("pg_ctl");
command.env("PGDATA", &self.datadir);
command.env("PGHOST", &self.datadir);
Ok(command)
}
pub fn running(&self) -> Result<bool, ClusterError> {
self.status().map(|status| status == ClusterStatus::Running)
}
pub fn status(&self) -> Result<ClusterStatus, ClusterError> {
let output = self.ctl()?.process_group(0).arg("status").output()?;
match output.status.code() {
Some(0) => Ok(ClusterStatus::Running),
Some(3) => Ok(ClusterStatus::Stopped),
Some(4) if !exists(self) => Ok(ClusterStatus::Missing),
_ => Err(ClusterError::CommandError(output)),
}
}
fn lockfile(&self) -> PathBuf {
self.datadir.join(files::LOCK)
}
fn ownerfile(&self) -> PathBuf {
self.datadir.join(files::OWNER)
}
pub fn pidfile(&self) -> PathBuf {
self.datadir.join("postmaster.pid")
}
pub fn logfile(&self) -> PathBuf {
self.datadir.join("postmaster.log")
}
pub fn create(&self) -> Result<State, ClusterError> {
if exists(self) {
Ok(Unmodified)
} else {
fs::create_dir_all(&self.datadir)?;
let initdir = self.datadir.join(files::INIT);
let version_init = self.datadir.join(files::VERSION_INIT);
if !version_init.exists() {
match fs::remove_dir_all(&initdir) {
Err(err) if err.kind() != io::ErrorKind::NotFound => Err(err)?,
_ => (),
}
self.check_datadir_empty()?;
self.initdb(&initdir)?;
fs::rename(initdir.join(files::VERSION), &version_init)?;
}
match fs::read_dir(&initdir) {
Ok(entries) => {
for entry in entries {
let entry = entry?;
fs::rename(entry.path(), self.datadir.join(entry.file_name()))?;
}
fs::set_permissions(&self.datadir, fs::metadata(&initdir)?.permissions())?;
fs::remove_dir(&initdir)?;
}
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => Err(err)?,
}
fs::rename(&version_init, self.datadir.join(files::VERSION))?;
Ok(Modified)
}
}
fn create_interrupted(&self) -> bool {
self.datadir.join(files::VERSION_INIT).exists()
}
fn check_datadir_empty(&self) -> Result<(), ClusterError> {
let mut entries = fs::read_dir(&self.datadir)?
.map(|entry| entry.map(|entry| entry.file_name()))
.filter(|name| !matches!(name, Ok(name) if files::is_pgdo(name)))
.map(|name| name.map(|name| name.to_string_lossy().into_owned()))
.collect::<Result<Vec<_>, _>>()?;
if entries.is_empty() {
Ok(())
} else {
entries.sort();
Err(ClusterError::DataDirNotEmpty(self.datadir.clone(), entries))
}
}
fn initdb(&self, dir: &Path) -> Result<State, ClusterError> {
let mut command = self.runtime()?.execute("pg_ctl");
#[allow(clippy::suspicious_command_arg_space)]
command
.env("PGDATA", dir)
.arg("init")
.arg("-s")
.arg("-o")
.arg("-E utf8 --locale C -A trust")
.env("TZ", "UTC");
bugs::retry_pg_ctl(&mut command, |_| Ok(()))
}
pub fn start(
&self,
options: &[(config::Parameter, config::Value)],
) -> Result<State, ClusterError> {
self.create()?;
if self.running()? {
Ok(Unmodified)
} else {
let options = {
let mut arg: Vec<u8> = b"-h '' -k ".into();
arg.push_quoted(Sh, &self.datadir);
arg.extend(format!(" -p {PORT}").as_bytes());
for (parameter, value) in options {
arg.extend(b" -c ");
arg.push_quoted(Sh, &format!("{parameter}={value}"));
}
OsString::from_vec(arg)
};
let logfile = self.logfile();
let logfile_name = logfile
.file_name()
.unwrap_or_else(|| "<unknown.log>".as_ref())
.display();
let mut log: logfile::LogFile = logfile.as_path().try_into()?;
let mut command = self.ctl()?;
command
.arg("start")
.arg("-l")
.arg(&logfile)
.arg("-s")
.arg("-w")
.arg("-o")
.arg(options);
let append_logs_to_stderr = |output: &mut Output| {
writeln!(&mut output.stderr)?;
writeln!(&mut output.stderr, "-- {logfile_name} --")?;
log.read_to_end(&mut output.stderr)?;
Ok(())
};
bugs::retry_pg_ctl(&mut command, append_logs_to_stderr)
}
}
fn connect(&self, database: Option<&str>) -> Result<client::Client, ClusterError> {
let user = crate::util::current_user()?;
let database = database.unwrap_or(DATABASE_POSTGRES);
Ok(client::Client::connect(&self.datadir, &user, database)?)
}
pub fn url(&self, database: &str) -> Result<Option<url::Url>, url::ParseError> {
match self.datadir.to_str() {
Some(datadir) => url::Url::parse_with_params(
"postgresql://",
[
("host", datadir),
("port", &PORT.to_string()),
("dbname", database),
],
)
.map(Some),
None => Ok(None),
}
}
pub fn shell(&self, database: Option<&str>) -> Result<ExitStatus, ClusterError> {
let mut command = self.runtime()?.execute("psql");
self.set_env(command.arg("--quiet"), database)?;
Ok(command.spawn()?.wait()?)
}
pub fn exec<T: AsRef<OsStr>>(
&self,
database: Option<&str>,
command: T,
args: &[T],
) -> Result<ExitStatus, ClusterError> {
let mut command = self.runtime()?.command(command);
self.set_env(command.args(args), database)?;
Ok(command.spawn()?.wait()?)
}
fn set_env(&self, command: &mut Command, database: Option<&str>) -> Result<(), ClusterError> {
let database = database.unwrap_or(DATABASE_POSTGRES);
command.env("PGDATA", &self.datadir);
command.env("PGHOST", &self.datadir);
command.env("PGPORT", PORT.to_string());
command.env("PGDATABASE", database);
match self.url(database)? {
Some(url) => command.env("DATABASE_URL", url.as_str()),
None => command.env_remove("DATABASE_URL"),
};
Ok(())
}
pub fn databases(&self) -> Result<Vec<String>, ClusterError> {
let rows = self
.connect(None)?
.query("SELECT datname FROM pg_catalog.pg_database ORDER BY datname")?;
let datnames = rows
.iter()
.filter_map(|row| row.get(0).map(str::to_owned))
.collect();
Ok(datnames)
}
pub fn createdb(&self, database: &str) -> Result<State, ClusterError> {
let statement = format!(
"CREATE DATABASE {}",
postgres_protocol::escape::escape_identifier(database)
);
match self.connect(None)?.execute(&statement) {
Err(client::ClientError::ServerError(err))
if err.code == client::DUPLICATE_DATABASE =>
{
Ok(Unmodified)
}
Err(err) => Err(err)?,
Ok(()) => Ok(Modified),
}
}
pub fn dropdb(&self, database: &str) -> Result<State, ClusterError> {
let statement = format!(
"DROP DATABASE {}",
postgres_protocol::escape::escape_identifier(database)
);
match self.connect(None)?.execute(&statement) {
Err(client::ClientError::ServerError(err))
if err.code == client::UNDEFINED_DATABASE =>
{
Ok(Unmodified)
}
Err(err) => Err(err)?,
Ok(()) => Ok(Modified),
}
}
pub fn stop(&self) -> Result<State, ClusterError> {
if self.running()? {
let output = self
.ctl()?
.process_group(0)
.arg("stop")
.arg("-s")
.arg("-w")
.arg("-m")
.arg("fast")
.output()?;
if output.status.success() {
Ok(Modified) } else {
Err(ClusterError::CommandError(output))
}
} else {
Ok(Unmodified)
}
}
pub fn destroy(&self) -> Result<State, ClusterError> {
self.stop()?;
self.remove()
}
fn remove(&self) -> Result<State, ClusterError> {
let entries = match fs::read_dir(&self.datadir) {
Ok(entries) => entries,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Unmodified),
Err(err) => Err(err)?,
};
for entry in entries {
let entry = entry?;
if !files::is_session(&entry.file_name()) {
let path = entry.path();
if entry.file_type()?.is_dir() {
fs::remove_dir_all(path)?;
} else {
fs::remove_file(path)?;
}
}
}
match fs::remove_dir(&self.datadir) {
Err(err)
if matches!(
err.kind(),
io::ErrorKind::NotFound | io::ErrorKind::DirectoryNotEmpty
) => {}
result => result?,
}
Ok(Modified)
}
}
impl AsRef<Path> for Cluster {
fn as_ref(&self) -> &Path {
&self.datadir
}
}
pub fn exists<P: AsRef<Path>>(datadir: P) -> bool {
let datadir = datadir.as_ref();
datadir.is_dir() && datadir.join("PG_VERSION").is_file()
}
pub fn version<P: AsRef<Path>>(
datadir: P,
) -> Result<Option<version::PartialVersion>, ClusterError> {
let versionfile = datadir.as_ref().join("PG_VERSION");
match std::fs::read_to_string(versionfile) {
Ok(version) => Ok(Some(version.parse()?)),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err)?,
}
}
pub fn determine_superuser_role_names(
cluster: &Cluster,
) -> Result<std::collections::HashSet<String>, ClusterError> {
use regex::Regex;
use std::io::Write;
use std::panic::panic_any;
use std::process::Stdio;
use std::sync::LazyLock;
const QUERY: &[u8] = b"select rolname from pg_roles where rolsuper and rolcanlogin\n";
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"\brolname\s*=\s*"(.+)""#)
.expect("invalid regex (for matching single-user role names)")
});
let mut child = cluster
.runtime()?
.execute("postgres")
.arg("--single")
.arg("-D")
.arg(&cluster.datadir)
.arg("postgres")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let mut stdin = child.stdin.take().expect("could not take stdin");
let writer = std::thread::spawn(move || stdin.write_all(QUERY));
let output = child.wait_with_output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let superusers: std::collections::HashSet<_> = RE
.captures_iter(&stdout)
.filter_map(|capture| capture.get(1))
.map(|m| m.as_str().to_owned())
.collect();
match writer.join() {
Err(err) => panic_any(err),
Ok(result) => result?,
}
if superusers.is_empty() {
Err(ClusterError::CommandError(output))
} else {
Ok(superusers)
}
}
pub type Options<'a> = &'a [(config::Parameter<'a>, config::Value)];
mod logfile {
use std::fs::File;
use std::io::{self, ErrorKind::NotFound, Read, Seek};
use std::path::{Path, PathBuf};
pub(super) struct LogFile {
path: PathBuf,
file: Option<File>,
}
impl TryFrom<&Path> for LogFile {
type Error = io::Error;
fn try_from(path: &Path) -> Result<Self, Self::Error> {
let file = match File::open(path) {
Err(err) if err.kind() == NotFound => None,
Err(err) => Err(err)?,
Ok(mut file) => {
file.seek(io::SeekFrom::End(0))?;
Some(file)
}
};
Ok(LogFile { path: path.to_owned(), file })
}
}
impl Read for LogFile {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if let Some(ref mut file) = self.file {
file.read(buf)
} else {
self.file = match File::open(&self.path) {
Err(err) if err.kind() == NotFound => return Ok(0),
Err(err) => Err(err)?,
Ok(file) => Some(file),
};
match self.file {
Some(ref mut file) => file.read(buf),
None => Ok(0),
}
}
}
}
}
mod bugs {
use super::{ClusterError, State, State::Modified};
use regex::bytes::Regex;
use std::process::{Command, Output};
use std::{fmt::Write, sync, time::Duration};
pub(super) fn retry_pg_ctl(
command: &mut Command,
mut supplement: impl FnMut(&mut Output) -> Result<(), ClusterError>,
) -> Result<State, ClusterError> {
static SEMGET_BUG_LOG: sync::Once = sync::Once::new();
static SEMGET_BUG_RE1: sync::LazyLock<Regex> = sync::LazyLock::new(|| {
Regex::new(r"\bFATAL:\s+could not create semaphores: Invalid argument\b")
.expect("invalid regex (for matching semaphore errors #1)")
});
static SEMGET_BUG_RE2: sync::LazyLock<Regex> = sync::LazyLock::new(|| {
Regex::new(r"\bDETAIL:\s+Failed system call was semget\b")
.expect("invalid regex (for matching semaphore errors #2)")
});
fn is_retryable(logs: &[u8]) -> bool {
SEMGET_BUG_RE1.is_match(logs) && SEMGET_BUG_RE2.is_match(logs)
}
let command_summary = command.get_args().take(1).fold(
command.get_program().display().to_string(),
|mut summary, arg| {
write!(&mut summary, " {}", arg.display()).ok();
summary
},
);
let run = || {
use backoff::Error;
use ClusterError::{CommandError, IoError};
match command.output() {
Ok(output) if output.status.success() => Ok(Modified),
Ok(mut output) => {
supplement(&mut output)?;
if is_retryable(output.stderr.as_slice()) {
Err(Error::transient(CommandError(output)))
} else {
Err(Error::permanent(CommandError(output)))
}
}
Err(err) => Err(Error::permanent(IoError(err))),
}
};
let notify = move |_, delay: Duration| {
SEMGET_BUG_LOG.call_once(|| {
log::info!(concat!(
"In all presently released versions of PostgreSQL, `pg_ctl` can fail on ",
"some platforms (macOS, some BSDs) due to a semget(2) failure. This is a ",
"particular problem in PostgreSQL 17.x (the linked bug report has more ",
"information). As an imperfect workaround, `pgdo` retries the command a ",
"few times when it detects this specific error. Original bug report: ",
"https://www.postgresql.org/message-id/CALL7chmzY3eXHA7zHnODUVGZLSvK3wYCSP0RmcDFHJY8f28Q3g@mail.gmail.com.",
));
});
log::warn!("`{command_summary}` failed; retrying in {delay:?}…");
};
let state = backoff::retry_notify(
backoff::ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::from_millis(200))
.with_max_elapsed_time(Some(Duration::from_mins(1)))
.with_max_interval(Duration::from_secs(10))
.build(),
run,
notify,
)
.map_err(|err| match err {
backoff::Error::Permanent(err) => err,
backoff::Error::Transient { err, .. } => err,
})?;
Ok(state)
}
}