38#include "access/xact.h"
39#include "postmaster/bgworker.h"
40#include "storage/fd.h"
43#include "utils/array.h"
44#include "access/htup_details.h"
45#include "utils/builtins.h"
49#ifdef PROVSQL_INPROCESS_STORE
55void provsql_buffer_ensure(
size_t need)
57 if(need > buffercap) {
58 size_t newcap = buffercap ? buffercap * 2 : 4096;
63 provsql_error(
"ProvSQL: out of memory growing the IPC buffer");
70 return QueryCancelPending || ProcDiePending;
84#define WORKER_READ_BUFFER (64 * 1024)
114 if(fstat(fd, &st) == 0 && st.st_size > 0) {
122 provsql_log(
"replaying %zu bytes of store messages spooled during "
154 size_t take = avail < n ? avail : n;
164 if(r < 0 && errno == EINTR)
180 size_t remaining = n;
181 while(remaining > 0) {
182 ssize_t r = read(fd, p, remaining);
183 if(r < 0 && errno == EINTR)
198 size_t remaining = n;
199 while(remaining > 0) {
200 ssize_t r = write(fd, p, remaining);
220 bool cancelled =
false;
226 if(!cancelled && (QueryCancelPending || ProcDiePending)) {
231 r = poll(&pfd, 1, cancelled ? -1 : 100);
233 if(r > 0 || (r < 0 && errno != EINTR))
243#if PG_VERSION_NUM >= 190000
255static void provsql_worker_die(SIGNAL_ARGS)
258 (errcode(ERRCODE_ADMIN_SHUTDOWN),
259 errmsg(
"terminating background worker \"%s\" due to administrator command",
260 MyBgworkerEntry->bgw_type)));
266#if PG_VERSION_NUM >= 190000
267 pqsignal(SIGTERM, provsql_worker_die);
269 BackgroundWorkerUnblockSignals();
279 provsql_log(
"%s initialized", MyBgworkerEntry->bgw_name);
288 BackgroundWorker worker;
290 snprintf(worker.bgw_name, BGW_MAXLEN,
"ProvSQL MMap Worker");
291 snprintf(worker.bgw_type, BGW_MAXLEN,
"ProvSQL MMap");
293 worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
294 worker.bgw_start_time = BgWorkerStart_PostmasterStart;
295 worker.bgw_restart_time = 1;
297 snprintf(worker.bgw_library_name, BGW_MAXLEN,
"provsql");
298 snprintf(worker.bgw_function_name, BGW_MAXLEN,
"provsql_mmap_worker");
299 worker.bgw_main_arg = (Datum) 0;
300 worker.bgw_notify_pid = 0;
302 RegisterBackgroundWorker(&worker);
344 provsql_error(
"Cannot communicate with pipe (message type S)");
353 case XACT_EVENT_PRE_COMMIT:
354 case XACT_EVENT_PRE_PREPARE:
360 case XACT_EVENT_COMMIT:
361 case XACT_EVENT_ABORT:
362 case XACT_EVENT_PREPARE:
363 case XACT_EVENT_PARALLEL_COMMIT:
364 case XACT_EVENT_PARALLEL_ABORT:
379 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
380 errmsg(
"cannot write to the ProvSQL circuit store during recovery"),
381 errdetail(
"The store is maintained on a standby by replaying the "
382 "primary's records; a backend writing to it would make "
384 errhint(
"Provenance queries create gates as they run, reads "
385 "included, so they only work on the primary.")));
415 char flag = dry_run ? 1 : 0;
416 unsigned long n = (
unsigned long) nb_roots;
435#ifdef PROVSQL_INPROCESS_STORE
439 provsql_error(
"circuit_cleanup is not available in the single-process build");
442 unsigned long per_batch = PIPE_BUF /
sizeof(
pg_uuid_t);
443 for(
unsigned long i = 0; i < n; ) {
446 for(j = 0; j < per_batch && i < n; ++j, ++i)
459 provsql_error(
"Cannot read response from pipe (message type X)");
465#ifndef PROVSQL_INPROCESS_STORE
472#ifndef PROVSQL_INPROCESS_STORE
487#ifdef PROVSQL_INPROCESS_STORE
488 (void) data; (void) len;
490 const char *p = data;
510 O_WRONLY | O_CREAT | O_APPEND | PG_BINARY,
532 size_t chunk = left > PIPE_BUF ? PIPE_BUF : left;
546 if(!
READB(result,
char) || !
READB(stored,
double)) {
548 provsql_error(
"Cannot read the reply to a replayed store message");
570 unsigned long nb_gates, nb_mapping, next_value,
571 dangling, unreferenced, bad_wires, bad_extra;
574 bool nulls[8] = {
false,
false,
false,
false,
false,
false,
false,
false};
582 || !
READB(unclean,
char)
583 || !
READB(nb_gates,
unsigned long)
584 || !
READB(nb_mapping,
unsigned long)
585 || !
READB(next_value,
unsigned long)
586 || !
READB(dangling,
unsigned long)
587 || !
READB(unreferenced,
unsigned long)
588 || !
READB(bad_wires,
unsigned long)
589 || !
READB(bad_extra,
unsigned long)) {
591 provsql_error(
"Cannot communicate with pipe (message type k)");
595 if(get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
596 provsql_error(
"check_store: expected composite return type");
597 tupdesc = BlessTupleDesc(tupdesc);
599 values[0] = BoolGetDatum(unclean != 0);
600 values[1] = Int64GetDatum((int64) nb_gates);
601 values[2] = Int64GetDatum((int64) nb_mapping);
602 values[3] = Int64GetDatum((int64) next_value);
603 values[4] = Int64GetDatum((int64) dangling);
604 values[5] = Int64GetDatum((int64) unreferenced);
605 values[6] = Int64GetDatum((int64) bad_wires);
606 values[7] = Int64GetDatum((int64) bad_extra);
608 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
630 unsigned *nb_children_out,
634 unsigned nb_children = 0;
653 provsql_error(
"Cannot communicate on pipe (message type t)");
667 provsql_error(
"Cannot communicate on pipe (message type c during get_gate_type)");
670 if(nb_children > 0) {
671 children = calloc(nb_children,
sizeof(
pg_uuid_t));
674 provsql_error(
"Cannot read children from pipe (during get_gate_type)");
689 *nb_children_out = nb_children;
690 *children_out = children;
696 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
699 unsigned nb_children = 0;
706 if(children) free(children);
719 unsigned nb_children,
734 size_t header =
sizeof(char) + 2 *
sizeof(Oid) +
sizeof(
pg_uuid_t)
736 size_t len = header + nb_children *
sizeof(
pg_uuid_t);
737 char *msg = palloc(len);
740 memcpy(p, &MyDatabaseId,
sizeof(Oid)); p +=
sizeof(Oid);
741 memcpy(p, &MyDatabaseTableSpace,
sizeof(Oid)); p +=
sizeof(Oid);
744 memcpy(p, &nb_children,
sizeof(
unsigned)); p +=
sizeof(unsigned);
745 for(
unsigned i=0; i<nb_children; ++i) {
746 memcpy(p, &children_data[i],
sizeof(
pg_uuid_t));
760#ifdef PROVSQL_INPROCESS_STORE
770 for(
unsigned i=0; i<nb_children; ++i)
780#ifndef PROVSQL_INPROCESS_STORE
784 unsigned children_per_batch = PIPE_BUF/
sizeof(
pg_uuid_t);
793 for(
unsigned j=0; j<1+(nb_children-1)/children_per_batch; ++j) {
796 for(
unsigned i=j*children_per_batch; i<(j+1)*children_per_batch && i<nb_children; ++i) {
836 provsql_error(
"Cannot communicate with pipe (message type P)");
870 provsql_error(
"Cannot communicate with pipe (message type q)");
880 unsigned nb_children,
882 bool has_infos,
unsigned info1,
883 unsigned info2,
const char *extra)
885 char flag = has_infos ? 1 : 0;
886 unsigned len = extra != NULL ? (unsigned) strlen(extra) : 0;
887 size_t total =
sizeof(char) + 2 *
sizeof(Oid) +
sizeof(
pg_uuid_t)
890 +
sizeof(char) + 3 *
sizeof(
unsigned) + len;
891 char *msg = palloc(total);
896 memcpy(p, &MyDatabaseId,
sizeof(Oid)); p +=
sizeof(Oid);
897 memcpy(p, &MyDatabaseTableSpace,
sizeof(Oid)); p +=
sizeof(Oid);
900 memcpy(p, &nb_children,
sizeof(
unsigned)); p +=
sizeof(unsigned);
901 for(i=0; i<nb_children; ++i) {
902 memcpy(p, &children[i],
sizeof(
pg_uuid_t));
906 memcpy(p, &info1,
sizeof(
unsigned)); p +=
sizeof(unsigned);
907 memcpy(p, &info2,
sizeof(
unsigned)); p +=
sizeof(unsigned);
908 memcpy(p, &len,
sizeof(
unsigned)); p +=
sizeof(unsigned);
910 memcpy(p, extra, len);
918#ifdef PROVSQL_INPROCESS_STORE
920 if(!provsql_inproc_send(msg, total)) {
926 if(total <= PIPE_BUF) {
938 size_t chunk = left > PIPE_BUF ? PIPE_BUF : left;
958 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
959 Oid oid_type = PG_GETARG_INT32(1);
960 ArrayType *children = PG_ARGISNULL(2)?NULL:PG_GETARG_ARRAYTYPE_P(2);
961 unsigned nb_children = 0;
966 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
970 if(ARR_NDIM(children) > 1)
971 provsql_error(
"Invalid multi-dimensional array passed to create_gate");
972 if(array_contains_nulls(children))
973 provsql_error(
"create_gate: children array must not contain NULL "
974 "elements (filter them out before calling)");
975 if(ARR_NDIM(children) == 1)
976 nb_children = *ARR_DIMS(children);
992 children_data = (
pg_uuid_t*) ARR_DATA_PTR(children);
994 children_data = NULL;
999 bool has_infos = !PG_ARGISNULL(3) || !PG_ARGISNULL(4);
1000 unsigned info1 = PG_ARGISNULL(3) ? 0 : (unsigned) PG_GETARG_INT32(3);
1001 unsigned info2 = PG_ARGISNULL(4) ? 0 : (unsigned) PG_GETARG_INT32(4);
1002 char *extra = PG_ARGISNULL(5) ? NULL : text_to_cstring(PG_GETARG_TEXT_PP(5));
1005 has_infos, info1, info2, extra);
1020#ifdef PROVSQL_INPROCESS_STORE
1021 if(!provsql_inproc_send(msg, len))
1022 provsql_error(
"Cannot write to pipe (message type %c)", msg[0]);
1029 provsql_error(
"Cannot write to pipe (message type %c)", msg[0]);
1039 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1040 unsigned info1 = PG_ARGISNULL(1) ? 0 : (unsigned) PG_GETARG_INT32(1);
1041 unsigned info2 = PG_ARGISNULL(2) ? 0 : (unsigned) PG_GETARG_INT32(2);
1042 char msg[
sizeof(char) + 2 *
sizeof(Oid) +
sizeof(
pg_uuid_t) + 2 *
sizeof(
unsigned)];
1048 memcpy(p, &MyDatabaseId,
sizeof(Oid)); p +=
sizeof(Oid);
1049 memcpy(p, &MyDatabaseTableSpace,
sizeof(Oid)); p +=
sizeof(Oid);
1051 memcpy(p, &info1,
sizeof(
unsigned)); p +=
sizeof(unsigned);
1052 memcpy(p, &info2,
sizeof(
unsigned));
1062 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1063 char *str = text_to_cstring(PG_GETARG_TEXT_PP(1));
1064 unsigned len = (unsigned) strlen(str);
1065 size_t total =
sizeof(char) + 2 *
sizeof(Oid) +
sizeof(
pg_uuid_t) +
sizeof(
unsigned) + len;
1066 char *msg = palloc(total);
1070 memcpy(p, &MyDatabaseId,
sizeof(Oid)); p +=
sizeof(Oid);
1071 memcpy(p, &MyDatabaseTableSpace,
sizeof(Oid)); p +=
sizeof(Oid);
1073 memcpy(p, &len,
sizeof(
unsigned)); p +=
sizeof(unsigned);
1074 memcpy(p, str, len);
1086 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1102 provsql_error(
"Cannot communicate with pipe (message type e)");
1105 result = palloc(len + VARHDRSZ);
1106 SET_VARSIZE(result, VARHDRSZ + len);
1110 provsql_error(
"Cannot communicate with pipe (message type e)");
1115 PG_RETURN_TEXT_P(result);
1132 provsql_error(
"Cannot communicate with pipe (message type n)");
1137 PG_RETURN_INT64((
long) nb);
1144 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1145 ArrayType *result = NULL;
1146 unsigned nb_children;
1148 Datum *children_ptr;
1169 if(!
READB(nb_children,
unsigned)) {
1171 provsql_error(
"Cannot read response from pipe (message type c)");
1174 children=calloc(nb_children,
sizeof(
pg_uuid_t));
1190 children_ptr = palloc(nb_children *
sizeof(Datum));
1191 for(
unsigned i=0; i<nb_children; ++i)
1192 children_ptr[i] = UUIDPGetDatum(&children[i]);
1195 result = construct_array(
1202 pfree(children_ptr);
1205 PG_RETURN_ARRAYTYPE_P(result);
1212 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1227 provsql_error(
"Cannot communicate with pipe (message type p)");
1235 PG_RETURN_FLOAT8(result);
1239 unsigned *info1,
unsigned *info2)
1250 provsql_error(
"Cannot communicate with pipe (message type i)");
1258 text *t = DatumGetTextPP(DirectFunctionCall1(
get_extra,
1259 UUIDPGetDatum(token)));
1260 return text_to_cstring(t);
1267 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
1268 unsigned info1 =0, info2 = 0;
1278 bool nulls[2] = {
false,
false};
1280 get_call_result_type(fcinfo,NULL,&tupdesc);
1281 tupdesc = BlessTupleDesc(tupdesc);
1283 values[0] = Int32GetDatum(info1);
1284 values[1] = Int32GetDatum(info2);
1286 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
void destroy_provsql_mmap()
Unmap and close the mmap files.
void provsql_mmap_main_loop()
Main processing loop of the mmap background worker.
void initialize_provsql_mmap()
Initialise the circuit store.
C-linkage interface to the in-process provenance circuit cache.
gate_type circuit_cache_get_type(pg_uuid_t token)
Retrieve the type of a cached gate.
unsigned circuit_cache_get_children(pg_uuid_t token, pg_uuid_t **children)
Retrieve the children of a cached gate.
bool circuit_cache_create_gate(pg_uuid_t token, gate_type type, unsigned nb_children, const pg_uuid_t *children)
Insert a new gate into the circuit cache.
Datum get_gate_type(PG_FUNCTION_ARGS)
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
#define provsql_log(fmt,...)
Write a ProvSQL message to the server log only.
bool provsql_write_all(int fd, const void *src, size_t n)
Write the n bytes of src to fd, however many write calls it takes; false on error.
bool provsql_store_synchronous_commit
Global variable set by the provsql.store_synchronous_commit run-time configuration parameter: when tr...
Datum get_infos(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for get_infos().
void provsql_mmap_worker(Datum ignored)
Entry point for the ProvSQL mmap background worker.
bool provsql_store_written(void)
Whether this transaction has written to the circuit store.
void provsql_internal_create_gate(const pg_uuid_t *token, gate_type type, unsigned nb_children, const pg_uuid_t *children_data)
Internal entry point behind create_gate(): cache + worker IPC.
void provsql_replay_store_message(const char *data, size_t len)
Feed a logged store message back to the worker.
void provsql_request_start(void)
Start a request that may keep the worker busy (the building of a circuit): no cancel is pending for i...
#define WORKER_READ_BUFFER
static void provsql_take_replay_spool(void)
Datum check_store(PG_FUNCTION_ARGS)
Report what does not add up in this database's circuit store.
void provsql_internal_create_gate_with(const pg_uuid_t *token, gate_type type, unsigned nb_children, const pg_uuid_t *children, bool has_infos, unsigned info1, unsigned info2, const char *extra)
Create a gate together with its infos and its text, in one message that is not answered.
static int replay_spool_fd
void provsql_store_note_write(void)
Note that this transaction has written to the circuit store.
static void provsql_store_xact_callback(XactEvent event, void *arg)
static void provsql_store_sync_barrier(void)
Send the sync barrier and wait for the worker's acknowledgement.
static void provsql_before_store_write(const char *data, size_t len)
The same, for a mutation made by a live transaction: it also arms the at-commit sync barrier.
void provsql_replay_spool_close(void)
Sync and close the replay spool at the end of WAL replay (the resource manager's cleanup).
bool provsql_read_all(int fd, void *dst, size_t n)
Read exactly n bytes from fd into dst; false on EOF/error.
static void send_unanswered(const char *msg, size_t len)
static size_t worker_buffer_pos
static bool store_written
Whether the current transaction has written anything to the store.
Datum set_extra(PG_FUNCTION_ARGS)
Entry point of the set_extra of earlier versions' scripts.
Datum get_nb_gates(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for get_nb_gates().
Datum create_gate(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for create_gate().
bool provsql_worker_spooled(void)
Whether store messages spooled during crash recovery remain to be applied (they come before the pipe)...
static bool store_callbacks_registered
gate_type provsql_fetch_gate(const pg_uuid_t *token, unsigned *nb_children_out, pg_uuid_t **children_out)
PostgreSQL-callable wrapper for get_gate_type().
Datum set_infos(PG_FUNCTION_ARGS)
Entry point of the set_infos of earlier versions' scripts.
void provsql_circuit_cleanup_request(bool dry_run, const pg_uuid_t *roots, int64 nb_roots, provsql_cleanup_result *out)
Ask the worker to rebuild this database's store, keeping only what roots reach.
bool provsql_worker_buffered(void)
Whether the worker's read buffer holds unread bytes, which poll() cannot see.
bool provsql_worker_read(void *dst, size_t n)
The worker's buffered read of the request pipe: what the pipe holds is read in one call,...
Datum get_gate_type(PG_FUNCTION_ARGS)
static size_t worker_buffer_len
provsql_set_prob_result provsql_internal_set_prob(const pg_uuid_t *token, double prob, double *existing)
Write a gate's probability from in-extension C/C++ code.
bool provsql_store_cancel_requested(void)
Whether the circuit being built should be given up: in the worker, the backend that asked for it was ...
char buffer[PIPE_BUF]
Shared write buffer used with STARTWRITEM / ADDWRITEM / SENDWRITEM.
bool provsql_internal_get_prob_written(const pg_uuid_t *token, double *prob)
Report whether a probability has been written on a gate.
void provsql_internal_clear_prob(const pg_uuid_t *token)
Drop a gate's probability, leaving it as it was before anyone wrote one.
void provsql_internal_get_infos(const pg_uuid_t *token, unsigned *info1, unsigned *info2)
Read a gate's info1 and info2, through the worker.
Datum get_children(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for get_children().
bool provsql_wait_reply(void)
Wait, under the store lock, until the worker's reply to this backend's request can be read.
static void provsql_log_store_write(const char *data, size_t len)
What every store mutation does before it reaches the pipe: refuse it on a standby,...
Datum get_extra(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for get_extra().
char * provsql_internal_get_extra(const pg_uuid_t *token)
Read a gate's extra text, through the worker; palloc'd.
static provsql_set_prob_result provsql_send_set_prob(const pg_uuid_t *token, double prob, double *existing, bool tracked)
Send a probability write and read back what the store made of it.
static char worker_buffer[WORKER_READ_BUFFER]
Datum get_prob(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for get_prob().
unsigned bufferpos
Current write position within buffer.
void RegisterProvSQLMMapWorker(void)
Register the ProvSQL mmap background worker with PostgreSQL.
bool provsql_worker_from_spool
Whether the message being applied comes from the replay spool: it is not answered,...
Background worker and IPC primitives for mmap-backed circuit storage.
#define ADDWRITEDB()
Append the per-message database header to the write buffer.
#define READB_BYTES(ptr, n)
Read exactly n bytes of a reply from the main-to-background pipe.
provsql_set_prob_result
Outcome of a probability write, mirroring MMappedCircuit::SetProbResult across the IPC boundary.
#define READB(var, type)
Read one value of type from the main-to-background pipe.
#define STARTWRITEM()
Reset the shared write buffer for a new batched write.
#define ADDWRITEM(pvar, type)
Append one value of type to the shared write buffer.
#define SENDWRITEM()
Flush the shared write buffer to the background-to-main pipe atomically.
#define PROVSQL_REPLAY_SPOOL
Where WAL replay writes the store messages while the worker is not running, relative to the data dire...
void provsql_wal_log_store_message(const char *data, size_t len)
Write one store message to the WAL, if WAL logging is on.
bool provsql_store_write_allowed(void)
Whether this process may write to the circuit store.
WAL-logging the circuit store: the entry points other files use.
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_lock_exclusive_wait(void)
Acquire the ProvSQL LWLock in exclusive mode, waiting whatever happens: for the background worker,...
void provsql_shmem_lock_shared(void)
Acquire the ProvSQL LWLock in shared mode.
Shared-memory segment and inter-process pipe management.
constants_t get_constants(bool failure_if_not_possible)
Retrieve the cached OID constants for the current database.
Core types, constants, and utilities shared across ProvSQL.
Structure to store the value of various constants.
Oid GATE_TYPE_TO_OID[nb_gate_types]
Array of the OID of each provenance_gate ENUM value.
Oid OID_TYPE_UUID
OID of the uuid TYPE.
What provsql.circuit_cleanup() reports.
uint64 wires_before
Child wires before.
uint64 wires_after
Child wires kept.
uint64 extra_before
Annotation bytes before.
uint64 gates_after
Gate records kept.
uint64 extra_after
Annotation bytes kept.
uint64 gates_before
Gate records before.