ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_interrupt.cpp
Go to the documentation of this file.
1/**
2 * @file provsql_interrupt.cpp
3 * @brief The SIGINT handler that stops ProvSQL's evaluation loops.
4 *
5 * See @c provsql_interrupt.h for how an evaluation is cancelled.
6 */
7extern "C" {
8#include "postgres.h"
9#include "access/xact.h"
10#include "miscadmin.h"
11#include "storage/ipc.h"
12#include "storage/latch.h"
13#include "utils/memutils.h"
14#include "provsql_utils.h"
15}
16
17#include "provsql_interrupt.h"
18
19#include <csignal>
20#include <ctime>
21#include <string>
22
23#include <unistd.h>
24
25#include "process_memory.h"
26
27unsigned provsql_poll_count = 0;
28
29/** The scopes alive, and the statement they were opened in: a scope left
30 * by a longjmp (an error raised within it) is not counted past it */
31static int scope_depth = 0;
32static TimestampTz scope_statement = 0;
33
34/** The resource limit the evaluation stopped at, if any: its tag, message
35 * and hint, raised by provsql_cancel_if_interrupted */
36static bool limit_hit = false;
37static std::string limit_tag, limit_message, limit_hint;
38
39/** Resident memory at the first evaluation of the statement, and when that
40 * statement started (the budget is per statement) */
41static size_t baseline_rss = 0;
42static TimestampTz baseline_statement = 0;
43
44/** When the memory budget was last checked, in milliseconds of a coarse
45 * monotonic clock, and how long to wait before the next check */
46static long long last_memory_check = 0;
47static constexpr long long memory_check_interval = 50;
48
49/** @brief A coarse monotonic clock, in milliseconds: cheap enough to read at
50 * every poll. */
51static long long coarse_now_ms()
52{
53 struct timespec ts;
54#ifdef CLOCK_MONOTONIC_COARSE
55 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
56#else
57 clock_gettime(CLOCK_MONOTONIC, &ts);
58#endif
59 return (long long) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
60}
61
62/** @brief The resident memory of this backend, in bytes, or 0 where the
63 * platform does not say. */
64static size_t current_rss()
65{
66 return provsql_process_rss(getpid());
67}
68
69/**
70 * @brief SIGINT handler that sets the global interrupted flag.
71 *
72 * The signal number argument is required by the @c signal() API but is
73 * not used.
74 *
75 * In addition to the @c provsql_interrupted flag polled by the long
76 * evaluation loops (Monte-Carlo, possible worlds, the semiring evaluation
77 * of a circuit and its enumeration of worlds), we drive PG's
78 * standard cancel pipeline (@c InterruptPending / @c QueryCancelPending
79 * + @c SetLatch) the same way PG's own @c StatementCancelHandler does.
80 * That makes a SIGINT delivered to the backend (e.g. via
81 * @c pg_cancel_backend) outside of a @c system() wait turn into a
82 * proper 57014 cancel at the next @c CHECK_FOR_INTERRUPTS instead of
83 * being silently absorbed. (The matching case where an external
84 * compiler is running is handled in @c run_external_tool, which runs the
85 * tool in its own process group and @c SIGKILLs that group on a pending
86 * cancel, then lets @c CHECK_FOR_INTERRUPTS raise it.)
87 */
89{
91
92 if (!proc_exit_inprogress) {
93 InterruptPending = true;
94 QueryCancelPending = true;
95 }
96 SetLatch(MyLatch);
97}
98
99/** PG's SIGTERM handler (@c die), which @c provsql_sigterm_handler chains to. */
100static void (*pg_sigterm_handler)(int) = SIG_DFL;
101
102/**
103 * @brief SIGTERM: stop the evaluation, then let PG terminate the backend.
104 *
105 * A terminate request (@c pg_terminate_backend, a fast shutdown) runs PG's
106 * @c die, which only sets @c ProcDiePending for the next
107 * @c CHECK_FOR_INTERRUPTS: the C++ loops, which poll
108 * @c provsql_interrupted, would never see it. Set the flag too; once the
109 * evaluation has unwound, @c CHECK_FOR_INTERRUPTS terminates the backend.
110 */
112{
113 provsql_interrupted = true;
114 if (pg_sigterm_handler != SIG_DFL && pg_sigterm_handler != SIG_IGN)
115 pg_sigterm_handler(signo);
116}
117
119{
120 TimestampTz statement = GetCurrentStatementStartTimestamp();
121 provsql_interrupted = false;
122 limit_hit = false;
123 prev_ = signal(SIGINT, provsql_sigint_handler);
124 prev_term_ = signal(SIGTERM, provsql_sigterm_handler);
125 /* A nested scope finds our own handler installed: keep PG's. */
128 if (statement != baseline_statement) {
129 baseline_statement = statement;
131 }
132 if (statement != scope_statement) {
133 scope_statement = statement;
134 scope_depth = 0;
135 }
136 ++scope_depth;
137}
138
140{
141 if (scope_depth > 0)
142 --scope_depth;
143 /* The flag stays: the catch that follows reads it */
144 signal(SIGINT, prev_);
145 signal(SIGTERM, prev_term_);
146}
147
149{
150 return scope_depth > 0 &&
151 scope_statement == GetCurrentStatementStartTimestamp();
152}
153
155{
156 size_t rss;
157 if (baseline_rss == 0 ||
158 baseline_statement != GetCurrentStatementStartTimestamp())
159 return 0;
160 rss = current_rss();
161 return rss > baseline_rss ? rss - baseline_rss : 0;
162}
163
168
173
174void provsql_check_memory_for(size_t extra)
175{
176 if (provsql_max_memory <= 0)
177 return;
179 if (provsql_memory_used() + extra > (size_t)provsql_max_memory * 1024 * 1024)
181 "memory-limit",
182 ("the evaluation used more memory than provsql.max_memory (" +
183 std::to_string(provsql_max_memory) + " MB)").c_str(),
184 "Raise provsql.max_memory (e.g., SET provsql.max_memory = '2GB'), or "
185 "set it to 0 for no limit.");
186}
187
189{
190 return limit_hit;
191}
192
194{
195 return MaxAllocSize - VARHDRSZ;
196}
197
198void provsql_check_result_size(size_t bytes, const char *message)
199{
200 if (bytes > provsql_max_result_size())
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).");
205}
206
207void provsql_limit_exceeded(const char *tag, const char *message,
208 const char *hint)
209{
210 limit_hit = true;
211 limit_tag = tag;
212 limit_message = message;
213 limit_hint = hint;
214 throw CircuitException(message);
215}
216
218{
220 provsql_interrupted = false;
221 CHECK_FOR_INTERRUPTS();
222 }
223 if (limit_hit) {
224 /* Copied out: ereport does not return, and the strings would otherwise
225 * keep the text of this limit until the next one */
226 char *tag = pstrdup(limit_tag.c_str());
227 char *message = pstrdup(limit_message.c_str());
228 char *hint = pstrdup(limit_hint.c_str());
229 limit_hit = false;
230 ereport(ERROR,
231 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
232 errmsg("ProvSQL: %s", message),
233 errdetail("provsql-reason: %s; scope: deliberate", tag),
234 errhint("%s", hint)));
235 }
236}
Exception type thrown by circuit operations on invalid input.
Definition Circuit.h:382
void(* prev_term_)(int)
The SIGTERM handler to restore.
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.
Definition provsql.c:111
int provsql_max_memory
Memory, in MB, that the evaluations of a statement may add to the backend's resident set,...
Definition provsql.c:131
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.
Definition provsql.c:111
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.