|
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 | + * 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 | + |
59 | 100 | typedef enum JobLockLifetime
|
60 | 101 | {
|
61 | 102 | SESSION_LOCK = 0,
|
@@ -1141,6 +1182,13 @@ ts_bgw_job_entrypoint(PG_FUNCTION_ARGS)
|
1141 | 1182 | pqsignal(SIGTERM, die);
|
1142 | 1183 | BackgroundWorkerUnblockSignals();
|
1143 | 1184 |
|
| 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 | + |
1144 | 1192 | BackgroundWorkerInitializeConnectionByOid(db_oid, params.user_oid, 0);
|
1145 | 1193 |
|
1146 | 1194 | log_min_messages = ts_guc_bgw_log_level;
|
|
0 commit comments