Fix using injection points at backend startup in EXEC_BACKEND mode
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 26 Jul 2024 11:55:04 +0000 (14:55 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 26 Jul 2024 12:11:50 +0000 (15:11 +0300)
Commit 86db52a506 changed the locking of injection points to use only
atomic ops and spinlocks, to make it possible to define injection
points in processes that don't have a PGPROC entry (yet). However, it
didn't work in EXEC_BACKEND mode, because the pointer to shared memory
area was not initialized until the process "attaches" to all the
shared memory structs. To fix, pass the pointer to the child process
along with other global variables that need to be set up early.

Backpatch-through: 17

src/backend/postmaster/launch_backend.c
src/backend/utils/misc/injection_point.c
src/include/utils/injection_point.h

index e9fc9827878b751e06d86d53a762331526e9d19d..fafe5feecc244196ad54d39191d30b1e36586de3 100644 (file)
@@ -63,6 +63,7 @@
 #include "utils/builtins.h"
 #include "utils/datetime.h"
 #include "utils/guc.h"
+#include "utils/injection_point.h"
 #include "utils/memutils.h"
 #include "utils/timestamp.h"
 
@@ -104,6 +105,9 @@ typedef struct
    void       *UsedShmemSegAddr;
    slock_t    *ShmemLock;
    struct bkend *ShmemBackendArray;
+#ifdef USE_INJECTION_POINTS
+   struct InjectionPointsCtl *ActiveInjectionPoints;
+#endif
 #ifndef HAVE_SPINLOCKS
    PGSemaphore *SpinlockSemaArray;
 #endif
@@ -710,6 +714,10 @@ save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
    param->ShmemLock = ShmemLock;
    param->ShmemBackendArray = ShmemBackendArray;
 
+#ifdef USE_INJECTION_POINTS
+   param->ActiveInjectionPoints = ActiveInjectionPoints;
+#endif
+
 #ifndef HAVE_SPINLOCKS
    param->SpinlockSemaArray = SpinlockSemaArray;
 #endif
@@ -969,6 +977,10 @@ restore_backend_variables(BackendParameters *param)
    ShmemLock = param->ShmemLock;
    ShmemBackendArray = param->ShmemBackendArray;
 
+#ifdef USE_INJECTION_POINTS
+   ActiveInjectionPoints = param->ActiveInjectionPoints;
+#endif
+
 #ifndef HAVE_SPINLOCKS
    SpinlockSemaArray = param->SpinlockSemaArray;
 #endif
index 8ad0c27bc8a18bf748d352449a0fa11588e20c31..8ab5bc6327621e62d95ed586b194dded259e445d 100644 (file)
@@ -85,7 +85,7 @@ typedef struct InjectionPointsCtl
    InjectionPointEntry entries[MAX_INJECTION_POINTS];
 } InjectionPointsCtl;
 
-static InjectionPointsCtl *ActiveInjectionPoints;
+NON_EXEC_STATIC InjectionPointsCtl *ActiveInjectionPoints;
 
 /*
  * Backend local cache of injection callbacks already loaded, stored in
index a385e3df64965d7b8378a51cfe3f1cb3aa7a7098..a5b4a1f0f9f5d0596519ce3abd5e6575fe7b08c5 100644 (file)
@@ -43,4 +43,8 @@ extern void InjectionPointRun(const char *name);
 extern void InjectionPointCached(const char *name);
 extern bool InjectionPointDetach(const char *name);
 
+#ifdef EXEC_BACKEND
+extern PGDLLIMPORT struct InjectionPointsCtl *ActiveInjectionPoints;
+#endif
+
 #endif                         /* INJECTION_POINT_H */