ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_shmem.c
Go to the documentation of this file.
1/**
2 * @file provsql_shmem.c
3 * @brief Shared-memory segment lifecycle and LWLock management.
4 *
5 * Implements the functions declared in @c provsql_shmem.h:
6 * - @c provsql_shmem_startup(): allocates (or attaches to) the
7 * @c provsqlSharedState segment, initialises the @c LWLock, and
8 * calls @c pipe(2) to create the two inter-process pipes. Chains
9 * to @c prev_shmem_startup if set.
10 * - @c provsql_memsize(): returns the size of the shared segment.
11 * - @c provsql_shmem_request(): requests shared memory on PG ≥ 15.
12 * - @c provsql_shmem_lock_exclusive() / @c provsql_shmem_lock_shared() /
13 * @c provsql_shmem_unlock(): wrappers around the @c LWLock functions, the
14 * wait for the lock stopped by a cancel.
15 */
16#include <unistd.h>
17
18#include "postgres.h"
19#include "miscadmin.h"
20#include "pgstat.h" /* PG_WAIT_EXTENSION */
21#include "storage/latch.h"
22#include "storage/shmem.h"
23
24#include "provsql_shmem.h"
25#include "provsql_mmap.h"
26
27#if PG_VERSION_NUM < 120000
28/* WL_EXIT_ON_PM_DEATH appeared in PostgreSQL 12; before, a dead postmaster
29 * wakes the wait, and the lock, tried again, is what the caller waits for. */
30#define WL_EXIT_ON_PM_DEATH WL_POSTMASTER_DEATH
31#endif
32
33/* In provsql_interrupt.cpp, whose header is C++ */
34extern bool provsql_in_evaluation(void);
35
36shmem_startup_hook_type prev_shmem_startup = NULL;
37#if (PG_VERSION_NUM >= 150000)
38shmem_request_hook_type prev_shmem_request = NULL;
39#endif
40
42
43#ifdef PROVSQL_INPROCESS_STORE
44
45/* Single-process build: the circuit store lives in this process, reached
46 through two in-memory FIFOs instead of pipes + a background worker.
47 There is one backend and no shared memory, so the locks are no-ops. */
48
49static provsqlSharedState inproc_state;
50
51/* Write the heap-backed circuit store back to its files before the backend
52 exits, so a later backend (and PGlite's persistence) sees the data. The
53 shared-mmap build does not need this: the kernel flushes the mapping. */
54static void provsql_inproc_atexit(int code, Datum arg)
55{
56 (void) code;
57 (void) arg;
59}
60
61void provsql_inproc_init(void)
62{
63 memset(&inproc_state, 0, sizeof(inproc_state));
64 provsql_shared_state = &inproc_state;
65 /* The write-back hook is registered lazily in provsql_inproc_send, not
66 here: _PG_init runs in the postmaster under shared_preload_libraries,
67 and a forked backend resets the inherited on_proc_exit list, so a hook
68 registered now would never fire in the backend that owns the data. */
69}
70
71bool provsql_fifo_push(provsql_fifo *f, const void *src, size_t n)
72{
73 if(f->tail + n > f->cap) {
74 /* Reclaim already-consumed bytes before growing. */
75 if(f->head > 0) {
76 memmove(f->buf, f->buf + f->head, f->tail - f->head);
77 f->tail -= f->head;
78 f->head = 0;
79 }
80 if(f->tail + n > f->cap) {
81 size_t newcap = f->cap ? f->cap * 2 : 4096;
82 while(newcap < f->tail + n)
83 newcap *= 2;
84 f->buf = realloc(f->buf, newcap);
85 if(!f->buf)
86 provsql_error("ProvSQL: out of memory growing in-process FIFO");
87 f->cap = newcap;
88 }
89 }
90 memcpy(f->buf + f->tail, src, n);
91 f->tail += n;
92 return true;
93}
94
95bool provsql_fifo_pop(provsql_fifo *f, void *dst, size_t n)
96{
97 if(f->tail - f->head < n)
98 return false;
99 memcpy(dst, f->buf + f->head, n);
100 f->head += n;
101 if(f->head == f->tail)
102 f->head = f->tail = 0;
103 return true;
104}
105
106bool provsql_inproc_send(const char *buf, size_t len)
107{
108 static bool atexit_registered = false;
109 char c;
110 Oid db_oid, db_tablespace;
111
112 /* Arm the write-back hook in this backend on its first store access
113 (see provsql_inproc_init for why this cannot be done at _PG_init). */
114 if(!atexit_registered) {
115 on_proc_exit(provsql_inproc_atexit, (Datum) 0);
116 atexit_registered = true;
117 }
118
119 provsql_fifo_push(&provsql_shared_state->req, buf, len);
120
121 if(!READM(c, char) || !READM(db_oid, Oid) || !READM(db_tablespace, Oid))
122 return false;
123 provsql_mmap_dispatch(c, db_oid, db_tablespace);
124 return true;
125}
126
127/* No shared memory and no background worker to set up. */
128void provsql_shmem_startup(void) {}
129Size provsql_memsize(void) { return 0; }
130void provsql_shmem_request(void) {}
131
132/* One backend: nothing to serialise. */
135bool provsql_shmem_lock_exclusive_unless_cancelled(void) { return true; }
136void provsql_shmem_lock_shared(void) {}
137void provsql_shmem_unlock(void) {}
138
139#else
140
142{
143 bool found;
144 int pipes_b_to_m[2];
145 int pipes_m_to_b[2];
146
149
150 // Reset in case of restart
152
153 LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
154
155 provsql_shared_state = ShmemInitStruct(
156 "provsql",
157 sizeof(provsqlSharedState),
158 &found);
159
160 if(!found) {
161 provsql_shared_state->lock =&(GetNamedLWLockTranche("provsql"))->lock;
162 }
163
164 LWLockRelease(AddinShmemInitLock);
165
166 // Already initialized
167 if(found)
168 return;
169
170 if(pipe(pipes_b_to_m) || pipe(pipes_m_to_b))
171 provsql_error("Cannot create pipe to communicate with MMap worker");
172
173 provsql_shared_state->pipebmr=pipes_b_to_m[0];
174 provsql_shared_state->pipebmw=pipes_b_to_m[1];
175 provsql_shared_state->pipembr=pipes_m_to_b[0];
176 provsql_shared_state->pipembw=pipes_m_to_b[1];
177 provsql_shared_state->kcmcp_endpoint[0]='\0';
178 provsql_shared_state->worker_ready=false;
179 pg_atomic_init_u32(&provsql_shared_state->cancel_request, 0);
180}
181
183{
184 return MAXALIGN(sizeof(provsqlSharedState));
185}
186
188{
189#if (PG_VERSION_NUM >= 150000)
192#endif
193
194 RequestAddinShmemSpace(provsql_memsize());
195
196 RequestNamedLWLockTranche("provsql", 1);
197}
198
199/**
200 * @brief Take the lock in @p mode, unless a cancel or a terminate arrives
201 * first: then return @c false, without it.
202 *
203 * @c LWLockAcquire sleeps until the lock is free, deaf to interrupts. The
204 * lock is tried instead, between waits on the latch, which a cancel sets:
205 * short ones first, as the lock is mostly held briefly.
206 */
207static bool lock_unless_cancelled(LWLockMode mode)
208{
209 long timeout = 1;
210
211 while(!LWLockConditionalAcquire(provsql_shared_state->lock, mode)) {
212 if(QueryCancelPending || ProcDiePending)
213 return false;
214 (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
215 timeout, PG_WAIT_EXTENSION);
216 ResetLatch(MyLatch);
217 if(timeout < 10)
218 timeout *= 2;
219 }
220 return true;
221}
222
223/** @brief Take the lock in @p mode, a cancel stopping the wait outside a
224 * ProvSQL evaluation (see @c provsql_shmem_lock_exclusive). */
225static void lock_cancellable(LWLockMode mode)
226{
227 if(!provsql_in_evaluation()) {
228 if(lock_unless_cancelled(mode))
229 return;
230 CHECK_FOR_INTERRUPTS();
231 /* Not raised, interrupts being held: wait as LWLockAcquire does */
232 }
233 LWLockAcquire(provsql_shared_state->lock, mode);
234}
235
237{
238 lock_cancellable(LW_EXCLUSIVE);
239}
240
242{
243 LWLockAcquire(provsql_shared_state->lock, LW_EXCLUSIVE);
244}
245
247{
248 return lock_unless_cancelled(LW_EXCLUSIVE);
249}
250
252{
253 lock_cancellable(LW_SHARED);
254}
255
257{
258 LWLockRelease(provsql_shared_state->lock);
259}
260
261#endif /* PROVSQL_INPROCESS_STORE */
void destroy_provsql_mmap()
Unmap and close the mmap files.
void provsql_mmap_dispatch(char c, Oid db_oid, Oid db_tablespace)
Serve one message of the store's protocol.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Background worker and IPC primitives for mmap-backed circuit storage.
#define READM(var, type)
Read one value of type from the background-to-main pipe.
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
void provsql_shmem_lock_exclusive(void)
Acquire the ProvSQL LWLock in exclusive mode.
bool provsql_shmem_lock_exclusive_unless_cancelled(void)
Acquire the ProvSQL LWLock in exclusive mode, unless the statement is cancelled (or the backend termi...
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).
static bool lock_unless_cancelled(LWLockMode mode)
Take the lock in mode, unless a cancel or a terminate arrives first: then return false,...
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.
static void lock_cancellable(LWLockMode mode)
Take the lock in mode, a cancel stopping the wait outside a ProvSQL evaluation (see provsql_shmem_loc...
void provsql_shmem_lock_exclusive_wait(void)
Acquire the ProvSQL LWLock in exclusive mode, waiting whatever happens: for the background worker,...
Size provsql_memsize(void)
Return the number of bytes required for the shared-memory segment.
shmem_request_hook_type prev_shmem_request
Saved pointer to the previous shmem_request_hook (PG ≥ 15), for chaining.
void provsql_shmem_lock_shared(void)
Acquire the ProvSQL LWLock in shared mode.
bool provsql_in_evaluation(void)
Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by lon...
Shared-memory segment and inter-process pipe management.
Shared state stored in the PostgreSQL shared-memory segment.