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

The SIGINT handler that stops ProvSQL's evaluation loops. More...

#include "postgres.h"
#include "access/xact.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "utils/memutils.h"
#include "provsql_utils.h"
#include "provsql_interrupt.h"
#include <csignal>
#include <ctime>
#include <string>
#include <unistd.h>
#include "process_memory.h"
Include dependency graph for provsql_interrupt.cpp:

Go to the source code of this file.

Functions

static long long coarse_now_ms ()
 A coarse monotonic clock, in milliseconds: cheap enough to read at every poll.
static size_t current_rss ()
 The resident memory of this backend, in bytes, or 0 where the platform does not say.
void provsql_sigint_handler (int)
 SIGINT handler that sets the global interrupted flag.
void provsql_sigterm_handler (int signo)
 SIGTERM: stop the evaluation, then let PG terminate the backend.
bool provsql_in_evaluation (void)
 Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by longjmp would skip C++ frames.
size_t provsql_memory_used (void)
 How much the backend's resident memory has grown since the first evaluation of the statement, in bytes (0 before it).
bool provsql_memory_check_due (void)
 Whether the last check of the memory budget is old enough for another, whatever the number of polls since.
void provsql_check_memory (void)
 Throw if the evaluations of the statement have made the backend's resident memory grow by more than provsql.max_memory.
void provsql_check_memory_for (size_t extra)
 Throw if extra more bytes would take the evaluations of the statement past provsql.max_memory: before a large copy, such as that of a result into a PostgreSQL value.
bool provsql_limit_pending (void)
 Whether the evaluation stopped at a resource limit (provsql_limit_exceeded), not yet raised: a catch that would otherwise carry on must let it through.
size_t provsql_max_result_size (void)
 The size of the largest text value PostgreSQL holds, in bytes.
void provsql_check_result_size (size_t bytes, const char *message)
 Throw if a text value of bytes is past what PostgreSQL can hold in a value (1 GB), with message as error: a rendering of a circuit, which writes out a sub-circuit shared by several gates at each of them, can grow exponentially with the circuit.
void provsql_limit_exceeded (const char *tag, const char *message, const char *hint)
 Stop the evaluation at a resource limit (provsql.max_memory, provsql.max_worlds): throws, like a cancel, after recording the limit, which provsql_cancel_if_interrupted then raises as a program_limit_exceeded error (54000) with tag on its DETAIL line and hint as its HINT, instead of an internal error.
void provsql_cancel_if_interrupted (void)
 Raise PG's cancel if the evaluation stopped for one; nothing otherwise.

Variables

unsigned provsql_poll_count = 0
 Polls since the last check of the memory budget.
static int scope_depth = 0
 The scopes alive, and the statement they were opened in: a scope left by a longjmp (an error raised within it) is not counted past it.
static TimestampTz scope_statement = 0
static bool limit_hit = false
 The resource limit the evaluation stopped at, if any: its tag, message and hint, raised by provsql_cancel_if_interrupted.
static std::string limit_tag
static std::string limit_message
static std::string limit_hint
static size_t baseline_rss = 0
 Resident memory at the first evaluation of the statement, and when that statement started (the budget is per statement).
static TimestampTz baseline_statement = 0
static long long last_memory_check = 0
 When the memory budget was last checked, in milliseconds of a coarse monotonic clock, and how long to wait before the next check.
static constexpr long long memory_check_interval = 50
static void(* pg_sigterm_handler )(int) = SIG_DFL
 PG's SIGTERM handler (die), which provsql_sigterm_handler chains to.

Detailed Description

The SIGINT handler that stops ProvSQL's evaluation loops.

See provsql_interrupt.h for how an evaluation is cancelled.

Definition in file provsql_interrupt.cpp.

Function Documentation

◆ coarse_now_ms()

long long coarse_now_ms ( )
static

A coarse monotonic clock, in milliseconds: cheap enough to read at every poll.

Definition at line 51 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ current_rss()

size_t current_rss ( )
static

The resident memory of this backend, in bytes, or 0 where the platform does not say.

Definition at line 64 of file provsql_interrupt.cpp.

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

◆ provsql_cancel_if_interrupted()

void provsql_cancel_if_interrupted ( void )

Raise PG's cancel if the evaluation stopped for one; nothing otherwise.

Called in a catch, once the scope is gone.

Definition at line 217 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_check_memory()

void provsql_check_memory ( void )

Throw if the evaluations of the statement have made the backend's resident memory grow by more than provsql.max_memory.

Definition at line 169 of file provsql_interrupt.cpp.

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

◆ provsql_check_memory_for()

void provsql_check_memory_for ( size_t extra)

Throw if extra more bytes would take the evaluations of the statement past provsql.max_memory: before a large copy, such as that of a result into a PostgreSQL value.

Definition at line 174 of file provsql_interrupt.cpp.

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

◆ provsql_check_result_size()

void provsql_check_result_size ( size_t bytes,
const char * message )

Throw if a text value of bytes is past what PostgreSQL can hold in a value (1 GB), with message as error: a rendering of a circuit, which writes out a sub-circuit shared by several gates at each of them, can grow exponentially with the circuit.

Definition at line 198 of file provsql_interrupt.cpp.

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

◆ provsql_in_evaluation()

bool provsql_in_evaluation ( void )

Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by longjmp would skip C++ frames.

Definition at line 148 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_limit_exceeded()

void provsql_limit_exceeded ( const char * tag,
const char * message,
const char * hint )

Stop the evaluation at a resource limit (provsql.max_memory, provsql.max_worlds): throws, like a cancel, after recording the limit, which provsql_cancel_if_interrupted then raises as a program_limit_exceeded error (54000) with tag on its DETAIL line and hint as its HINT, instead of an internal error.

Definition at line 207 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_limit_pending()

bool provsql_limit_pending ( void )

Whether the evaluation stopped at a resource limit (provsql_limit_exceeded), not yet raised: a catch that would otherwise carry on must let it through.

Definition at line 188 of file provsql_interrupt.cpp.

◆ provsql_max_result_size()

size_t provsql_max_result_size ( void )

The size of the largest text value PostgreSQL holds, in bytes.

Definition at line 193 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_memory_check_due()

bool provsql_memory_check_due ( void )

Whether the last check of the memory budget is old enough for another, whatever the number of polls since.

Definition at line 164 of file provsql_interrupt.cpp.

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

◆ provsql_memory_used()

size_t provsql_memory_used ( void )

How much the backend's resident memory has grown since the first evaluation of the statement, in bytes (0 before it).

An external tool's memory is counted against the same budget, added to this.

Definition at line 154 of file provsql_interrupt.cpp.

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

◆ provsql_sigint_handler()

void provsql_sigint_handler ( int )

SIGINT handler that sets the global interrupted flag.

SIGINT handler setting provsql_interrupted, and PG's cancel flags as PG's own StatementCancelHandler does.

The signal number argument is required by the signal() API but is not used.

In addition to the provsql_interrupted flag polled by the long evaluation loops (Monte-Carlo, possible worlds, the semiring evaluation of a circuit and its enumeration of worlds), we drive PG's standard cancel pipeline (InterruptPending / QueryCancelPending

  • SetLatch) the same way PG's own StatementCancelHandler does. That makes a SIGINT delivered to the backend (e.g. via pg_cancel_backend) outside of a system() wait turn into a proper 57014 cancel at the next CHECK_FOR_INTERRUPTS instead of being silently absorbed. (The matching case where an external compiler is running is handled in run_external_tool, which runs the tool in its own process group and SIGKILLs that group on a pending cancel, then lets CHECK_FOR_INTERRUPTS raise it.)

Definition at line 88 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

◆ provsql_sigterm_handler()

void provsql_sigterm_handler ( int signo)

SIGTERM: stop the evaluation, then let PG terminate the backend.

SIGTERM handler setting provsql_interrupted, then running PG's own (die), so that a terminated backend stops its evaluation as a cancelled one does.

A terminate request (pg_terminate_backend, a fast shutdown) runs PG's die, which only sets ProcDiePending for the next CHECK_FOR_INTERRUPTS: the C++ loops, which poll provsql_interrupted, would never see it. Set the flag too; once the evaluation has unwound, CHECK_FOR_INTERRUPTS terminates the backend.

Definition at line 111 of file provsql_interrupt.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ baseline_rss

size_t baseline_rss = 0
static

Resident memory at the first evaluation of the statement, and when that statement started (the budget is per statement).

Definition at line 41 of file provsql_interrupt.cpp.

◆ baseline_statement

TimestampTz baseline_statement = 0
static

Definition at line 42 of file provsql_interrupt.cpp.

◆ last_memory_check

long long last_memory_check = 0
static

When the memory budget was last checked, in milliseconds of a coarse monotonic clock, and how long to wait before the next check.

Definition at line 46 of file provsql_interrupt.cpp.

◆ limit_hint

std::string limit_hint
static

Definition at line 37 of file provsql_interrupt.cpp.

◆ limit_hit

bool limit_hit = false
static

The resource limit the evaluation stopped at, if any: its tag, message and hint, raised by provsql_cancel_if_interrupted.

Definition at line 36 of file provsql_interrupt.cpp.

◆ limit_message

std::string limit_message
static

Definition at line 37 of file provsql_interrupt.cpp.

◆ limit_tag

std::string limit_tag
static

Definition at line 37 of file provsql_interrupt.cpp.

◆ memory_check_interval

long long memory_check_interval = 50
staticconstexpr

Definition at line 47 of file provsql_interrupt.cpp.

◆ pg_sigterm_handler

void(* pg_sigterm_handler) (int) ( int ) = SIG_DFL
static

PG's SIGTERM handler (die), which provsql_sigterm_handler chains to.

Definition at line 100 of file provsql_interrupt.cpp.

◆ provsql_poll_count

unsigned provsql_poll_count = 0

Polls since the last check of the memory budget.

Definition at line 27 of file provsql_interrupt.cpp.

◆ scope_depth

int scope_depth = 0
static

The scopes alive, and the statement they were opened in: a scope left by a longjmp (an error raised within it) is not counted past it.

Definition at line 31 of file provsql_interrupt.cpp.

◆ scope_statement

TimestampTz scope_statement = 0
static

Definition at line 32 of file provsql_interrupt.cpp.