Skip to content

Commit 9d9b9d4

Browse files
committed
Move cancel key generation to after forking the backend
Move responsibility of generating the cancel key to the backend process. The cancel key is now generated after forking, and the backend advertises it in the ProcSignal array. When a cancel request arrives, the backend handling it scans the ProcSignal array to find the target pid and cancel key. This is similar to how this previously worked in the EXEC_BACKEND case with the ShmemBackendArray, just reusing the ProcSignal array. One notable change is that we no longer generate cancellation keys for non-backend processes. We generated them before just to prevent a malicious user from canceling them; the keys for non-backend processes were never actually given to anyone. There is now an explicit flag indicating whether a process has a valid key or not. I wrote this originally in preparation for supporting longer cancel keys, but it's a nice cleanup on its own. Reviewed-by: Jelte Fennema-Nio Discussion: https://www.postgresql.org/message-id/508d0505-8b7a-4864-a681-e7e5edfe32aa@iki.fi
1 parent 19de089 commit 9d9b9d4

File tree

12 files changed

+193
-262
lines changed

12 files changed

+193
-262
lines changed

src/backend/postmaster/auxprocess.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ AuxiliaryProcessMainCommon(void)
7171

7272
BaseInit();
7373

74-
ProcSignalInit();
74+
ProcSignalInit(false, 0);
7575

7676
/*
7777
* Auxiliary processes don't run transactions, but they may need a

src/backend/postmaster/launch_backend.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "storage/pg_shmem.h"
5959
#include "storage/pmsignal.h"
6060
#include "storage/proc.h"
61+
#include "storage/procsignal.h"
6162
#include "tcop/backend_startup.h"
6263
#include "tcop/tcopprot.h"
6364
#include "utils/builtins.h"
@@ -94,7 +95,6 @@ typedef int InheritableSocket;
9495
typedef struct
9596
{
9697
char DataDir[MAXPGPATH];
97-
int32 MyCancelKey;
9898
int MyPMChildSlot;
9999
#ifndef WIN32
100100
unsigned long UsedShmemSegID;
@@ -104,7 +104,6 @@ typedef struct
104104
#endif
105105
void *UsedShmemSegAddr;
106106
slock_t *ShmemLock;
107-
struct bkend *ShmemBackendArray;
108107
#ifdef USE_INJECTION_POINTS
109108
struct InjectionPointsCtl *ActiveInjectionPoints;
110109
#endif
@@ -119,6 +118,7 @@ typedef struct
119118
PGPROC *AuxiliaryProcs;
120119
PGPROC *PreparedXactProcs;
121120
volatile PMSignalData *PMSignalState;
121+
ProcSignalHeader *ProcSignal;
122122
pid_t PostmasterPid;
123123
TimestampTz PgStartTime;
124124
TimestampTz PgReloadTime;
@@ -702,7 +702,6 @@ save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
702702

703703
strlcpy(param->DataDir, DataDir, MAXPGPATH);
704704

705-
param->MyCancelKey = MyCancelKey;
706705
param->MyPMChildSlot = MyPMChildSlot;
707706

708707
#ifdef WIN32
@@ -712,7 +711,6 @@ save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
712711
param->UsedShmemSegAddr = UsedShmemSegAddr;
713712

714713
param->ShmemLock = ShmemLock;
715-
param->ShmemBackendArray = ShmemBackendArray;
716714

717715
#ifdef USE_INJECTION_POINTS
718716
param->ActiveInjectionPoints = ActiveInjectionPoints;
@@ -729,6 +727,7 @@ save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
729727
param->AuxiliaryProcs = AuxiliaryProcs;
730728
param->PreparedXactProcs = PreparedXactProcs;
731729
param->PMSignalState = PMSignalState;
730+
param->ProcSignal = ProcSignal;
732731

733732
param->PostmasterPid = PostmasterPid;
734733
param->PgStartTime = PgStartTime;
@@ -965,7 +964,6 @@ restore_backend_variables(BackendParameters *param)
965964

966965
SetDataDir(param->DataDir);
967966

968-
MyCancelKey = param->MyCancelKey;
969967
MyPMChildSlot = param->MyPMChildSlot;
970968

971969
#ifdef WIN32
@@ -975,7 +973,6 @@ restore_backend_variables(BackendParameters *param)
975973
UsedShmemSegAddr = param->UsedShmemSegAddr;
976974

977975
ShmemLock = param->ShmemLock;
978-
ShmemBackendArray = param->ShmemBackendArray;
979976

980977
#ifdef USE_INJECTION_POINTS
981978
ActiveInjectionPoints = param->ActiveInjectionPoints;
@@ -992,6 +989,7 @@ restore_backend_variables(BackendParameters *param)
992989
AuxiliaryProcs = param->AuxiliaryProcs;
993990
PreparedXactProcs = param->PreparedXactProcs;
994991
PMSignalState = param->PMSignalState;
992+
ProcSignal = param->ProcSignal;
995993

996994
PostmasterPid = param->PostmasterPid;
997995
PgStartTime = param->PgStartTime;

0 commit comments

Comments
 (0)