ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
canonical_form_sql.cpp
Go to the documentation of this file.
1/**
2 * @file canonical_form_sql.cpp
3 * @brief SQL functions @c provsql.circuit_canonical_form(),
4 * @c provsql.circuit_island_key(), @c provsql.prob_cache_stats()
5 * and @c provsql.prob_cache_reset() – inspect the canonical form
6 * of a circuit and the cache of compiled d-DNNFs.
7 *
8 * Diagnostics for the cache of @c ProbCache.h: the first two show the
9 * keys a circuit is looked up under, the last two read and empty the
10 * cache of the current session.
11 */
12extern "C" {
13#include "postgres.h"
14#include "fmgr.h"
15#if PG_VERSION_NUM >= 160000
16#include "varatt.h"
17#endif
18#include "catalog/pg_type.h"
19#include "utils/jsonb.h"
20#include "utils/fmgrprotos.h"
21#include "utils/uuid.h"
22#include "provsql_shmem.h"
23#include "provsql_utils.h"
24
25PG_FUNCTION_INFO_V1(circuit_canonical_form);
26PG_FUNCTION_INFO_V1(circuit_island_key);
27PG_FUNCTION_INFO_V1(prob_cache_stats);
28PG_FUNCTION_INFO_V1(prob_cache_reset);
29}
30
31#include "c_cpp_compatibility.h"
32#include "BooleanCircuit.h"
33#include "ProbCache.h"
35#include "CircuitFromMMap.h"
36#include "provsql_utils_cpp.h"
37
38#include <sstream>
39#include <string>
40
41using namespace std;
42
43namespace {
44
45BooleanCircuit booleanViewOf(pg_uuid_t token, gate_t &root)
46{
47 BooleanCircuit c = getBooleanCircuit(token, root);
49 return c;
50}
51
52}
53
54Datum circuit_canonical_form(PG_FUNCTION_ARGS)
55{
56 try {
57 if(PG_ARGISNULL(0))
58 PG_RETURN_NULL();
59 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
60
61 gate_t root;
62 BooleanCircuit c = booleanViewOf(token, root);
64
65 text *result = reinterpret_cast<text*>(
66 palloc(f.form.size() + VARHDRSZ));
67 SET_VARSIZE(result, f.form.size() + VARHDRSZ);
68 memcpy(VARDATA(result), f.form.data(), f.form.size());
69 PG_RETURN_TEXT_P(result);
70 } catch(const std::exception &e) {
71 provsql_error("circuit_canonical_form: %s", e.what());
72 } catch(...) {
73 provsql_error("circuit_canonical_form: Unknown exception");
74 }
75 PG_RETURN_NULL();
76}
77
78Datum circuit_island_key(PG_FUNCTION_ARGS)
79{
80 try {
81 if(PG_ARGISNULL(0))
82 PG_RETURN_NULL();
83 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
84
85 gate_t root;
86 BooleanCircuit c = booleanViewOf(token, root);
88
89 std::ostringstream out;
90 out << "residual: " << key.residual.form;
91 for(size_t i = 0; i < key.islands.size(); ++i)
92 out << "\nisland " << (i + 1) << ": " << key.islands[i].form;
93 const std::string s = out.str();
94
95 text *result = reinterpret_cast<text*>(palloc(s.size() + VARHDRSZ));
96 SET_VARSIZE(result, s.size() + VARHDRSZ);
97 memcpy(VARDATA(result), s.data(), s.size());
98 PG_RETURN_TEXT_P(result);
99 } catch(const std::exception &e) {
100 provsql_error("circuit_island_key: %s", e.what());
101 } catch(...) {
102 provsql_error("circuit_island_key: Unknown exception");
103 }
104 PG_RETURN_NULL();
105}
106
107Datum prob_cache_stats(PG_FUNCTION_ARGS)
108{
109 try {
112 std::ostringstream out;
113 out << '{'
114 << "\"hits\":" << s.hits
115 << ",\"misses\":" << s.misses
116 << ",\"inserts\":" << s.inserts
117 << ",\"evictions\":" << s.evictions
118 << ",\"rejections\":" << s.rejections
119 << ",\"entries\":" << s.entries
120 << ",\"bytes\":" << s.bytes
121 << ",\"budget\":" << s.budget
122 << '}';
123 Datum json_datum = DirectFunctionCall1(
124 jsonb_in, CStringGetDatum(pstrdup(out.str().c_str())));
125 PG_RETURN_DATUM(json_datum);
126 } catch(const std::exception &e) {
127 provsql_error("prob_cache_stats: %s", e.what());
128 } catch(...) {
129 provsql_error("prob_cache_stats: Unknown exception");
130 }
131 PG_RETURN_NULL();
132}
133
134Datum prob_cache_reset(PG_FUNCTION_ARGS)
135{
137 PG_RETURN_VOID();
138}
Boolean provenance circuit with support for knowledge compilation.
Canonical form of a Boolean circuit, and its split into islands.
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.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:53
Cache of compiled d-DNNFs, and folding of independent parts.
Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.
Datum circuit_island_key(PG_FUNCTION_ARGS)
Datum prob_cache_reset(PG_FUNCTION_ARGS)
Datum prob_cache_stats(PG_FUNCTION_ARGS)
Datum circuit_canonical_form(PG_FUNCTION_ARGS)
Boolean circuit for provenance formula evaluation.
void rewriteMultivaluedGates()
Rewrite all MULVAR/MULIN gate clusters into standard AND/OR/NOT circuits.
static ProbCache & instance()
The cache of this backend.
void reset()
Empties the cache and its counters.
ProbCacheStats stats() const
The counters, with the current number and size of entries.
IslandKey computeIslandKey(const BooleanCircuit &c, gate_t root)
Splits the circuit below root into its outermost islands and the residual.
CanonicalForm computeCanonicalForm(const BooleanCircuit &c, gate_t root, const GateSet *abstract_leaves)
Canonical form of the circuit below root.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.
C++ utility functions for UUID manipulation.
Canonical form of a circuit below a root.
std::string form
The form, e.g. (OR (AND V V) (AND V #4)).
A circuit split into its outermost islands and what remains.
CanonicalForm residual
Form of the circuit, each island read as an input.
std::vector< CanonicalForm > islands
Form of each island, in the same order.
Counters of the cache since it was last emptied.
Definition ProbCache.h:38
uint64_t evictions
Entries dropped to stay under the size limit.
Definition ProbCache.h:42
uint64_t inserts
Entries stored.
Definition ProbCache.h:41
uint64_t hits
Lookups served from an entry.
Definition ProbCache.h:39
size_t bytes
Estimated size of the entries held now.
Definition ProbCache.h:45
uint64_t misses
Lookups that found no usable entry.
Definition ProbCache.h:40
size_t budget
Size the entries may reach before some are dropped.
Definition ProbCache.h:46
size_t entries
Entries held now.
Definition ProbCache.h:44
uint64_t rejections
d-DNNFs too large to be stored
Definition ProbCache.h:43