![]() |
ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
|
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"
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). | |
Shared-memory segment lifecycle and LWLock management.
Implements the functions declared in provsql_shmem.h:
provsql_shmem_startup(): allocates (or attaches to) the provsqlSharedState segment, initialises the LWLock, and calls pipe(2) to create the two inter-process pipes. Chains to prev_shmem_startup if set.provsql_memsize(): returns the size of the shared segment.provsql_shmem_request(): requests shared memory on PG ≥ 15.provsql_shmem_lock_exclusive() / provsql_shmem_lock_shared() / provsql_shmem_unlock(): wrappers around the LWLock functions, the wait for the lock stopped by a cancel. Definition in file provsql_shmem.c.
|
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.


|
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.

|
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.

| Size provsql_memsize | ( | void | ) |
Return the number of bytes required for the shared-memory segment.
Used by PostgreSQL's shared-memory allocator during startup.
provsqlSharedState structure. Definition at line 182 of file provsql_shmem.c.

| 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.


| 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.
provsql_shmem_unlock(). Definition at line 246 of file provsql_shmem.c.


| 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.

| 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.


| 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.


| 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.

| 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.

| 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.
| 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.
| 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.