Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abb0b4f

Browse files
Teach autoprewarm to use the dynamic shared memory registry.
Besides showcasing the DSM registry, this prevents pg_prewarm from stealing from the main shared memory segment's extra buffer space when autoprewarm_start_worker() and autoprewarm_dump_now() are used without loading the module via shared_preload_libraries. Suggested-by: Michael Paquier Reviewed-by: Bharath Rupireddy Discussion: https://postgr.es/m/20231205034647.GA2705267%40nathanxps13
1 parent 8b2bcf3 commit abb0b4f

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

contrib/pg_prewarm/autoprewarm.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
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);
9595
static bool apw_init_shmem(void);
9696
static void apw_detach_shmem(int code, Datum arg);
9797
static 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. */
102100
static 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

Comments
 (0)