3232#include "access/xact.h"
3333#include "catalog/pg_class.h"
3434#include "catalog/pg_type.h"
35- #include "miscadmin.h"
3635#include "pgstat.h"
3736#include "postmaster/bgworker.h"
3837#include "postmaster/interrupt.h"
3938#include "storage/buf_internals.h"
4039#include "storage/dsm.h"
40+ #include "storage/dsm_registry.h"
4141#include "storage/fd.h"
4242#include "storage/ipc.h"
4343#include "storage/latch.h"
@@ -95,8 +95,6 @@ static void apw_start_database_worker(void);
9595static bool apw_init_shmem (void );
9696static void apw_detach_shmem (int code , Datum arg );
9797static int apw_compare_blockinfo (const void * p , const void * q );
98- static void autoprewarm_shmem_request (void );
99- static shmem_request_hook_type prev_shmem_request_hook = NULL ;
10098
10199/* Pointer to shared-memory state. */
102100static AutoPrewarmSharedState * apw_state = NULL ;
@@ -140,26 +138,11 @@ _PG_init(void)
140138
141139 MarkGUCPrefixReserved ("pg_prewarm" );
142140
143- prev_shmem_request_hook = shmem_request_hook ;
144- shmem_request_hook = autoprewarm_shmem_request ;
145-
146141 /* Register autoprewarm worker, if enabled. */
147142 if (autoprewarm )
148143 apw_start_leader_worker ();
149144}
150145
151- /*
152- * Requests any additional shared memory required for autoprewarm.
153- */
154- static void
155- autoprewarm_shmem_request (void )
156- {
157- if (prev_shmem_request_hook )
158- prev_shmem_request_hook ();
159-
160- RequestAddinShmemSpace (MAXALIGN (sizeof (AutoPrewarmSharedState )));
161- }
162-
163146/*
164147 * Main entry point for the leader autoprewarm process. Per-database workers
165148 * have a separate entry point.
@@ -767,6 +750,16 @@ autoprewarm_dump_now(PG_FUNCTION_ARGS)
767750 PG_RETURN_INT64 ((int64 ) num_blocks );
768751}
769752
753+ static void
754+ apw_init_state (void * ptr )
755+ {
756+ AutoPrewarmSharedState * state = (AutoPrewarmSharedState * ) ptr ;
757+
758+ LWLockInitialize (& state -> lock , LWLockNewTrancheId ());
759+ state -> bgworker_pid = InvalidPid ;
760+ state -> pid_using_dumpfile = InvalidPid ;
761+ }
762+
770763/*
771764 * Allocate and initialize autoprewarm related shared memory, if not already
772765 * done, and set up backend-local pointer to that state. Returns true if an
@@ -777,19 +770,10 @@ apw_init_shmem(void)
777770{
778771 bool found ;
779772
780- LWLockAcquire (AddinShmemInitLock , LW_EXCLUSIVE );
781- apw_state = ShmemInitStruct ("autoprewarm" ,
782- sizeof (AutoPrewarmSharedState ),
783- & found );
784- if (!found )
785- {
786- /* First time through ... */
787- LWLockInitialize (& apw_state -> lock , LWLockNewTrancheId ());
788- apw_state -> bgworker_pid = InvalidPid ;
789- apw_state -> pid_using_dumpfile = InvalidPid ;
790- }
791- LWLockRelease (AddinShmemInitLock );
792-
773+ apw_state = GetNamedDSMSegment ("autoprewarm" ,
774+ sizeof (AutoPrewarmSharedState ),
775+ apw_init_state ,
776+ & found );
793777 LWLockRegisterTranche (apw_state -> lock .tranche , "autoprewarm" );
794778
795779 return found ;
0 commit comments