ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
MMappedTableInfo.h
Go to the documentation of this file.
1/**
2 * @file MMappedTableInfo.h
3 * @brief Per-relation provenance metadata: the in-memory record.
4 *
5 * A @c ProvenanceTableInfo describes one relation ProvSQL tracks, and
6 * feeds the safe-query optimisation: the planner-time hierarchy detector
7 * needs to know whether each base relation is TID (independent leaves;
8 * the default after @c add_provenance) or BID (block-correlated leaves;
9 * produced by @c repair_key) before it can decide whether a query is safe
10 * to rewrite into read-once form.
11 *
12 * The records live in the @c provsql.table_info heap table
13 * (@c table_info.c), where metadata about relations belongs: every change
14 * follows the transaction that made it, and @c pg_dump carries them.
15 * This struct is what the C side reads them into.
16 *
17 * Before ProvSQL 1.13.0 they lived in a fifth mmap-backed file,
18 * @c provsql_table_info.mmap, laid out as a fixed-stride vector of this
19 * struct behind the usual 16-byte ProvSQL header.
20 * @c provsql.migrate_table_info() (@c TableInfoMigrate.cpp) still reads
21 * that layout, which is why the field order below must not change while
22 * any installation may still have such a file to import.
23 */
24#ifndef MMAPPED_TABLE_INFO_H
25#define MMAPPED_TABLE_INFO_H
26
27#ifdef __cplusplus
28#include <cstdint>
29#else
30#include <stdint.h>
31#endif
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36#include "postgres.h"
37#include "access/attnum.h"
38#ifdef __cplusplus
39}
40#endif
41
42/**
43 * @brief Cap on the number of block-key columns recorded per relation.
44 *
45 * BID tables (produced by @c repair_key) can have multi-column keys.
46 * We store the column numbers inline in a fixed-size array so each
47 * record is fixed-stride and @c MMappedVector can back the file
48 * directly. Sixteen is generous in practice – provenance-tracked
49 * tables rarely use composite keys wider than a handful of columns.
50 * @c repair_key raises a clear error if a wider key is requested.
51 */
52#define PROVSQL_TABLE_INFO_MAX_BLOCK_KEY 16
53
54/**
55 * @brief Cap on the number of base ancestors recorded per relation.
56 *
57 * The base-ancestor set lists the @c pg_class OIDs of the original
58 * @c add_provenance / @c repair_key relations a derived (CTAS / @c
59 * SELECT @c INTO / @c CREATE @c MATERIALIZED @c VIEW) relation's
60 * provenance ultimately reads from. Base tables carry @c {self}; the
61 * safe-query rewriter consults the set to enforce that joined FROM
62 * entries have disjoint base ancestors before firing the read-once
63 * factoring. Sixty-four covers practical CTAS workloads (typical
64 * derivations span 1-10 sources); set-ancestors raises a clear error
65 * if a wider set is requested, in which case the relation should be
66 * left untracked (the safe-query rewriter will then refuse it on the
67 * missing-ancestry conservative path).
68 */
69#define PROVSQL_TABLE_INFO_MAX_ANCESTORS 64
70
71/**
72 * @brief How the provenance leaves of a tracked relation are correlated.
73 *
74 * Three cases need distinguishing for the safe-query rewriter:
75 *
76 * - @c PROVSQL_TABLE_TID -- independent input leaves; the
77 * post-@c add_provenance default. Each row's provenance token is a
78 * fresh @c gate_input with its own probability.
79 * - @c PROVSQL_TABLE_BID -- block-correlated leaves produced by
80 * @c repair_key. Rows sharing the same value of @c block_key are
81 * mutually exclusive (they originate from a single block
82 * @c gate_input via @c gate_mulinput children). An empty
83 * @c block_key means the whole table is one block.
84 * - @c PROVSQL_TABLE_OPAQUE -- correlations are unknown. Used for
85 * relations whose provenance is derived from a tracked source via
86 * @c CREATE @c TABLE @c AS @c SELECT, @c INSERT @c INTO @c SELECT,
87 * or @c UPDATE under @c provsql.update_provenance. The safe-query
88 * rewriter must bail on these.
89 *
90 * Stored as @c uint8_t in @c ProvenanceTableInfo so the on-disk size
91 * matches the previous @c bool field exactly.
92 *
93 * @warning ON-DISK ABI: these integer values are persisted in
94 * @c provsql_table_info.mmap. Do not reorder or renumber existing
95 * members; new kinds must be appended.
96 */
102
103/**
104 * @brief Per-relation metadata for the safe-query optimisation.
105 *
106 * One record per provenance-tracked relation. @c relid is the
107 * @c pg_class OID of the relation and acts as the primary key during
108 * linear lookup. @c kind discriminates between TID, BID, and OPAQUE
109 * (see @c provsql_table_kind). For BID, @c block_key[0..block_key_n-1]
110 * lists the column numbers whose tuples partition the table into
111 * mutually-exclusive blocks; an empty key means the whole table is
112 * one block. @c block_key is left empty for TID and OPAQUE.
113 * @c ancestors[0..ancestor_n-1] lists the @c pg_class OIDs of the
114 * original @c add_provenance / @c repair_key base relations this
115 * relation's atoms ultimately come from (a sorted, deduplicated set).
116 * Base tables have @c ancestor_n @c == @c 1 with @c ancestors[0]
117 * @c == @c relid; CTAS-derived tables inherit the union of their
118 * sources' ancestor sets. @c ancestor_n @c == @c 0 means the
119 * registry has no information for this relation -- the safe-query
120 * rewriter then conservatively refuses to fire when ancestry-based
121 * disjointness is required.
122 */
123typedef struct ProvenanceTableInfo {
124 Oid relid; ///< pg_class OID of the relation (primary key)
125 uint8_t kind; ///< One of @c provsql_table_kind
126 uint16_t block_key_n; ///< Number of valid entries in @c block_key
127 AttrNumber block_key[PROVSQL_TABLE_INFO_MAX_BLOCK_KEY]; ///< Block-key column numbers
128 uint16_t ancestor_n; ///< Number of valid entries in @c ancestors (0 = no registry info)
129 Oid ancestors[PROVSQL_TABLE_INFO_MAX_ANCESTORS]; ///< Sorted, deduplicated base-relation OIDs
131
132#endif /* MMAPPED_TABLE_INFO_H */
#define PROVSQL_TABLE_INFO_MAX_BLOCK_KEY
Cap on the number of block-key columns recorded per relation.
provsql_table_kind
How the provenance leaves of a tracked relation are correlated.
@ PROVSQL_TABLE_TID
@ PROVSQL_TABLE_BID
@ PROVSQL_TABLE_OPAQUE
#define PROVSQL_TABLE_INFO_MAX_ANCESTORS
Cap on the number of base ancestors recorded per relation.
Per-relation metadata for the safe-query optimisation.
Oid relid
pg_class OID of the relation (primary key)
AttrNumber block_key[PROVSQL_TABLE_INFO_MAX_BLOCK_KEY]
Block-key column numbers.
uint16_t block_key_n
Number of valid entries in block_key.
Oid ancestors[PROVSQL_TABLE_INFO_MAX_ANCESTORS]
Sorted, deduplicated base-relation OIDs.
uint8_t kind
One of provsql_table_kind.
uint16_t ancestor_n
Number of valid entries in ancestors (0 = no registry info).