Macro pgx::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
String
andVec
are not supported.
For complex data structures like vecs and maps, pgx
prefers the use of types from
heapless
.
Custom types need to also implement the PGXSharedMemory
trait.
Extensions that use shared memory must be loaded via
postgresql.conf
’sshared_preload_libraries
configuration setting.
Example
use pgx::prelude::*;
use pgx::{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);
}