ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_shmem.c File Reference

Shared-memory segment lifecycle and LWLock management. More...

#include <unistd.h>
#include "postgres.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/latch.h"
#include "storage/shmem.h"
#include "provsql_shmem.h"
#include "provsql_mmap.h"
Include dependency graph for provsql_shmem.c:

Go to the source code of this file.

Functions

bool provsql_in_evaluation (void)
 Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by longjmp would skip C++ frames.
void provsql_shmem_startup (void)
 Initialise the ProvSQL shared-memory segment.
Size provsql_memsize (void)
 Return the number of bytes required for the shared-memory segment.
void provsql_shmem_request (void)
 Request shared memory from PostgreSQL (PG ≥ 15).
static bool lock_unless_cancelled (LWLockMode mode)
 Take the lock in mode, unless a cancel or a terminate arrives first: then return false, without it.
static void lock_cancellable (LWLockMode mode)
 Take the lock in mode, a cancel stopping the wait outside a ProvSQL evaluation (see provsql_shmem_lock_exclusive).
void provsql_shmem_lock_exclusive (void)
 Acquire the ProvSQL LWLock in exclusive mode.
void provsql_shmem_lock_exclusive_wait (void)
 Acquire the ProvSQL LWLock in exclusive mode, waiting whatever happens: for the background worker, WAL replay, and the KCMCP supervisor, which have no statement to cancel.
bool provsql_shmem_lock_exclusive_unless_cancelled (void)
 Acquire the ProvSQL LWLock in exclusive mode, unless the statement is cancelled (or the backend terminated) during the wait.
void provsql_shmem_lock_shared (void)
 Acquire the ProvSQL LWLock in shared mode.
void provsql_shmem_unlock (void)
 Release the ProvSQL LWLock.

Variables

shmem_startup_hook_type prev_shmem_startup = NULL
 Saved pointer to the previous shmem_startup_hook, for chaining.
shmem_request_hook_type prev_shmem_request = NULL
 Saved pointer to the previous shmem_request_hook (PG ≥ 15), for chaining.
provsqlSharedState * provsql_shared_state = NULL
 Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).

Detailed Description

Shared-memory segment lifecycle and LWLock management.

Implements the functions declared in provsql_shmem.h:

Definition in file provsql_shmem.c.

Function Documentation

◆ lock_cancellable()

void lock_cancellable ( LWLockMode mode)
static

Take the lock in mode, a cancel stopping the wait outside a ProvSQL evaluation (see provsql_shmem_lock_exclusive).

Definition at line 225 of file provsql_shmem.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_unless_cancelled()

bool lock_unless_cancelled ( LWLockMode mode)
static

Take the lock in mode, unless a cancel or a terminate arrives first: then return false, without it.

LWLockAcquire sleeps until the lock is free, deaf to interrupts. The lock is tried instead, between waits on the latch, which a cancel sets: short ones first, as the lock is mostly held briefly.

Definition at line 207 of file provsql_shmem.c.

Here is the caller graph for this function:

◆ provsql_in_evaluation()

bool provsql_in_evaluation ( void )
extern

Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by longjmp would skip C++ frames.

Definition at line 148 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_memsize()

Size provsql_memsize ( void )

Return the number of bytes required for the shared-memory segment.

Used by PostgreSQL's shared-memory allocator during startup.

Returns
Size in bytes of the provsqlSharedState structure.

Definition at line 182 of file provsql_shmem.c.

Here is the caller graph for this function:

◆ provsql_shmem_lock_exclusive()

void provsql_shmem_lock_exclusive ( void )

Acquire the ProvSQL LWLock in exclusive mode.

A backend may wait long for it, behind the building of a large circuit for another: outside a ProvSQL evaluation, a cancel, a statement_timeout or a terminate then stops the statement during the wait, as it would anywhere else. Within one (a provsql_interrupt_scope), whose C++ frames an error would jump over, the wait goes on.

Callers must pair this with provsql_shmem_unlock().

Definition at line 236 of file provsql_shmem.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_shmem_lock_exclusive_unless_cancelled()

bool provsql_shmem_lock_exclusive_unless_cancelled ( void )

Acquire the ProvSQL LWLock in exclusive mode, unless the statement is cancelled (or the backend terminated) during the wait.

For a C++ caller, which then throws.

Returns
whether the lock was taken; if so, callers must pair this with provsql_shmem_unlock().

Definition at line 246 of file provsql_shmem.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_shmem_lock_exclusive_wait()

void provsql_shmem_lock_exclusive_wait ( void )

Acquire the ProvSQL LWLock in exclusive mode, waiting whatever happens: for the background worker, WAL replay, and the KCMCP supervisor, which have no statement to cancel.

Callers must pair this with provsql_shmem_unlock().

Definition at line 241 of file provsql_shmem.c.

Here is the caller graph for this function:

◆ provsql_shmem_lock_shared()

void provsql_shmem_lock_shared ( void )

Acquire the ProvSQL LWLock in shared mode.

Multiple backends may hold the shared lock simultaneously. A cancel stops the wait as for provsql_shmem_lock_exclusive(). Callers must pair this with provsql_shmem_unlock().

Definition at line 251 of file provsql_shmem.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_shmem_request()

void provsql_shmem_request ( void )

Request shared memory from PostgreSQL (PG ≥ 15).

Called from the shmem_request_hook (introduced in PostgreSQL 15) to reserve the segment before shared memory is finalised. Chains to prev_shmem_request if set. On older PostgreSQL versions this is a no-op because memory reservation happened in _PG_init().

Definition at line 187 of file provsql_shmem.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_shmem_startup()

void provsql_shmem_startup ( void )

Initialise the ProvSQL shared-memory segment.

Called from the shmem_startup_hook. Creates (or attaches to) the provsqlSharedState segment and initialises the embedded LWLock and pipe file descriptors. Chains to prev_shmem_startup if set.

Definition at line 141 of file provsql_shmem.c.

Here is the caller graph for this function:

◆ provsql_shmem_unlock()

void provsql_shmem_unlock ( void )

Release the ProvSQL LWLock.

Must be called after provsql_shmem_lock_exclusive() or provsql_shmem_lock_shared().

Definition at line 256 of file provsql_shmem.c.

Here is the caller graph for this function:

Variable Documentation

◆ prev_shmem_request

shmem_request_hook_type prev_shmem_request = NULL

Saved pointer to the previous shmem_request_hook (PG ≥ 15), for chaining.

Definition at line 38 of file provsql_shmem.c.

◆ prev_shmem_startup

shmem_startup_hook_type prev_shmem_startup = NULL

Saved pointer to the previous shmem_startup_hook, for chaining.

Definition at line 36 of file provsql_shmem.c.

◆ provsql_shared_state

provsqlSharedState* provsql_shared_state = NULL

Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).

Definition at line 41 of file provsql_shmem.c.