[][src]Macro pgx::pg_shmem_init

macro_rules! pg_shmem_init {
    ($thing:expr) => { ... };
}

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 and Vec 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's shared_preload_libraries configuration setting.

Example

use pgx::*;

// 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);
}