ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_utils.h
Go to the documentation of this file.
1/**
2 * @file provsql_utils.h
3 * @brief Core types, constants, and utilities shared across ProvSQL.
4 *
5 * This header is included by virtually every source file in the
6 * extension. It provides:
7 * - The @c gate_type enumeration listing all circuit-gate kinds
8 * recognised by ProvSQL (input, semiring operations, aggregation, etc.)
9 * - The @c constants_t structure caching PostgreSQL OIDs for the types,
10 * functions, and operators that ProvSQL installs, so that OID lookups
11 * happen once per session rather than on every query.
12 * - The @c database_constants_t wrapper for per-database OID caches.
13 * - Helper declarations for OID lookup and UUID manipulation.
14 * - Global flags controlling interrupt handling, where-provenance, and
15 * verbosity.
16 * - An implicit inclusion of @c provsql_error.h for the @c provsql_error
17 * / @c provsql_warning / @c provsql_notice / @c provsql_log macros.
18 */
19#ifndef PROVSQL_UTILS_H
20#define PROVSQL_UTILS_H
21
22#include "pg_config.h" // for PG_VERSION_NUM
23#include "c.h" // for int16
24
25#include "postgres.h"
26#include "utils/uuid.h"
27
28#if PG_VERSION_NUM < 100000
29/// Number of bytes in a UUID
30#define UUID_LEN 16
31
32/** UUID structure. In versions of PostgreSQL < 10, pg_uuid_t is declared
33 * to be an opaque struct pg_uuid_t in uuid.h, so we have to give the
34 * definition of struct pg_uuid_t; this problem is resolved in PostgreSQL 10. */
36{
37 unsigned char data[UUID_LEN]; ///< Raw 16-byte UUID storage
38};
39#endif /* PG_VERSION_NUM */
40
41#include "postgres_ext.h"
42#include "nodes/pg_list.h"
43
44/** Possible gate type in the provenance circuit. */
45typedef enum gate_type {
46 gate_input, ///< Input (variable) gate of the circuit
47 gate_plus, ///< Semiring plus
48 gate_times, ///< Semiring times
49 gate_monus, ///< M-Semiring monus
50 gate_project, ///< Project gate (for where provenance)
51 gate_zero, ///< Semiring zero
52 gate_one, ///< Semiring one
53 gate_eq, ///< Equijoin gate (for where provenance)
54 gate_agg, ///< Aggregation operator (for aggregate provenance)
55 gate_semimod, ///< Semimodule scalar multiplication (for aggregate provenance)
56 gate_cmp, ///< Currently unused, meant for comparison of aggregate values
57 gate_delta, ///< δ-semiring operator (see Amsterdamer, Deutch, Tannen, PODS 2011)
58 gate_value, ///< Scalar value (for aggregate provenance)
59 gate_mulinput, ///< Multivalued input (for Boolean provenance)
60 gate_update, ///< Update operation
61 gate_invalid, ///< Invalid gate type
62 nb_gate_types ///< Total number of gate types
64
65/** Names of gate types */
66extern const char *gate_type_name[];
67
68/** Structure to store the value of various constants. This is needed to
69 * uniquely identify types, functions, etc., in PostgreSQL through their
70 * Object Identifier Types (OIDs). */
71typedef struct constants_t {
72 Oid OID_SCHEMA_PROVSQL; ///< OID of the provsql SCHEMA
73 Oid OID_TYPE_GATE_TYPE; ///< OID of the provenance_gate TYPE
74 Oid OID_TYPE_AGG_TOKEN; ///< OID of the agg_token TYPE
75 Oid OID_TYPE_UUID; ///< OID of the uuid TYPE
76 Oid OID_TYPE_UUID_ARRAY; ///< OID of the uuid[] TYPE
77 Oid OID_TYPE_BOOL; ///< OID of the BOOL TYPE
78 Oid OID_TYPE_INT; ///< OID of the INT TYPE
79 Oid OID_TYPE_INT_ARRAY; ///< OID of the INT[] TYPE
80 Oid OID_TYPE_FLOAT; ///< OID of the FLOAT TYPE
81 Oid OID_TYPE_VARCHAR; ///< OID of the VARCHAR TYPE
82 Oid OID_FUNCTION_ARRAY_AGG; ///< OID of the array_agg FUNCTION
83 Oid OID_FUNCTION_PROVENANCE_PLUS; ///< OID of the provenance_plus FUNCTION
84 Oid OID_FUNCTION_PROVENANCE_TIMES; ///< OID of the provenance_times FUNCTION
85 Oid OID_FUNCTION_PROVENANCE_MONUS; ///< OID of the provenance_monus FUNCTION
86 Oid OID_FUNCTION_PROVENANCE_PROJECT; ///< OID of the provenance_project FUNCTION
87 Oid OID_FUNCTION_PROVENANCE_EQ;///< OID of the provenance_eq FUNCTION
88 Oid OID_FUNCTION_PROVENANCE_CMP; ///< OID of the provenance_cmp FUNCTION
89 Oid OID_FUNCTION_PROVENANCE; ///< OID of the provenance FUNCTION
90 Oid GATE_TYPE_TO_OID[nb_gate_types]; ///< Array of the OID of each provenance_gate ENUM value
91 Oid OID_FUNCTION_PROVENANCE_DELTA; ///< OID of the provenance_delta FUNCTION
92 Oid OID_FUNCTION_PROVENANCE_AGGREGATE; ///< OID of the provenance_aggregate FUNCTION
93 Oid OID_FUNCTION_PROVENANCE_SEMIMOD; ///< OID of the provenance_semimod FUNCTION
94 Oid OID_FUNCTION_GATE_ZERO; ///< OID of the provenance_zero FUNCTION
95 Oid OID_FUNCTION_GATE_ONE; ///< OID of the provenance_one FUNCTION
96 Oid OID_OPERATOR_NOT_EQUAL_UUID; ///< OID of the <> operator on UUIDs FUNCTION
97 Oid OID_FUNCTION_NOT_EQUAL_UUID; ///< OID of the = operator on UUIDs FUNCTION
98 Oid OID_FUNCTION_AGG_TOKEN_UUID; ///< OID of the agg_token_uuid FUNCTION
99 bool ok; ///< true if constants were loaded
101
102
103
104
105/** Structure to store the value of various constants for a specific
106 * database. */
107typedef struct database_constants_t {
108 Oid database; ///< OID of the database these constants belong to
109 constants_t constants; ///< Cached OID constants for this database
111
112/**
113 * @brief Retrieve the cached OID constants for the current database.
114 *
115 * On first call (or after a cache miss) this function looks up the OIDs
116 * of all ProvSQL-specific types, functions, and operators in the system
117 * catalogs and stores them in a per-database cache. Subsequent calls
118 * return the cached values without touching the catalogs.
119 *
120 * @param failure_if_not_possible If @c true, call @c provsql_error when
121 * the ProvSQL schema cannot be found (e.g. the extension is not
122 * installed in the current database). If @c false, return a
123 * constants_t with @c ok==false instead of aborting.
124 * @return A @c constants_t whose @c ok field is @c true on success.
125 */
126constants_t get_constants(bool failure_if_not_possible);
127
128/**
129 * @brief Find the equality operator OID for two given types.
130 *
131 * Searches @c pg_operator for the @c = operator that accepts
132 * @p ltypeId on the left and @p rtypeId on the right.
133 *
134 * @param ltypeId OID of the left operand type.
135 * @param rtypeId OID of the right operand type.
136 * @return The operator OID, or @c InvalidOid if none is found.
137 */
138Oid find_equality_operator(Oid ltypeId, Oid rtypeId);
139
140/** Global variable that becomes true if this particular backend received
141 * an interrupt signal. */
142extern bool provsql_interrupted;
143
144/** Global variable that indicates if where-provenance support has been
145 * activated through the provsql.where_provenance run-time configuration
146 * parameter. */
147extern bool provsql_where_provenance;
148
149/** Global variable that indicates the verbosity level set by the
150 * provsql.verbose_level run-time configuration parameter was set */
151extern int provsql_verbose;
152
153#include "provsql_error.h"
154
155#endif /* PROVSQL_UTILS_H */
Uniform error-reporting macros for ProvSQL.
bool provsql_where_provenance
Global variable that indicates if where-provenance support has been activated through the provsql....
Definition provsql.c:64
int provsql_verbose
Global variable that indicates the verbosity level set by the provsql.verbose_level run-time configur...
Definition provsql.c:66
const char * gate_type_name[]
Names of gate types.
Oid find_equality_operator(Oid ltypeId, Oid rtypeId)
Find the equality operator OID for two given types.
constants_t get_constants(bool failure_if_not_possible)
Retrieve the cached OID constants for the current database.
gate_type
Possible gate type in the provenance circuit.
@ gate_update
Update operation.
@ gate_monus
M-Semiring monus.
@ gate_eq
Equijoin gate (for where provenance)
@ gate_value
Scalar value (for aggregate provenance)
@ gate_delta
δ-semiring operator (see Amsterdamer, Deutch, Tannen, PODS 2011)
@ gate_mulinput
Multivalued input (for Boolean provenance)
@ gate_one
Semiring one.
@ gate_zero
Semiring zero.
@ gate_agg
Aggregation operator (for aggregate provenance)
@ gate_project
Project gate (for where provenance)
@ gate_invalid
Invalid gate type.
@ gate_times
Semiring times.
@ gate_cmp
Currently unused, meant for comparison of aggregate values.
@ nb_gate_types
Total number of gate types.
@ gate_plus
Semiring plus.
@ gate_semimod
Semimodule scalar multiplication (for aggregate provenance)
@ gate_input
Input (variable) gate of the circuit.
#define UUID_LEN
Number of bytes in a UUID.
bool provsql_interrupted
Global variable that becomes true if this particular backend received an interrupt signal.
Definition provsql.c:62
Structure to store the value of various constants.
Oid OID_FUNCTION_PROVENANCE_EQ
OID of the provenance_eq FUNCTION.
Oid OID_FUNCTION_PROVENANCE_AGGREGATE
OID of the provenance_aggregate FUNCTION.
Oid OID_FUNCTION_PROVENANCE_SEMIMOD
OID of the provenance_semimod FUNCTION.
Oid OID_FUNCTION_PROVENANCE
OID of the provenance FUNCTION.
Oid OID_FUNCTION_AGG_TOKEN_UUID
OID of the agg_token_uuid FUNCTION.
Oid OID_TYPE_VARCHAR
OID of the VARCHAR TYPE.
Oid OID_FUNCTION_GATE_ZERO
OID of the provenance_zero FUNCTION.
Oid OID_SCHEMA_PROVSQL
OID of the provsql SCHEMA.
Oid OID_TYPE_GATE_TYPE
OID of the provenance_gate TYPE.
Oid OID_FUNCTION_PROVENANCE_PROJECT
OID of the provenance_project FUNCTION.
Oid OID_TYPE_FLOAT
OID of the FLOAT TYPE.
Oid OID_TYPE_AGG_TOKEN
OID of the agg_token TYPE.
Oid OID_FUNCTION_ARRAY_AGG
OID of the array_agg FUNCTION.
Oid OID_TYPE_INT
OID of the INT TYPE.
Oid GATE_TYPE_TO_OID[nb_gate_types]
Array of the OID of each provenance_gate ENUM value.
Oid OID_FUNCTION_PROVENANCE_PLUS
OID of the provenance_plus FUNCTION.
Oid OID_OPERATOR_NOT_EQUAL_UUID
OID of the <> operator on UUIDs FUNCTION.
Oid OID_TYPE_UUID
OID of the uuid TYPE.
bool ok
true if constants were loaded
Oid OID_TYPE_INT_ARRAY
OID of the INT[] TYPE.
Oid OID_FUNCTION_PROVENANCE_DELTA
OID of the provenance_delta FUNCTION.
Oid OID_FUNCTION_PROVENANCE_TIMES
OID of the provenance_times FUNCTION.
Oid OID_FUNCTION_PROVENANCE_MONUS
OID of the provenance_monus FUNCTION.
Oid OID_TYPE_BOOL
OID of the BOOL TYPE.
Oid OID_FUNCTION_NOT_EQUAL_UUID
OID of the = operator on UUIDs FUNCTION.
Oid OID_FUNCTION_GATE_ONE
OID of the provenance_one FUNCTION.
Oid OID_TYPE_UUID_ARRAY
OID of the uuid[] TYPE.
Oid OID_FUNCTION_PROVENANCE_CMP
OID of the provenance_cmp FUNCTION.
Structure to store the value of various constants for a specific database.
Oid database
OID of the database these constants belong to.
constants_t constants
Cached OID constants for this database.
UUID structure.
unsigned char data[UUID_LEN]
Raw 16-byte UUID storage.