21#include "storage/latch.h"
22#include "storage/shmem.h"
27#if PG_VERSION_NUM < 120000
30#define WL_EXIT_ON_PM_DEATH WL_POSTMASTER_DEATH
37#if (PG_VERSION_NUM >= 150000)
43#ifdef PROVSQL_INPROCESS_STORE
54static void provsql_inproc_atexit(
int code, Datum arg)
61void provsql_inproc_init(
void)
63 memset(&inproc_state, 0,
sizeof(inproc_state));
71bool provsql_fifo_push(provsql_fifo *f,
const void *src,
size_t n)
73 if(f->tail + n > f->cap) {
76 memmove(f->buf, f->buf + f->head, f->tail - f->head);
80 if(f->tail + n > f->cap) {
81 size_t newcap = f->cap ? f->cap * 2 : 4096;
82 while(newcap < f->tail + n)
84 f->buf = realloc(f->buf, newcap);
86 provsql_error(
"ProvSQL: out of memory growing in-process FIFO");
90 memcpy(f->buf + f->tail, src, n);
95bool provsql_fifo_pop(provsql_fifo *f,
void *dst,
size_t n)
97 if(f->tail - f->head < n)
99 memcpy(dst, f->buf + f->head, n);
101 if(f->head == f->tail)
102 f->head = f->tail = 0;
106bool provsql_inproc_send(
const char *buf,
size_t len)
108 static bool atexit_registered =
false;
110 Oid db_oid, db_tablespace;
114 if(!atexit_registered) {
115 on_proc_exit(provsql_inproc_atexit, (Datum) 0);
116 atexit_registered =
true;
121 if(!
READM(c,
char) || !
READM(db_oid, Oid) || !
READM(db_tablespace, Oid))
153 LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
164 LWLockRelease(AddinShmemInitLock);
170 if(pipe(pipes_b_to_m) || pipe(pipes_m_to_b))
171 provsql_error(
"Cannot create pipe to communicate with MMap worker");
189#if (PG_VERSION_NUM >= 150000)
196 RequestNamedLWLockTranche(
"provsql", 1);
212 if(QueryCancelPending || ProcDiePending)
214 (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
215 timeout, PG_WAIT_EXTENSION);
230 CHECK_FOR_INTERRUPTS();
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.