Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .schema/pgdog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"idle_healthcheck_delay": 5000,
"idle_healthcheck_interval": 30000,
"idle_timeout": 60000,
"jemalloc_background_thread": false,
"load_balancing_strategy": "random",
"load_schema": "auto",
"log_connections": true,
Expand Down Expand Up @@ -811,6 +812,11 @@
"default": 60000,
"minimum": 0
},
"jemalloc_background_thread": {
"description": "Enable jemalloc's background purge threads so freed memory is returned to the OS after allocation bursts. Equivalent to `_RJEM_MALLOC_CONF=background_thread:true`.\n\n_Default:_ `false`\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#jemalloc_background_thread>",
"type": "boolean",
"default": false
},
"load_balancing_strategy": {
"description": "Which strategy to use for load balancing read queries.\n\n_Default:_ `random`\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#load_balancing_strategy>",
"$ref": "#/$defs/LoadBalancingStrategy",
Expand Down
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pgdog-config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,14 @@ pub struct General {
/// <https://docs.pgdog.dev/configuration/pgdog.toml/general/#cutover_save_config>
#[serde(default)]
pub cutover_save_config: bool,

/// Enable jemalloc's background purge threads so freed memory is returned to the OS after allocation bursts. Equivalent to `_RJEM_MALLOC_CONF=background_thread:true`.
///
/// _Default:_ `false`
///
/// <https://docs.pgdog.dev/configuration/pgdog.toml/general/#jemalloc_background_thread>
#[serde(default = "General::jemalloc_background_thread")]
pub jemalloc_background_thread: bool,
}

impl Default for General {
Expand Down Expand Up @@ -911,6 +919,7 @@ impl Default for General {
cutover_timeout: Self::cutover_timeout(),
cutover_timeout_action: Self::cutover_timeout_action(),
cutover_save_config: bool::default(),
jemalloc_background_thread: Self::jemalloc_background_thread(),
unique_id_function: Self::unique_id_function(),
}
}
Expand Down Expand Up @@ -982,6 +991,10 @@ impl General {
Self::env_bool_or_default("PGDOG_SCHEMA_RELOAD_ON_DDL", true)
}

fn jemalloc_background_thread() -> bool {
Self::env_bool_or_default("PGDOG_JEMALLOC_BACKGROUND_THREAD", false)
}

fn idle_healthcheck_interval() -> u64 {
Self::env_or_default("PGDOG_IDLE_HEALTHCHECK_INTERVAL", 30_000)
}
Expand Down Expand Up @@ -1737,6 +1750,15 @@ mod tests {
assert!(!General::query_log_stdout());
}

#[test]
fn test_jemalloc_background_thread_env() {
let _guard = set_env_var("PGDOG_JEMALLOC_BACKGROUND_THREAD", "true");
assert!(General::jemalloc_background_thread());

let _guard = remove_env_var("PGDOG_JEMALLOC_BACKGROUND_THREAD");
assert!(!General::jemalloc_background_thread());
}

#[test]
fn test_env_numeric_fields() {
let _guard = set_env_var("PGDOG_BROADCAST_PORT", "7432");
Expand Down
1 change: 1 addition & 0 deletions pgdog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ libc = "0.2"

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.6"
tikv-jemalloc-ctl = "0.6"


[build-dependencies]
Expand Down
17 changes: 17 additions & 0 deletions pgdog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: &stats_alloc::StatsAlloc<System> = &stats_alloc::INSTRUMENTED_SYSTEM;

/// Enable jemalloc's background purge threads so freed memory is returned to
/// the OS after allocation bursts. No-op when disabled.
#[cfg(all(not(test), not(target_env = "msvc")))]
pub fn set_jemalloc_background_thread(enabled: bool) {
if !enabled {
return;
}
match tikv_jemalloc_ctl::background_thread::write(true) {
Ok(()) => tracing::info!("jemalloc background_thread enabled"),
Err(err) => tracing::warn!("could not enable jemalloc background_thread: {}", err),
}
}

/// No-op fallback where jemalloc is not the active allocator.
#[cfg(any(test, target_env = "msvc"))]
pub fn set_jemalloc_background_thread(_enabled: bool) {}

/// Filter that dynamically installs or removes an inner
/// [`TracingRateLimitLayer`] at runtime.
///
Expand Down
2 changes: 2 additions & 0 deletions pgdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

config::overrides(overrides);

pgdog::set_jemalloc_background_thread(config.config.general.jemalloc_background_thread);

plugin::load_from_config()?;

let runtime = build_runtime(
Expand Down
Loading