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

Heap-backed storage for per-relation provenance metadata. More...

#include "postgres.h"
#include "access/htup_details.h"
#include "access/heapam.h"
#include "access/genam.h"
#include "access/skey.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "funcapi.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "provsql_utils.h"
#include "MMappedTableInfo.h"
Include dependency graph for table_info.c:

Go to the source code of this file.

Macros

#define table_open(r, l)
#define table_close(r, l)
#define PROVSQL_TABLE_INFO_ATT_RELID   1
 Column numbers of provsql.table_info.
#define PROVSQL_TABLE_INFO_ATT_KIND   2
#define PROVSQL_TABLE_INFO_ATT_BLOCK_KEY   3
#define PROVSQL_TABLE_INFO_ATT_ANCESTORS   4

Functions

static Oid provsql_table_info_relation (Oid *index_oid)
 Resolve provsql.table_info and its primary-key index.
static bool provsql_read_table_info (Oid relid, ProvenanceTableInfo *out)
 Read the row of relid into out.
bool provsql_fetch_table_info (Oid relid, ProvenanceTableInfo *out)
 Raw IPC fetch (no cache).
bool provsql_fetch_ancestry (Oid relid, uint16 *ancestor_n_out, Oid *ancestors_out)
 Raw IPC fetch for the ancestry half (no cache).
static void provsql_table_info_exec (const char *sql, int nargs, Oid *argtypes, Datum *values)
 Run sql with nargs bound parameters through SPI.
static uint8_t parse_table_kind (const char *label)
 Translate a SQL-side kind label into the persisted enum value.
static const char * table_kind_label (uint8_t kind)
 Inverse of parse_table_kind for use by get_table_info.
Datum set_table_info (PG_FUNCTION_ARGS)
 Upsert the kind half of a relation's provsql.table_info row.
Datum remove_table_info (PG_FUNCTION_ARGS)
 Delete a relation's provsql.table_info row.
Datum set_ancestors (PG_FUNCTION_ARGS)
 Replace the ancestor half of a relation's row, keeping its kind / block_key.
Datum remove_ancestors (PG_FUNCTION_ARGS)
 Clear a relation's ancestor set, keeping kind / block_key.
Datum get_table_info (PG_FUNCTION_ARGS)
 PostgreSQL-callable wrapper around the cached kind lookup.
Datum get_ancestors (PG_FUNCTION_ARGS)
 PostgreSQL-callable wrapper around the cached ancestry lookup.
Datum provsql_table_info_invalidate (PG_FUNCTION_ARGS)
 Row trigger on provsql.table_info: broadcast a relcache invalidation for the relation whose metadata changed.

Detailed Description

Heap-backed storage for per-relation provenance metadata.

The provsql.table_info table holds one row per relation ProvSQL tracks: its TID / BID / OPAQUE classification, the block-key columns of a BID relation, and the base relations its atoms come from. This file implements the SQL entry points that read and write it (set_table_info, remove_table_info, get_table_info, set_ancestors, remove_ancestors, get_ancestors) and the uncached C fetchers behind the planner-hot-path caches in provsql_utils.c.

Metadata about relations is catalog-shaped data, so the heap is its natural home: every change follows the transaction that made it, a concurrent session sees it only once it commits, and pg_dump carries it (the table is registered with pg_extension_config_dump). The circuit store proper holds only the circuit.

Reads go through systable_beginscan rather than SPI: the planner hook consults them for every provenance-tracked range-table entry, and running a full query through the planner from inside the planner hook is both slow and needlessly re-entrant. Writes, which happen once per add_provenance / repair_key / guard-trigger fire, use SPI for brevity.

The provsql_table_info_invalidate row trigger on the table broadcasts a relcache invalidation for each changed relation, which is what drops the stale entry from every backend's cache – including after a direct UPDATE on the table or a pg_restore that loads it with COPY.

Definition in file table_info.c.

Macro Definition Documentation

◆ PROVSQL_TABLE_INFO_ATT_ANCESTORS

#define PROVSQL_TABLE_INFO_ATT_ANCESTORS   4

Definition at line 67 of file table_info.c.

◆ PROVSQL_TABLE_INFO_ATT_BLOCK_KEY

#define PROVSQL_TABLE_INFO_ATT_BLOCK_KEY   3

Definition at line 66 of file table_info.c.

◆ PROVSQL_TABLE_INFO_ATT_KIND

#define PROVSQL_TABLE_INFO_ATT_KIND   2

Definition at line 65 of file table_info.c.

◆ PROVSQL_TABLE_INFO_ATT_RELID

#define PROVSQL_TABLE_INFO_ATT_RELID   1

Column numbers of provsql.table_info.

Definition at line 64 of file table_info.c.

◆ table_close

#define table_close ( r,
l )
Value:
heap_close((r), (l))

Definition at line 42 of file table_info.c.

◆ table_open

#define table_open ( r,
l )
Value:
heap_open((r), (l))

Definition at line 41 of file table_info.c.

Function Documentation

◆ get_ancestors()

Datum get_ancestors ( PG_FUNCTION_ARGS )

PostgreSQL-callable wrapper around the cached ancestry lookup.

Returns NULL when no row exists for relid, or its ancestor set is empty; otherwise an oid[] listing the base-relation OIDs.

Definition at line 420 of file table_info.c.

Here is the call graph for this function:

◆ get_table_info()

Datum get_table_info ( PG_FUNCTION_ARGS )

PostgreSQL-callable wrapper around the cached kind lookup.

Returns NULL when no row exists for relid; otherwise a record (kind text, block_key int2[]). Goes through provsql_lookup_table_info so repeated calls in the same session do not re-scan the table.

Definition at line 379 of file table_info.c.

Here is the call graph for this function:

◆ parse_table_kind()

uint8_t parse_table_kind ( const char * label)
static

Translate a SQL-side kind label into the persisted enum value.

Definition at line 211 of file table_info.c.

Here is the caller graph for this function:

◆ provsql_fetch_ancestry()

bool provsql_fetch_ancestry ( Oid relid,
uint16 * ancestor_n_out,
Oid * ancestors_out )

Raw IPC fetch for the ancestry half (no cache).

Implementation detail of provsql_lookup_ancestry, exposed so the cache layer in provsql_utils.c can reach it. Callers in the planner hot path should go through provsql_lookup_ancestry.

Definition at line 180 of file table_info.c.

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

◆ provsql_fetch_table_info()

bool provsql_fetch_table_info ( Oid relid,
ProvenanceTableInfo * out )

Raw IPC fetch (no cache).

Implementation detail of provsql_lookup_table_info, exposed only so the cache layer in provsql_utils.c can reach it. Callers in the planner hot path should go through provsql_lookup_table_info.

Definition at line 175 of file table_info.c.

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

◆ provsql_read_table_info()

bool provsql_read_table_info ( Oid relid,
ProvenanceTableInfo * out )
static

Read the row of relid into out.

Returns
true when a row exists; out is then fully populated (both the kind and the ancestor halves).

Definition at line 99 of file table_info.c.

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

◆ provsql_table_info_exec()

void provsql_table_info_exec ( const char * sql,
int nargs,
Oid * argtypes,
Datum * values )
static

Run sql with nargs bound parameters through SPI.

Definition at line 197 of file table_info.c.

Here is the caller graph for this function:

◆ provsql_table_info_invalidate()

Datum provsql_table_info_invalidate ( PG_FUNCTION_ARGS )

Row trigger on provsql.table_info: broadcast a relcache invalidation for the relation whose metadata changed.

Each backend caches the metadata of the relations its queries touch (provsql_lookup_table_info / provsql_lookup_ancestry) and drops an entry when PostgreSQL invalidates that relation's relcache entry. Putting the broadcast in a trigger rather than in the setters covers every writer: the setters, a hand-written UPDATE on the table, and the COPY a pg_restore performs.

The pg_class probe skips relations that are already gone, which is the normal case for the DELETE the sql_drop event trigger performs.

Definition at line 462 of file table_info.c.

◆ provsql_table_info_relation()

Oid provsql_table_info_relation ( Oid * index_oid)
static

Resolve provsql.table_info and its primary-key index.

Returns InvalidOid (and leaves *index_oid untouched) when the table does not exist – which is the normal state while CREATE EXTENSION runs, and on an installation still on an extension version that predates it. Every caller then behaves as "no metadata recorded", the conservative direction.

Definition at line 78 of file table_info.c.

Here is the caller graph for this function:

◆ remove_ancestors()

Datum remove_ancestors ( PG_FUNCTION_ARGS )

Clear a relation's ancestor set, keeping kind / block_key.

Definition at line 353 of file table_info.c.

Here is the call graph for this function:

◆ remove_table_info()

Datum remove_table_info ( PG_FUNCTION_ARGS )

Delete a relation's provsql.table_info row.

No-op when absent.

Definition at line 290 of file table_info.c.

Here is the call graph for this function:

◆ set_ancestors()

Datum set_ancestors ( PG_FUNCTION_ARGS )

Replace the ancestor half of a relation's row, keeping its kind / block_key.

Silently no-op when relid has no row 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 315 of file table_info.c.

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

◆ set_table_info()

Datum set_table_info ( PG_FUNCTION_ARGS )

Upsert the kind half of a relation's provsql.table_info row.

Forward declaration of the C SQL entry points.

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'. The relation's existing ancestors are preserved.

Definition at line 244 of file table_info.c.

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

◆ table_kind_label()

const char * table_kind_label ( uint8_t kind)
static

Inverse of parse_table_kind for use by get_table_info.

Definition at line 222 of file table_info.c.

Here is the caller graph for this function: