Skip to content

Commit 4b698ab

Browse files
committed
Add callback to mem_guard if loaded
Add code to call mem_guard to enable memory tracking for background workers. This is enabled for all background workers started by timescaledb, but not to other workers started by PostgreSQL.
1 parent 9115e12 commit 4b698ab

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/bgw/job.c

+48
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,47 @@
5656
static scheduler_test_hook_type scheduler_test_hook = NULL;
5757
static char *job_entrypoint_function_name = "ts_bgw_job_entrypoint";
5858

59+
/*
60+
* This is copied from mem_guard and have to be the same as the type in
61+
* mem_guard.
62+
*
63+
* These are intended as an interim solution and will be removed once we have
64+
* a stable plugin ABI for TimescaleDB.
65+
*/
66+
67+
typedef void (*mg_toggle_allocation_blocking)(bool enable);
68+
typedef size_t (*mg_get_allocated_memory)();
69+
typedef size_t (*mg_get_total_allocated_memory)();
70+
typedef bool (*mg_enabled)();
71+
72+
typedef struct MGCallbacks
73+
{
74+
int64 version_num;
75+
mg_toggle_allocation_blocking toggle_allocation_blocking;
76+
mg_get_allocated_memory get_allocated_memory;
77+
mg_get_total_allocated_memory get_total_allocated_memory;
78+
mg_enabled enabled;
79+
} MGCallbacks;
80+
81+
/*
82+
* Get the mem_guard callbacks.
83+
*
84+
* You might get a NULL pointer back if there are no mem_guard installed, so
85+
* check before using.
86+
*/
87+
static MGCallbacks *
88+
get_mem_guard_callbacks()
89+
{
90+
static MGCallbacks **mem_guard_callback_ptr = NULL;
91+
92+
if (mem_guard_callback_ptr)
93+
return *mem_guard_callback_ptr;
94+
95+
mem_guard_callback_ptr = (MGCallbacks **) find_rendezvous_variable("mg_callbacks");
96+
97+
return *mem_guard_callback_ptr;
98+
}
99+
59100
typedef enum JobLockLifetime
60101
{
61102
SESSION_LOCK = 0,
@@ -1141,6 +1182,13 @@ ts_bgw_job_entrypoint(PG_FUNCTION_ARGS)
11411182
pqsignal(SIGTERM, die);
11421183
BackgroundWorkerUnblockSignals();
11431184

1185+
/*
1186+
* Set up mem_guard before starting to allocate memory.
1187+
*/
1188+
MGCallbacks *callbacks = get_mem_guard_callbacks();
1189+
if (callbacks && callbacks->toggle_allocation_blocking && !callbacks->enabled)
1190+
callbacks->toggle_allocation_blocking(/*enable=*/true);
1191+
11441192
BackgroundWorkerInitializeConnectionByOid(db_oid, params.user_oid, 0);
11451193

11461194
log_min_messages = ts_guc_bgw_log_level;

0 commit comments

Comments
 (0)