9#include "access/xact.h"
11#include "storage/ipc.h"
12#include "storage/latch.h"
13#include "utils/memutils.h"
54#ifdef CLOCK_MONOTONIC_COARSE
55 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
57 clock_gettime(CLOCK_MONOTONIC, &ts);
59 return (
long long) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
92 if (!proc_exit_inprogress) {
93 InterruptPending =
true;
94 QueryCancelPending =
true;
120 TimestampTz statement = GetCurrentStatementStartTimestamp();
144 signal(SIGINT,
prev_);
182 (
"the evaluation used more memory than provsql.max_memory (" +
184 "Raise provsql.max_memory (e.g., SET provsql.max_memory = '2GB'), or "
185 "set it to 0 for no limit.");
195 return MaxAllocSize - VARHDRSZ;
202 "result-too-large", message,
203 "Evaluate a smaller part of the circuit, or a semiring whose values do "
204 "not grow with it (e.g., sr_boolean or sr_counting).");
221 CHECK_FOR_INTERRUPTS();
231 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
232 errmsg(
"ProvSQL: %s", message),
233 errdetail(
"provsql-reason: %s; scope: deliberate", tag),
234 errhint(
"%s", hint)));
Exception type thrown by circuit operations on invalid input.
provsql_interrupt_scope()
void(* prev_term_)(int)
The SIGTERM handler to restore.
~provsql_interrupt_scope()
void(* prev_)(int)
The SIGINT handler to restore.
std::size_t provsql_process_rss(pid_t pid)
The resident memory of process pid, in bytes, or 0 where the platform does not say.
Resident memory of a process, or of a process group.
bool provsql_interrupted
Set by provsql_sigint_handler: this backend received a cancel.
int provsql_max_memory
Memory, in MB, that the evaluations of a statement may add to the backend's resident set,...
static long long coarse_now_ms()
A coarse monotonic clock, in milliseconds: cheap enough to read at every poll.
static size_t baseline_rss
Resident memory at the first evaluation of the statement, and when that statement started (the budget...
static TimestampTz baseline_statement
static TimestampTz scope_statement
void provsql_sigterm_handler(int signo)
SIGTERM: stop the evaluation, then let PG terminate the backend.
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),...
bool provsql_memory_check_due(void)
Whether the last check of the memory budget is old enough for another, whatever the number of polls s...
static size_t current_rss()
The resident memory of this backend, in bytes, or 0 where the platform does not say.
static constexpr long long memory_check_interval
void provsql_sigint_handler(int)
SIGINT handler that sets the global interrupted flag.
static std::string limit_tag
bool provsql_limit_pending(void)
Whether the evaluation stopped at a resource limit (provsql_limit_exceeded), not yet raised: a catch ...
void provsql_cancel_if_interrupted(void)
Raise PG's cancel if the evaluation stopped for one; nothing otherwise.
static bool limit_hit
The resource limit the evaluation stopped at, if any: its tag, message and hint, raised by provsql_ca...
static long long last_memory_check
When the memory budget was last checked, in milliseconds of a coarse monotonic clock,...
static std::string limit_hint
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,...
static std::string limit_message
static int scope_depth
The scopes alive, and the statement they were opened in: a scope left by a longjmp (an error raised w...
void provsql_check_memory_for(size_t extra)
Throw if extra more bytes would take the evaluations of the statement past provsql....
size_t provsql_memory_used(void)
How much the backend's resident memory has grown since the first evaluation of the statement,...
size_t provsql_max_result_size(void)
The size of the largest text value PostgreSQL holds, in bytes.
unsigned provsql_poll_count
Polls since the last check of the memory budget.
bool provsql_in_evaluation(void)
Whether a provsql_interrupt_scope is alive: within a ProvSQL evaluation, where an error raised by lon...
static void(* pg_sigterm_handler)(int)
PG's SIGTERM handler (die), which provsql_sigterm_handler chains to.
void provsql_check_memory(void)
Throw if the evaluations of the statement have made the backend's resident memory grow by more than p...
Cancellation of the long C++ evaluation loops.
void provsql_sigint_handler(int)
SIGINT handler setting provsql_interrupted, and PG's cancel flags as PG's own StatementCancelHandler ...
bool provsql_interrupted
Set by provsql_sigint_handler: this backend received a cancel.
void provsql_sigterm_handler(int)
SIGTERM handler setting provsql_interrupted, then running PG's own (die), so that a terminated backen...
Core types, constants, and utilities shared across ProvSQL.