mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Add a new shmem_request_hook hook.
Currently, preloaded libraries are expected to request additional shared memory and LWLocks in _PG_init(). However, it is not unusal for such requests to depend on MaxBackends, which won't be initialized at that time. Such requests could also depend on GUCs that other modules might change. This introduces a new hook where modules can safely use MaxBackends and GUCs to request additional shared memory and LWLocks. Furthermore, this change restricts requests for shared memory and LWLocks to this hook. Previously, libraries could make requests until the size of the main shared memory segment was calculated. Unlike before, we no longer silently ignore requests received at invalid times. Instead, we FATAL if someone tries to request additional shared memory or LWLocks outside of the hook. Nathan Bossart and Julien Rouhaud Discussion: https://postgr.es/m/20220412210112.GA2065815%40nathanxps13 Discussion: https://postgr.es/m/Yn2jE/lmDhKtkUdr@paquier.xyz
This commit is contained in:
@ -252,6 +252,7 @@ static int exec_nested_level = 0;
|
||||
static int plan_nested_level = 0;
|
||||
|
||||
/* Saved hook values in case of unload */
|
||||
static shmem_request_hook_type prev_shmem_request_hook = NULL;
|
||||
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
|
||||
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
|
||||
static planner_hook_type prev_planner_hook = NULL;
|
||||
@ -316,6 +317,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
|
||||
|
||||
static void pgss_shmem_request(void);
|
||||
static void pgss_shmem_startup(void);
|
||||
static void pgss_shmem_shutdown(int code, Datum arg);
|
||||
static void pgss_post_parse_analyze(ParseState *pstate, Query *query,
|
||||
@ -451,17 +453,11 @@ _PG_init(void)
|
||||
|
||||
MarkGUCPrefixReserved("pg_stat_statements");
|
||||
|
||||
/*
|
||||
* Request additional shared resources. (These are no-ops if we're not in
|
||||
* the postmaster process.) We'll allocate or attach to the shared
|
||||
* resources in pgss_shmem_startup().
|
||||
*/
|
||||
RequestAddinShmemSpace(pgss_memsize());
|
||||
RequestNamedLWLockTranche("pg_stat_statements", 1);
|
||||
|
||||
/*
|
||||
* Install hooks.
|
||||
*/
|
||||
prev_shmem_request_hook = shmem_request_hook;
|
||||
shmem_request_hook = pgss_shmem_request;
|
||||
prev_shmem_startup_hook = shmem_startup_hook;
|
||||
shmem_startup_hook = pgss_shmem_startup;
|
||||
prev_post_parse_analyze_hook = post_parse_analyze_hook;
|
||||
@ -480,6 +476,20 @@ _PG_init(void)
|
||||
ProcessUtility_hook = pgss_ProcessUtility;
|
||||
}
|
||||
|
||||
/*
|
||||
* shmem_request hook: request additional shared resources. We'll allocate or
|
||||
* attach to the shared resources in pgss_shmem_startup().
|
||||
*/
|
||||
static void
|
||||
pgss_shmem_request(void)
|
||||
{
|
||||
if (prev_shmem_request_hook)
|
||||
prev_shmem_request_hook();
|
||||
|
||||
RequestAddinShmemSpace(pgss_memsize());
|
||||
RequestNamedLWLockTranche("pg_stat_statements", 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* shmem_startup hook: allocate or attach to shared memory,
|
||||
* then load any pre-existing statistics from file.
|
||||
|
Reference in New Issue
Block a user