ProvSQL SQL API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Provenance table management

Functions for enabling, disabling, and configuring provenance tracking on user tables. More...

Functions

TRIGGER provsql.delete_statement_trigger ()
 Trigger function for DELETE statement provenance tracking.
VOID provsql.set_table_info (OID relid, TEXT kind, INT2[] block_key=ARRAY[]::INT2[])
 Record per-relation provenance metadata used by the safe-query optimisation.
VOID provsql.remove_table_info (OID relid)
 Remove per-relation provenance metadata.
RECORD provsql.get_table_info (OID relid, OUT TEXT kind, INT2[] &block_key)
 Read per-relation provenance metadata.
VOID provsql.set_ancestors (OID relid, OID[] ancestors=ARRAY[]::OID[])
 Record the base-relation ancestor set of a tracked relation.
VOID provsql.remove_ancestors (OID relid)
 Clear the ancestor half of a per-relation RECORD (keeps kind/block_key).
OID[] provsql.get_ancestors (OID relid)
 Read the base-relation ancestor set of a tracked relation.
TRIGGER provsql.provenance_guard ()
 BEFORE INSERT OR UPDATE OF provsql row trigger installed by add_provenance.
VOID provsql.add_provenance (REGCLASS _tbl)
 Enable provenance tracking on an existing table.
VOID provsql.remove_provenance (REGCLASS _tbl)
 Remove provenance tracking from a table.
VOID provsql.repair_key (REGCLASS _tbl, TEXT key_att)
 Set up provenance for a table with duplicate key values.
event_trigger provsql.cleanup_table_info ()
 Event trigger that purges per-table provenance metadata when a tracked relation is dropped outside of remove_provenance().
VOID provsql.create_provenance_mapping (TEXT newtbl, REGCLASS oldtbl, TEXT att, BOOL preserve_case='f')
 Create a provenance mapping table from an attribute.
VOID provsql.create_provenance_mapping_view (TEXT newview, REGCLASS oldtbl, TEXT att, BOOL preserve_case=false)
 Create a view mapping provenance tokens to attribute values.

Variables

DROP EVENT TRIGGER IF EXISTS provsql.provsql_cleanup_table_info

Detailed Description

Functions for enabling, disabling, and configuring provenance tracking on user tables.

Function Documentation

◆ add_provenance()

VOID provsql.add_provenance ( REGCLASS _tbl)

Enable provenance tracking on an existing table.

Adds a provsql UUID column to the table, an index for fast UUID-keyed lookups, and a BEFORE INSERT/UPDATE row trigger (provenance_guard) that mints a fresh uuid_generate_v4 leaf when the user omits the column on INSERT, or flips the table's metadata to OPAQUE when the user supplies their own value. Input gates for existing rows are created lazily when first referenced by a query.

Parameters
_tblthe table to add provenance tracking to
Source code
provsql.sql line 387

◆ cleanup_table_info()

CREATE EVENT TRIGGER provsql_cleanup_table_info ON sql_drop EXECUTE PROCEDURE provsql provsql.cleanup_table_info ( )

Event trigger that purges per-table provenance metadata when a tracked relation is dropped outside of remove_provenance().

EXECUTE PROCEDURE (rather than the PG 11+ EXECUTE

Plain DROP TABLE bypasses remove_provenance() and would otherwise leave a stale entry in the table-info store keyed by a now-recycled OID, with confusing consequences for the safe-query rewriter the next time the OID is reused. This trigger forwards every dropped relation OID to provsql.remove_table_info(), which is a no-op for relations that were not tracked.

Source code
provsql.sql line 577

FUNCTION alias) so the extension installs on PG 10 too.

◆ create_provenance_mapping()

VOID provsql.create_provenance_mapping ( TEXT newtbl,
REGCLASS oldtbl,
TEXT att,
BOOL preserve_case = 'f' )

Create a provenance mapping table from an attribute.

Creates a new table mapping provenance tokens to values of a given attribute, for use with semiring evaluation functions.

Parameters
newtblname of the mapping table to create
oldtblsource table with provenance tracking
attattribute whose values populate the mapping
preserve_caseif true, quote the table name to preserve case
Source code
provsql.sql line 609

◆ create_provenance_mapping_view()

VOID provsql.create_provenance_mapping_view ( TEXT newview,
REGCLASS oldtbl,
TEXT att,
BOOL preserve_case = false )

Create a view mapping provenance tokens to attribute values.

Like create_provenance_mapping but creates a view instead of a table, so it always reflects the current state of the source table.

Parameters
newviewname of the view to create
oldtblsource table with provenance tracking
attattribute whose values populate the mapping
preserve_caseif true, quote the view name to preserve case
Source code
provsql.sql line 641

◆ delete_statement_trigger()

TRIGGER provsql.delete_statement_trigger ( )

Trigger function for DELETE statement provenance tracking.

Records the deletion and applies monus to provenance tokens of deleted rows. This is the version for PostgreSQL < 14.

Source code
provsql.sql line 214

◆ get_ancestors()

OID[] provsql.get_ancestors ( OID relid)

Read the base-relation ancestor set of a tracked relation.

Returns NULL when no ancestor RECORD exists for relid (or the RECORD is empty – both cases make the safe-query rewriter take its conservative refuse path, so they collapse here).

Source code
provsql.sql line 332

◆ get_table_info()

RECORD provsql.get_table_info ( OID relid,
OUT TEXT kind,
INT2[] & block_key )

Read per-relation provenance metadata.

Returns NULL if no RECORD exists. kind is one of 'tid' / 'bid' / 'opaque'; block_key is the (possibly empty) array of block-key column numbers, only meaningful when kind = 'bid'. Used by the planner-time hierarchy detector to gate the safe-query rewrite.

Source code
provsql.sql line 291

◆ provenance_guard()

TRIGGER provsql.provenance_guard ( )

BEFORE INSERT OR UPDATE OF provsql row trigger installed by add_provenance.

Two jobs:

  1. Fill NEW.provsql with a fresh uuid_generate_v4 leaf when the user did not supply one (this replaces the column DEFAULT that add_provenance used to install: a real DEFAULT would fire before the trigger sees the row, so we could not tell "user omitted the column" from "user supplied a value").
  2. When the user does supply a non-NULL provsql on INSERT, or changes it on UPDATE, flip the table's per-table metadata to OPAQUE. The user is free to write whatever UUIDs they want (cross-table reuse, compound tokens minted via create_gate, ...); the cost is that the safe-query rewriter then refuses to fire on this table, because TID independence can no longer be assumed.
    Source code
    provsql.sql line 355

◆ remove_ancestors()

VOID provsql.remove_ancestors ( OID relid)

Clear the ancestor half of a per-relation RECORD (keeps kind/block_key).

No-op when missing.

Source code
provsql.sql line 321

◆ remove_provenance()

VOID provsql.remove_provenance ( REGCLASS _tbl)

Remove provenance tracking from a table.

Drops the provsql column and associated triggers.

Parameters
_tblthe table to remove provenance tracking from
Source code
provsql.sql line 422

◆ remove_table_info()

VOID provsql.remove_table_info ( OID relid)

Remove per-relation provenance metadata.

No-op when missing.

Source code
provsql.sql line 278

◆ repair_key()

VOID provsql.repair_key ( REGCLASS _tbl,
TEXT key_att )

Set up provenance for a table with duplicate key values.

When a table has duplicate rows for a given key, this function replaces simple input gates with multivalued input (mulinput) gates that model a uniform distribution over duplicates.

Parameters
_tblthe table to repair
key_attthe key attribute(s) as a comma-separated string, or empty string if the whole table is one group
Source code
provsql.sql line 460

◆ set_ancestors()

VOID provsql.set_ancestors ( OID relid,
OID[] ancestors = ARRAY[]::OID[] )

Record the base-relation ancestor set of a tracked relation.

Base tables created with add_provenance / repair_key carry {self}; CTAS-derived tables inherit the union of their sources' ancestor sets. The safe-query rewriter consults the registry to enforce that joined FROM entries have disjoint base ancestors before firing the read-once factoring.

The worker preserves the relation's existing kind / block_key half on update; it silently no-ops when no kind RECORD exists for relid (callers should run add_provenance / repair_key first). The ancestor list is capped at 64 entries (clear error if exceeded).

Parameters
relidpg_class OID of the relation.
ancestorsSorted, deduplicated base-relation OIDs.
Source code
provsql.sql line 314

◆ set_table_info()

VOID provsql.set_table_info ( OID relid,
TEXT kind,
INT2[] block_key = ARRAY[]::INT2[] )

Record per-relation provenance metadata used by the safe-query optimisation.

Stores a (relid, kind, block_key) RECORD in the persistent mmap-backed table-info store. kind is one of:

  • 'tid' – independent input leaves (post-add_provenance default)
  • 'bid' – block-correlated leaves; rows sharing the same value of block_key are mutually exclusive. An empty block_key means the whole table is one block.
  • 'opaque' – arbitrary correlations from a derived source (CREATE TABLE AS the safe-query rewriter must bail on these.
Parameters
relidpg_class OID of the relation.
kindOne of 'tid' / 'bid' / 'opaque'.
block_keyBlock-key column numbers (only meaningful for 'bid'; ignored otherwise but conventionally passed empty).
Source code
provsql.sql line 272

Variable Documentation

◆ provsql_cleanup_table_info

DROP EVENT TRIGGER IF EXISTS provsql.provsql_cleanup_table_info