File tree 3 files changed +57
-0
lines changed
3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ Implements: #7788 Add callback to mem_guard for background workers
Original file line number Diff line number Diff line change 56
56
static scheduler_test_hook_type scheduler_test_hook = NULL ;
57
57
static char * job_entrypoint_function_name = "ts_bgw_job_entrypoint" ;
58
58
59
+ /*
60
+ * Get the mem_guard callbacks.
61
+ *
62
+ * You might get a NULL pointer back if there are no mem_guard installed, so
63
+ * check before using.
64
+ */
65
+ MGCallbacks *
66
+ ts_get_mem_guard_callbacks (void )
67
+ {
68
+ static MGCallbacks * * mem_guard_callback_ptr = NULL ;
69
+
70
+ if (mem_guard_callback_ptr )
71
+ return * mem_guard_callback_ptr ;
72
+
73
+ mem_guard_callback_ptr = (MGCallbacks * * ) find_rendezvous_variable (MG_CALLBACKS_VAR_NAME );
74
+
75
+ return * mem_guard_callback_ptr ;
76
+ }
77
+
59
78
typedef enum JobLockLifetime
60
79
{
61
80
SESSION_LOCK = 0 ,
@@ -1141,6 +1160,16 @@ ts_bgw_job_entrypoint(PG_FUNCTION_ARGS)
1141
1160
pqsignal (SIGTERM , die );
1142
1161
BackgroundWorkerUnblockSignals ();
1143
1162
1163
+ /*
1164
+ * Set up mem_guard before starting to allocate (any significant amounts
1165
+ * of) memory but after we have unblocked signals since we have no control
1166
+ * over how the callback behaves.
1167
+ */
1168
+ MGCallbacks * callbacks = ts_get_mem_guard_callbacks ();
1169
+ if (callbacks && callbacks -> version_num == MG_CALLBACKS_VERSION &&
1170
+ callbacks -> toggle_allocation_blocking && !callbacks -> enabled )
1171
+ callbacks -> toggle_allocation_blocking (/*enable=*/ true);
1172
+
1144
1173
BackgroundWorkerInitializeConnectionByOid (db_oid , params .user_oid , 0 );
1145
1174
1146
1175
log_min_messages = ts_guc_bgw_log_level ;
Original file line number Diff line number Diff line change 15
15
#define TELEMETRY_INITIAL_NUM_RUNS 12
16
16
#define SCHEDULER_APPNAME "TimescaleDB Background Worker Scheduler"
17
17
18
+ /*
19
+ * This is copied from mem_guard and have to be the same as the type in
20
+ * mem_guard.
21
+ *
22
+ * These are intended as an interim solution and will be removed once we have
23
+ * a stable plugin ABI for TimescaleDB.
24
+ */
25
+
26
+ #define MG_CALLBACKS_VERSION 1
27
+ #define MG_CALLBACKS_VAR_NAME "mg_callbacks"
28
+
29
+ typedef void (* mg_toggle_allocation_blocking )(bool enable );
30
+ typedef size_t (* mg_get_allocated_memory )();
31
+ typedef size_t (* mg_get_total_allocated_memory )();
32
+ typedef bool (* mg_enabled )();
33
+
34
+ typedef struct MGCallbacks
35
+ {
36
+ int64 version_num ;
37
+ mg_toggle_allocation_blocking toggle_allocation_blocking ;
38
+ mg_get_allocated_memory get_allocated_memory ;
39
+ mg_get_total_allocated_memory get_total_allocated_memory ;
40
+ mg_enabled enabled ;
41
+ } MGCallbacks ;
42
+
18
43
typedef struct BgwJobHistory
19
44
{
20
45
int64 id ;
@@ -85,3 +110,5 @@ ScanTupleResult ts_bgw_job_change_owner(TupleInfo *ti, void *data);
85
110
86
111
extern TSDLLEXPORT Oid ts_bgw_job_get_funcid (BgwJob * job );
87
112
extern TSDLLEXPORT const char * ts_bgw_job_function_call_string (BgwJob * job );
113
+
114
+ extern TSDLLEXPORT MGCallbacks * ts_get_mem_guard_callbacks (void );
You can’t perform that action at this time.
0 commit comments