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

PostgreSQL planner hook for transparent provenance tracking. More...

#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pg_config.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_class.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/print.h"
#include "executor/executor.h"
#include "optimizer/planner.h"
#include "parser/parse_coerce.h"
#include "parser/parse_oper.h"
#include "utils/builtins.h"
#include "parser/parsetree.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/ruleutils.h"
#include "utils/syscache.h"
#include "catalog/namespace.h"
#include "catalog/pg_cast.h"
#include "commands/createas.h"
#include "executor/spi.h"
#include "tcop/utility.h"
#include <time.h>
#include "classify_query.h"
#include "provsql_mmap.h"
#include "provsql_shmem.h"
#include "provsql_utils.h"
#include "safe_query.h"
#include "compatibility.h"
Include dependency graph for provsql.c:

Go to the source code of this file.

Classes

struct  reduce_varattno_mutator_context
 Context for the reduce_varattno_mutator tree walker. More...
struct  aggregation_type_mutator_context
 Context for the aggregation_type_mutator tree walker. More...
struct  aggregation_mutator_context
 Context for the aggregation_mutator tree walker. More...
struct  provenance_mutator_context
 Context for the provenance_mutator tree walker. More...
struct  insert_agg_token_casts_context
 Context for the insert_agg_token_casts_mutator. More...
struct  ProvSQLCtasCapture
 State captured by the pre-execution pass for the post-execution one. More...

Enumerations

enum  semiring_operation { SR_PLUS , SR_MONUS , SR_TIMES }
 Semiring operation used to combine provenance tokens. More...
enum  qual_class {
  QUAL_DETERMINISTIC , QUAL_PURE_AGG , QUAL_PURE_RV , QUAL_MIXED_AGG_DET ,
  QUAL_MIXED_RV_DET , QUAL_MIXED_AGG_RV
}
 Categorisation of a top-level WHERE conjunct. More...

Functions

void _PG_init (void)
 Extension initialization – called once when the shared library is loaded.
void _PG_fini (void)
 Extension teardown — restores the planner and shmem hooks.
static Query * process_query (const constants_t *constants, Query *q, bool **removed, bool wrap_root)
 Rewrite a single SELECT query to carry provenance.
static Expr * wrap_in_assume_boolean (const constants_t *constants, Expr *expr)
 Wrap expr in a provsql.assume_boolean FuncExpr.
static Var * make_provenance_attribute (const constants_t *constants, Query *q, RangeTblEntry *r, Index relid, AttrNumber attid)
 Build a Var node that references the provenance column of a relation.
static Node * reduce_varattno_mutator (Node *node, void *ctx)
 Tree-mutator callback that adjusts Var attribute numbers.
static void reduce_varattno_by_offset (List *targetList, Index varno, int *offset)
 Adjust Var attribute numbers in targetList after columns are removed.
static bool is_target_agg_var (Node *node, aggregation_type_mutator_context *context)
 Check if a Var matches the target aggregate column.
static Node * aggregation_type_mutator (Node *node, void *ctx)
 Tree-mutator that retypes a specific Var to agg_token.
static void fix_type_of_aggregation_result (const constants_t *constants, Query *q, Index rteid, List *targetList)
 Retypes aggregation-result Vars in q from UUID to agg_token.
static void inline_ctes_in_rtable (List *rtable, List *cteList)
 Inline CTE references as subqueries within a query.
static void inline_ctes (Query *q)
 Inline all CTE references in q as subqueries.
static List * get_provenance_attributes (const constants_t *constants, Query *q)
 Collect all provenance Var nodes reachable from q's range table.
static Bitmapset * remove_provenance_attributes_select (const constants_t *constants, Query *q, bool **removed)
 Strip provenance UUID columns from q's SELECT list.
static Expr * add_eq_from_OpExpr_to_Expr (const constants_t *constants, OpExpr *fromOpExpr, Expr *toExpr, int **columns)
 Wrap toExpr in a provenance_eq gate if fromOpExpr is an equality between two tracked columns.
static Expr * add_eq_from_Quals_to_Expr (const constants_t *constants, Node *quals, Expr *result, int **columns)
 Walk a join-condition or WHERE quals node and add eq gates for every equality it contains.
static Expr * combine_prov_atts (const constants_t *constants, List *prov_atts, semiring_operation op)
 Build the per-row provenance token for an aggregate rewrite.
static Expr * make_rv_aggregate_expression (const constants_t *constants, Aggref *agg_ref, List *prov_atts, semiring_operation op)
 Inline rewrite of an RV-returning aggregate into the same aggregate over provenance-wrapped per-row arguments.
static Expr * make_aggregation_expression (const constants_t *constants, Aggref *agg_ref, List *prov_atts, semiring_operation op)
 Build the provenance expression for a single aggregate function.
static FuncExpr * having_Expr_to_provenance_cmp (Expr *expr, const constants_t *constants, bool negated)
 Dispatch a HAVING sub-expression to the appropriate converter.
static bool needs_having_lift (Node *havingQual, const constants_t *constants)
 Return true if havingQual contains anything the HAVING-lift path needs to handle (an agg_token Var or a provenance_aggregate wrapper).
static FuncExpr * having_OpExpr_to_provenance_cmp (OpExpr *opExpr, const constants_t *constants, bool negated)
 Convert a comparison OpExpr on aggregate results into a provenance_cmp gate expression.
static FuncExpr * having_BoolExpr_to_provenance (BoolExpr *be, const constants_t *constants, bool negated)
 Convert a Boolean combination of HAVING comparisons into a provenance_times / provenance_plus gate expression.
static int rv_cmp_index (const constants_t *constants, Oid funcoid)
 Test whether funcoid is one of the random_variable_* comparison procedures, and if so return its ComparisonOperator index.
static Expr * wrap_random_variable_uuid (Node *operand, const constants_t *constants)
 Wrap an expression returning random_variable in a binary-coercible cast to uuid.
static FuncExpr * rv_Expr_to_provenance (Expr *expr, const constants_t *constants, bool negated)
 Dispatch a WHERE sub-expression to the appropriate RV converter.
static FuncExpr * rv_OpExpr_to_provenance_cmp (OpExpr *opExpr, const constants_t *constants, bool negated)
 Convert a single RV-comparison OpExpr into a provenance_cmp() FuncExpr returning UUID.
static FuncExpr * rv_BoolExpr_to_provenance (BoolExpr *be, const constants_t *constants, bool negated)
 Convert a Boolean combination of RV comparisons into a provenance_times / provenance_plus expression.
static bool expr_contains_rv_cmp (Node *node, const constants_t *constants)
 Test whether an Expr (sub-)tree contains any RV comparison.
static bool check_expr_on_rv (Expr *expr, const constants_t *constants)
 Test whether expr is a Boolean combination of only random_variable comparisons (no other leaves allowed).
static Expr * make_provenance_expression (const constants_t *constants, Query *q, List *prov_atts, bool aggregation, bool group_by_rewrite, semiring_operation op, int **columns, int nbcols, bool wrap_assumed_boolean)
 Build the combined provenance expression to be added to the SELECT list.
static bool provenance_function_walker (Node *node, void *data)
 Tree walker that returns true if any provenance() call is found.
static Query * build_inner_for_distinct_key (Query *q, Expr *key_expr, List *groupby_tes)
 Build the inner GROUP-BY subquery for one AGG(DISTINCT key).
static Query * build_outer_for_distinct_key (TargetEntry *orig_agg_te, Query *inner, int n_gb, const constants_t *constants)
 Wrap inner in an outer query that applies the original aggregate.
static Query * rewrite_agg_distinct (Query *q, const constants_t *constants)
 Rewrite every AGG(DISTINCT key) in q using independent subqueries.
static Node * aggregation_mutator (Node *node, void *ctx)
 Tree-mutator that replaces Aggrefs with provenance-aware aggregates.
static Node * wrap_agg_token_with_cast (FuncExpr *prov_agg, const constants_t *constants)
 Wrap a provenance_aggregate FuncExpr with a cast to the original aggregate return type.
static void maybe_cast_agg_token_args (List *args, Oid parent_funcid, const constants_t *constants)
 Cast provenance_aggregate arguments of an operator or function when the formal parameter type requires it.
static Node * cast_agg_token_mutator (Node *node, void *ctx)
 Tree-mutator that casts provenance_aggregate results back to the original aggregate return type where needed.
static void replace_aggregations_by_provenance_aggregate (const constants_t *constants, Query *q, List *prov_atts, semiring_operation op)
 Replace every Aggref in q with a provenance-aware aggregate.
static void add_to_select (Query *q, Expr *provenance)
 Append the provenance expression to q's target list.
static bool expr_contains_aggref_walker (Node *node, void *context)
 expression_tree_walker predicate: returns true on the first Aggref it encounters.
static Node * provenance_mutator (Node *node, void *ctx)
 Tree-mutator that replaces provenance() calls with the actual provenance expression.
static void replace_provenance_function_by_expression (const constants_t *constants, Query *q, Expr *provsql)
 Replace every explicit provenance() call in q with provsql.
static void transform_distinct_into_group_by (Query *q)
 Convert a SELECT DISTINCT into an equivalent GROUP BY.
static void remove_provenance_attribute_groupref (Query *q, const Bitmapset *removed_sortgrouprefs)
 Remove sort/group references that belonged to removed provenance columns.
static void remove_provenance_attribute_setoperations (Query *q, bool *removed)
 Strip the provenance column's type info from a set-operation node.
static Query * rewrite_non_all_into_external_group_by (Query *q)
 Wrap a non-ALL set operation in an outer GROUP BY query.
static bool provenance_function_in_group_by (const constants_t *constants, Query *q)
 Check whether a provenance() call appears in the GROUP BY list.
static bool has_rv_or_provenance_call (Node *node, void *data)
 Tree walker that detects any provenance-bearing relation or provenance() call.
static bool has_provenance_walker (Node *node, void *data)
static bool has_provenance (const constants_t *constants, Query *q)
 Return true if q involves any provenance-bearing relation or contains an explicit provenance() call.
static bool aggtoken_walker (Node *node, void *data)
 Tree walker that detects any Var of type agg_token.
static bool has_aggtoken (Node *node, const constants_t *constants)
 Return true if node contains a Var of type agg_token.
static bool having_lift_walker (Node *node, void *data)
 Walker for needs_having_lift: detect any operand shape that the HAVING-lift rewriter (having_OpExpr_to_provenance_cmp) needs to handle specially.
static bool transform_except_into_join (const constants_t *constants, Query *q)
 Rewrite an EXCEPT query into a LEFT JOIN with monus provenance.
static void process_set_operation_union (const constants_t *constants, SetOperationStmt *stmt, Query *q)
 Recursively annotate a UNION tree with the provenance UUID type.
static void add_select_non_zero (const constants_t *constants, Query *q, Expr *provsql)
 Add a WHERE condition filtering out zero-provenance tuples.
static Node * add_to_havingQual (Node *havingQual, Expr *expr)
 Append expr to havingQual with an AND, creating one if needed.
static bool check_selection_on_aggregate (OpExpr *op, const constants_t *constants)
 Check whether op is a supported comparison on an aggregate result.
static bool check_boolexpr_on_aggregate (BoolExpr *be, const constants_t *constants)
 Check whether every leaf of a Boolean expression is a supported comparison on an aggregate result.
static bool check_expr_on_aggregate (Expr *expr, const constants_t *constants)
 Top-level dispatcher for supported WHERE-on-aggregate patterns.
static void build_column_map (Query *q, int **columns, int *nbcols)
 Build the per-RTE column-numbering map used by where-provenance.
static qual_class classify_qual (Expr *expr, const constants_t *constants)
 Classify expr along the qual_class axis.
static void error_for_mixed_qual (qual_class c)
 Raise the user-facing error appropriate to a mixed c.
static List * migrate_probabilistic_quals (const constants_t *constants, Query *q)
 Unified WHERE classifier – routes each top-level conjunct to the right evaluation site in a single pass.
static Oid get_agg_token_orig_type (Var *v, insert_agg_token_casts_context *ctx)
 Look up the original aggregate return type for an agg_token Var.
static void cast_agg_token_in_list (ListCell *lc, insert_agg_token_casts_context *ctx)
 Wrap an agg_token Var in a cast to its original type, in place.
static void cast_agg_token_args (List *args, insert_agg_token_casts_context *ctx)
 Wrap any agg_token Vars in an argument list.
static Node * insert_agg_token_casts_mutator (Node *node, void *data)
 Insert agg_token casts for Vars used in expressions.
static void insert_agg_token_casts (const constants_t *constants, Query *q)
 Walk query and insert agg_token casts where needed.
static void process_insert_select (const constants_t *constants, Query *q)
 Propagate provenance through INSERT ... SELECT.
static PlannedStmt * provsql_planner (Query *q, int cursorOptions, ParamListInfo boundParams)
static void provsql_executor_start (QueryDesc *queryDesc, int eflags)
static void provsql_executor_end (QueryDesc *queryDesc)
static void provsql_ProcessUtility_capture (Node *parsetree, ProvSQLCtasCapture *cap)
 Decide whether parsetree is a CTAS that should trigger the ancestry hook, and if so populate cap with the inner classification, the (single) source's block-key columns, and the transitive ancestor union.
static const char * provsql_ctas_kind_label (provsql_table_kind k)
 Map provsql_table_kind to its textual label (set_table_info accepts text).
Datum set_table_info (PG_FUNCTION_ARGS)
 Forward declaration of the C SQL entry points.
Datum set_ancestors (PG_FUNCTION_ARGS)
 PostgreSQL-callable wrapper for setTableAncestry() over the IPC pipe.
static void provsql_ProcessUtility_apply (Node *parsetree, ProvSQLCtasCapture *cap)
 Apply cap to the freshly-created relation stmt->into->rel.
static void provsql_ProcessUtility (PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, char *completionTag)

Variables

 PG_MODULE_MAGIC
 Required PostgreSQL extension magic block.
bool provsql_interrupted = false
 Global variable that becomes true if this particular backend received an interrupt signal.
static bool provsql_active = true
 true while ProvSQL query rewriting is enabled
bool provsql_where_provenance = false
 Global variable that indicates if where-provenance support has been activated through the provsql.where_provenance run-time configuration parameter.
static bool provsql_update_provenance = false
 true when provenance tracking for DML is enabled
int provsql_verbose = 100
 Verbosity level; controlled by the provsql.verbose_level GUC.
bool provsql_aggtoken_text_as_uuid = false
 When true, agg_token::text emits the underlying provenance UUID instead of "value (*)".
char * provsql_tool_search_path = NULL
 Colon-separated directory list prepended to PATH when invoking external tools (d4, c2d, minic2d, dsharp, weightmc, graph-easy); controlled by the provsql.tool_search_path GUC.
int provsql_monte_carlo_seed = -1
 Seed for the Monte Carlo sampler; -1 means non-deterministic (std::random_device); controlled by the provsql.monte_carlo_seed GUC.
int provsql_rv_mc_samples = 10000
 Default sample count for analytical-evaluator MC fallbacks; 0 disables fallback (callers raise instead); controlled by the provsql.rv_mc_samples GUC.
bool provsql_simplify_on_load = true
 Run universal cmp-resolution passes when getGenericCircuit returns; controlled by the provsql.simplify_on_load GUC.
bool provsql_hybrid_evaluation = true
 Run the hybrid-evaluator simplifier inside probability_evaluate; controlled by the provsql.hybrid_evaluation GUC.
bool provsql_cmp_probability_evaluation = true
 Run closed-form / analytic probability evaluators for gate_cmps inside probability_evaluate (currently the Poisson-binomial pre-pass for HAVING-COUNT; future MIN / MAX / SUM evaluators will gate on the same GUC); controlled by the provsql.cmp_probability_evaluation GUC.
bool provsql_boolean_provenance = false
 Opt-in safe-query optimisation: when true, rewrites hierarchical conjunctive queries to a read-once form whose probability is computable in linear time. The resulting circuit is tagged so that semiring evaluations admitting no homomorphism from Boolean functions refuse to run on it. Controlled by the provsql.boolean_provenance GUC.
static planner_hook_type prev_planner = NULL
 Previous planner hook (chained).
static int provsql_executor_depth = 0
 PostgreSQL planner hook — entry point for provenance rewriting.
static ExecutorStart_hook_type prev_ExecutorStart = NULL
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL
static ProcessUtility_hook_type prev_ProcessUtility = NULL

Detailed Description

PostgreSQL planner hook for transparent provenance tracking.

This file installs a planner_hook that intercepts every SELECT query and rewrites it to propagate a provenance circuit token (UUID) alongside normal result tuples. The rewriting proceeds in three conceptual phases:

  1. Discovery – scan the range table for relations/subqueries that already carry a provsql UUID column (get_provenance_attributes).
  2. Expression building – combine the discovered tokens according to the semiring operation that corresponds to the SQL operator in use (⊗ for joins, ⊕ for duplicate elimination, ⊖ for EXCEPT) and wrap aggregations (make_provenance_expression, make_aggregation_expression).
  3. Splice – append the resulting provenance expression to the target list and replace any explicit provenance() call in the query with the computed expression (add_to_select, replace_provenance_function_by_expression).

Definition in file provsql.c.

Enumeration Type Documentation

◆ qual_class

enum qual_class

Categorisation of a top-level WHERE conjunct.

Drives the unified WHERE classifier that replaced the original pair migrate_aggtoken_quals_to_having + extract_rv_cmps_from_quals. Both probabilistic flavours (agg_token's "moved to HAVING" world and random_variable's "lifted to provenance" world) are special cases of "this conjunct involves a probabilistic value the executor cannot evaluate as a Boolean directly, so the planner has to route it to a different evaluation site". The classifier reports which site, or (for unsupported mixes) errors.

Enumerator
QUAL_DETERMINISTIC 

no probabilistic value; stays in WHERE

QUAL_PURE_AGG 

pure agg_token expression; route to HAVING

QUAL_PURE_RV 

pure random_variable expression; lift to provenance

QUAL_MIXED_AGG_DET 

agg_token mixed with non-agg leaves; error

QUAL_MIXED_RV_DET 

random_variable mixed with non-RV leaves; error

QUAL_MIXED_AGG_RV 

agg_token and random_variable in the same expr; error

Definition at line 3653 of file provsql.c.

◆ semiring_operation

Semiring operation used to combine provenance tokens.

SR_TIMES corresponds to the multiplicative operation (joins, Cartesian products), SR_PLUS to the additive operation (duplicate elimination), and SR_MONUS to the monus / set-difference operation (EXCEPT).

See also
https://provsql.org/lean-docs/Provenance/QueryRewriting.html Lean 4 formalization of rewriting rules (R1)–(R5) and correctness theorem Query.rewriting_valid.
Enumerator
SR_PLUS 

Semiring addition (UNION, SELECT DISTINCT).

SR_MONUS 

Semiring monus / set difference (EXCEPT).

SR_TIMES 

Semiring multiplication (JOIN, Cartesian product).

Definition at line 646 of file provsql.c.

Function Documentation

◆ _PG_fini()

void _PG_fini ( void )
extern

Extension teardown — restores the planner and shmem hooks.

Definition at line 5386 of file provsql.c.

◆ _PG_init()

void _PG_init ( void )
extern

Extension initialization – called once when the shared library is loaded.

Registers the GUC variables (provsql.active, where_provenance, update_provenance, verbose_level, aggtoken_text_as_uuid, tool_search_path), installs the planner hook and shared-memory hooks, and launches the background MMap worker.

Must be loaded via shared_preload_libraries; raises an error otherwise.

Definition at line 5115 of file provsql.c.

Here is the call graph for this function:

◆ add_eq_from_OpExpr_to_Expr()

Expr * add_eq_from_OpExpr_to_Expr ( const constants_t * constants,
OpExpr * fromOpExpr,
Expr * toExpr,
int ** columns )
static

Wrap toExpr in a provenance_eq gate if fromOpExpr is an equality between two tracked columns.

Used for where-provenance: each equijoin condition (and some WHERE equalities) introduces an eq gate that records which attribute positions were compared. Because this function is also called for WHERE predicates, it applies extra guards and silently returns toExpr unchanged when the expression does not match the expected shape (both sides must be Var nodes, possibly wrapped in a RelabelType).

Parameters
constantsExtension OID cache.
fromOpExprThe equality OpExpr to inspect.
toExprExisting provenance expression to wrap.
columnsPer-RTE column-numbering array. EQ gate positions carry the same sequential-number caveat as PROJECT gate positions (see build_column_map()); they are only correct when each operand's RTE is either a join RTE or a subquery, not a bare provenance-tracked base table.
Returns
toExpr wrapped in provenance_eq(toExpr, col1, col2), or toExpr unchanged if the shape is unsupported.

Definition at line 679 of file provsql.c.

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

◆ add_eq_from_Quals_to_Expr()

Expr * add_eq_from_Quals_to_Expr ( const constants_t * constants,
Node * quals,
Expr * result,
int ** columns )
static

Walk a join-condition or WHERE quals node and add eq gates for every equality it contains.

Dispatches to add_eq_from_OpExpr_to_Expr for simple OpExpr nodes and iterates over the arguments of an AND BoolExpr. OR/NOT inside a join ON clause are rejected with an error.

Parameters
constantsExtension OID cache.
qualsRoot of the quals tree (OpExpr or BoolExpr), or NULL (in which case result is returned unchanged).
resultProvenance expression to wrap.
columnsPer-RTE column-numbering array.
Returns
Updated provenance expression with zero or more eq gates added.

Definition at line 755 of file provsql.c.

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

◆ add_select_non_zero()

void add_select_non_zero ( const constants_t * constants,
Query * q,
Expr * provsql )
static

Add a WHERE condition filtering out zero-provenance tuples.

For EXCEPT queries, tuples whose provenance evaluates to zero (i.e., the right-hand side fully subsumes the left-hand side) must be excluded from the result. This function appends provsql <> gate_zero() to q->jointree->quals, ANDing with any existing WHERE condition.

Parameters
constantsExtension OID cache.
qQuery to modify in place.
provsqlProvenance expression that was added to the SELECT list.

Definition at line 3415 of file provsql.c.

Here is the caller graph for this function:

◆ add_to_havingQual()

Node * add_to_havingQual ( Node * havingQual,
Expr * expr )
static

Append expr to havingQual with an AND, creating one if needed.

If havingQual is NULL, returns expr directly. If it is already an AND BoolExpr, appends to its argument list. Otherwise wraps both in a new AND node.

Parameters
havingQualExisting HAVING qualifier, or NULL.
exprExpression to conjoin.
Returns
The updated HAVING qualifier.

Definition at line 3452 of file provsql.c.

Here is the caller graph for this function:

◆ add_to_select()

void add_to_select ( Query * q,
Expr * provenance )
static

Append the provenance expression to q's target list.

Inserts a new TargetEntry named provsql immediately before any resjunk entries (which must remain last) and adjusts the resno of subsequent entries accordingly.

Parameters
qQuery to modify in place.
provenanceExpression to add (becomes the provsql output column).

Definition at line 2515 of file provsql.c.

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

◆ aggregation_mutator()

Node * aggregation_mutator ( Node * node,
void * ctx )
static

Tree-mutator that replaces Aggrefs with provenance-aware aggregates.

Parameters
nodeCurrent expression tree node.
ctxPointer to an aggregation_mutator_context (prov_atts, op, and constants).
Returns
Possibly modified node.

Definition at line 2331 of file provsql.c.

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

◆ aggregation_type_mutator()

Node * aggregation_type_mutator ( Node * node,
void * ctx )
static

Tree-mutator that retypes a specific Var to agg_token.

When the target Var is inside a cast FuncExpr, replaces the cast function with the equivalent agg_token→target cast from pg_cast. When the Var appears bare (e.g. in a TargetEntry for display), it is retyped to agg_token directly. In all other contexts (arithmetic, window functions, etc.), wraps the Var in an explicit agg_token→original cast so that parent nodes receive the expected type.

Parameters
nodeCurrent expression tree node.
ctxPointer to an aggregation_type_mutator_context (varno, varattno, and constants).
Returns
Possibly modified node.

Definition at line 242 of file provsql.c.

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

◆ aggtoken_walker()

bool aggtoken_walker ( Node * node,
void * data )
static

Tree walker that detects any Var of type agg_token.

Parameters
nodeCurrent expression tree node.
dataPointer to a constants_t (extension OID cache).
Returns
true if an agg_token Var is found in node.

Definition at line 3078 of file provsql.c.

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

◆ build_column_map()

void build_column_map ( Query * q,
int ** columns,
int * nbcols )
static

Build the per-RTE column-numbering map used by where-provenance.

Assigns a sequential position (1, 2, 3, …) to every non-provenance, non-join, non-empty column across all RTEs in q->rtable. The provsql column is assigned -1 so callers can detect provenance-tracked RTEs. Join-RTE columns and empty-named columns (used for anonymous GROUP BY keys) are assigned 0.

Note
For RTE_RELATION entries that are provenance-tracked, the sequential numbers produced here must not be used as PROJECT gate positions. Because numbering is query-order-dependent, the sequential number for a column of a provenance table that is not the first RTE will exceed nb_columns of that table's IN gate, causing WhereCircuit::evaluate() to return an empty locator set. Instead, callers should use varattno directly (see make_provenance_expression()). The -1 sentinel is the reliable way to identify a provenance-tracked RTE.
Parameters
qQuery whose range table is mapped.
columnsPre-allocated array of length q->rtable->length. Each element is allocated and filled by this function.
nbcolsOut-param: total number of non-provenance output columns.

Definition at line 3600 of file provsql.c.

Here is the caller graph for this function:

◆ build_inner_for_distinct_key()

Query * build_inner_for_distinct_key ( Query * q,
Expr * key_expr,
List * groupby_tes )
static

Build the inner GROUP-BY subquery for one AGG(DISTINCT key).

Produces:

SELECT key_expr, gb_col1, gb_col2, ...
FROM <same tables as q>
GROUP BY key_expr, gb_col1, gb_col2, ...
Parameters
qOriginal query (supplies FROM / WHERE).
key_exprThe DISTINCT argument expression.
groupby_tesNon-aggregate target entries that are GROUP BY columns.
Returns
Fresh inner Query.

Definition at line 1935 of file provsql.c.

Here is the caller graph for this function:

◆ build_outer_for_distinct_key()

Query * build_outer_for_distinct_key ( TargetEntry * orig_agg_te,
Query * inner,
int n_gb,
const constants_t * constants )
static

Wrap inner in an outer query that applies the original aggregate.

Produces:

SELECT AGG(key_col), gb_col1, gb_col2, ...
FROM inner
GROUP BY gb_col1, gb_col2, ...

The DISTINCT flag is cleared; inner provides exactly one row per (key, group-by) combination, so the plain aggregate gives the right count.

Parameters
orig_agg_teOriginal TargetEntry containing AGG(DISTINCT key).
innerInner query from build_inner_for_distinct_key.
n_gbNumber of GROUP BY columns (trailing entries in inner).
constantsExtension OID cache.
Returns
Fresh outer Query.

Definition at line 2005 of file provsql.c.

Here is the caller graph for this function:

◆ cast_agg_token_args()

void cast_agg_token_args ( List * args,
insert_agg_token_casts_context * ctx )
static

Wrap any agg_token Vars in an argument list.

Definition at line 3923 of file provsql.c.

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

◆ cast_agg_token_in_list()

void cast_agg_token_in_list ( ListCell * lc,
insert_agg_token_casts_context * ctx )
static

Wrap an agg_token Var in a cast to its original type, in place.

Definition at line 3889 of file provsql.c.

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

◆ cast_agg_token_mutator()

Node * cast_agg_token_mutator ( Node * node,
void * ctx )
static

Tree-mutator that casts provenance_aggregate results back to the original aggregate return type where needed.

After the aggregation mutator replaces Aggrefs with provenance_aggregate calls (returning agg_token), this post-processing step inserts casts where the surrounding expression expects a different type (e.g. SUM(id)+1). Arguments to functions that accept agg_token or polymorphic types are left alone.

Parameters
nodeCurrent expression tree node.
ctxPointer to the constants_t OID cache.
Returns
Possibly modified node.

Definition at line 2440 of file provsql.c.

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

◆ check_boolexpr_on_aggregate()

bool check_boolexpr_on_aggregate ( BoolExpr * be,
const constants_t * constants )
static

Check whether every leaf of a Boolean expression is a supported comparison on an aggregate result.

Recursively validates OpExpr leaves via check_selection_on_aggregate and descends into nested BoolExpr nodes.

Parameters
beThe Boolean expression to validate.
constantsExtension OID cache.
Returns
True if all leaves are supported, false if any is not.

Definition at line 3535 of file provsql.c.

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

◆ check_expr_on_aggregate()

bool check_expr_on_aggregate ( Expr * expr,
const constants_t * constants )
static

Top-level dispatcher for supported WHERE-on-aggregate patterns.

Parameters
exprExpression to validate (OpExpr or BoolExpr).
constantsExtension OID cache.
Returns
True if ProvSQL can handle this expression.

Definition at line 3561 of file provsql.c.

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

◆ check_expr_on_rv()

bool check_expr_on_rv ( Expr * expr,
const constants_t * constants )
static

Test whether expr is a Boolean combination of only random_variable comparisons (no other leaves allowed).

Mirrors check_expr_on_aggregate / check_boolexpr_on_aggregate for the agg_token WHERE-to-HAVING migration path. Recursively accepts:

  • BoolExpr (AND/OR/NOT) all of whose children pass; and
  • OpExpr matching one of the random_variable_* comparators.

Anything else (a non-RV OpExpr, a Var, a Const, a non-cmp FuncExpr) makes the expression mixed and unsupportable by the RV-only walker, so the function returns false and the caller raises a clear error.

Definition at line 1446 of file provsql.c.

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

◆ check_selection_on_aggregate()

bool check_selection_on_aggregate ( OpExpr * op,
const constants_t * constants )
static

Check whether op is a supported comparison on an aggregate result.

Returns true iff op is a two-argument operator where at least one argument is a Var of type agg_token (or an implicit-cast wrapper thereof) and the other is a Const (possibly cast). This is the set of WHERE-on-aggregate patterns that ProvSQL can safely move to a HAVING clause.

Parameters
opThe OpExpr to inspect.
constantsExtension OID cache.
Returns
True if the pattern is supported, false otherwise.

Definition at line 3485 of file provsql.c.

Here is the caller graph for this function:

◆ classify_qual()

qual_class classify_qual ( Expr * expr,
const constants_t * constants )
static

Classify expr along the qual_class axis.

Decision table (the predicates has_aggtoken, expr_contains_rv_cmp, check_expr_on_aggregate, and check_expr_on_rv each return whether the expression "contains" or "is purely" the corresponding flavour):

aggtoken rv_cmp check_agg check_rv classification
yes yes - - QUAL_MIXED_AGG_RV
yes no true - QUAL_PURE_AGG
yes no false - QUAL_MIXED_AGG_DET
no yes - true QUAL_PURE_RV
no yes - false QUAL_MIXED_RV_DET
no no - - QUAL_DETERMINISTIC

Definition at line 3679 of file provsql.c.

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

◆ combine_prov_atts()

Expr * combine_prov_atts ( const constants_t * constants,
List * prov_atts,
semiring_operation op )
static

Build the per-row provenance token for an aggregate rewrite.

Used by both make_aggregation_expression (for the agg_token / provenance_semimod path) and make_rv_aggregate_expression (for the inline RV-aggregate path). Combines prov_atts via provenance_times (under SR_TIMES) or provenance_monus (under SR_MONUS); a single prov_att is returned as-is.

Returns
An Expr returning UUID; never NULL.

Definition at line 798 of file provsql.c.

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

◆ error_for_mixed_qual()

void error_for_mixed_qual ( qual_class c)
static

Raise the user-facing error appropriate to a mixed c.

Each provsql_error call is ereport(ERROR), which does not return; the explicit break statements below are present only to keep -Wimplicit-fallthrough happy (PostgreSQL's elog macro is not marked noreturn for the compiler's flow analysis).

Definition at line 3705 of file provsql.c.

Here is the caller graph for this function:

◆ expr_contains_aggref_walker()

bool expr_contains_aggref_walker ( Node * node,
void * context )
static

expression_tree_walker predicate: returns true on the first Aggref it encounters.

Used to decide whether the provenance expression about to be substituted would inject a nested aggregate when a provenance() call lives inside another Aggref's argument tree.

Definition at line 2578 of file provsql.c.

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

◆ expr_contains_rv_cmp()

bool expr_contains_rv_cmp ( Node * node,
const constants_t * constants )
static

Test whether an Expr (sub-)tree contains any RV comparison.

Used by the WHERE-clause extractor to decide whether a top-level conjunct mentions any random_variable comparator and therefore needs lifting (or, if the conjunct mixes RV and non-RV operators in a way we cannot rewrite, errors).

Definition at line 1409 of file provsql.c.

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

◆ fix_type_of_aggregation_result()

void fix_type_of_aggregation_result ( const constants_t * constants,
Query * q,
Index rteid,
List * targetList )
static

Retypes aggregation-result Vars in q from UUID to agg_token.

After a subquery that contains provenance_aggregate is processed, its result type is agg_token rather than plain UUID. This mutator walks the outer query and updates the type of every Var referencing that result column so that subsequent type-checking passes correctly.

Parameters
constantsExtension OID cache.
qOuter query to patch.
rteidRange-table index of the subquery in q.
targetListTarget list of the subquery (to locate provenance_aggregate columns).

Definition at line 297 of file provsql.c.

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

◆ get_agg_token_orig_type()

Oid get_agg_token_orig_type ( Var * v,
insert_agg_token_casts_context * ctx )
static

Look up the original aggregate return type for an agg_token Var.

Navigates from the Var's varno/varattno to the subquery's target list, finds the provenance_aggregate() FuncExpr, and extracts the type OID from its second argument (aggtype).

Definition at line 3861 of file provsql.c.

Here is the caller graph for this function:

◆ get_provenance_attributes()

List * get_provenance_attributes ( const constants_t * constants,
Query * q )
static

Collect all provenance Var nodes reachable from q's range table.

Walks every RTE in q->rtable:

  • RTE_RELATION: looks for a column named provsql of type UUID.
  • RTE_SUBQUERY: recursively calls process_query and splices the resulting provenance column back into the parent's column list, also patching outer Var attribute numbers if inner columns were removed.
  • RTE_CTE: non-recursive CTEs are inlined as RTE_SUBQUERY before the main loop, then processed as above. Recursive CTEs raise an error.
  • RTE_FUNCTION: handled when the function returns a single UUID column named provsql.
  • RTE_JOIN / RTE_VALUES / RTE_GROUP: handled passively (the underlying base-table RTEs supply the tokens).
Parameters
constantsExtension OID cache.
qQuery whose range table is scanned (subquery RTEs are modified in place by the recursive call).
Returns
List of Var nodes, one per provenance source; NIL if the query has no provenance-bearing relation.

Definition at line 420 of file provsql.c.

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

◆ has_aggtoken()

bool has_aggtoken ( Node * node,
const constants_t * constants )
static

Return true if node contains a Var of type agg_token.

Used to detect whether a WHERE clause references an aggregate result (which must be moved to HAVING).

Parameters
nodeExpression tree to inspect.
constantsExtension OID cache.
Returns
True if an agg_token Var is found anywhere in node.

Definition at line 3102 of file provsql.c.

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

◆ has_provenance()

bool has_provenance ( const constants_t * constants,
Query * q )
static

Return true if q involves any provenance-bearing relation or contains an explicit provenance() call.

This is the gate condition checked by provsql_planner before doing any rewriting: if neither condition holds the query is passed through unchanged.

Parameters
constantsExtension OID cache.
qQuery to inspect.
Returns
True if provenance rewriting is needed.

Definition at line 3068 of file provsql.c.

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

◆ has_provenance_walker()

bool has_provenance_walker ( Node * node,
void * data )
static

Definition at line 2967 of file provsql.c.

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

◆ has_rv_or_provenance_call()

bool has_rv_or_provenance_call ( Node * node,
void * data )
static

Tree walker that detects any provenance-bearing relation or provenance() call.

Parameters
nodeCurrent expression tree node.
dataPointer to constants_t (cast from void*).
Returns
true if provenance rewriting is needed for this node.

Recursive helper for has_provenance_walker that detects rv_cmp OpExpr and provenance() FuncExpr in expression subtrees.

Stops at Query boundaries: SubLink subselects (used as scalar/array subqueries in expressions) are not rewritten by the outer planner_hook pass, so a tracked relation inside one must not cause the OUTER query's gate to engage. Only the testexpr of a SubLink is followed (it lives in the outer's evaluation scope).

Definition at line 2937 of file provsql.c.

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

◆ having_BoolExpr_to_provenance()

FuncExpr * having_BoolExpr_to_provenance ( BoolExpr * be,
const constants_t * constants,
bool negated )
static

Convert a Boolean combination of HAVING comparisons into a provenance_times / provenance_plus gate expression.

Applies De Morgan duality when negated is true: AND becomes provenance_plus (OR) and vice-versa. NOT is handled by flipping negated and delegating to having_Expr_to_provenance_cmp.

Parameters
beBoolean expression from the HAVING clause.
constantsExtension OID cache.
negatedWhether the expression appears under a NOT.
Returns
A FuncExpr combining the sub-expressions.

Definition at line 1154 of file provsql.c.

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

◆ having_Expr_to_provenance_cmp()

FuncExpr * having_Expr_to_provenance_cmp ( Expr * expr,
const constants_t * constants,
bool negated )
static

Dispatch a HAVING sub-expression to the appropriate converter.

Entry point for the mutual recursion between having_BoolExpr_to_provenance and having_OpExpr_to_provenance_cmp.

Parameters
exprSub-expression to convert (BoolExpr or OpExpr).
constantsExtension OID cache.
negatedWhether the expression appears under a NOT.
Returns
Converted FuncExpr.

Definition at line 1204 of file provsql.c.

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

◆ having_lift_walker()

bool having_lift_walker ( Node * node,
void * data )
static

Walker for needs_having_lift: detect any operand shape that the HAVING-lift rewriter (having_OpExpr_to_provenance_cmp) needs to handle specially.

Returns true on:

  • a Var of type agg_token; or
  • a FuncExpr whose funcid is provenance_aggregate (the wrapper the planner-hook puts around aggregates over tracked non-RV columns – yields agg_token).

Anything else (deterministic scalars, plain Const, FuncExpr over random_variable like expected / variance / moment, comparisons of those) is left for PostgreSQL to evaluate natively; the HAVING-lift never needs to touch it.

Definition at line 3122 of file provsql.c.

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

◆ having_OpExpr_to_provenance_cmp()

FuncExpr * having_OpExpr_to_provenance_cmp ( OpExpr * opExpr,
const constants_t * constants,
bool negated )
static

Convert a comparison OpExpr on aggregate results into a provenance_cmp gate expression.

Each argument of opExpr must be one of:

  • A Var of type agg_token (or a FuncExpr implicit-cast wrapper around one) → cast to UUID via agg_token_to_uuid.
  • A scalar Const → wrapped in provenance_semimod(const, gate_one()).

If negated is true the operator OID is replaced by its negator so that NOT(a < b) becomes a >= b at the provenance level.

Parameters
opExprThe comparison expression from the HAVING clause.
constantsExtension OID cache.
negatedWhether the expression appears under a NOT.
Returns
A provenance_cmp(lhs, op_oid, rhs) FuncExpr.

Definition at line 1050 of file provsql.c.

Here is the caller graph for this function:

◆ inline_ctes()

void inline_ctes ( Query * q)
static

Inline all CTE references in q as subqueries.

Definition at line 392 of file provsql.c.

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

◆ inline_ctes_in_rtable()

void inline_ctes_in_rtable ( List * rtable,
List * cteList )
static

Inline CTE references as subqueries within a query.

Replaces each non-recursive RTE_CTE entry in rtable with an RTE_SUBQUERY containing a copy of the CTE's query, looking up definitions in cteList. Recurses into newly inlined subqueries to handle nested CTE references (ctelevelsup > 0).

Parameters
rtableRange table to scan for RTE_CTE entries.
cteListCTE definitions to look up names in.

Definition at line 358 of file provsql.c.

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

◆ insert_agg_token_casts()

void insert_agg_token_casts ( const constants_t * constants,
Query * q )
static

Walk query and insert agg_token casts where needed.

Definition at line 3976 of file provsql.c.

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

◆ insert_agg_token_casts_mutator()

Node * insert_agg_token_casts_mutator ( Node * node,
void * data )
static

Insert agg_token casts for Vars used in expressions.

After the WHERE-to-HAVING migration, agg_token Vars remaining in expression nodes (OpExpr, WindowFunc, CoalesceExpr, MinMaxExpr, etc.) need explicit casts to their original type so that operators and functions receive correct values. The original type is looked up from the provenance_aggregate() call in the subquery.

Definition at line 3943 of file provsql.c.

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

◆ is_target_agg_var()

bool is_target_agg_var ( Node * node,
aggregation_type_mutator_context * context )
static

Check if a Var matches the target aggregate column.

Definition at line 217 of file provsql.c.

Here is the caller graph for this function:

◆ make_aggregation_expression()

Expr * make_aggregation_expression ( const constants_t * constants,
Aggref * agg_ref,
List * prov_atts,
semiring_operation op )
static

Build the provenance expression for a single aggregate function.

For SR_PLUS (union context) returns the first provenance attribute directly. For SR_TIMES or SR_MONUS, constructs:

provenance_aggregate(fn_oid, result_type,
original_aggref,
array_agg(provenance_semimod(arg, times_or_monus_token)))

COUNT(*) and COUNT(expr) are remapped to SUM so that the semimodule semantics (scalar × token → token) work correctly.

Parameters
constantsExtension OID cache.
agg_refThe original Aggref node from the query.
prov_attsList of provenance Var nodes.
opSemiring operation (determines how tokens are combined).
Returns
Provenance expression of type agg_token.

Definition at line 916 of file provsql.c.

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

◆ make_provenance_attribute()

Var * make_provenance_attribute ( const constants_t * constants,
Query * q,
RangeTblEntry * r,
Index relid,
AttrNumber attid )
static

Build a Var node that references the provenance column of a relation.

Creates a Var pointing to attribute attid of range-table entry relid, typed as UUID, and marks the column as selected in the permission bitmap so PostgreSQL grants access correctly.

Parameters
constantsExtension OID cache.
qOwning query (needed to update permission info on PG 16+).
rRange-table entry that owns the provenance column.
relid1-based index of r in q->rtable.
attid1-based attribute number of the provenance column in r.
Returns
A freshly allocated Var node.

Definition at line 116 of file provsql.c.

Here is the caller graph for this function:

◆ make_provenance_expression()

Expr * make_provenance_expression ( const constants_t * constants,
Query * q,
List * prov_atts,
bool aggregation,
bool group_by_rewrite,
semiring_operation op,
int ** columns,
int nbcols,
bool wrap_assumed_boolean )
static

Build the combined provenance expression to be added to the SELECT list.

Combines the tokens in prov_atts according to op:

  • SR_PLUS → use the first token directly (union branch; the outer array_agg / provenance_plus is added later if needed).
  • SR_TIMES → wrap all tokens in provenance_times(...).
  • SR_MONUS → wrap all tokens in provenance_monus(...).

When aggregation or group_by_rewrite is true, wraps the result in array_agg + provenance_plus to collapse groups. A provenance_delta gate is added for plain aggregations without a HAVING clause.

If a HAVING clause is present it is removed from q->havingQual and converted into a provenance expression via having_Expr_to_provenance_cmp.

If provsql_where_provenance is enabled, equality gates (provenance_eq) are prepended for join conditions and WHERE equalities, and a projection gate is appended if the output columns form a proper subset of the input columns.

Parameters
constantsExtension OID cache.
qQuery being rewritten (HAVING is cleared if present).
prov_attsList of provenance Var nodes.
aggregationTrue if the query contains aggregate functions.
group_by_rewriteTrue if a GROUP BY requires the plus-aggregate wrapper.
opSemiring operation to use for combining tokens.
columnsPer-RTE column-numbering array (for where-provenance). For provenance-tracked RTE_RELATION entries, the -1 sentinel is used to identify them; the PROJECT gate positions for their columns use varattno rather than the query-order-dependent sequential numbers (see build_column_map() for the rationale).
nbcolsTotal number of non-provenance output columns.
wrap_assumed_booleanIf true, wrap the result in provenance_assumed_boolean so downstream probability evaluators may treat it as Boolean.
Returns
The provenance Expr to be appended to the target list.

Definition at line 1510 of file provsql.c.

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

◆ make_rv_aggregate_expression()

Expr * make_rv_aggregate_expression ( const constants_t * constants,
Aggref * agg_ref,
List * prov_atts,
semiring_operation op )
static

Inline rewrite of an RV-returning aggregate into the same aggregate over provenance-wrapped per-row arguments.

Originally Phase 1 of the SUM-over-RV story (see aggregation-of-rvs.md); extended to any aggregate whose result type is random_variable (e.g. provsql.sum, provsql.avg). Replaces agg(x) with an Aggref whose per-row argument is lifted through rv_aggregate_semimod to attach the row's provenance: each row contributes mixture(prov_token, X_i, as_random(0)). The aggregate itself (aggfnoid) is preserved verbatim, so its SFUNC / FFUNC decide what gate shape to build from the per-row mixtures. In particular:

  • sum(random_variable) collects the mixtures into a single gate_arith PLUS root, realising \(\mathrm{SUM}(x) = \sum_i \mathbf{1}\{\varphi_i\} \cdot X_i\);
  • avg(random_variable) walks each mixture to recover prov_i and emits gate_arith(DIV, sum(mixture(p_i,x_i,0)), sum(mixture(p_i,1,0))), the natural lift of "AVG = SUM / COUNT" into the random_variable algebra.

Routing happens at make_aggregation_expression on agg_ref->aggtype == OID_TYPE_RANDOM_VARIABLE, so any future RV-returning aggregate inherits the same per-row provenance wrap without further C-side dispatch.

SR_PLUS (UNION outer level) is handled by the caller; this builder never runs for SR_PLUS.

Definition at line 855 of file provsql.c.

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

◆ maybe_cast_agg_token_args()

void maybe_cast_agg_token_args ( List * args,
Oid parent_funcid,
const constants_t * constants )
static

Cast provenance_aggregate arguments of an operator or function when the formal parameter type requires it.

For each argument in args that is a provenance_aggregate call, check the corresponding formal parameter type of the parent function parent_funcid. If the formal type is polymorphic or agg_token itself, the argument is left alone. Otherwise a cast to the original aggregate return type is inserted.

Parameters
argsArgument list to inspect (modified in place).
parent_funcidOID of the parent function / operator implementor.
constantsExtension OID cache.

Definition at line 2394 of file provsql.c.

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

◆ migrate_probabilistic_quals()

List * migrate_probabilistic_quals ( const constants_t * constants,
Query * q )
static

Unified WHERE classifier – routes each top-level conjunct to the right evaluation site in a single pass.

Replaces and consolidates the original migrate_aggtoken_quals_to_having (agg-only) and extract_rv_cmps_from_quals (rv-only). The two old functions were structurally isomorphic: each walked the WHERE clause, classified each top-level conjunct, and routed pure-X conjuncts somewhere semantic (HAVING vs the returned rv_cmps list); the deterministic conjuncts stayed in WHERE. Doing it in one pass means the rare conjunct that mixes agg_token and random_variable (which neither old function would have caught cleanly) gets a deterministic, useful error message.

Supported shapes mirror the union of the two predecessors:

  • Whole WHERE is a single conjunct: classify and route or error.
  • Top-level AND of conjuncts: classify each, route, and (after walking) collapse the AND if it has zero or one remaining children so downstream code does not see a degenerate Boolean node.
  • Top-level OR / NOT containing both deterministic and probabilistic leaves: error.
Parameters
constantsExtension OID cache.
qQuery whose jointree->quals and havingQual may both be mutated in place.
Returns
List of FuncExpr nodes (one per lifted RV conjunct), each producing a UUID. The caller conjoins these into prov_atts before make_provenance_expression.

Definition at line 3759 of file provsql.c.

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

◆ needs_having_lift()

bool needs_having_lift ( Node * havingQual,
const constants_t * constants )
static

Return true if havingQual contains anything the HAVING-lift path needs to handle (an agg_token Var or a provenance_aggregate wrapper).

A qual that returns false is left in place for PostgreSQL to evaluate, while the per-group provenance still gets a gate_delta wrapper.

This is what lets a HAVING like expected(avg(rv)) > 20 work directly: provsql.avg returns random_variable (not agg_token), expected collapses to a scalar double, and the surrounding comparison is a plain Boolean that PostgreSQL can filter groups by without any provenance-side rewriting.

Definition at line 3155 of file provsql.c.

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

◆ process_insert_select()

void process_insert_select ( const constants_t * constants,
Query * q )
static

Propagate provenance through INSERT ... SELECT.

If the source SELECT involves provenance-tracked tables and the target table has a provsql column, rewrites the source SELECT to carry provenance and maps its provsql output to the target's provsql column, replacing the default uuid_generate_v4().

If the target has no provsql column, emits a warning instead.

Definition at line 4406 of file provsql.c.

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

◆ process_query()

Query * process_query ( const constants_t * constants,
Query * q,
bool ** removed,
bool wrap_root )
static

Rewrite a single SELECT query to carry provenance.

This is the recursive entry point for the provenance rewriter. It is called from provsql_planner for top-level queries and re-entered from get_provenance_attributes for subqueries in FROM.

High-level steps:

  1. Strip any provsql column propagated into this query's target list.
  2. Detect and rewrite structural forms requiring pre-processing: non-ALL set operations (wrap in outer GROUP BY), AGG DISTINCT (push into a subquery), DISTINCT (convert to GROUP BY).
  3. Collect provenance attributes via get_provenance_attributes.
  4. Build a column-numbering map for where-provenance (build_column_map).
  5. Handle aggregates, migrate WHERE-on-aggregate to HAVING, and set ops.
  6. Build and splice the combined provenance expression.
Parameters
constantsExtension OID cache.
qQuery to rewrite (modified in place).
removedOut-param: boolean array indicating which original target list entries were provenance columns and were removed. May be NULL if the caller does not need this info.
wrap_rootIf true, mark this query's provenance expression as a safe-query root that must be wrapped in provsql.assume_boolean before splicing.
Returns
The (possibly restructured) rewritten query, or NULL if the query has no FROM clause and can be skipped.

Definition at line 4043 of file provsql.c.

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

◆ process_set_operation_union()

void process_set_operation_union ( const constants_t * constants,
SetOperationStmt * stmt,
Query * q )
static

Recursively annotate a UNION tree with the provenance UUID type.

Walks the SetOperationStmt tree of a UNION and appends the UUID type to colTypes / colTypmods / colCollations on every node, and sets all = true so that PostgreSQL does not deduplicate the combined stream. The non-ALL deduplication has already been moved to an outer GROUP BY by rewrite_non_all_into_external_group_by before this is called.

Parameters
constantsExtension OID cache.
stmtRoot (or subtree) of the UNION SetOperationStmt.
qOuter query (to look up subquery RTEs for agg_token type updates).

Definition at line 3365 of file provsql.c.

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

◆ provenance_function_in_group_by()

bool provenance_function_in_group_by ( const constants_t * constants,
Query * q )
static

Check whether a provenance() call appears in the GROUP BY list.

When the user writes GROUP BY provenance(), ProvSQL must not add its own group-by wrapper (the query is already grouping on the token).

Parameters
constantsExtension OID cache.
qQuery to inspect.
Returns
True if any GROUP BY key contains a provenance() call.

Definition at line 2880 of file provsql.c.

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

◆ provenance_function_walker()

bool provenance_function_walker ( Node * node,
void * data )
static

Tree walker that returns true if any provenance() call is found.

Used to detect whether a query explicitly calls provenance(), which triggers the substitution in replace_provenance_function_by_expression.

Parameters
nodeCurrent expression tree node.
dataPointer to constants_t (cast from void*).
Returns
true if a provenance() call is found anywhere in node.

Definition at line 2855 of file provsql.c.

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

◆ provenance_mutator()

Node * provenance_mutator ( Node * node,
void * ctx )
static

Tree-mutator that replaces provenance() calls with the actual provenance expression.

Parameters
nodeCurrent expression tree node.
ctxPointer to a provenance_mutator_context (provenance expression and constants).
Returns
Possibly modified node.

Definition at line 2593 of file provsql.c.

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

◆ provsql_ctas_kind_label()

const char * provsql_ctas_kind_label ( provsql_table_kind k)
static

Map provsql_table_kind to its textual label (set_table_info accepts text).

Definition at line 4856 of file provsql.c.

Here is the caller graph for this function:

◆ provsql_executor_end()

void provsql_executor_end ( QueryDesc * queryDesc)
static

Definition at line 4650 of file provsql.c.

Here is the caller graph for this function:

◆ provsql_executor_start()

void provsql_executor_start ( QueryDesc * queryDesc,
int eflags )
static

Definition at line 4633 of file provsql.c.

Here is the caller graph for this function:

◆ provsql_planner()

PlannedStmt * provsql_planner ( Query * q,
int cursorOptions,
ParamListInfo boundParams )
static

Definition at line 4540 of file provsql.c.

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

◆ provsql_ProcessUtility()

void provsql_ProcessUtility ( PlannedStmt * pstmt,
const char * queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment * queryEnv,
DestReceiver * dest,
char * completionTag )
static

Definition at line 5056 of file provsql.c.

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

◆ provsql_ProcessUtility_apply()

void provsql_ProcessUtility_apply ( Node * parsetree,
ProvSQLCtasCapture * cap )
static

Apply cap to the freshly-created relation stmt->into->rel.

For BID sources: walks the inner query's target list to align each source block-key column to its output resno. If any block-key column is missing from the projection (the CTAS dropped it), the new relation cannot honour the BID invariant under that column – the hook demotes to TID rather than asserting a now-stale block key.

Installs provenance_guard via SPI so subsequent INSERT / UPDATE OF provsql on the new relation flip its kind to OPAQUE through the standard guard path.

Definition at line 4883 of file provsql.c.

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

◆ provsql_ProcessUtility_capture()

void provsql_ProcessUtility_capture ( Node * parsetree,
ProvSQLCtasCapture * cap )
static

Decide whether parsetree is a CTAS that should trigger the ancestry hook, and if so populate cap with the inner classification, the (single) source's block-key columns, and the transitive ancestor union.

Fires only when the inner SELECT's target list projects a base- level Var (possibly through RelabelType wrappers) that resolves to the provsql column of an RTE_RELATION whose metadata is non-OPAQUE. Anything else (no provsql in the projection, classifier says OPAQUE, the projected source is itself OPAQUE) leaves cap->fire false and the post-pass becomes a no-op.

Definition at line 4740 of file provsql.c.

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

◆ reduce_varattno_by_offset()

void reduce_varattno_by_offset ( List * targetList,
Index varno,
int * offset )
static

Adjust Var attribute numbers in targetList after columns are removed.

When provenance columns are stripped from a subquery's target list, the remaining columns shift left. This function applies a pre-computed offset array (one entry per original column) to correct all Var nodes that reference range-table entry varno.

Parameters
targetListTarget list of the outer query to patch.
varnoRange-table entry whose attribute numbers need fixing.
offsetCumulative shift per original attribute (negative or zero).

Definition at line 196 of file provsql.c.

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

◆ reduce_varattno_mutator()

Node * reduce_varattno_mutator ( Node * node,
void * ctx )
static

Tree-mutator callback that adjusts Var attribute numbers.

Parameters
nodeCurrent expression tree node.
ctxPointer to a reduce_varattno_mutator_context.
Returns
Possibly modified node.

Definition at line 168 of file provsql.c.

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

◆ remove_provenance_attribute_groupref()

void remove_provenance_attribute_groupref ( Query * q,
const Bitmapset * removed_sortgrouprefs )
static

Remove sort/group references that belonged to removed provenance columns.

After remove_provenance_attributes_select strips provenance entries from the target list, any GROUP BY, ORDER BY, or DISTINCT clause that referenced them by tleSortGroupRef must be cleaned up.

Parameters
qQuery to modify in place.
removed_sortgrouprefsBitmapset of ressortgroupref values to remove.

Definition at line 2705 of file provsql.c.

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

◆ remove_provenance_attribute_setoperations()

void remove_provenance_attribute_setoperations ( Query * q,
bool * removed )
static

Strip the provenance column's type info from a set-operation node.

When a provenance column is removed from a UNION/EXCEPT query's target list, the matching entries in the SetOperationStmt's colTypes, colTypmods, and colCollations lists must also be removed.

Parameters
qQuery containing setOperations.
removedBoolean array (from remove_provenance_attributes_select) indicating which columns were removed.

Definition at line 2742 of file provsql.c.

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

◆ remove_provenance_attributes_select()

Bitmapset * remove_provenance_attributes_select ( const constants_t * constants,
Query * q,
bool ** removed )
static

Strip provenance UUID columns from q's SELECT list.

Scans the target list and removes every Var entry whose column name is provsql and whose type is UUID. The remaining entries have their resno values decremented to fill the gaps.

Parameters
constantsExtension OID cache.
qQuery to modify in place.
removedOut-param: allocated boolean array (length = original target list length) where true means the corresponding entry was removed. The caller must pfree this array when done.
Returns
Bitmapset of ressortgroupref values whose entries were removed (so the caller can clean up GROUP BY / ORDER BY).

Definition at line 577 of file provsql.c.

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

◆ replace_aggregations_by_provenance_aggregate()

void replace_aggregations_by_provenance_aggregate ( const constants_t * constants,
Query * q,
List * prov_atts,
semiring_operation op )
static

Replace every Aggref in q with a provenance-aware aggregate.

Walks the query tree and substitutes each Aggref node with the result of make_aggregation_expression, which wraps the original aggregate in the semimodule machinery (provenance_semimod + array_agg + provenance_aggregate).

Parameters
constantsExtension OID cache.
qQuery to mutate in place.
prov_attsList of provenance Var nodes.
opSemiring operation for combining tokens across rows.

Definition at line 2477 of file provsql.c.

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

◆ replace_provenance_function_by_expression()

void replace_provenance_function_by_expression ( const constants_t * constants,
Query * q,
Expr * provsql )
static

Replace every explicit provenance() call in q with provsql.

Users can write provenance() in the target list or WHERE to refer to the provenance token of the current tuple. This mutator substitutes those calls with the actual computed provenance expression.

Parameters
constantsExtension OID cache.
qQuery to mutate in place.
provsqlProvenance expression to substitute.

Definition at line 2648 of file provsql.c.

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

◆ rewrite_agg_distinct()

Query * rewrite_agg_distinct ( Query * q,
const constants_t * constants )
static

Rewrite every AGG(DISTINCT key) in q using independent subqueries.

For a single DISTINCT aggregate, produces a subquery:

SELECT AGG(key), gb... FROM (SELECT key, gb... FROM t GROUP BY key, gb...) GROUP BY gb...

For multiple DISTINCT aggregates with different keys, produces an JOIN of one such subquery per aggregate, joined on the GROUP BY columns. Non-DISTINCT aggregates are left untouched.

Parameters
qQuery to inspect and possibly rewrite.
constantsExtension OID cache.
Returns
Rewritten query, or NULL if no AGG(DISTINCT) was found.

Definition at line 2113 of file provsql.c.

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

◆ rewrite_non_all_into_external_group_by()

Query * rewrite_non_all_into_external_group_by ( Query * q)
static

Wrap a non-ALL set operation in an outer GROUP BY query.

UNION / EXCEPT (without ALL) would deduplicate tuples before ProvSQL can attach provenance tokens. To avoid this, the set operation is converted to UNION ALL / EXCEPT ALL and a new outer query is built that groups the results by all non-provenance columns, collecting tokens into an array for the provenance_plus evaluation.

After this rewrite the recursive call to process_query handles the now-ALL inner set operation normally.

Parameters
qQuery whose setOperations is non-ALL (modified to ALL in place).
Returns
New outer query that wraps q as a subquery RTE.

Definition at line 2783 of file provsql.c.

Here is the caller graph for this function:

◆ rv_BoolExpr_to_provenance()

FuncExpr * rv_BoolExpr_to_provenance ( BoolExpr * be,
const constants_t * constants,
bool negated )
static

Convert a Boolean combination of RV comparisons into a provenance_times / provenance_plus expression.

Same De Morgan handling as having_BoolExpr_to_provenance: under negation, AND ↔ OR (which means PROVENANCE_TIMES ↔ PROVENANCE_PLUS). NOT flips negated and recurses.

Definition at line 1336 of file provsql.c.

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

◆ rv_cmp_index()

int rv_cmp_index ( const constants_t * constants,
Oid funcoid )
static

Test whether funcoid is one of the random_variable_* comparison procedures, and if so return its ComparisonOperator index.

Parameters
constantsExtension OID cache.
funcoidProcedure OID to test (typically OpExpr->opfuncid).
Returns
Index in [0..6) on match, -1 otherwise. Match indices line up with ComparisonOperator (EQ=0, NE=1, LE=2, LT=3, GE=4, GT=5).

Definition at line 1242 of file provsql.c.

Here is the caller graph for this function:

◆ rv_Expr_to_provenance()

FuncExpr * rv_Expr_to_provenance ( Expr * expr,
const constants_t * constants,
bool negated )
static

Dispatch a WHERE sub-expression to the appropriate RV converter.

Entry point for the mutual recursion between rv_BoolExpr_to_provenance and rv_OpExpr_to_provenance_cmp.

Definition at line 1386 of file provsql.c.

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

◆ rv_OpExpr_to_provenance_cmp()

FuncExpr * rv_OpExpr_to_provenance_cmp ( OpExpr * opExpr,
const constants_t * constants,
bool negated )
static

Convert a single RV-comparison OpExpr into a provenance_cmp() FuncExpr returning UUID.

If negated is true the operator OID is replaced by its negator (so NOT (a > b) becomes a ≤ b at the provenance level), exactly as having_OpExpr_to_provenance_cmp does.

Parameters
opExprThe comparison expression from the WHERE clause. Must satisfy rv_cmp_index(opExpr->opfuncid) ≥ 0; callers are responsible for the type check.
constantsExtension OID cache.
negatedWhether the expression appears under a NOT.

Definition at line 1297 of file provsql.c.

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

◆ set_ancestors()

Datum set_ancestors ( PG_FUNCTION_ARGS )
extern

PostgreSQL-callable wrapper for setTableAncestry() over the IPC pipe.

Records the base-relation ancestor set of a tracked relation. relid is the pg_class OID of the relation; ancestors is an oid[] (possibly empty) listing the base add_provenance / repair_key relations this one's atoms ultimately come from. The worker preserves the relation's existing kind / block_key half on update.

Silently no-op on the worker side when relid has no kind record yet – the safe-query rewriter only consults ancestry for tracked relations, so callers should run add_provenance / repair_key / set_table_info first.

Definition at line 777 of file provsql_mmap.c.

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

◆ set_table_info()

Datum set_table_info ( PG_FUNCTION_ARGS )
extern

Forward declaration of the C SQL entry points.

Forward declaration of the C SQL entry points.

Stores per-relation provenance metadata used by the safe-query optimisation. relid is the pg_class OID of the relation; kind is one of the textual labels 'tid' / 'bid' / 'opaque' (see provsql_table_kind in MMappedTableInfo.h); block_key is an int2 array (possibly empty) listing the block-key column numbers when kind is 'bid'.

Definition at line 565 of file provsql_mmap.c.

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

◆ transform_distinct_into_group_by()

void transform_distinct_into_group_by ( Query * q)
static

Convert a SELECT DISTINCT into an equivalent GROUP BY.

ProvSQL cannot handle DISTINCT directly (it would collapse provenance tokens that should remain separate). This function moves every entry from q->distinctClause into q->groupClause (skipping any that are already there) and clears q->distinctClause.

Parameters
qQuery to modify in place.

Definition at line 2672 of file provsql.c.

Here is the caller graph for this function:

◆ transform_except_into_join()

bool transform_except_into_join ( const constants_t * constants,
Query * q )
static

Rewrite an EXCEPT query into a LEFT JOIN with monus provenance.

EXCEPT cannot be handled directly because it deduplicates. This function transforms:

SELECT … FROM A EXCEPT SELECT … FROM B

into a LEFT JOIN of A and B on equality of all non-provenance columns, clears setOperations, and leaves the monus token combination to make_provenance_expression (which will see SR_MONUS).

Only simple (non-chained) EXCEPT is supported; chained EXCEPT raises an error.

Parameters
constantsExtension OID cache.
qQuery to rewrite in place.
Returns
Always true (errors out on unsupported cases).

Definition at line 3179 of file provsql.c.

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

◆ wrap_agg_token_with_cast()

Node * wrap_agg_token_with_cast ( FuncExpr * prov_agg,
const constants_t * constants )
static

Wrap a provenance_aggregate FuncExpr with a cast to the original aggregate return type.

Parameters
prov_aggThe provenance_aggregate FuncExpr to wrap.
constantsExtension OID cache.
Returns
Cast FuncExpr wrapping prov_agg.

Definition at line 2353 of file provsql.c.

Here is the caller graph for this function:

◆ wrap_in_assume_boolean()

Expr * wrap_in_assume_boolean ( const constants_t * constants,
Expr * expr )
static

Wrap expr in a provsql.assume_boolean FuncExpr.

Used by make_provenance_expression when its caller (the safe-query rewrite path in process_query) flagged the result as needing the gate_assumed_boolean structural marker. Wrapping at expression-build time rather than at splice time means add_to_select and replace_provenance_function_by_expression both consume the already-wrapped expression, so every per-row root occurrence in the final target list – the auto-added provsql column and every substituted user-side provenance() call – carries the wrapper uniformly.

Parameters
constantsExtension OID cache.
exprProvenance expression to wrap.
Returns
A FuncExpr applying provsql.assume_boolean to expr.

Definition at line 4000 of file provsql.c.

Here is the caller graph for this function:

◆ wrap_random_variable_uuid()

Expr * wrap_random_variable_uuid ( Node * operand,
const constants_t * constants )
static

Wrap an expression returning random_variable in a binary-coercible cast to uuid.

Operand of the comparison may be a Var, a constant lifted by an implicit cast, or another OpExpr (e.g. a + b). random_variable and uuid share the same byte layout, so we emit a RelabelType node – the planner sees a zero-cost type relabel, the executor never dispatches through a runtime conversion function.

Definition at line 1263 of file provsql.c.

Here is the caller graph for this function:

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Required PostgreSQL extension magic block.

Definition at line 68 of file provsql.c.

◆ prev_ExecutorEnd

ExecutorEnd_hook_type prev_ExecutorEnd = NULL
static

Definition at line 4631 of file provsql.c.

◆ prev_ExecutorStart

ExecutorStart_hook_type prev_ExecutorStart = NULL
static

Definition at line 4630 of file provsql.c.

◆ prev_planner

planner_hook_type prev_planner = NULL
static

Previous planner hook (chained).

Definition at line 92 of file provsql.c.

◆ prev_ProcessUtility

ProcessUtility_hook_type prev_ProcessUtility = NULL
static

Definition at line 4712 of file provsql.c.

◆ provsql_active

bool provsql_active = true
static

true while ProvSQL query rewriting is enabled

Definition at line 75 of file provsql.c.

◆ provsql_aggtoken_text_as_uuid

bool provsql_aggtoken_text_as_uuid = false

When true, agg_token::text emits the underlying provenance UUID instead of "value (*)".

Global flag controlling agg_token text output: when true, agg_token_out emits the underlying provenance UUID instead of the default "value (*)" display string.

Definition at line 79 of file provsql.c.

◆ provsql_boolean_provenance

bool provsql_boolean_provenance = false

Opt-in safe-query optimisation: when true, rewrites hierarchical conjunctive queries to a read-once form whose probability is computable in linear time. The resulting circuit is tagged so that semiring evaluations admitting no homomorphism from Boolean functions refuse to run on it. Controlled by the provsql.boolean_provenance GUC.

GUC: opt-in safe-query optimisation, declared in provsql.c.

Opt-in safe-query optimisation for hierarchical conjunctive queries; see the provsql.boolean_provenance GUC.

Definition at line 86 of file provsql.c.

◆ provsql_cmp_probability_evaluation

bool provsql_cmp_probability_evaluation = true

Run closed-form / analytic probability evaluators for gate_cmps inside probability_evaluate (currently the Poisson-binomial pre-pass for HAVING-COUNT; future MIN / MAX / SUM evaluators will gate on the same GUC); controlled by the provsql.cmp_probability_evaluation GUC.

Hidden diagnostic flag for the family of closed-form / analytic probability evaluators that resolve gate_cmps inside probability_evaluate ; see the provsql.cmp_probability_evaluation GUC.

Definition at line 85 of file provsql.c.

◆ provsql_executor_depth

int provsql_executor_depth = 0
static

PostgreSQL planner hook — entry point for provenance rewriting.

Replaces (or chains after) the standard planner. For every CMD_SELECT that involves at least one provenance-bearing relation or an explicit provenance() call, rewrites the query via process_query before handing the result to the standard planner. Non-SELECT commands and queries without provenance are passed through unchanged.

Parameters
qThe query to plan.
cursorOptionsCursor options bitmask.
boundParamsPre-bound parameter values.
Returns
The planned statement.

Executor nesting depth.

Tracks how deep we are inside Executor invocations. Incremented in provsql_executor_start, decremented in provsql_executor_end. The classifier NOTICE only fires when this is zero, which corresponds to the user's outermost statement being planned (before any executor entry). Plans built for PL/pgSQL function bodies that the rewriter inserts – provenance_times, provenance_plus, provenance_aggregate, ... – happen during execution of the user's plan, so they see depth >= 1 and skip the NOTICE.

Definition at line 4538 of file provsql.c.

◆ provsql_hybrid_evaluation

bool provsql_hybrid_evaluation = true

Run the hybrid-evaluator simplifier inside probability_evaluate; controlled by the provsql.hybrid_evaluation GUC.

Run the hybrid evaluator (simplifier + per-cmp island decomposer) before dispatching a probability_evaluate query.

Definition at line 84 of file provsql.c.

◆ provsql_interrupted

bool provsql_interrupted = false

Global variable that becomes true if this particular backend received an interrupt signal.

Definition at line 74 of file provsql.c.

◆ provsql_monte_carlo_seed

int provsql_monte_carlo_seed = -1

Seed for the Monte Carlo sampler; -1 means non-deterministic (std::random_device); controlled by the provsql.monte_carlo_seed GUC.

Seed for the Monte Carlo sampler, set by the provsql.monte_carlo_seed run-time configuration parameter.

Definition at line 81 of file provsql.c.

◆ provsql_rv_mc_samples

int provsql_rv_mc_samples = 10000

Default sample count for analytical-evaluator MC fallbacks; 0 disables fallback (callers raise instead); controlled by the provsql.rv_mc_samples GUC.

Default sample count for Monte Carlo fallbacks when an analytical evaluator (Expectation, future hybrid evaluator, ...) cannot decompose a sub-circuit structurally.

Definition at line 82 of file provsql.c.

◆ provsql_simplify_on_load

bool provsql_simplify_on_load = true

Run universal cmp-resolution passes when getGenericCircuit returns; controlled by the provsql.simplify_on_load GUC.

When true (default), every GenericCircuit returned by getGenericCircuit is run through the universal cmp-resolution passes (RangeCheck for now, plus any future passes that decide comparators to certain Boolean values).

Definition at line 83 of file provsql.c.

◆ provsql_tool_search_path

char* provsql_tool_search_path = NULL

Colon-separated directory list prepended to PATH when invoking external tools (d4, c2d, minic2d, dsharp, weightmc, graph-easy); controlled by the provsql.tool_search_path GUC.

Colon-separated list of directories prepended to PATH when ProvSQL spawns external tools (d4, c2d, minic2d, dsharp, weightmc, graph-easy), set by the provsql.tool_search_path run-time configuration parameter.

Definition at line 80 of file provsql.c.

◆ provsql_update_provenance

bool provsql_update_provenance = false
static

true when provenance tracking for DML is enabled

Definition at line 77 of file provsql.c.

◆ provsql_verbose

int provsql_verbose = 100

Verbosity level; controlled by the provsql.verbose_level GUC.

Global variable that indicates the verbosity level set by the provsql.verbose_level run-time configuration parameter was set.

Definition at line 78 of file provsql.c.

◆ provsql_where_provenance

bool provsql_where_provenance = false

Global variable that indicates if where-provenance support has been activated through the provsql.where_provenance run-time configuration parameter.

Definition at line 76 of file provsql.c.