Macro pgrx::pg_shmem_init
source · macro_rules! pg_shmem_init { ($thing:expr) => { ... }; }
Expand description
In order to store a type in Postgres Shared Memory, it must be passed to
pg_shmem_init!() during _PG_init().
Additionally, the type must be a static global and also be #[derive(Copy, Clone)].
Types that allocate on the heap, such as
StringandVecare not supported.
For complex data structures like vecs and maps, pgrx prefers the use of types from
heapless.
Custom types need to also implement the PGRXSharedMemory trait.
Extensions that use shared memory must be loaded via
postgresql.conf’sshared_preload_librariesconfiguration setting.
Example
use pgrx::prelude::*;
use pgrx::{PgAtomic, PgLwLock, pg_shmem_init, PgSharedMemoryInitialization};
// primitive types must be protected behind a `PgLwLock`
static PRIMITIVE: PgLwLock<i32> = PgLwLock::new();
// Rust atomics can be used without locks, wrapped in a `PgAtomic`
static ATOMIC: PgAtomic<std::sync::atomic::AtomicBool> = PgAtomic::new();
#[pg_guard]
pub extern "C" fn _PG_init() {
pg_shmem_init!(PRIMITIVE);
pg_shmem_init!(ATOMIC);
}