ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
probability_store.c File Reference

Write-once probabilities and their rollback. More...

#include "postgres.h"
#include <math.h>
#include "access/xact.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
#include "probability_store.h"
#include "provsql_mmap.h"
#include "provsql_utils.h"
Include dependency graph for probability_store.c:

Go to the source code of this file.

Classes

struct  prob_write
 One recorded probability write, awaiting commit or rollback. More...

Functions

static void prob_writes_unwind (int level, bool undo)
 Drop the recorded writes at nesting level level or deeper, clearing each one's probability in the store when undo.
static void prob_writes_reparent (int level)
 Move the recorded writes at nesting level level up to its parent, so a later outer rollback still undoes them.
static void prob_writes_forget (void)
 Release the list without touching the store.
static void prob_xact_callback (XactEvent event, void *arg)
static void prob_subxact_callback (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
static void prob_writes_record (const pg_uuid_t *token)
 Record that this transaction wrote token's probability.
static void fresh_leaves_record (const pg_uuid_t *token)
 Remember that this transaction minted token as a replacement leaf for a tracked row.
Datum note_fresh_leaf (PG_FUNCTION_ARGS)
 Declare a just-minted leaf gate a replacement for a tracked row.
Datum is_fresh_leaf (PG_FUNCTION_ARGS)
 Whether token was minted as a replacement leaf by this transaction (see note_fresh_leaf).
void provsql_set_prob_tracked (const pg_uuid_t *token, double prob)
 Write token's probability and record the write, so that a rollback of the current (sub)transaction clears it.
Datum set_prob (PG_FUNCTION_ARGS)
 Write a gate's probability, once.
Datum probability_is_set (PG_FUNCTION_ARGS)
 Report whether a probability has been written on a gate.

Variables

static prob_write * prob_writes = NULL
static int prob_writes_len = 0
static int prob_writes_cap = 0
static bool prob_callbacks_registered = false
static pg_uuid_t * fresh_leaves = NULL
 Replacement leaves minted by this transaction; see the block below.
static int fresh_leaves_len = 0
static int fresh_leaves_cap = 0

Detailed Description

Write-once probabilities and their rollback.

The circuit store is append-only: a gate, once created, never changes, and a gate created by a transaction that later rolls back is an orphan rather than an inconsistency – no committed row can reference it, and the same expression recomputed lands on the same content-addressed UUID. A probability is the one thing about a gate that used to be rewritten in place, which is what made a rolled-back set_prob stick.

This file makes a probability a fact appended to the circuit like the gate itself:

  • set_prob writes it when the gate has none, reports success without writing when the gate already holds exactly that value (so setup scripts and notebook cells stay re-runnable, the same idempotence create_gate and add_provenance offer), and raises otherwise;
  • every write is recorded in a backend-local list keyed by subtransaction nesting level, and an aborted (sub)transaction clears the probabilities it wrote. No old value is kept, because there never was one: this is an undo list of tokens, not of values;
  • changing a probability means minting a fresh input gate with the new probability and rewriting the rows that carry the old token, which provsql.replace_input does; the base table's token column is the place of truth, exactly as it is for the token rewrites the data-modification triggers perform.

What is not closed here is isolation: another session can read a probability between the write and the rollback. Closing that would mean deferring the write to XACT_EVENT_PRE_COMMIT behind a backend-local overlay, at the price of making every reader consult the overlay.

Definition in file probability_store.c.

Function Documentation

◆ fresh_leaves_record()

void fresh_leaves_record ( const pg_uuid_t * token)
static

Remember that this transaction minted token as a replacement leaf for a tracked row.

Definition at line 222 of file probability_store.c.

Here is the caller graph for this function:

◆ is_fresh_leaf()

Datum is_fresh_leaf ( PG_FUNCTION_ARGS )

Whether token was minted as a replacement leaf by this transaction (see note_fresh_leaf).

Definition at line 254 of file probability_store.c.

◆ note_fresh_leaf()

Datum note_fresh_leaf ( PG_FUNCTION_ARGS )

Declare a just-minted leaf gate a replacement for a tracked row.

Called by provsql.replace_input / replace_block right after they create the gate, so that the UPDATE which stores it does not look to provenance_guard like a user pasting in an arbitrary token.

Definition at line 243 of file probability_store.c.

Here is the call graph for this function:

◆ prob_subxact_callback()

void prob_subxact_callback ( SubXactEvent event,
SubTransactionId mySubid,
SubTransactionId parentSubid,
void * arg )
static

Definition at line 152 of file probability_store.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ prob_writes_forget()

void prob_writes_forget ( void )
static

Release the list without touching the store.

Definition at line 102 of file probability_store.c.

Here is the caller graph for this function:

◆ prob_writes_record()

void prob_writes_record ( const pg_uuid_t * token)
static

Record that this transaction wrote token's probability.

Definition at line 184 of file probability_store.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ prob_writes_reparent()

void prob_writes_reparent ( int level)
static

Move the recorded writes at nesting level level up to its parent, so a later outer rollback still undoes them.

Definition at line 94 of file probability_store.c.

Here is the caller graph for this function:

◆ prob_writes_unwind()

void prob_writes_unwind ( int level,
bool undo )
static

Drop the recorded writes at nesting level level or deeper, clearing each one's probability in the store when undo.

Definition at line 77 of file probability_store.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ prob_xact_callback()

void prob_xact_callback ( XactEvent event,
void * arg )
static

Definition at line 107 of file probability_store.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ probability_is_set()

Datum probability_is_set ( PG_FUNCTION_ARGS )

Report whether a probability has been written on a gate.

get_prob() answers the value an evaluation would use, so it cannot tell a gate nobody gave a probability from one written as 1. This can, which is what a UI needs in order to offer "set" on the first and "replace" on the second.

Definition at line 385 of file probability_store.c.

Here is the call graph for this function:

◆ provsql_set_prob_tracked()

void provsql_set_prob_tracked ( const pg_uuid_t * token,
double prob )

Write token's probability and record the write, so that a rollback of the current (sub)transaction clears it.

Raises when the gate carries no probability, when prob is outside [0, 1], and when the gate already holds a different probability. Writing the value the gate already holds is a no-op.

Definition at line 268 of file probability_store.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_prob()

Datum set_prob ( PG_FUNCTION_ARGS )

Write a gate's probability, once.

Transparent gate_annotation wrappers (an inversion-free certificate / order marker attached by the planner to a certified query's row roots) are peeled first: a probability set on a wrapped token belongs to the input gate underneath, so the documented "set_prob(provenance(), p) FROM t" pattern keeps working when the query happens to be certified.

Definition at line 346 of file probability_store.c.

Here is the call graph for this function:

Variable Documentation

◆ fresh_leaves

pg_uuid_t* fresh_leaves = NULL
static

Replacement leaves minted by this transaction; see the block below.

Definition at line 71 of file probability_store.c.

◆ fresh_leaves_cap

int fresh_leaves_cap = 0
static

Definition at line 73 of file probability_store.c.

◆ fresh_leaves_len

int fresh_leaves_len = 0
static

Definition at line 72 of file probability_store.c.

◆ prob_callbacks_registered

bool prob_callbacks_registered = false
static

Definition at line 68 of file probability_store.c.

◆ prob_writes

prob_write* prob_writes = NULL
static

Definition at line 65 of file probability_store.c.

◆ prob_writes_cap

int prob_writes_cap = 0
static

Definition at line 67 of file probability_store.c.

◆ prob_writes_len

int prob_writes_len = 0
static

Definition at line 66 of file probability_store.c.