mirror of
https://github.com/neondatabase/neon.git
synced 2026-05-31 20:10:38 +00:00
If database was created with `is_template true` Postgres doesn't allow dropping it right away and throws error ``` ERROR: cannot drop a template database ``` so we have to unset `is_template` first. Fixing it, I noticed that our `escape_literal` isn't exactly correct and following the same logic as in `quote_literal_internal`, we need to prepend string with `E`. Otherwise, it's not possible to filter `pg_database` using `escape_literal()` result if name contains `\`, for example. Also use `FORCE` to drop database even if there are active connections. We run this from `cloud_admin`, so it should have enough privileges. NB: there could be other db states, which prevent us from dropping the database. For example, if db is used by any active subscription or logical replication slot. TODO: deal with it once we allow logical replication. Proper fix should involve returning an error code to the control plane, so it could figure out that this is a non-retryable error, return it to the user and mark operation as permanently failed. Related to neondatabase/cloud#4258
92 lines
2.8 KiB
Rust
92 lines
2.8 KiB
Rust
use std::fs::{File, OpenOptions};
|
|
use std::io;
|
|
use std::io::prelude::*;
|
|
use std::path::Path;
|
|
|
|
use anyhow::Result;
|
|
|
|
use crate::pg_helpers::escape_conf_value;
|
|
use crate::pg_helpers::PgOptionsSerialize;
|
|
use compute_api::spec::{ComputeMode, ComputeSpec};
|
|
|
|
/// Check that `line` is inside a text file and put it there if it is not.
|
|
/// Create file if it doesn't exist.
|
|
pub fn line_in_file(path: &Path, line: &str) -> Result<bool> {
|
|
let mut file = OpenOptions::new()
|
|
.read(true)
|
|
.write(true)
|
|
.create(true)
|
|
.append(false)
|
|
.open(path)?;
|
|
let buf = io::BufReader::new(&file);
|
|
let mut count: usize = 0;
|
|
|
|
for l in buf.lines() {
|
|
if l? == line {
|
|
return Ok(false);
|
|
}
|
|
count = 1;
|
|
}
|
|
|
|
write!(file, "{}{}", "\n".repeat(count), line)?;
|
|
Ok(true)
|
|
}
|
|
|
|
/// Create or completely rewrite configuration file specified by `path`
|
|
pub fn write_postgres_conf(path: &Path, spec: &ComputeSpec) -> Result<()> {
|
|
// File::create() destroys the file content if it exists.
|
|
let mut file = File::create(path)?;
|
|
|
|
// Write the postgresql.conf content from the spec file as is.
|
|
if let Some(conf) = &spec.cluster.postgresql_conf {
|
|
writeln!(file, "{}", conf)?;
|
|
}
|
|
|
|
write!(file, "{}", &spec.cluster.settings.as_pg_settings())?;
|
|
|
|
// Add options for connecting to storage
|
|
writeln!(file, "# Neon storage settings")?;
|
|
if let Some(s) = &spec.pageserver_connstring {
|
|
writeln!(file, "neon.pageserver_connstring={}", escape_conf_value(s))?;
|
|
}
|
|
if !spec.safekeeper_connstrings.is_empty() {
|
|
writeln!(
|
|
file,
|
|
"neon.safekeepers={}",
|
|
escape_conf_value(&spec.safekeeper_connstrings.join(","))
|
|
)?;
|
|
}
|
|
if let Some(s) = &spec.tenant_id {
|
|
writeln!(file, "neon.tenant_id={}", escape_conf_value(&s.to_string()))?;
|
|
}
|
|
if let Some(s) = &spec.timeline_id {
|
|
writeln!(
|
|
file,
|
|
"neon.timeline_id={}",
|
|
escape_conf_value(&s.to_string())
|
|
)?;
|
|
}
|
|
|
|
match spec.mode {
|
|
ComputeMode::Primary => {}
|
|
ComputeMode::Static(lsn) => {
|
|
// hot_standby is 'on' by default, but let's be explicit
|
|
writeln!(file, "hot_standby=on")?;
|
|
writeln!(file, "recovery_target_lsn='{lsn}'")?;
|
|
}
|
|
ComputeMode::Replica => {
|
|
// hot_standby is 'on' by default, but let's be explicit
|
|
writeln!(file, "hot_standby=on")?;
|
|
}
|
|
}
|
|
|
|
// If there are any extra options in the 'settings' field, append those
|
|
if spec.cluster.settings.is_some() {
|
|
writeln!(file, "# Managed by compute_ctl: begin")?;
|
|
write!(file, "{}", spec.cluster.settings.as_pg_settings())?;
|
|
writeln!(file, "# Managed by compute_ctl: end")?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|