ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_mmap.h
Go to the documentation of this file.
1/**
2 * @file provsql_mmap.h
3 * @brief Background worker and IPC primitives for mmap-backed circuit storage.
4 *
5 * ProvSQL persists the provenance circuit in memory-mapped files so that
6 * data survives transaction boundaries and is shared across backend
7 * processes. Because multiple backends may create gates concurrently, a
8 * dedicated PostgreSQL background worker (@c provsql_mmap_worker) is the
9 * sole writer to those files; normal backends communicate with it through
10 * a pair of anonymous pipes described in @c provsqlSharedState.
11 *
12 * This header exposes:
13 * - Functions to register, start, and manage the background worker.
14 * - A set of pipe I/O macros (@c READM, @c READB, @c WRITEB, @c WRITEM)
15 * that wrap @c read()/@c write() calls on the inter-process pipes.
16 * - A buffered-write interface (@c STARTWRITEM, @c ADDWRITEM, @c SENDWRITEM)
17 * that batches multiple fields into a single @c write() to stay within
18 * the atomic @c PIPE_BUF guarantee.
19 */
20#ifndef PROVSQL_MMAP_H
21#define PROVSQL_MMAP_H
22
23#include "limits.h"
24
25#include "postgres.h"
26#include "provsql_utils.h"
27
28/**
29 * @brief Entry point for the ProvSQL mmap background worker.
30 *
31 * Called by the postmaster when it launches the background worker.
32 * Enters the main loop (@c provsql_mmap_main_loop()) and never returns
33 * normally. The single @c Datum argument is required by the
34 * background-worker API but is not used.
35 */
37
38/**
39 * @brief Register the ProvSQL mmap background worker with PostgreSQL.
40 *
41 * Must be called from the extension's @c _PG_init() function so that
42 * the postmaster starts the worker on the next connection.
43 */
45
46/**
47 * @brief Open (or create) the mmap files and initialise the circuit store.
48 *
49 * Called once by the background worker at startup. Creates the four
50 * mmap-backed data files if they do not yet exist and maps them into the
51 * worker's address space.
52 */
54
55/**
56 * @brief Unmap and close the mmap files.
57 *
58 * Called by the background worker on shutdown to release resources and
59 * ensure all dirty pages are synced to disk via @c msync().
60 */
61void destroy_provsql_mmap(void);
62
63/**
64 * @brief Main processing loop of the mmap background worker.
65 *
66 * Waits for gate-creation requests from backend processes, processes them
67 * by writing to the mmap files, and handles SIGTERM for graceful shutdown.
68 */
69void provsql_mmap_main_loop(void);
70
71/** Shared write buffer used with @c STARTWRITEM / @c ADDWRITEM / @c SENDWRITEM. */
72extern char buffer[PIPE_BUF];
73/** Current write position within @c buffer. */
74extern unsigned bufferpos;
75
76/** @brief Read one value of @p type from the background-to-main pipe. */
77#define READM(var, type) (read(provsql_shared_state->pipebmr, &var, sizeof(type))-sizeof(type)>=0) // flawfinder: ignore
78/** @brief Read one value of @p type from the main-to-background pipe. */
79#define READB(var, type) (read(provsql_shared_state->pipembr, &var, sizeof(type))-sizeof(type)>=0) // flawfinder: ignore
80/** @brief Write one value of @p type to the main-to-background pipe. */
81#define WRITEB(pvar, type) (write(provsql_shared_state->pipembw, pvar, sizeof(type))!=-1)
82/** @brief Write one value of @p type to the background-to-main pipe. */
83#define WRITEM(pvar, type) (write(provsql_shared_state->pipebmw, pvar, sizeof(type))!=-1)
84
85/** @brief Reset the shared write buffer for a new batched write. */
86#define STARTWRITEM() (bufferpos=0)
87/** @brief Append one value of @p type to the shared write buffer. */
88#define ADDWRITEM(pvar, type) (memcpy(buffer+bufferpos, pvar, sizeof(type)), bufferpos+=sizeof(type))
89/** @brief Flush the shared write buffer to the background-to-main pipe atomically. */
90#define SENDWRITEM() (write(provsql_shared_state->pipebmw, buffer, bufferpos)!=-1)
91
92#endif /* PROVSQL_COLUMN_NAME */
void initialize_provsql_mmap(void)
Open (or create) the mmap files and initialise the circuit store.
void provsql_mmap_worker(Datum)
Entry point for the ProvSQL mmap background worker.
void destroy_provsql_mmap(void)
Unmap and close the mmap files.
char buffer[PIPE_BUF]
Shared write buffer used with STARTWRITEM / ADDWRITEM / SENDWRITEM.
void provsql_mmap_main_loop(void)
Main processing loop of the mmap background worker.
unsigned bufferpos
Current write position within buffer.
void RegisterProvSQLMMapWorker(void)
Register the ProvSQL mmap background worker with PostgreSQL.
Core types, constants, and utilities shared across ProvSQL.