ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_shmem.h
Go to the documentation of this file.
1/**
2 * @file provsql_shmem.h
3 * @brief Shared-memory segment and inter-process pipe management.
4 *
5 * ProvSQL uses a small PostgreSQL shared-memory segment (@c provsqlSharedState)
6 * to hold the file descriptors of the anonymous pipes that connect
7 * normal backends to the mmap background worker, together with a
8 * lightweight lock that serialises concurrent gate-creation requests.
9 *
10 * This header declares:
11 * - @c provsqlSharedState, the layout of the shared segment.
12 * - Hook functions (@c provsql_shmem_startup, @c provsql_shmem_request)
13 * that integrate with PostgreSQL's shared-memory lifecycle.
14 * - Convenience wrappers (@c provsql_shmem_lock_exclusive, etc.) around
15 * the embedded @c LWLock.
16 * - Saved pointers to the previous hook functions so that ProvSQL can
17 * chain into an existing hook chain.
18 */
19#ifndef PROVSQL_SHMEM_H
20#define PROVSQL_SHMEM_H
21
22#include "postgres.h"
23#include "miscadmin.h"
24#include "storage/ipc.h"
25#include "storage/lwlock.h"
26
27/** @brief Saved pointer to the previous @c shmem_startup_hook, for chaining. */
28extern shmem_startup_hook_type prev_shmem_startup;
29#if (PG_VERSION_NUM >= 150000)
30/** @brief Saved pointer to the previous @c shmem_request_hook (PG ≥ 15), for chaining. */
31extern shmem_request_hook_type prev_shmem_request;
32#endif
33
34/**
35 * @brief Initialise the ProvSQL shared-memory segment.
36 *
37 * Called from the @c shmem_startup_hook. Creates (or attaches to) the
38 * @c provsqlSharedState segment and initialises the embedded @c LWLock
39 * and pipe file descriptors. Chains to @c prev_shmem_startup if set.
40 */
41void provsql_shmem_startup(void);
42
43/**
44 * @brief Return the number of bytes required for the shared-memory segment.
45 *
46 * Used by PostgreSQL's shared-memory allocator during startup.
47 *
48 * @return Size in bytes of the @c provsqlSharedState structure.
49 */
50Size provsql_memsize(void);
51
52/**
53 * @brief Request shared memory from PostgreSQL (PG ≥ 15).
54 *
55 * Called from the @c shmem_request_hook (introduced in PostgreSQL 15) to
56 * reserve the segment before shared memory is finalised. Chains to
57 * @c prev_shmem_request if set. On older PostgreSQL versions this is a
58 * no-op because memory reservation happened in @c _PG_init().
59 */
60void provsql_shmem_request(void);
61
62/**
63 * @brief Shared state stored in the PostgreSQL shared-memory segment.
64 *
65 * All backends and the background worker access this structure through
66 * the @c provsql_shared_state global pointer.
67 *
68 * @c lock serialises gate-creation messages so that only one backend
69 * writes to the pipe at a time. The four @c long fields hold OS file
70 * descriptors for the two anonymous pipes:
71 * - @c pipembr / @c pipembw: main-backend → background (write end in main)
72 * - @c pipebmr / @c pipebmw: background → main-backend (write end in worker)
73 */
74typedef struct provsqlSharedState
75{
76 LWLock *lock; ///< Mutual-exclusion lock for pipe writes
77 long pipebmr; ///< Background-to-main pipe: read end (worker reads)
78 long pipebmw; ///< Background-to-main pipe: write end (worker writes)
79 long pipembr; ///< Main-to-background pipe: read end (worker reads)
80 long pipembw; ///< Main-to-background pipe: write end (backend writes)
82
83/** @brief Pointer to the ProvSQL shared-memory segment (set in @c provsql_shmem_startup). */
85
86/**
87 * @brief Acquire the ProvSQL LWLock in exclusive mode.
88 *
89 * Callers must pair this with @c provsql_shmem_unlock().
90 */
92
93/**
94 * @brief Acquire the ProvSQL LWLock in shared mode.
95 *
96 * Multiple backends may hold the shared lock simultaneously.
97 * Callers must pair this with @c provsql_shmem_unlock().
98 */
100
101/**
102 * @brief Release the ProvSQL LWLock.
103 *
104 * Must be called after @c provsql_shmem_lock_exclusive() or
105 * @c provsql_shmem_lock_shared().
106 */
107void provsql_shmem_unlock(void);
108
109#endif /* ifndef PROVSQL_SHMEM_H */
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
void provsql_shmem_lock_exclusive(void)
Acquire the ProvSQL LWLock in exclusive mode.
provsqlSharedState * provsql_shared_state
Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).
void provsql_shmem_request(void)
Request shared memory from PostgreSQL (PG ≥ 15).
shmem_startup_hook_type prev_shmem_startup
Saved pointer to the previous shmem_startup_hook, for chaining.
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_lock_shared(void)
Acquire the ProvSQL LWLock in shared mode.
Shared state stored in the PostgreSQL shared-memory segment.
long pipebmr
Background-to-main pipe: read end (worker reads)
LWLock * lock
Mutual-exclusion lock for pipe writes.
long pipembr
Main-to-background pipe: read end (worker reads)
long pipebmw
Background-to-main pipe: write end (worker writes)
long pipembw
Main-to-background pipe: write end (backend writes)