pub struct Cluster {
pub datadir: PathBuf,
pub strategy: Strategy,
}Expand description
Representation of a PostgreSQL cluster.
The cluster may not yet exist on disk. It may exist but be stopped, or it
may be running. The methods here can be used to create, start, introspect,
stop, and destroy the cluster. There’s no protection against concurrent
changes to the cluster made by other processes; use a Session for that.
In other words, Cluster is the low-level API; Session is the locking
API, and is what most code should use. In particular, create,
start, stop, and destroy are unsafe to call while another
process might be doing the same, or might be using the cluster, so call them
directly only when you know no other process can be.
§Blocking
All methods here block. Most run external programs like pg_ctl, initdb,
or psql, and block until those finish; the rest talk to the cluster over a
socket with pgdo’s minimal internal client, and block on that I/O. All are
safe to call from async code, but will block the calling thread; to avoid
that, call them within something like Tokio’s spawn_blocking.
Fields§
§datadir: PathBufThe data directory of the cluster.
Corresponds to the PGDATA environment variable. pgdo keeps a few files
of its own in here too, all named pgdo.*.
This is also where the cluster’s Unix-domain socket lives. Clusters
managed by pgdo listen only on a Unix-domain socket, not on TCP. To
connect with a client library, use this as the host, e.g. for SQLx use
PgConnectOptions::new().socket(&cluster.datadir). See also
url.
strategy: StrategyHow to select the PostgreSQL installation to use with this cluster.
Implementations§
Source§impl Cluster
impl Cluster
Sourcepub fn session(self, options: Options<'_>) -> Result<Session, ClusterError>
pub fn session(self, options: Options<'_>) -> Result<Session, ClusterError>
Start a Session with this cluster, creating and starting it as
necessary.
The cluster’s data directory is created if it does not exist, and the cluster’s path is made absolute (canonicalized).
Sourcepub fn session_with_takeover(
self,
options: Options<'_>,
takeover: Takeover,
) -> Result<Session, ClusterError>
pub fn session_with_takeover( self, options: Options<'_>, takeover: Takeover, ) -> Result<Session, ClusterError>
Source§impl Cluster
impl Cluster
Sourcepub fn new<P: AsRef<Path>, S: Into<Strategy>>(
datadir: P,
strategy: S,
) -> Result<Self, ClusterError>
pub fn new<P: AsRef<Path>, S: Into<Strategy>>( datadir: P, strategy: S, ) -> Result<Self, ClusterError>
Represent a cluster at the given path.
Sourcepub fn running(&self) -> Result<bool, ClusterError>
pub fn running(&self) -> Result<bool, ClusterError>
Check if this cluster is running.
Convenient call-through to status; only returns
true when the cluster is definitely running.
Sourcepub fn status(&self) -> Result<ClusterStatus, ClusterError>
pub fn status(&self) -> Result<ClusterStatus, ClusterError>
Check the status of this cluster.
Tries to distinguish carefully between “definitely running”, “definitely
not running”, “missing”, and “don’t know”. The latter results in
ClusterError.
Sourcepub fn pidfile(&self) -> PathBuf
pub fn pidfile(&self) -> PathBuf
Return the path to the PID file used in this cluster.
The PID file does not necessarily exist.
Sourcepub fn logfile(&self) -> PathBuf
pub fn logfile(&self) -> PathBuf
Return the path to the log file used in this cluster.
The log file does not necessarily exist.
Sourcepub fn create(&self) -> Result<State, ClusterError>
pub fn create(&self) -> Result<State, ClusterError>
Create the cluster if it does not already exist.
initdb refuses to use a directory that is not empty, but pgdo keeps
its own files – e.g. pgdo.lock – in the data directory. So this runs
initdb in a subdirectory, pgdo.init, then moves the result up:
- Move
pgdo.init/PG_VERSIONtoPG_VERSION.init. This marks a move in progress, and means that neither directory looks like a cluster. - Move everything else from
pgdo.initinto the data directory, then removepgdo.init. - Move
PG_VERSION.inittoPG_VERSION.
If interrupted, calling this again will finish the job: if
PG_VERSION.init exists, it resumes the move; otherwise it discards
pgdo.init, if it exists, and runs initdb again.
This refuses to create a cluster in a data directory that contains
anything other than pgdo’s own files; see
ClusterError::DataDirNotEmpty.
Use this only while holding an exclusive lock on the cluster, e.g. via a
Session, or when no other process can be using the cluster.
Sourcepub fn start(
&self,
options: &[(Parameter<'_>, Value)],
) -> Result<State, ClusterError>
pub fn start( &self, options: &[(Parameter<'_>, Value)], ) -> Result<State, ClusterError>
Start the cluster if it’s not already running, with the given options.
Returns State::Unmodified if the cluster is already running, meaning
the given options were NOT applied.
Sourcepub fn url(&self, database: &str) -> Result<Option<Url>, ParseError>
pub fn url(&self, database: &str) -> Result<Option<Url>, ParseError>
Return a URL for connecting to the given database in this cluster, e.g.
postgresql://?host=%2Fpath%2Fto%2Fcluster&port=5432&dbname=postgres.
The URL has no user; clients typically default to the current user.
It is not possible to return a URL for a cluster when its
datadir is not valid UTF-8, in which case
Ok(None) is returned.
Sourcepub fn shell(&self, database: Option<&str>) -> Result<ExitStatus, ClusterError>
pub fn shell(&self, database: Option<&str>) -> Result<ExitStatus, ClusterError>
Run psql against this cluster, in the given database.
When the database is not specified, connects to DATABASE_POSTGRES.
Sourcepub fn exec<T: AsRef<OsStr>>(
&self,
database: Option<&str>,
command: T,
args: &[T],
) -> Result<ExitStatus, ClusterError>
pub fn exec<T: AsRef<OsStr>>( &self, database: Option<&str>, command: T, args: &[T], ) -> Result<ExitStatus, ClusterError>
Run the given command against this cluster.
The command is run with the PGDATA, PGHOST, and PGDATABASE
environment variables set appropriately.
When the database is not specified, uses DATABASE_POSTGRES.
Sourcepub fn databases(&self) -> Result<Vec<String>, ClusterError>
pub fn databases(&self) -> Result<Vec<String>, ClusterError>
The names of databases in this cluster.
Sourcepub fn createdb(&self, database: &str) -> Result<State, ClusterError>
pub fn createdb(&self, database: &str) -> Result<State, ClusterError>
Create the named database.
Returns Unmodified if the database already exists, otherwise it
returns Modified.
Sourcepub fn dropdb(&self, database: &str) -> Result<State, ClusterError>
pub fn dropdb(&self, database: &str) -> Result<State, ClusterError>
Drop the named database.
Returns Unmodified if the database does not exist, otherwise it
returns Modified.
Sourcepub fn stop(&self) -> Result<State, ClusterError>
pub fn stop(&self) -> Result<State, ClusterError>
Stop the cluster if it’s running.
Sourcepub fn destroy(&self) -> Result<State, ClusterError>
pub fn destroy(&self) -> Result<State, ClusterError>
Destroy the cluster if it exists, after stopping it.
This removes everything in the data directory except the files that
sessions use to coordinate, i.e. pgdo.lock and pgdo.owner, then
removes the data directory if that leaves it empty.
A Session that ends with Finish::Destroy removes pgdo’s files –
safely – and the data directory too.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Cluster
impl !UnwindSafe for Cluster
impl Forget for Cluster
impl Freeze for Cluster
impl Move for Cluster
impl Send for Cluster
impl Sync for Cluster
impl Unpin for Cluster
impl UnsafeUnpin for Cluster
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more