ProvSQL SQL API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Update provenance (PostgreSQL 14+)

Installation-time advisory: if provsql is not in the database's default search_path, point the user at setup_search_path(). More...

Types

TYPE  update_provenance
 Table recording the history of INSERT, UPDATE, DELETE, and UNDO operations. More...

Functions

UUID transaction_token ()
 The update gate standing for the current transaction.
trigger stamp_commit_time ()
 Deferred trigger stamping a log row with its commit time.
DO TRIGGER insert_statement_trigger ()
 Trigger function for INSERT statement provenance tracking.
TRIGGER update_statement_trigger ()
 Trigger function for UPDATE statement provenance tracking.

Detailed Description

Installation-time advisory: if provsql is not in the database's default search_path, point the user at setup_search_path().

reset_val reflects the configured session default (postgresql.conf / ALTER DATABASE / ALTER ROLE), unaffected by the SET search_path statements this script ran. CREATE EXTENSION raises client_min_messages to WARNING for the duration of the script, so we lower it around the RAISE NOTICE. SET LOCAL only: it unwinds by itself when CREATE EXTENSION's transaction ends. An explicit save/restore here would capture the WARNING clamp (already in force when this block runs) and restore that at session level, leaving the whole installing session with NOTICEs suppressed. Final constants-cache refresh. The planned SELECT statements earlier in this script (reset_constants_cache itself, the zero/one create_gate calls) make the installing session memoize the OID constants mid-script, while objects defined later (notably the choose aggregate, used by the scalar-subquery decorrelation) do not exist yet. Their optional lookups then stay InvalidOid for the rest of the session, silently disabling the corresponding rewrites (e.g. IN/NOT IN over a tracked relation would raise "Subqueries ... not supported") until a new connection. Refreshing here, after every object exists, repairs the installing session's cache.

Extended provenance tracking for INSERT, UPDATE, DELETE, and UNDO operations, including temporal validity ranges.

Function Documentation

◆ insert_statement_trigger()

DO TRIGGER insert_statement_trigger ( )

Trigger function for INSERT statement provenance tracking.

Records the insertion in update_provenance and multiplies provenance tokens of inserted rows with the insert token.

Source code
provsql.sql line 12232

◆ stamp_commit_time()

trigger stamp_commit_time ( )

Deferred trigger stamping a log row with its commit time.

CURRENT_TIMESTAMP is the transaction's start time, so two overlapping transactions can commit in the opposite order of the validity they recorded. This fires at commit – it is a constraint trigger declared DEFERRABLE INITIALLY DEFERRED – and moves the row's TIMESTAMP and the lower bound of its validity to clock_timestamp(), which by then is the commit time to within the commit itself.

Source code
provsql.sql line 12069

◆ transaction_token()

UUID transaction_token ( )

The update gate standing for the current transaction.

Each data-modification statement already mints an update gate of its own, but nothing tied the statements of one transaction together: a reader of update_provenance could not tell that two rows came from the same transaction, and undo() could reverse a statement but not "the transaction". This mints one gate per transaction, on the first tracked modification, and hands the same one back for the rest of it.

Where a transaction's gate lives is SET LOCAL, so it vanishes when the transaction ends, whether it commits or rolls back – and a rolled-back transaction leaves no update_provenance row for it either, since that row is an ordinary heap insert.

Source code
provsql.sql line 12020

◆ update_statement_trigger()

TRIGGER update_statement_trigger ( )

Trigger function for UPDATE statement provenance tracking.

Records the update in update_provenance. Multiplies new-row tokens with the update token and applies monus to old-row tokens.

Source code
provsql.sql line 12289