ProvSQL SQL API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Circuit gate manipulation

Low-level functions for creating and querying provenance circuit gates. More...

Functions

VOID provsql.create_gate (UUID token, PROVENANCE_GATE type, UUID[] children=NULL)
 Create a new gate in the provenance circuit.
PROVENANCE_GATE provsql.get_gate_type (UUID token)
 Return the gate type of a provenance token.
UUID[] provsql.get_children (UUID token)
 Return the children of a provenance gate.
VOID provsql.set_prob (UUID token, DOUBLE PRECISION p)
 Set the probability of an input gate.
DOUBLE PRECISION provsql.get_prob (UUID token)
 Get the probability associated with an input gate.
VOID provsql.set_infos (UUID token, INT info1, INT info2=NULL)
 Set additional INTEGER values on provenance circuit gate.
RECORD provsql.get_infos (UUID token, OUT INT info1, OUT INT info2)
 Get the INTEGER info values associated with a circuit gate.
UUID provsql.provenance_assume (UUID token, TEXT assumption)
 Wrap token in a fresh gate_assumed carrying assumption as its label, and return the wrapper's UUID.
UUID provsql.assume_boolean (UUID token)
 Wrap token in a Boolean-assumption marker (compatibility name; see provenance_assume).
UUID provsql.annotate (UUID token, TEXT extra)
 Wrap token in a fresh transparent gate_annotation carrying extra, and return the wrapper's UUID.
UUID provsql.strip_annotations (UUID token)
 Peel every transparent gate_annotation wrapper off token, returning the first non-annotation gate underneath.
UUID provsql.cond (UUID target, UUID evidence)
 Condition a provenance token (a Boolean event) on another.
BOOLEAN provsql.UUID_op_UUID (UUID left, UUID right)
 Binary | : value-level conditioning, "target | evidence".
UUID provsql.cond_predicate (UUID target, BOOLEAN predicate)
 Placeholder for "X | (predicate)" on a UUID event.
BOOLEAN provsql.UUID_op_boolean (UUID left, BOOLEAN right)
UUID provsql.predicate_cond_predicate (BOOLEAN target, BOOLEAN evidence)
 Placeholder for "(predicate) | (predicate)" on two events.
BOOLEAN provsql.boolean_op_boolean (BOOLEAN left, BOOLEAN right)
UUID provsql.regular_indicator (BOOLEAN cond)
 Deterministic indicator gate for an ordinary (regular) comparison.
UUID provsql.given (UUID evidence)
 Whole-tuple output conditioning directive: "given(evidence)".
UUID provsql.given (BOOLEAN predicate)
 Prefix unary | : alias for given, "| evidence".
UUID provsql.provenance_not (UUID event)
 Event negation: "! event" / "provenance_not(event)".
TEXT provsql.inversion_free_key (TEXT root, TEXT sec, INT factor)
 Prefix unary !
VOID provsql.set_extra (UUID token, TEXT data)
 Set extra TEXT information on provenance circuit gate.
TEXT provsql.get_extra (UUID token)
 Get the TEXT-encoded extra data associated with a circuit gate.
BIGINT provsql.get_nb_gates ()
 Return the total number of materialized gates in the provenance circuit.

Detailed Description

Low-level functions for creating and querying provenance circuit gates.

Function Documentation

◆ annotate()

UUID provsql.annotate ( UUID token,
TEXT extra )

Wrap token in a fresh transparent gate_annotation carrying extra, and return the wrapper's UUID.

Unlike every other gate, the annotation wrapper's UUID folds in extra (not just the child): uuid_generate_v5 over concat('annotation', token, extra). This is deliberate – two annotations over the same child with different extra must be distinct gates (e.g. the same input tuple carrying different per-occurrence order keys, or two queries attaching different certificates to a shared root). The wrapper is transparent (identity) for EVERY evaluator; extra is inert metadata read only by the code that placed it. No-op (returns NULL) on a NULL input.

Source code
provsql.sql line 243

◆ assume_boolean()

UUID provsql.assume_boolean ( UUID token)

Wrap token in a Boolean-assumption marker (compatibility name; see provenance_assume).

Source code
provsql.sql line 225

◆ boolean_op_boolean()

BOOLEAN provsql.boolean_op_boolean ( BOOLEAN left,
BOOLEAN right )

◆ cond()

UUID provsql.cond ( UUID target,
UUID evidence )

Condition a provenance token (a Boolean event) on another.

Builds the terminal gate_conditioned that the measure evaluators read as "P(target ∧ evidence) / P(evidence)". This is the backing function of the binary | operator ("target | evidence", value-level conditioning of the UUID carrier).

The gate stores three children [target, evidence, joint] with joint = times(target, evidence); evaluation is then the plain ratio P(joint)/P(evidence), and content-addressing makes a base tuple shared by target and evidence the same input gate in both circuits, so the conditional is exact and correlation-aware.

Conventions:

  • Conditioning on a certain or absent event is a no-op: evidence NULL or gate_one() returns target unchanged ("P(X|true)=P(X)").
  • A target with no provenance defaults to the certain event 1, so "1 | c" is the well-defined certain-row posterior.
  • Nested conditioning folds (sequential Bayesian update): "(X | A) | B = X | (A ∧ B)" – the gate never nests, it stays one level deep with the evidence accumulated by times.

The result is TERMINAL: a conditioned token may not become a child of a plus / times / monus / agg gate (those constructors refuse it); the only operation it admits is more conditioning.

Source code
provsql.sql line 309

◆ cond_predicate()

UUID provsql.cond_predicate ( UUID target,
BOOLEAN predicate )

Placeholder for "X | (predicate)" on a UUID event.

Lets the conditioning event be written as a natural Boolean combination of random_variable / aggregate comparisons (e.g. "event | (sensor > 3)") instead of a hand-built gate. Never executes: the ProvSQL planner hook converts the Boolean operand into a condition gate and emits cond.

Source code
provsql.sql line 364

◆ create_gate()

VOID provsql.create_gate ( UUID token,
PROVENANCE_GATE type,
UUID[] children = NULL )

Create a new gate in the provenance circuit.

Parameters
tokenUUID identifying the new gate
typegate type (see PROVENANCE_GATE)
childrenoptional array of child gate UUIDs
Source code
provsql.sql line 112

◆ get_children()

UUID[] provsql.get_children ( UUID token)

Return the children of a provenance gate.

Source code
provsql.sql line 129

◆ get_extra()

TEXT provsql.get_extra ( UUID token)

Get the TEXT-encoded extra data associated with a circuit gate.

Source code
provsql.sql line 575

◆ get_gate_type()

PROVENANCE_GATE provsql.get_gate_type ( UUID token)

Return the gate type of a provenance token.

Returns 'input' for any token not yet materialized in the circuit, since input is the default semantics of an unmaterialized provenance token.

Source code
provsql.sql line 124

◆ get_infos()

RECORD provsql.get_infos ( UUID token,
OUT INT info1,
OUT INT info2 )

Get the INTEGER info values associated with a circuit gate.

Source code
provsql.sql line 171

◆ get_nb_gates()

BIGINT provsql.get_nb_gates ( )

Return the total number of materialized gates in the provenance circuit.

Input gates for provenance-tracked table rows are created lazily on first reference; rows that have never appeared in a query result are not counted.

Source code
provsql.sql line 586

◆ get_prob()

DOUBLE PRECISION provsql.get_prob ( UUID token)

Get the probability associated with an input gate.

Source code
provsql.sql line 144

◆ given() [1/2]

UUID provsql.given ( BOOLEAN predicate)

Prefix unary | : alias for given, "| evidence".

Disambiguated from the binary | by the absence of a left operand ("a, | c" parses "| c" as the prefix form). PostgreSQL keeps prefix operators on every supported version (postfix operators were removed in PG14), so "| c" is safe across the CI matrix.

Conditioning-evidence from a predicate: "given(predicate)" (also the prefix "| (predicate)").

Two uses of the same marker:

  • whole-tuple output conditioning written as a select-list term, the natural-predicate spelling of given ("SELECT a, given(sensor > 3)");
  • per-row evidence for a latent-variable posterior, folded with and_agg"and_agg(given(normal(mu,1) = x))" turns each row's observation into likelihood-weighting evidence.

Never executes: the planner converts the Boolean operand into a condition gate and emits given(gate); a point-equality "Y = d" on a bare random-variable leaf then becomes an observation (see given(UUID) / evidence_as_observation).

Source code
provsql.sql line 447

◆ given() [2/2]

UUID provsql.given ( UUID evidence)

Whole-tuple output conditioning directive: "given(evidence)".

Written as a term in the select list, given(c) conditions the OUTPUT provenance of the current query's rows on c:

SELECT a, b, given((SELECT provenance() FROM tests
WHERE patient_id = s.id AND result = 'positive'))
FROM source s;
-- visible columns: a, b (the given(...) term is stripped)
-- per-row output provenance: provenance() | <that row's evidence>

The query rewriter recognises the marker, STRIPS it from the visible projection, and wraps each output row's provenance expression in cond(row_provenance, c) – deriving a new conditioned relation, never mutating any stored provenance. c is evaluated per output row and may correlate with the row's columns, so each tuple is conditioned on its own evidence. When the rewriter is inactive the call is a harmless identity (it returns evidence as an ordinary column).

When executed rather than stripped – i.e. nested inside an expression, the idiom "and_agg(given(Y = d))" that folds one observation per row into a latent-variable evidence circuit -- a point-equality "Y = d" on a bare random-variable leaf is turned into likelihood-weighting evidence (evidence_as_observation); any other evidence passes through unchanged.

Source code
provsql.sql line 447

◆ inversion_free_key()

TEXT provsql.inversion_free_key ( TEXT root,
TEXT sec,
INT factor )

Prefix unary !

: alias for provenance_not, "! event".

Prefix operators are kept on every supported PostgreSQL version (postfix operators were removed in PG14), and core PG defines no prefix ! on UUID, so "! event" is safe across the CI matrix.

Build a per-input order-key string for the inversion-free path.

Emitted by the planner per certified atom: K-prefixed, length-prefixed "K<factor> <octet_length(root)>:<root><octet_length(sec)>:<sec>", parsed back at evaluation by safe_cert_key_parse. root / sec are the tuple's root- and secondary-class column values (TEXT-cast by the caller); the byte-length prefixes keep the values unambiguous for any column type, including TEXT containing spaces, colons or digits. factor is the atom's factor id (or -1 for the shared self-join guard). IMMUTABLE so the planner can fold it and the marker dedups by content-addressing.

Source code
provsql.sql line 549

◆ predicate_cond_predicate()

UUID provsql.predicate_cond_predicate ( BOOLEAN target,
BOOLEAN evidence )

Placeholder for "(predicate) | (predicate)" on two events.

Conditions one comparison event on another when both operands are written as comparisons rather than pre-built tokens (e.g. "probability((x >= 2000) | (x >= 1000))"): an random_variable / AGG_TOKEN comparison is statically BOOLEAN-typed, so neither the "UUID | UUID" (cond) nor the "UUID | BOOLEAN" (cond_predicate) operator resolves. Never executes: the ProvSQL planner hook lowers each Boolean operand to its event gate and emits cond(target, evidence), so the result carries the correlation-aware Pr(A ∧ B) / Pr(B). Returns UUID, so "A | B" is a first-class event token in every position (a probability(UUID) argument, a projected column, a further "|").

Source code
provsql.sql line 390

◆ provenance_assume()

UUID provsql.provenance_assume ( UUID token,
TEXT assumption )

Wrap token in a fresh gate_assumed carrying assumption as its label, and return the wrapper's UUID.

Public primitive callable from any rewrite or driver that needs to flag a sub-circuit as sound only under an evaluation assumption:

  • 'BOOLEAN' – the sub-circuit only preserves the Boolean function of the lineage (e.g. the safe-query rewrite collapses derivation multiplicities); transparent for semirings admitting a homomorphism from Boolean functions.
  • 'absorptive' – the sub-circuit was truncated at the absorptive value fixpoint (cyclic recursive query); transparent for absorptive semirings (probability, BOOLEAN, min-plus over nonnegative costs...), fatal for the rest (counting, why-provenance).

Incompatible evaluators raise a CircuitException. Always kept as an explicit node in PROV-XML export.

The wrapper UUID is content-derived via uuid_generate_v5 on the assumption and the child, so identical children always wrap to the same outer UUID per assumption. No-op (returns NULL) on a NULL input.

Source code
provsql.sql line 200

◆ provenance_not()

UUID provsql.provenance_not ( UUID event)

Event negation: "! event" / "provenance_not(event)".

The complement of a Boolean provenance event: "!x" holds in exactly the worlds where x does not. It is sugar for "monus(one, x)" -- an ordinary m-semiring expression (Boolean NOT, probability "1 - P(x)"), NOT a measure-only marker – so it composes like any monus, and a conditioned / terminal token is refused as its child (so "!(x | c)" errors, as conditioning cannot be buried under further algebra).

The motivating use is conditioning on the NON-occurrence of an arbitrary violation query W (a denial constraint), where W itself is built with ordinary idioms and needs no hand-rolled gates:

-- W = "some pair of overlapping same-room bookings is present"
WITH w AS (SELECT provenance() AS tok
FROM bookings a JOIN bookings b
ON a.id < b.id AND a.room = b.room
AND a.lo < b.hi AND b.lo < a.hi
GROUP BY ())
SELECT probability_evaluate((SELECT provenance() FROM bookings WHERE id=1)
| !w.tok) -- P(booking 1 | no overlap)
FROM w;

Named provenance_not, after the "provenance_times / _plus / _monus" family; the prefix ! operator is the ergonomic form (SQL's reserved NOT keyword cannot serve as a function name).

Source code
provsql.sql line 522

◆ regular_indicator()

UUID provsql.regular_indicator ( BOOLEAN cond)

Deterministic indicator gate for an ordinary (regular) comparison.

The predicate-provenance of an ordinary comparison (both sides of regular type, e.g. "region = 'north'") is the deterministic indicator "χ(cond)": gate_one() when the comparison holds on the row, gate_zero() otherwise (Definition in the HAVING-provenance semantics). The planner emits this for a regular comparison appearing inside a MIXED conditioning predicate (one that also has a random_variable / aggregate comparison); cond is evaluated per row, so the indicator is the row's own truth value, combined by / with the probabilistic gates.

Source code
provsql.sql line 414

◆ set_extra()

VOID provsql.set_extra ( UUID token,
TEXT data )

Set extra TEXT information on provenance circuit gate.

This function sets TEXT-encoded data associated to a circuit gate, used in different ways by different gate types:

  • for project, it is a TEXT-encoded ARRAY of two-element ARRAYs that indicate mappings between input attribute (first element) and output attribute (second element)
  • for value and agg, it is the TEXT-encoded (base for value, computed for agg) scalar value
Parameters
tokenUUID of the circuit gate
dataTEXT-encoded information
Source code
provsql.sql line 570

◆ set_infos()

VOID provsql.set_infos ( UUID token,
INT info1,
INT info2 = NULL )

Set additional INTEGER values on provenance circuit gate.

This function sets two INTEGER values associated to a circuit gate, used in different ways by different gate types:

  • for mulinput, info1 indicates the value of this multivalued variable
  • for eq, info1 and info2 indicate the attribute index of the equijoin in, respectively, the first and second columns
  • for agg, info1 is the oid of the aggregate function and info2 the oid of the aggregate result type
  • for cmp, info1 is the oid of the comparison operator
Parameters
tokenUUID of the circuit gate
info1first INTEGER value
info2second INTEGER value
Source code
provsql.sql line 165

◆ set_prob()

VOID provsql.set_prob ( UUID token,
DOUBLE PRECISION p )

Set the probability of an input gate.

Parameters
tokenUUID of the input gate
pprobability value in [0,1]
Source code
provsql.sql line 139

◆ strip_annotations()

UUID provsql.strip_annotations ( UUID token)

Peel every transparent gate_annotation wrapper off token, returning the first non-annotation gate underneath.

The dual of annotate, for consumers keyed to gate identity rather than gate value: a provenance mapping matches input-gate UUIDs, and the reachability edge classifier matches token shapes, so both must see the wrapped gate, not the wrapper (e.g. the inversion-free certificate / order marker the planner attaches to a certified query's row roots). Identity on a token with no annotation wrapper; NULL on NULL.

Source code
provsql.sql line 271

◆ UUID_op_boolean()

BOOLEAN provsql.UUID_op_boolean ( UUID left,
BOOLEAN right )

◆ UUID_op_UUID()

BOOLEAN provsql.UUID_op_UUID ( UUID left,
UUID right )

Binary | : value-level conditioning, "target | evidence".

Carrier-parametric in its left operand; the UUID form builds the terminal gate_conditioned via cond. Does not collide with core PostgreSQL's INTEGER bitwise | (different argument types).

Source code
provsql.sql line 354