Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4f2400c

Browse files
committed
Add a new shmem_request_hook hook.
Currently, preloaded libraries are expected to request additionalshared memory and LWLocks in _PG_init(). However, it is not unusalfor such requests to depend on MaxBackends, which won't beinitialized at that time. Such requests could also depend on GUCsthat other modules might change. This introduces a new hook wheremodules can safely use MaxBackends and GUCs to request additionalshared memory and LWLocks.Furthermore, this change restricts requests for shared memory andLWLocks to this hook. Previously, libraries could make requestsuntil the size of the main shared memory segment was calculated.Unlike before, we no longer silently ignore requests received atinvalid times. Instead, we FATAL if someone tries to requestadditional shared memory or LWLocks outside of the hook.Nathan Bossart and Julien RouhaudDiscussion:https://postgr.es/m/20220412210112.GA2065815%40nathanxps13Discussion:https://postgr.es/m/Yn2jE/lmDhKtkUdr@paquier.xyz
1 parent8c8d307 commit4f2400c

File tree

9 files changed

+81
-38
lines changed

9 files changed

+81
-38
lines changed

‎contrib/pg_prewarm/autoprewarm.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static void apw_start_database_worker(void);
9696
staticboolapw_init_shmem(void);
9797
staticvoidapw_detach_shmem(intcode,Datumarg);
9898
staticintapw_compare_blockinfo(constvoid*p,constvoid*q);
99+
staticvoidautoprewarm_shmem_request(void);
100+
staticshmem_request_hook_typeprev_shmem_request_hook=NULL;
99101

100102
/* Pointer to shared-memory state. */
101103
staticAutoPrewarmSharedState*apw_state=NULL;
@@ -139,13 +141,26 @@ _PG_init(void)
139141

140142
MarkGUCPrefixReserved("pg_prewarm");
141143

142-
RequestAddinShmemSpace(MAXALIGN(sizeof(AutoPrewarmSharedState)));
144+
prev_shmem_request_hook=shmem_request_hook;
145+
shmem_request_hook=autoprewarm_shmem_request;
143146

144147
/* Register autoprewarm worker, if enabled. */
145148
if (autoprewarm)
146149
apw_start_leader_worker();
147150
}
148151

152+
/*
153+
* Requests any additional shared memory required for autoprewarm.
154+
*/
155+
staticvoid
156+
autoprewarm_shmem_request(void)
157+
{
158+
if (prev_shmem_request_hook)
159+
prev_shmem_request_hook();
160+
161+
RequestAddinShmemSpace(MAXALIGN(sizeof(AutoPrewarmSharedState)));
162+
}
163+
149164
/*
150165
* Main entry point for the leader autoprewarm process. Per-database workers
151166
* have a separate entry point.

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ static intexec_nested_level = 0;
252252
staticintplan_nested_level=0;
253253

254254
/* Saved hook values in case of unload */
255+
staticshmem_request_hook_typeprev_shmem_request_hook=NULL;
255256
staticshmem_startup_hook_typeprev_shmem_startup_hook=NULL;
256257
staticpost_parse_analyze_hook_typeprev_post_parse_analyze_hook=NULL;
257258
staticplanner_hook_typeprev_planner_hook=NULL;
@@ -316,6 +317,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
316317
PG_FUNCTION_INFO_V1(pg_stat_statements);
317318
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
318319

320+
staticvoidpgss_shmem_request(void);
319321
staticvoidpgss_shmem_startup(void);
320322
staticvoidpgss_shmem_shutdown(intcode,Datumarg);
321323
staticvoidpgss_post_parse_analyze(ParseState*pstate,Query*query,
@@ -451,17 +453,11 @@ _PG_init(void)
451453

452454
MarkGUCPrefixReserved("pg_stat_statements");
453455

454-
/*
455-
* Request additional shared resources. (These are no-ops if we're not in
456-
* the postmaster process.) We'll allocate or attach to the shared
457-
* resources in pgss_shmem_startup().
458-
*/
459-
RequestAddinShmemSpace(pgss_memsize());
460-
RequestNamedLWLockTranche("pg_stat_statements",1);
461-
462456
/*
463457
* Install hooks.
464458
*/
459+
prev_shmem_request_hook=shmem_request_hook;
460+
shmem_request_hook=pgss_shmem_request;
465461
prev_shmem_startup_hook=shmem_startup_hook;
466462
shmem_startup_hook=pgss_shmem_startup;
467463
prev_post_parse_analyze_hook=post_parse_analyze_hook;
@@ -480,6 +476,20 @@ _PG_init(void)
480476
ProcessUtility_hook=pgss_ProcessUtility;
481477
}
482478

479+
/*
480+
* shmem_request hook: request additional shared resources. We'll allocate or
481+
* attach to the shared resources in pgss_shmem_startup().
482+
*/
483+
staticvoid
484+
pgss_shmem_request(void)
485+
{
486+
if (prev_shmem_request_hook)
487+
prev_shmem_request_hook();
488+
489+
RequestAddinShmemSpace(pgss_memsize());
490+
RequestNamedLWLockTranche("pg_stat_statements",1);
491+
}
492+
483493
/*
484494
* shmem_startup hook: allocate or attach to shared memory,
485495
* then load any pre-existing statistics from file.

‎doc/src/sgml/xfunc.sgml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,22 +3402,30 @@ CREATE FUNCTION make_array(anyelement) RETURNS anyarray
34023402
startup. The add-in's shared library must be preloaded by specifying
34033403
it in
34043404
<xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>.
3405+
The shared library should register a <literal>shmem_request_hook</literal>
3406+
in its <function>_PG_init</function> function. This
3407+
<literal>shmem_request_hook</literal> can reserve LWLocks or shared memory.
34053408
Shared memory is reserved by calling:
34063409
<programlisting>
34073410
void RequestAddinShmemSpace(int size)
34083411
</programlisting>
3409-
from your <function>_PG_init</function> function.
3412+
from your <literal>shmem_request_hook</literal>.
34103413
</para>
34113414
<para>
34123415
LWLocks are reserved by calling:
34133416
<programlisting>
34143417
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
34153418
</programlisting>
3416-
from<function>_PG_init</function>. This will ensure that an array of
3419+
fromyour <literal>shmem_request_hook</literal>. This will ensure that an array of
34173420
<literal>num_lwlocks</literal> LWLocks is available under the name
34183421
<literal>tranche_name</literal>. Use <function>GetNamedLWLockTranche</function>
34193422
to get a pointer to this array.
34203423
</para>
3424+
<para>
3425+
An example of a <literal>shmem_request_hook</literal> can be found in
3426+
<filename>contrib/pg_stat_statements/pg_stat_statements.c</filename> in the
3427+
<productname>PostgreSQL</productname> source tree.
3428+
</para>
34213429
<para>
34223430
To avoid possible race-conditions, each backend should use the LWLock
34233431
<function>AddinShmemInitLock</function> when connecting to and initializing

‎src/backend/postmaster/postmaster.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,11 @@ PostmasterMain(int argc, char *argv[])
10421042
*/
10431043
InitializeMaxBackends();
10441044

1045+
/*
1046+
* Give preloaded libraries a chance to request additional shared memory.
1047+
*/
1048+
process_shmem_requests();
1049+
10451050
/*
10461051
* Now that loadable modules have had their chance to request additional
10471052
* shared memory, determine the value of any runtime-computed GUCs that

‎src/backend/storage/ipc/ipci.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,21 @@ intshared_memory_type = DEFAULT_SHARED_MEMORY_TYPE;
5555
shmem_startup_hook_typeshmem_startup_hook=NULL;
5656

5757
staticSizetotal_addin_request=0;
58-
staticbooladdin_request_allowed= true;
59-
6058

6159
/*
6260
* RequestAddinShmemSpace
6361
*Request that extra shmem space be allocated for use by
6462
*a loadable module.
6563
*
66-
* This is only useful if called from the _PG_init hook of a library that
67-
* is loaded into the postmaster via shared_preload_libraries. Once
68-
* shared memory has been allocated, calls will be ignored. (We could
69-
* raise an error, but it seems better to make it a no-op, so that
70-
* libraries containing such calls can be reloaded if needed.)
64+
* This may only be called via the shmem_request_hook of a library that is
65+
* loaded into the postmaster via shared_preload_libraries. Calls from
66+
* elsewhere will fail.
7167
*/
7268
void
7369
RequestAddinShmemSpace(Sizesize)
7470
{
75-
if (IsUnderPostmaster|| !addin_request_allowed)
76-
return;/* too late */
71+
if (!process_shmem_requests_in_progress)
72+
elog(FATAL,"cannot request additional shared memory outside shmem_request_hook");
7773
total_addin_request=add_size(total_addin_request,size);
7874
}
7975

@@ -83,9 +79,6 @@ RequestAddinShmemSpace(Size size)
8379
*
8480
* If num_semaphores is not NULL, it will be set to the number of semaphores
8581
* required.
86-
*
87-
* Note that this function freezes the additional shared memory request size
88-
* from loadable modules.
8982
*/
9083
Size
9184
CalculateShmemSize(int*num_semaphores)
@@ -152,8 +145,7 @@ CalculateShmemSize(int *num_semaphores)
152145
size=add_size(size,ShmemBackendArraySize());
153146
#endif
154147

155-
/* freeze the addin request size and include it */
156-
addin_request_allowed= false;
148+
/* include additional requested shmem from preload libraries */
157149
size=add_size(size,total_addin_request);
158150

159151
/* might as well round it off to a multiple of a typical page size */

‎src/backend/storage/lmgr/lwlock.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ intNamedLWLockTrancheRequests = 0;
243243
/* points to data in shared memory: */
244244
NamedLWLockTranche*NamedLWLockTrancheArray=NULL;
245245

246-
staticboollock_named_request_allowed= true;
247-
248246
staticvoidInitializeLWLocks(void);
249247
staticinlinevoidLWLockReportWaitStart(LWLock*lock);
250248
staticinlinevoidLWLockReportWaitEnd(void);
@@ -458,9 +456,6 @@ LWLockShmemSize(void)
458456
for (i=0;i<NamedLWLockTrancheRequests;i++)
459457
size=add_size(size,strlen(NamedLWLockTrancheRequestArray[i].tranche_name)+1);
460458

461-
/* Disallow adding any more named tranches. */
462-
lock_named_request_allowed= false;
463-
464459
returnsize;
465460
}
466461

@@ -691,12 +686,9 @@ LWLockRegisterTranche(int tranche_id, const char *tranche_name)
691686
*Request that extra LWLocks be allocated during postmaster
692687
*startup.
693688
*
694-
* This is only useful for extensions if called from the _PG_init hook
695-
* of a library that is loaded into the postmaster via
696-
* shared_preload_libraries. Once shared memory has been allocated, calls
697-
* will be ignored. (We could raise an error, but it seems better to make
698-
* it a no-op, so that libraries containing such calls can be reloaded if
699-
* needed.)
689+
* This may only be called via the shmem_request_hook of a library that is
690+
* loaded into the postmaster via shared_preload_libraries. Calls from
691+
* elsewhere will fail.
700692
*
701693
* The tranche name will be user-visible as a wait event name, so try to
702694
* use a name that fits the style for those.
@@ -706,8 +698,8 @@ RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
706698
{
707699
NamedLWLockTrancheRequest*request;
708700

709-
if (IsUnderPostmaster|| !lock_named_request_allowed)
710-
return;/* too late */
701+
if (!process_shmem_requests_in_progress)
702+
elog(FATAL,"cannot request additional LWLocks outside shmem_request_hook");
711703

712704
if (NamedLWLockTrancheRequestArray==NULL)
713705
{

‎src/backend/utils/init/miscinit.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,9 @@ char *local_preload_libraries_string = NULL;
16181618
boolprocess_shared_preload_libraries_in_progress= false;
16191619
boolprocess_shared_preload_libraries_done= false;
16201620

1621+
shmem_request_hook_typeshmem_request_hook=NULL;
1622+
boolprocess_shmem_requests_in_progress= false;
1623+
16211624
/*
16221625
* load the shared libraries listed in 'libraries'
16231626
*
@@ -1701,6 +1704,18 @@ process_session_preload_libraries(void)
17011704
true);
17021705
}
17031706

1707+
/*
1708+
* process any shared memory requests from preloaded libraries
1709+
*/
1710+
void
1711+
process_shmem_requests(void)
1712+
{
1713+
process_shmem_requests_in_progress= true;
1714+
if (shmem_request_hook)
1715+
shmem_request_hook();
1716+
process_shmem_requests_in_progress= false;
1717+
}
1718+
17041719
void
17051720
pg_bindtextdomain(constchar*domain)
17061721
{

‎src/include/miscadmin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ extern void BaseInit(void);
465465
externPGDLLIMPORTboolIgnoreSystemIndexes;
466466
externPGDLLIMPORTboolprocess_shared_preload_libraries_in_progress;
467467
externPGDLLIMPORTboolprocess_shared_preload_libraries_done;
468+
externPGDLLIMPORTboolprocess_shmem_requests_in_progress;
468469
externPGDLLIMPORTchar*session_preload_libraries_string;
469470
externPGDLLIMPORTchar*shared_preload_libraries_string;
470471
externPGDLLIMPORTchar*local_preload_libraries_string;
@@ -478,9 +479,13 @@ extern bool RecheckDataDirLockFile(void);
478479
externvoidValidatePgVersion(constchar*path);
479480
externvoidprocess_shared_preload_libraries(void);
480481
externvoidprocess_session_preload_libraries(void);
482+
externvoidprocess_shmem_requests(void);
481483
externvoidpg_bindtextdomain(constchar*domain);
482484
externboolhas_rolreplication(Oidroleid);
483485

486+
typedefvoid (*shmem_request_hook_type) (void);
487+
externPGDLLIMPORTshmem_request_hook_typeshmem_request_hook;
488+
484489
/* in executor/nodeHash.c */
485490
externsize_tget_hash_memory_limit(void);
486491

‎src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,6 +3651,7 @@ shm_mq_result
36513651
shm_toc
36523652
shm_toc_entry
36533653
shm_toc_estimator
3654+
shmem_request_hook_type
36543655
shmem_startup_hook_type
36553656
sig_atomic_t
36563657
sigjmp_buf

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp