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

Cancellation of the long C++ evaluation loops. More...

#include "Circuit.h"
Include dependency graph for provsql_interrupt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  provsql_interrupt_scope
 The SIGINT handler installed for the lifetime of the object, the previous one restored as it ends (a C++ exception included). More...

Functions

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.
void provsql_sigint_handler (int)
 SIGINT handler setting provsql_interrupted, and PG's cancel flags as PG's own StatementCancelHandler does.
void provsql_sigterm_handler (int)
 SIGTERM handler setting provsql_interrupted, then running PG's own (die), so that a terminated backend stops its evaluation as a cancelled one does.
void provsql_cancel_if_interrupted (void)
 Raise PG's cancel if the evaluation stopped for one; nothing otherwise.
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_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_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.
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.
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).
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_poll_interrupt ()
 Throw where an evaluation is to stop, its query cancelled.

Variables

bool provsql_interrupted
 Set by provsql_sigint_handler: this backend received a cancel.
int provsql_max_memory
 provsql.max_memory, in MB (0: no limit).
unsigned provsql_poll_count
 Polls since the last check of the memory budget.

Detailed Description

Cancellation of the long C++ evaluation loops.

A query cancel or a statement_timeout reaches the backend as a SIGINT. While an evaluation runs, the entry point installs provsql_sigint_handler, which sets provsql_interrupted (and PG's own cancel flags); the loops poll it with provsql_poll_interrupt and throw, unwinding the C++ stack (and freeing what it holds) instead of a longjmp through it. The entry point restores the previous handler, then lets CHECK_FOR_INTERRUPTS raise PG's native cancel (57014).

An entry point does so as

try {
provsql_interrupt_scope interrupt_scope;
...
} catch (const std::exception &e) {
provsql_error("...: %s", e.what());
}
The SIGINT handler installed for the lifetime of the object, the previous one restored as it ends (a ...
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
void provsql_cancel_if_interrupted(void)
Raise PG's cancel if the evaluation stopped for one; nothing otherwise.

the scope restoring the handler as the try is left.

The same polls enforce provsql.max_memory: every few thousand of them, and at least every few tens of milliseconds (a loop may take few, costly steps), the backend's resident memory is compared with what it was at the first evaluation of the statement, and past the budget the evaluation stops with an error, as it does on a cancel. The handler is defined in provsql_interrupt.cpp.

Definition in file provsql_interrupt.h.

Function Documentation

◆ 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.

◆ 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_poll_interrupt()

void provsql_poll_interrupt ( )
inline

Throw where an evaluation is to stop, its query cancelled.

Definition at line 144 of file provsql_interrupt.h.

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 setting provsql_interrupted, and PG's cancel flags as PG's own StatementCancelHandler does.

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 handler setting provsql_interrupted, then running PG's own (die), so that a terminated backend stops its evaluation as a cancelled one does.

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

◆ provsql_interrupted

bool provsql_interrupted
extern

Set by provsql_sigint_handler: this backend received a cancel.

Definition at line 111 of file provsql.c.

◆ provsql_max_memory

int provsql_max_memory
extern

provsql.max_memory, in MB (0: no limit).

provsql.max_memory, in MB (0: no limit).

Definition at line 131 of file provsql.c.

◆ provsql_poll_count

unsigned provsql_poll_count
extern

Polls since the last check of the memory budget.

Definition at line 27 of file provsql_interrupt.cpp.