![]() |
ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
|
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"
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 |
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:
provsql UUID column (get_provenance_attributes).make_provenance_expression, make_aggregation_expression).provenance() call in the query with the computed expression (add_to_select, replace_provenance_function_by_expression). Definition in file provsql.c.
| 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 |
| enum 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).
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). |
|
extern |
|
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.

|
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).
| constants | Extension OID cache. |
| fromOpExpr | The equality OpExpr to inspect. |
| toExpr | Existing provenance expression to wrap. |
| columns | Per-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. |
toExpr wrapped in provenance_eq(toExpr, col1, col2), or toExpr unchanged if the shape is unsupported. Definition at line 679 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| quals | Root of the quals tree (OpExpr or BoolExpr), or NULL (in which case result is returned unchanged). |
| result | Provenance expression to wrap. |
| columns | Per-RTE column-numbering array. |
eq gates added. Definition at line 755 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| q | Query to modify in place. |
| provsql | Provenance expression that was added to the SELECT list. |
Definition at line 3415 of file provsql.c.

|
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.
| havingQual | Existing HAVING qualifier, or NULL. |
| expr | Expression to conjoin. |
Definition at line 3452 of file provsql.c.

|
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.
| q | Query to modify in place. |
| provenance | Expression to add (becomes the provsql output column). |
Definition at line 2515 of file provsql.c.


|
static |
Tree-mutator that replaces Aggrefs with provenance-aware aggregates.
| node | Current expression tree node. |
| ctx | Pointer to an aggregation_mutator_context (prov_atts, op, and constants). |
Definition at line 2331 of file provsql.c.


|
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.
| node | Current expression tree node. |
| ctx | Pointer to an aggregation_type_mutator_context (varno, varattno, and constants). |
Definition at line 242 of file provsql.c.


|
static |
Tree walker that detects any Var of type agg_token.
| node | Current expression tree node. |
| data | Pointer to a constants_t (extension OID cache). |
true if an agg_token Var is found in node. Definition at line 3078 of file provsql.c.


|
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.
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.| q | Query whose range table is mapped. |
| columns | Pre-allocated array of length q->rtable->length. Each element is allocated and filled by this function. |
| nbcols | Out-param: total number of non-provenance output columns. |
Definition at line 3600 of file provsql.c.

|
static |
Build the inner GROUP-BY subquery for one AGG(DISTINCT key).
Produces:
| q | Original query (supplies FROM / WHERE). |
| key_expr | The DISTINCT argument expression. |
| groupby_tes | Non-aggregate target entries that are GROUP BY columns. |
Query. Definition at line 1935 of file provsql.c.

|
static |
Wrap inner in an outer query that applies the original aggregate.
Produces:
The DISTINCT flag is cleared; inner provides exactly one row per (key, group-by) combination, so the plain aggregate gives the right count.
| orig_agg_te | Original TargetEntry containing AGG(DISTINCT key). |
| inner | Inner query from build_inner_for_distinct_key. |
| n_gb | Number of GROUP BY columns (trailing entries in inner). |
| constants | Extension OID cache. |
Query. Definition at line 2005 of file provsql.c.

|
static |
|
static |
|
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.
| node | Current expression tree node. |
| ctx | Pointer to the constants_t OID cache. |
Definition at line 2440 of file provsql.c.


|
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.
| be | The Boolean expression to validate. |
| constants | Extension OID cache. |
Definition at line 3535 of file provsql.c.


|
static |
Top-level dispatcher for supported WHERE-on-aggregate patterns.
| expr | Expression to validate (OpExpr or BoolExpr). |
| constants | Extension OID cache. |
Definition at line 3561 of file provsql.c.


|
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; andOpExpr 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.


|
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.
| op | The OpExpr to inspect. |
| constants | Extension OID cache. |
Definition at line 3485 of file provsql.c.

|
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.


|
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.
Expr returning UUID; never NULL. Definition at line 798 of file provsql.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.

|
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.


|
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.


|
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.
| constants | Extension OID cache. |
| q | Outer query to patch. |
| rteid | Range-table index of the subquery in q. |
| targetList | Target list of the subquery (to locate provenance_aggregate columns). |
Definition at line 297 of file provsql.c.


|
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.

|
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).| constants | Extension OID cache. |
| q | Query whose range table is scanned (subquery RTEs are modified in place by the recursive call). |
Var nodes, one per provenance source; NIL if the query has no provenance-bearing relation. Definition at line 420 of file provsql.c.


|
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).
| node | Expression tree to inspect. |
| constants | Extension OID cache. |
agg_token Var is found anywhere in node. Definition at line 3102 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| q | Query to inspect. |
Definition at line 3068 of file provsql.c.


|
static |
|
static |
Tree walker that detects any provenance-bearing relation or provenance() call.
| node | Current expression tree node. |
| data | Pointer to constants_t (cast from void*). |
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.


|
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.
| be | Boolean expression from the HAVING clause. |
| constants | Extension OID cache. |
| negated | Whether the expression appears under a NOT. |
FuncExpr combining the sub-expressions. Definition at line 1154 of file provsql.c.


|
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.
| expr | Sub-expression to convert (BoolExpr or OpExpr). |
| constants | Extension OID cache. |
| negated | Whether the expression appears under a NOT. |
FuncExpr. Definition at line 1204 of file provsql.c.


|
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:
Var of type agg_token; orFuncExpr 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.


|
static |
Convert a comparison OpExpr on aggregate results into a provenance_cmp gate expression.
Each argument of opExpr must be one of:
Var of type agg_token (or a FuncExpr implicit-cast wrapper around one) → cast to UUID via agg_token_to_uuid.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.
| opExpr | The comparison expression from the HAVING clause. |
| constants | Extension OID cache. |
| negated | Whether the expression appears under a NOT. |
provenance_cmp(lhs, op_oid, rhs) FuncExpr. Definition at line 1050 of file provsql.c.

|
static |
|
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).
| rtable | Range table to scan for RTE_CTE entries. |
| cteList | CTE definitions to look up names in. |
Definition at line 358 of file provsql.c.


|
static |
|
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.


|
static |
|
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:
COUNT(*) and COUNT(expr) are remapped to SUM so that the semimodule semantics (scalar × token → token) work correctly.
| constants | Extension OID cache. |
| agg_ref | The original Aggref node from the query. |
| prov_atts | List of provenance Var nodes. |
| op | Semiring operation (determines how tokens are combined). |
agg_token. Definition at line 916 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| q | Owning query (needed to update permission info on PG 16+). |
| r | Range-table entry that owns the provenance column. |
| relid | 1-based index of r in q->rtable. |
| attid | 1-based attribute number of the provenance column in r. |
Var node. Definition at line 116 of file provsql.c.

|
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.
| constants | Extension OID cache. |
| q | Query being rewritten (HAVING is cleared if present). |
| prov_atts | List of provenance Var nodes. |
| aggregation | True if the query contains aggregate functions. |
| group_by_rewrite | True if a GROUP BY requires the plus-aggregate wrapper. |
| op | Semiring operation to use for combining tokens. |
| columns | Per-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). |
| nbcols | Total number of non-provenance output columns. |
| wrap_assumed_boolean | If true, wrap the result in provenance_assumed_boolean so downstream probability evaluators may treat it as Boolean. |
Expr to be appended to the target list. Definition at line 1510 of file provsql.c.


|
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.


|
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.
| args | Argument list to inspect (modified in place). |
| parent_funcid | OID of the parent function / operator implementor. |
| constants | Extension OID cache. |
Definition at line 2394 of file provsql.c.


|
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:
| constants | Extension OID cache. |
| q | Query whose jointree->quals and havingQual may both be mutated in place. |
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.


|
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.


|
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.


|
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:
provsql column propagated into this query's target list.get_provenance_attributes.build_column_map).| constants | Extension OID cache. |
| q | Query to rewrite (modified in place). |
| removed | Out-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_root | If true, mark this query's provenance expression as a safe-query root that must be wrapped in provsql.assume_boolean before splicing. |
NULL if the query has no FROM clause and can be skipped. Definition at line 4043 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| stmt | Root (or subtree) of the UNION SetOperationStmt. |
| q | Outer query (to look up subquery RTEs for agg_token type updates). |
Definition at line 3365 of file provsql.c.


|
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).
| constants | Extension OID cache. |
| q | Query to inspect. |
provenance() call. Definition at line 2880 of file provsql.c.


|
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.
| node | Current expression tree node. |
| data | Pointer to constants_t (cast from void*). |
true if a provenance() call is found anywhere in node. Definition at line 2855 of file provsql.c.


|
static |
Tree-mutator that replaces provenance() calls with the actual provenance expression.
| node | Current expression tree node. |
| ctx | Pointer to a provenance_mutator_context (provenance expression and constants). |
Definition at line 2593 of file provsql.c.


|
static |
Map provsql_table_kind to its textual label (set_table_info accepts text).
Definition at line 4856 of file provsql.c.

|
static |
|
static |
|
static |
|
static |
|
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.


|
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.


|
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.
| targetList | Target list of the outer query to patch. |
| varno | Range-table entry whose attribute numbers need fixing. |
| offset | Cumulative shift per original attribute (negative or zero). |
Definition at line 196 of file provsql.c.


|
static |
Tree-mutator callback that adjusts Var attribute numbers.
| node | Current expression tree node. |
| ctx | Pointer to a reduce_varattno_mutator_context. |
Definition at line 168 of file provsql.c.


|
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.
| q | Query to modify in place. |
| removed_sortgrouprefs | Bitmapset of ressortgroupref values to remove. |
Definition at line 2705 of file provsql.c.


|
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.
| q | Query containing setOperations. |
| removed | Boolean array (from remove_provenance_attributes_select) indicating which columns were removed. |
Definition at line 2742 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| q | Query to modify in place. |
| removed | Out-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. |
ressortgroupref values whose entries were removed (so the caller can clean up GROUP BY / ORDER BY). Definition at line 577 of file provsql.c.


|
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).
| constants | Extension OID cache. |
| q | Query to mutate in place. |
| prov_atts | List of provenance Var nodes. |
| op | Semiring operation for combining tokens across rows. |
Definition at line 2477 of file provsql.c.


|
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.
| constants | Extension OID cache. |
| q | Query to mutate in place. |
| provsql | Provenance expression to substitute. |
Definition at line 2648 of file provsql.c.


|
static |
Rewrite every AGG(DISTINCT key) in q using independent subqueries.
For a single DISTINCT aggregate, produces a subquery:
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.
| q | Query to inspect and possibly rewrite. |
| constants | Extension OID cache. |
NULL if no AGG(DISTINCT) was found. Definition at line 2113 of file provsql.c.


|
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.
| q | Query whose setOperations is non-ALL (modified to ALL in place). |
q as a subquery RTE. Definition at line 2783 of file provsql.c.

|
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.


|
static |
Test whether funcoid is one of the random_variable_* comparison procedures, and if so return its ComparisonOperator index.
| constants | Extension OID cache. |
| funcoid | Procedure OID to test (typically OpExpr->opfuncid). |
[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.

|
static |
|
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.
| opExpr | The comparison expression from the WHERE clause. Must satisfy rv_cmp_index(opExpr->opfuncid) ≥ 0; callers are responsible for the type check. |
| constants | Extension OID cache. |
| negated | Whether the expression appears under a NOT. |
Definition at line 1297 of file provsql.c.


|
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.


|
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.


|
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.
| q | Query to modify in place. |
Definition at line 2672 of file provsql.c.

|
static |
Rewrite an EXCEPT query into a LEFT JOIN with monus provenance.
EXCEPT cannot be handled directly because it deduplicates. This function transforms:
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.
| constants | Extension OID cache. |
| q | Query to rewrite in place. |
Definition at line 3179 of file provsql.c.


|
static |
Wrap a provenance_aggregate FuncExpr with a cast to the original aggregate return type.
| prov_agg | The provenance_aggregate FuncExpr to wrap. |
| constants | Extension OID cache. |
prov_agg. Definition at line 2353 of file provsql.c.

|
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.
| constants | Extension OID cache. |
| expr | Provenance expression to wrap. |
FuncExpr applying provsql.assume_boolean to expr. Definition at line 4000 of file provsql.c.

|
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.

|
static |
|
static |
|
static |
| bool provsql_aggtoken_text_as_uuid = false |
| 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.
| 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.
|
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.
| q | The query to plan. |
| cursorOptions | Cursor options bitmask. |
| boundParams | Pre-bound parameter values. |
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.
| bool provsql_hybrid_evaluation = true |
| bool provsql_interrupted = false |
| int provsql_monte_carlo_seed = -1 |
| 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.
| 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).
| 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.
|
static |
| int provsql_verbose = 100 |