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

OID lookup, constants cache, and utility functions for ProvSQL. More...

#include "postgres.h"
#include "access/htup_details.h"
#include "miscadmin.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "catalog/pg_enum.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "fmgr.h"
#include "nodes/value.h"
#include "parser/parse_func.h"
#include "utils/syscache.h"
#include "utils/lsyscache.h"
#include "provsql_utils.h"
Include dependency graph for provsql_utils.c:

Go to the source code of this file.

Macros

#define CheckOid(o)
 
#define GET_GATE_TYPE_OID(x)
 

Functions

static Oid binary_oper_exact (List *opname, Oid arg1, Oid arg2)
 Look up an exactly matching binary operator OID.
 
Oid find_equality_operator (Oid ltypeId, Oid rtypeId)
 Find the equality operator OID for two given types.
 
static Oid get_func_oid (char *s)
 Return the OID of a globally qualified function named s.
 
static Oid get_provsql_func_oid (char *s)
 Return the OID of a provsql-schema function named s.
 
static void OperatorGet (const char *operatorName, Oid operatorNamespace, Oid leftObjectId, Oid rightObjectId, Oid *operatorObjectId, Oid *functionObjectId)
 Retrieve operator and function OIDs for a named operator.
 
static Oid get_enum_oid (Oid enumtypoid, const char *label)
 Return the OID of a specific enum label within an enum type.
 
static constants_t initialize_constants (bool failure_if_not_possible)
 Query the system catalogs to populate a fresh constants_t.
 
constants_t get_constants (bool failure_if_not_possible)
 Retrieve the cached OID constants for the current database.
 
Datum reset_constants_cache (PG_FUNCTION_ARGS)
 SQL function to invalidate the OID constants cache.
 

Variables

const char * gate_type_name []
 Names of gate types.
 
database_constants_tconstants_cache
 Per-database OID constants cache (sorted by database OID)
 
unsigned constants_cache_len =0
 Number of valid entries in constants_cache.
 

Detailed Description

OID lookup, constants cache, and utility functions for ProvSQL.

Implements the functions declared in provsql_utils.h:

The constants cache is a sorted, dynamically-grown array of database_constants_t records (one per PostgreSQL database OID) stored in process-local memory and searched with binary search. The reset_constants_cache() SQL function forces a cache invalidation for the current database, which is needed after ALTER EXTENSION.

Several helper functions (get_func_oid, get_provsql_func_oid, OperatorGet, get_enum_oid, binary_oper_exact) are adapted from PostgreSQL source code that is not exported as a public API.

Definition in file provsql_utils.c.

Macro Definition Documentation

◆ CheckOid

#define CheckOid (   o)
Value:
if(constants.o==InvalidOid) { \
if(failure_if_not_possible) \
provsql_error("Could not initialize provsql constants"); \
else \
return constants; }

◆ GET_GATE_TYPE_OID

#define GET_GATE_TYPE_OID (   x)
Value:
{ \
constants.GATE_TYPE_TO_OID[gate_ ## x] = get_enum_oid( \
constants.OID_TYPE_GATE_TYPE, \
#x); \
if(constants.GATE_TYPE_TO_OID[gate_ ## x]==InvalidOid) \
provsql_error("Could not initialize provsql gate type " #x); }
static Oid get_enum_oid(Oid enumtypoid, const char *label)
Return the OID of a specific enum label within an enum type.

Function Documentation

◆ binary_oper_exact()

static Oid binary_oper_exact ( List *  opname,
Oid  arg1,
Oid  arg2 
)
static

Look up an exactly matching binary operator OID.

Copied and adapted from parse_oper.c (PostgreSQL internals, not exported). Returns InvalidOid if no exact match exists.

Parameters
opnameQualified operator name (a List of String nodes).
arg1OID of the left operand type.
arg2OID of the right operand type.
Returns
OID of the matching operator, or InvalidOid.

Definition at line 69 of file provsql_utils.c.

Here is the caller graph for this function:

◆ find_equality_operator()

Oid find_equality_operator ( Oid  ltypeId,
Oid  rtypeId 
)

Find the equality operator OID for two given types.

Searches pg_operator for the = operator that accepts ltypeId on the left and rtypeId on the right.

Parameters
ltypeIdOID of the left operand type.
rtypeIdOID of the right operand type.
Returns
The operator OID, or InvalidOid if none is found.

Definition at line 109 of file provsql_utils.c.

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

◆ get_constants()

constants_t get_constants ( bool  failure_if_not_possible)

Retrieve the cached OID constants for the current database.

On first call (or after a cache miss) this function looks up the OIDs of all ProvSQL-specific types, functions, and operators in the system catalogs and stores them in a per-database cache. Subsequent calls return the cached values without touching the catalogs.

Parameters
failure_if_not_possibleIf true, call provsql_error when the ProvSQL schema cannot be found (e.g. the extension is not installed in the current database). If false, return a constants_t with ok==false instead of aborting.
Returns
A constants_t whose ok field is true on success.

Definition at line 425 of file provsql_utils.c.

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

◆ get_enum_oid()

static Oid get_enum_oid ( Oid  enumtypoid,
const char *  label 
)
static

Return the OID of a specific enum label within an enum type.

Parameters
enumtypoidOID of the enum type (e.g. provenance_gate).
labelC-string label of the enum value to look up.
Returns
OID of the enum label's pg_enum row.

Definition at line 253 of file provsql_utils.c.

◆ get_func_oid()

static Oid get_func_oid ( char *  s)
static

Return the OID of a globally qualified function named s.

Looks up the function in the default search path. Returns 0 if no matching function is found.

Parameters
sFunction name (unqualified).
Returns
OID of the function, or 0 if not found.

Definition at line 149 of file provsql_utils.c.

Here is the caller graph for this function:

◆ get_provsql_func_oid()

static Oid get_provsql_func_oid ( char *  s)
static

Return the OID of a provsql-schema function named s.

Looks up the function in the provsql schema. Returns 0 if not found.

Parameters
sFunction name (without schema prefix).
Returns
OID of the function, or 0 if not found.

Definition at line 175 of file provsql_utils.c.

Here is the caller graph for this function:

◆ initialize_constants()

static constants_t initialize_constants ( bool  failure_if_not_possible)
static

Query the system catalogs to populate a fresh constants_t.

Performs all OID lookups required by ProvSQL in a single pass through the system caches. The CheckOid() macro aborts (or returns early, depending on failure_if_not_possible) if any OID resolves to InvalidOid.

Parameters
failure_if_not_possibleIf true, raise a provsql_error when any OID cannot be resolved. If false, return a constants_t with ok==false instead.
Returns
Fully populated constants_t on success, or ok==false on failure when failure_if_not_possible is false.

Abort or return early if OID field o of constants is invalid.

Look up the OID of provenance_gate enum value x and store it in constants.

Definition at line 288 of file provsql_utils.c.

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

◆ OperatorGet()

static void OperatorGet ( const char *  operatorName,
Oid  operatorNamespace,
Oid  leftObjectId,
Oid  rightObjectId,
Oid *  operatorObjectId,
Oid *  functionObjectId 
)
static

Retrieve operator and function OIDs for a named operator.

Copied and adapted from pg_operator.c (PostgreSQL internals, not exported). Looks up the operator by name, namespace, and operand types in the system cache.

Parameters
operatorNameOperator symbol string (e.g. "<>").
operatorNamespaceOID of the schema containing the operator.
leftObjectIdOID of the left operand type.
rightObjectIdOID of the right operand type.
operatorObjectIdOutput: OID of the operator, or 0 if not found.
functionObjectIdOutput: OID of the underlying function, or 0.

Definition at line 207 of file provsql_utils.c.

Here is the caller graph for this function:

◆ reset_constants_cache()

Datum reset_constants_cache ( PG_FUNCTION_ARGS  )

SQL function to invalidate the OID constants cache.

Forces a fresh OID lookup for the current database on the next call to get_constants(). Must be called after ALTER EXTENSION provsql UPDATE to ensure cached OIDs are refreshed.

Returns
Void datum.

Definition at line 466 of file provsql_utils.c.

Here is the call graph for this function:

Variable Documentation

◆ constants_cache

database_constants_t* constants_cache

Per-database OID constants cache (sorted by database OID)

Definition at line 422 of file provsql_utils.c.

◆ constants_cache_len

unsigned constants_cache_len =0

Number of valid entries in constants_cache.

Definition at line 423 of file provsql_utils.c.

◆ gate_type_name

const char* gate_type_name[]
Initial value:
= {
"input",
"plus",
"times",
"monus",
"project",
"zero",
"one",
"eq",
"agg",
"semimod",
"cmp",
"delta",
"value",
"mulinput",
"update",
"invalid"
}

Names of gate types.

Definition at line 38 of file provsql_utils.c.