![]() |
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_collation.h"#include "catalog/pg_operator.h"#include "catalog/pg_type.h"#include "nodes/makefuncs.h"#include "nodes/nodeFuncs.h"#include "nodes/print.h"#include "optimizer/planner.h"#include "parser/parse_oper.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 <time.h>#include "provsql_mmap.h"#include "provsql_shmem.h"#include "provsql_utils.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... | |
Enumerations | |
| enum | semiring_operation { SR_PLUS , SR_MONUS , SR_TIMES } |
| Semiring operation used to combine provenance tokens. 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) |
| Rewrite a single SELECT query to carry provenance. | |
| 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, reduce_varattno_mutator_context *context) |
| 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 Node * | aggregation_type_mutator (Node *node, aggregation_type_mutator_context *context) |
Tree-mutator that retyps 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 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 * | 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 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 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) |
| Build the combined provenance expression to be added to the SELECT list. | |
| 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, aggregation_mutator_context *context) |
| Tree-mutator that replaces Aggrefs with provenance-aware aggregates. | |
| 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 Node * | provenance_mutator (Node *node, provenance_mutator_context *context) |
| 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_walker (Node *node, void *data) |
Tree walker that returns true if any provenance() call is found. | |
| 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_provenance_walker (Node *node, void *data) |
| Tree walker that detects any provenance-bearing relation or provenance() call. | |
| 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, const constants_t *constants) |
| 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 | 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) |
| 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 void | migrate_aggtoken_quals_to_having (const constants_t *constants, Query *q) |
Move WHERE conditions on aggregate results (agg_token) to HAVING. | |
| static PlannedStmt * | provsql_planner (Query *q, int cursorOptions, ParamListInfo boundParams) |
| PostgreSQL planner hook — entry point for provenance rewriting. | |
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. | |
| 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. | |
| 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. | |
| static const char * | PROVSQL_COLUMN_NAME = "provsql" |
| Name of the provenance column added to tracked tables. | |
| static planner_hook_type | prev_planner = NULL |
| Previous planner hook (chained) | |
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 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).
| 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 four GUC variables (provsql.active, where_provenance, update_provenance, verbose_level), 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 2826 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. |
toExpr wrapped in provenance_eq(toExpr, col1, col2), or toExpr unchanged if the shape is unsupported. Definition at line 513 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 585 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 2269 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 2306 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 1654 of file provsql.c.


|
static |
Tree-mutator that replaces Aggrefs with provenance-aware aggregates.
| node | Current expression tree node. |
| context | Mutation context with prov_atts, op, and constants. |
Definition at line 1606 of file provsql.c.


|
static |
Tree-mutator that retyps a specific Var to agg_token.
| node | Current expression tree node. |
| context | Mutation context with varno, varattno, and constants. |
Definition at line 202 of file provsql.c.


|
static |
Tree walker that detects any Var of type agg_token.
| node | Current expression tree node. |
| constants | Extension OID cache. |
true if an agg_token Var is found in node. Definition at line 2080 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 it. Join-RTE columns and empty-named columns (used for anonymous GROUP BY keys) are assigned 0.
| 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 2444 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 1182 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 1252 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 2389 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 2415 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 2339 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 231 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_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 273 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 2103 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 2070 of file provsql.c.


|
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. Definition at line 2006 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 858 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 908 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 754 of file provsql.c.

|
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 636 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 96 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). |
| nbcols | Total number of non-provenance output columns. |
Expr to be appended to the target list. Definition at line 948 of file provsql.c.


|
static |
Move WHERE conditions on aggregate results (agg_token) to HAVING.
Supported patterns (moved to HAVING):
Unsupported patterns (e.g., "WHERE x=1 OR c>3") raise an error.
| constants | Extension OID cache. |
| q | Query to modify in place (jointree->quals and havingQual may both be updated). |
Definition at line 2499 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. |
NULL if the query has no FROM clause and can be skipped. Definition at line 2577 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. |
Definition at line 2240 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 1970 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 1945 of file provsql.c.


|
static |
Tree-mutator that replaces provenance() calls with the actual provenance expression.
| node | Current expression tree node. |
| context | Mutation context with the provenance expression and constants. |
Definition at line 1712 of file provsql.c.


|
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. |
Definition at line 2763 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 177 of file provsql.c.


|
static |
Tree-mutator callback that adjusts Var attribute numbers.
| node | Current expression tree node. |
| context | Mutation context carrying varno and offset. |
Definition at line 148 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 1795 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 1832 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 420 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 1634 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 1744 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 1360 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 1873 of file provsql.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 1762 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 2126 of file provsql.c.


|
static |
| bool provsql_active = true |
|
static |
| bool provsql_interrupted = false |
| bool provsql_update_provenance = false |
| int provsql_verbose = 100 |