Skip to content

Commit 9aff56e

Browse files
committed
Remove our own shared memory tooling
Since it just has one user now let's remove our own shared memory code and remove a lot of generic code that we do not need.
1 parent acb974d commit 9aff56e

File tree

5 files changed

+69
-218
lines changed

5 files changed

+69
-218
lines changed

contrib/pg_tde/src/catalog/tde_principal_key.c

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ typedef struct TdePrincipalKeySharedState
6060
LWLockPadded *Locks;
6161
dshash_table_handle hashHandle;
6262
void *rawDsaArea; /* DSA area pointer */
63-
6463
} TdePrincipalKeySharedState;
6564

6665
typedef struct TdePrincipalKeylocalState
@@ -84,10 +83,6 @@ static dshash_parameters principal_key_dsh_params = {
8483
static TdePrincipalKeylocalState principalKeyLocalState;
8584

8685
static void principal_key_info_attach_shmem(void);
87-
static Size initialize_shared_state(void *start_address);
88-
static void initialize_objects_in_dsa_area(dsa_area *dsa, void *raw_dsa_area);
89-
static Size required_shared_mem_size(void);
90-
static void shared_memory_shutdown(int code, Datum arg);
9186
static void clear_principal_key_cache(Oid databaseId);
9287
static inline dshash_table *get_principal_key_hash(void);
9388
static TDEPrincipalKey *get_principal_key_from_cache(Oid dbOid);
@@ -111,32 +106,6 @@ PG_FUNCTION_INFO_V1(pg_tde_set_server_key_using_global_key_provider);
111106

112107
static void pg_tde_set_principal_key_internal(Oid providerOid, Oid dbOid, const char *principal_key_name, const char *provider_name, bool ensure_new_key);
113108

114-
static const TDEShmemSetupRoutine principal_key_info_shmem_routine = {
115-
.init_shared_state = initialize_shared_state,
116-
.init_dsa_area_objects = initialize_objects_in_dsa_area,
117-
.required_shared_mem_size = required_shared_mem_size,
118-
.shmem_kill = shared_memory_shutdown
119-
};
120-
121-
void
122-
InitializePrincipalKeyInfo(void)
123-
{
124-
ereport(LOG, errmsg("Initializing TDE principal key info"));
125-
RegisterShmemRequest(&principal_key_info_shmem_routine);
126-
}
127-
128-
/*
129-
* Lock to guard internal/principal key. Usually, this lock has to be held until
130-
* the caller fetches an internal_key or rotates the principal.
131-
*/
132-
LWLock *
133-
tde_lwlock_enc_keys(void)
134-
{
135-
Assert(principalKeyLocalState.sharedPrincipalKeyState);
136-
137-
return &principalKeyLocalState.sharedPrincipalKeyState->Locks[TDE_LWLOCK_ENC_KEY].lock;
138-
}
139-
140109
/*
141110
* Request some pages so we can fit the DSA header, empty hash table plus some
142111
* extra. Additional memory to grow the hash map will be allocated as needed
@@ -147,50 +116,84 @@ tde_lwlock_enc_keys(void)
147116
*/
148117
#define CACHE_DSA_INITIAL_SIZE (4096 * 64)
149118

150-
static Size
151-
required_shared_mem_size(void)
119+
Size
120+
PrincipalKeyShmemSize(void)
152121
{
153122
Size sz = CACHE_DSA_INITIAL_SIZE;
154123

155124
sz = add_size(sz, sizeof(TdePrincipalKeySharedState));
156125
return MAXALIGN(sz);
157126
}
158127

159-
/*
160-
* Initialize the shared area for Principal key info.
161-
* This includes locks and cache area for principal key info
162-
*/
163-
164-
static Size
165-
initialize_shared_state(void *start_address)
128+
void
129+
PrincipalKeyShmemInit(void)
166130
{
167-
TdePrincipalKeySharedState *sharedState = (TdePrincipalKeySharedState *) start_address;
131+
bool found;
132+
char *free_start;
133+
Size required_shmem_size = PrincipalKeyShmemSize();
168134

169-
ereport(LOG, errmsg("initializing shared state for principal key"));
135+
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
170136

171-
sharedState->Locks = GetNamedLWLockTranche(TDE_TRANCHE_NAME);
137+
/* Create or attach to the shared memory state */
138+
ereport(NOTICE, errmsg("PrincipalKeyShmemInit: requested %ld bytes", required_shmem_size));
139+
free_start = ShmemInitStruct("pg_tde", required_shmem_size, &found);
172140

173-
principalKeyLocalState.sharedPrincipalKeyState = sharedState;
174-
principalKeyLocalState.sharedHash = NULL;
141+
if (!found)
142+
{
143+
TdePrincipalKeySharedState *sharedState;
144+
Size sz;
145+
Size dsa_area_size;
146+
dsa_area *dsa;
147+
dshash_table *dsh;
175148

176-
return sizeof(TdePrincipalKeySharedState);
177-
}
149+
/* Now place shared state structure */
150+
sharedState = (TdePrincipalKeySharedState *) free_start;
151+
sz = MAXALIGN(sizeof(TdePrincipalKeySharedState));
152+
free_start += sz;
153+
Assert(sz <= required_shmem_size);
178154

179-
static void
180-
initialize_objects_in_dsa_area(dsa_area *dsa, void *raw_dsa_area)
181-
{
182-
dshash_table *dsh;
183-
TdePrincipalKeySharedState *sharedState = principalKeyLocalState.sharedPrincipalKeyState;
155+
/* Create DSA area */
156+
dsa_area_size = required_shmem_size - sz;
157+
Assert(dsa_area_size > 0);
158+
159+
ereport(LOG, errmsg("creating DSA area of size %lu", dsa_area_size));
160+
161+
dsa = dsa_create_in_place(free_start,
162+
dsa_area_size,
163+
LWLockNewTrancheId(), 0);
164+
dsa_pin(dsa);
165+
166+
/* Limit area size during population to get a nice error */
167+
dsa_set_size_limit(dsa, dsa_area_size);
168+
169+
principal_key_dsh_params.tranche_id = LWLockNewTrancheId();
170+
dsh = dshash_create(dsa, &principal_key_dsh_params, NULL);
171+
172+
dsa_set_size_limit(dsa, -1);
184173

185-
ereport(LOG, errmsg("initializing dsa area objects for principal key"));
174+
sharedState->Locks = GetNamedLWLockTranche(TDE_TRANCHE_NAME);
175+
sharedState->hashHandle = dshash_get_hash_table_handle(dsh);
176+
sharedState->rawDsaArea = free_start;
186177

187-
Assert(sharedState != NULL);
178+
principalKeyLocalState.sharedPrincipalKeyState = sharedState;
179+
principalKeyLocalState.sharedHash = NULL;
188180

189-
sharedState->rawDsaArea = raw_dsa_area;
190-
principal_key_dsh_params.tranche_id = LWLockNewTrancheId();
191-
dsh = dshash_create(dsa, &principal_key_dsh_params, NULL);
192-
sharedState->hashHandle = dshash_get_hash_table_handle(dsh);
193-
dshash_detach(dsh);
181+
dshash_detach(dsh);
182+
}
183+
184+
LWLockRelease(AddinShmemInitLock);
185+
}
186+
187+
/*
188+
* Lock to guard internal/principal key. Usually, this lock has to be held until
189+
* the caller fetches an internal_key or rotates the principal.
190+
*/
191+
LWLock *
192+
tde_lwlock_enc_keys(void)
193+
{
194+
Assert(principalKeyLocalState.sharedPrincipalKeyState);
195+
196+
return &principalKeyLocalState.sharedPrincipalKeyState->Locks[TDE_LWLOCK_ENC_KEY].lock;
194197
}
195198

196199
/*
@@ -217,12 +220,6 @@ principal_key_info_attach_shmem(void)
217220
MemoryContextSwitchTo(oldcontext);
218221
}
219222

220-
static void
221-
shared_memory_shutdown(int code, Datum arg)
222-
{
223-
principalKeyLocalState.sharedPrincipalKeyState = NULL;
224-
}
225-
226223
void
227224
set_principal_key_with_keyring(const char *key_name, const char *provider_name,
228225
Oid providerOid, Oid dbOid, bool ensure_new_key)

contrib/pg_tde/src/common/pg_tde_shmem.c

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -11,122 +11,10 @@
1111

1212
#include "postgres.h"
1313

14-
#include "lib/dshash.h"
15-
#include "nodes/pg_list.h"
16-
#include "storage/ipc.h"
17-
#include "storage/lwlock.h"
18-
#include "storage/shmem.h"
19-
2014
#include "common/pg_tde_shmem.h"
2115

22-
static void tde_shmem_shutdown(int code, Datum arg);
23-
24-
List *registeredShmemRequests = NIL;
25-
bool shmemInited = false;
26-
27-
void
28-
RegisterShmemRequest(const TDEShmemSetupRoutine *routine)
29-
{
30-
Assert(shmemInited == false);
31-
registeredShmemRequests = lappend(registeredShmemRequests, (void *) routine);
32-
}
33-
34-
Size
35-
TdeRequiredSharedMemorySize(void)
36-
{
37-
Size sz = 0;
38-
ListCell *lc;
39-
40-
foreach(lc, registeredShmemRequests)
41-
{
42-
TDEShmemSetupRoutine *routine = (TDEShmemSetupRoutine *) lfirst(lc);
43-
44-
if (routine->required_shared_mem_size)
45-
sz = add_size(sz, routine->required_shared_mem_size());
46-
}
47-
return MAXALIGN(sz);
48-
}
49-
5016
int
5117
TdeRequiredLocksCount(void)
5218
{
5319
return TDE_LWLOCK_COUNT;
5420
}
55-
56-
void
57-
TdeShmemInit(void)
58-
{
59-
bool found;
60-
char *free_start;
61-
Size required_shmem_size = TdeRequiredSharedMemorySize();
62-
63-
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
64-
/* Create or attach to the shared memory state */
65-
ereport(NOTICE, errmsg("TdeShmemInit: requested %ld bytes", required_shmem_size));
66-
free_start = ShmemInitStruct("pg_tde", required_shmem_size, &found);
67-
68-
if (!found)
69-
{
70-
/* First time through ... */
71-
dsa_area *dsa;
72-
ListCell *lc;
73-
Size used_size = 0;
74-
Size dsa_area_size;
75-
76-
/* Now place all shared state structures */
77-
foreach(lc, registeredShmemRequests)
78-
{
79-
Size sz = 0;
80-
TDEShmemSetupRoutine *routine = (TDEShmemSetupRoutine *) lfirst(lc);
81-
82-
if (routine->init_shared_state)
83-
{
84-
sz = routine->init_shared_state(free_start);
85-
used_size += MAXALIGN(sz);
86-
free_start += MAXALIGN(sz);
87-
Assert(used_size <= required_shmem_size);
88-
}
89-
}
90-
/* Create DSA area */
91-
dsa_area_size = required_shmem_size - used_size;
92-
Assert(dsa_area_size > 0);
93-
94-
ereport(LOG, errmsg("creating DSA area of size %lu", dsa_area_size));
95-
dsa = dsa_create_in_place(free_start,
96-
dsa_area_size,
97-
LWLockNewTrancheId(), 0);
98-
dsa_pin(dsa);
99-
dsa_set_size_limit(dsa, dsa_area_size);
100-
101-
/* Initialize all DSA area objects */
102-
foreach(lc, registeredShmemRequests)
103-
{
104-
TDEShmemSetupRoutine *routine = (TDEShmemSetupRoutine *) lfirst(lc);
105-
106-
if (routine->init_dsa_area_objects)
107-
routine->init_dsa_area_objects(dsa, free_start);
108-
}
109-
ereport(LOG, errmsg("setting no limit to DSA area of size %lu", dsa_area_size));
110-
111-
dsa_set_size_limit(dsa, -1); /* Let it grow outside the shared
112-
* memory */
113-
114-
shmemInited = true;
115-
}
116-
LWLockRelease(AddinShmemInitLock);
117-
on_shmem_exit(tde_shmem_shutdown, (Datum) 0);
118-
}
119-
120-
static void
121-
tde_shmem_shutdown(int code, Datum arg)
122-
{
123-
ListCell *lc;
124-
125-
foreach(lc, registeredShmemRequests)
126-
{
127-
TDEShmemSetupRoutine *routine = (TDEShmemSetupRoutine *) lfirst(lc);
128-
129-
if (routine->shmem_kill)
130-
routine->shmem_kill(code, arg);
131-
}
132-
}

contrib/pg_tde/src/include/catalog/tde_principal_key.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ typedef struct XLogPrincipalKeyRotate
4242

4343
#define SizeoOfXLogPrincipalKeyRotate offsetof(XLogPrincipalKeyRotate, buff)
4444

45-
extern void InitializePrincipalKeyInfo(void);
45+
extern void PrincipalKeyShmemInit(void);
46+
extern Size PrincipalKeyShmemSize(void);
4647

4748
#ifndef FRONTEND
4849
extern void principal_key_startup_cleanup(Oid databaseId);

contrib/pg_tde/src/include/common/pg_tde_shmem.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#ifndef PG_TDE_SHMEM_H
99
#define PG_TDE_SHMEM_H
1010

11-
#include "postgres.h"
12-
#include "utils/dsa.h"
13-
1411
#define TDE_TRANCHE_NAME "pg_tde_tranche"
1512

1613
typedef enum
@@ -22,39 +19,6 @@ typedef enum
2219
TDE_LWLOCK_COUNT
2320
} TDELockTypes;
2421

25-
typedef struct TDEShmemSetupRoutine
26-
{
27-
/*
28-
* init_shared_state gets called at the time of extension load you can
29-
* initialize the data structures required to be placed in shared memory
30-
* in this callback The callback must return the size of the shared memory
31-
* area acquired. The argument to the function is the start of the shared
32-
* memory address that can be used to store the shared data structures.
33-
*/
34-
Size (*init_shared_state) (void *raw_dsa_area);
35-
36-
/*
37-
* shmem_startup gets called at the time of postmaster shutdown
38-
*/
39-
void (*shmem_kill) (int code, Datum arg);
40-
41-
/*
42-
* The callback must return the size of the shared memory acquired.
43-
*/
44-
Size (*required_shared_mem_size) (void);
45-
46-
/*
47-
* Gets called after all shared memory structures are initialized and here
48-
* you can create shared memory hash tables or any other shared objects
49-
* that needs to live in DSA area.
50-
*/
51-
void (*init_dsa_area_objects) (dsa_area *dsa, void *raw_dsa_area);
52-
} TDEShmemSetupRoutine;
53-
54-
/* Interface to register the shared memory requests */
55-
extern void RegisterShmemRequest(const TDEShmemSetupRoutine *routine);
56-
extern void TdeShmemInit(void);
57-
extern Size TdeRequiredSharedMemorySize(void);
5822
extern int TdeRequiredLocksCount(void);
5923

6024
#endif /* PG_TDE_SHMEM_H */

contrib/pg_tde/src/pg_tde.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ PG_FUNCTION_INFO_V1(pg_tdeam_handler);
5555
static void
5656
tde_shmem_request(void)
5757
{
58-
Size sz = TdeRequiredSharedMemorySize();
58+
Size sz = 0;
5959
int required_locks = TdeRequiredLocksCount();
6060

61+
sz = add_size(sz, PrincipalKeyShmemSize());
6162
sz = add_size(sz, TDEXLogEncryptStateSize());
6263

6364
if (prev_shmem_request_hook)
6465
prev_shmem_request_hook();
66+
6567
RequestAddinShmemSpace(sz);
6668
RequestNamedLWLockTranche(TDE_TRANCHE_NAME, required_locks);
6769
ereport(LOG, errmsg("tde_shmem_request: requested %ld bytes", sz));
@@ -73,8 +75,8 @@ tde_shmem_startup(void)
7375
if (prev_shmem_startup_hook)
7476
prev_shmem_startup_hook();
7577

76-
TdeShmemInit();
77-
InitializeKeyProviderInfo();
78+
KeyProviderShmemInit();
79+
PrincipalKeyShmemInit();
7880
TDEXLogShmemInit();
7981
TDEXLogSmgrInit();
8082
}
@@ -100,7 +102,6 @@ _PG_init(void)
100102
AesInit();
101103
TdeGucInit();
102104
TdeEventCaptureInit();
103-
InitializePrincipalKeyInfo();
104105
InstallFileKeyring();
105106
InstallVaultV2Keyring();
106107
InstallKmipKeyring();

0 commit comments

Comments
 (0)