17#include <boost/archive/binary_iarchive.hpp>
18#include <boost/iostreams/device/array.hpp>
19#include <boost/iostreams/stream.hpp>
53 return used >= max ? 1 : max - used;
62 (
"the circuit to evaluate is larger than provsql.max_memory (" +
64 "Raise provsql.max_memory (e.g., SET provsql.max_memory = '2GB'), or "
65 "set it to 0 for no limit.");
78#ifndef PROVSQL_INPROCESS_STORE
90 if(!
READB(size,
unsigned long))
91 provsql_error(
"Cannot read from pipe (message type %c)", message_char);
100 char *buf =
new char[size];
104 provsql_error(
"Cannot read from pipe (message type %c)", message_char);
137 provsql_error(
"Cannot write to pipe (message type %c)", message_char);
145 boost::iostreams::stream<boost::iostreams::array_source> stream(buf, size);
146 boost::archive::binary_iarchive ia(stream);
164 std::unordered_map<gate_t, gate_t> &gc_to_bc)
172 auto u=
static_cast<gate_t>(i);
191 const std::unordered_map<gate_t, gate_t> &gc_to_bc,
194 for(
const auto &[u, b]: gc_to_bc) {
209 std::unordered_map<gate_t, gate_t> gc_to_bc;
288#ifdef PROVSQL_INPROCESS_STORE
328 const std::vector<pg_uuid_t> &tokens)
330#ifdef PROVSQL_INPROCESS_STORE
339 char message_char =
'j';
340 unsigned nb_roots =
static_cast<unsigned>(tokens.size());
362 boost::iostreams::stream<boost::iostreams::array_source> stream(buf, size);
363 boost::archive::binary_iarchive ia(stream);
378 unsigned long cap,
unsigned long wide_cap)
381#ifdef PROVSQL_INPROCESS_STORE
382 r = provsql_inproc_formula_regions(requests, cap, wide_cap);
384 char message_char =
'F';
385 unsigned nb =
static_cast<unsigned>(requests.size());
392 for(
const auto &q : requests) {
406 if(!
READB(size,
unsigned long))
408 char *buf =
new char[size];
422 boost::iostreams::stream<boost::iostreams::array_source> stream(buf, size);
423 boost::archive::binary_iarchive ia(stream);
437 const std::vector<pg_uuid_t> &tokens,
438 std::vector<gate_t> &gates)
451 gates.resize(tokens.size());
452 for(std::size_t i = 0; i < tokens.size(); ++i)
464 std::vector<gate_t> gates;
466 getJointCircuit(std::vector<pg_uuid_t>{root_token, event_token}, gates);
467 root_gate = gates[0];
468 event_gate = gates[1];
Boolean-expression (lineage formula) semiring.
constexpr unsigned DNNF_CERT_INFO
d-DNNF certificate value for the (gate-type-specific) per-gate info field.
@ OR
Logical disjunction of child gates.
@ AND
Logical conjunction of child gates.
@ IN
Input (variable) gate representing a base tuple.
@ MULIN
Multivalued-input gate (one of several options).
FormulaRegions getFormulaRegions(const std::vector< FormulaRequest > &requests, unsigned long cap, unsigned long wide_cap)
The regions of requests, for sr_formula_rows (see FormulaRegions), with the load-time simplification ...
BooleanCircuit getBooleanCircuit(GenericCircuit &gc, pg_uuid_t token, gate_t &gate, std::unordered_map< gate_t, gate_t > &gc_to_bc)
Build a BooleanCircuit from an already-loaded GenericCircuit.
void applyProbabilisticSimplification(GenericCircuit &gc)
Simplify a loaded circuit for a probability readout.
static char * readCircuitReply(unsigned long &size, char message_char)
Read the worker's answer to a 'g' / 'j' request just sent, and release the store lock.
void propagateDNNFCertificate(const GenericCircuit &gc, const std::unordered_map< gate_t, gate_t > &gc_to_bc, BooleanCircuit &c)
Propagate the per-gate d-DNNF certificate from gc to c.
static unsigned long circuit_budget()
What the statement has left of provsql.max_memory, in bytes, for building a circuit; 0 for no limit.
static void circuit_load_cancelled()
Stop for a cancel (or a terminate) of the statement, received while its circuit was loaded: as the ev...
GenericCircuit getJointCircuit(const std::vector< pg_uuid_t > &tokens, std::vector< gate_t > &gates)
Multi-root variant of getJointCircuit.
static C getCircuitFromMMap(pg_uuid_t token, char message_char)
Read and deserialise a circuit rooted at token from the mmap worker.
static GenericCircuit getJointCircuitFromMMap(const std::vector< pg_uuid_t > &tokens)
IPC: ship a 'j' (joint) request to the mmap worker and deserialise the returned GenericCircuit.
GenericCircuit getGenericCircuit(pg_uuid_t token)
Build a GenericCircuit from the mmap store rooted at token.
static void applyLoadTimeSimplification(GenericCircuit &gc)
Apply the universal load-time simplification passes to gc.
static void circuit_over_budget()
Stop at provsql.max_memory: the circuit to load is larger than the statement has left of it.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Peephole simplifier for continuous gate_arith sub-circuits.
Persistent, mmap-backed storage for the full provenance circuit.
constexpr unsigned long PROVSQL_CIRCUIT_OVER_BUDGET
The size the worker answers a 'g' / 'j' request with, in place of a circuit, when building it went pa...
constexpr unsigned long PROVSQL_CIRCUIT_CANCELLED
The size the worker answers a 'g' / 'j' request with, in place of a circuit, when the requesting stat...
Support-based bound check for continuous-RV comparators.
Boolean circuit for provenance formula evaluation.
gate_t setGate(BooleanGate type) override
Allocate a new gate with type type and no UUID.
void setInfo(gate_t g, unsigned info)
Store an integer annotation on gate g.
Thrown by MMappedCircuit::createGenericCircuit when the circuit it builds takes the process past the ...
Thrown by MMappedCircuit::createGenericCircuit when the statement that asked for the circuit is cance...
Exception type thrown by circuit operations on invalid input.
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
gateType getGateType(gate_t g) const
Return the type of gate g.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
std::vector< gate_t >::size_type getNbGates() const
Return the total number of gates in the circuit.
const S::value_type & evaluate(gate_t g, std::unordered_map< gate_t, typename S::value_type > &provenance_mapping, S semiring) const
Evaluate the sub-circuit rooted at gate g over semiring semiring.
bool foldSemiringIdentities()
Drop semiring identity wires and collapse single-wire gate_times / gate_plus to their lone non-identi...
void foldAbsorptiveIdentities()
Absorptive-rules-only variant of foldBooleanIdentities(): the joint fixpoint of the absorptive fold r...
double getProb(gate_t g) const
Return the probability for gate g.
const std::set< gate_t > & getInputs() const
Return the set of input (leaf) gates.
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
void foldBooleanIdentities()
Apply the Boolean-only AND the absorptive simplification rules to gate_plus and gate_times,...
Provenance-as-Boolean-circuit semiring.
Provenance evaluation helper for HAVING-clause circuits.
void provsql_having(GenericCircuit &c, gate_t g, MapT &mapping, SemiringT S=SemiringT{})
Rewrite HAVING comparison gates in the circuit into their possible-world provenance (world enumeratio...
unsigned foldDegenerateMixtures(GenericCircuit &gc, bool read_probabilities)
Collapse degenerate Bernoulli gate_mixture gates whose selector is certainly true (pi = 1) or certain...
unsigned runConstantFold(GenericCircuit &gc)
Constant-fold pass over every gate_arith in gc.
unsigned runReflexiveCmpRewriter(GenericCircuit &gc)
Probability-side pre-pass: rewrite HAVING-style gate_cmp gates that are provably TRUE on the agg's va...
unsigned runRangeCheck(GenericCircuit &gc)
Run the support-based pruning pass over gc.
bool provsql_absorptive_provenance
Derived flag: the session's provenance class is 'absorptive' or 'boolean' – licenses constructions so...
bool provsql_simplify_on_load
Run universal cmp-resolution passes when getGenericCircuit returns; controlled by the provsql....
bool provsql_interrupted
Set by provsql_sigint_handler: this backend received a cancel.
bool provsql_boolean_provenance
Derived flag: the session's provenance class is 'boolean' – enables the Boolean-only machinery (safe-...
int provsql_max_memory
Memory, in MB, that the evaluations of a statement may add to the backend's resident set,...
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
void provsql_limit_exceeded(const char *tag, const char *message, const char *hint)
Stop the evaluation at a resource limit (provsql.max_memory, provsql.max_worlds): throws,...
size_t provsql_memory_used(void)
How much the backend's resident memory has grown since the first evaluation of the statement,...
void provsql_check_memory(void)
Throw if the evaluations of the statement have made the backend's resident memory grow by more than p...
Cancellation of the long C++ evaluation loops.
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...
bool provsql_wait_reply(void)
Wait, under the store lock, until the worker's reply to this backend's request can be read.
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.
#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.
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
bool provsql_shmem_lock_exclusive_unless_cancelled(void)
Acquire the ProvSQL LWLock in exclusive mode, unless the statement is cancelled (or the backend termi...
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.
string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
C++ utility functions for UUID manipulation.