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

Skip to content

Remove our own shared memory tooling #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contrib/pg_tde/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ src/keyring/keyring_api.o \
src/catalog/tde_keyring.o \
src/catalog/tde_keyring_parse_opts.o \
src/catalog/tde_principal_key.o \
src/common/pg_tde_shmem.o \
src/common/pg_tde_utils.o \
src/smgr/pg_tde_smgr.o \
src/pg_tde_event_capture.o \
Expand Down
1 change: 0 additions & 1 deletion contrib/pg_tde/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pg_tde_sources = files(
'src/catalog/tde_keyring.c',
'src/catalog/tde_keyring_parse_opts.c',
'src/catalog/tde_principal_key.c',
'src/common/pg_tde_shmem.c',
'src/common/pg_tde_utils.c',
'src/encryption/enc_aes.c',
'src/encryption/enc_tde.c',
Expand Down
43 changes: 7 additions & 36 deletions contrib/pg_tde/src/catalog/tde_keyring.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@

#ifndef FRONTEND
#include "access/heapam.h"
#include "funcapi.h"
#include "access/relscan.h"
#include "access/relation.h"
#include "access/relscan.h"
#include "catalog/namespace.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "common/pg_tde_shmem.h"
#else
#include "fe_utils/simple_list.h"
#include "pg_tde_fe.h"
Expand Down Expand Up @@ -87,12 +88,10 @@ PG_FUNCTION_INFO_V1(pg_tde_list_all_global_key_providers);

static const char *get_keyring_provider_typename(ProviderType p_type);
static List *GetAllKeyringProviders(Oid dbOid);
static Size initialize_shared_state(void *start_address);
static Datum pg_tde_add_key_provider_internal(PG_FUNCTION_ARGS, Oid dbOid);
static Datum pg_tde_change_key_provider_internal(PG_FUNCTION_ARGS, Oid dbOid);
static Datum pg_tde_delete_key_provider_internal(PG_FUNCTION_ARGS, Oid dbOid);
static Datum pg_tde_list_all_key_providers_internal(PG_FUNCTION_ARGS, const char *fname, Oid dbOid);
static Size required_shared_mem_size(void);
static List *scan_key_provider_file(ProviderScanType scanType, void *scanKey, Oid dbOid);
static void save_new_key_provider_info(KeyringProviderRecord *provider, Oid databaseId);
static void modify_key_provider_info(KeyringProviderRecord *provider, Oid databaseId);
Expand All @@ -101,47 +100,19 @@ static void check_provider_record(KeyringProviderRecord *provider_record);

#define PG_TDE_LIST_PROVIDERS_COLS 4

typedef struct TdeKeyProviderInfoSharedState
{
LWLockPadded *Locks;
} TdeKeyProviderInfoSharedState;

TdeKeyProviderInfoSharedState *sharedPrincipalKeyState = NULL; /* Lives in shared state */

static const TDEShmemSetupRoutine key_provider_info_shmem_routine = {
.init_shared_state = initialize_shared_state,
.init_dsa_area_objects = NULL,
.required_shared_mem_size = required_shared_mem_size,
.shmem_kill = NULL
};

static Size
required_shared_mem_size(void)
{
return MAXALIGN(sizeof(TdeKeyProviderInfoSharedState));
}

static Size
initialize_shared_state(void *start_address)
{
sharedPrincipalKeyState = (TdeKeyProviderInfoSharedState *) start_address;
sharedPrincipalKeyState->Locks = GetNamedLWLockTranche(TDE_TRANCHE_NAME);

return sizeof(TdeKeyProviderInfoSharedState);
}
static LWLockPadded *tdeLocks = NULL; /* Lives in shared state */

static inline LWLock *
tde_provider_info_lock(void)
{
Assert(sharedPrincipalKeyState);
return &sharedPrincipalKeyState->Locks[TDE_LWLOCK_PI_FILES].lock;
Assert(tdeLocks);
return &tdeLocks[TDE_LWLOCK_PI_FILES].lock;
}

void
InitializeKeyProviderInfo(void)
KeyProviderShmemInit(void)
{
ereport(LOG, errmsg("initializing TDE key provider info"));
RegisterShmemRequest(&key_provider_info_shmem_routine);
tdeLocks = GetNamedLWLockTranche(TDE_TRANCHE_NAME);
}

void
Expand Down
128 changes: 62 additions & 66 deletions contrib/pg_tde/src/catalog/tde_principal_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include "lib/dshash.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "common/pg_tde_shmem.h"
#else
#include "pg_tde_fe.h"
#endif
Expand All @@ -60,7 +59,6 @@ typedef struct TdePrincipalKeySharedState
LWLockPadded *Locks;
dshash_table_handle hashHandle;
void *rawDsaArea; /* DSA area pointer */

} TdePrincipalKeySharedState;

typedef struct TdePrincipalKeylocalState
Expand All @@ -84,10 +82,6 @@ static dshash_parameters principal_key_dsh_params = {
static TdePrincipalKeylocalState principalKeyLocalState;

static void principal_key_info_attach_shmem(void);
static Size initialize_shared_state(void *start_address);
static void initialize_objects_in_dsa_area(dsa_area *dsa, void *raw_dsa_area);
static Size required_shared_mem_size(void);
static void shared_memory_shutdown(int code, Datum arg);
static void clear_principal_key_cache(Oid databaseId);
static inline dshash_table *get_principal_key_hash(void);
static TDEPrincipalKey *get_principal_key_from_cache(Oid dbOid);
Expand All @@ -111,32 +105,6 @@ PG_FUNCTION_INFO_V1(pg_tde_set_server_key_using_global_key_provider);

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);

static const TDEShmemSetupRoutine principal_key_info_shmem_routine = {
.init_shared_state = initialize_shared_state,
.init_dsa_area_objects = initialize_objects_in_dsa_area,
.required_shared_mem_size = required_shared_mem_size,
.shmem_kill = shared_memory_shutdown
};

void
InitializePrincipalKeyInfo(void)
{
ereport(LOG, errmsg("Initializing TDE principal key info"));
RegisterShmemRequest(&principal_key_info_shmem_routine);
}

/*
* Lock to guard internal/principal key. Usually, this lock has to be held until
* the caller fetches an internal_key or rotates the principal.
*/
LWLock *
tde_lwlock_enc_keys(void)
{
Assert(principalKeyLocalState.sharedPrincipalKeyState);

return &principalKeyLocalState.sharedPrincipalKeyState->Locks[TDE_LWLOCK_ENC_KEY].lock;
}

/*
* Request some pages so we can fit the DSA header, empty hash table plus some
* extra. Additional memory to grow the hash map will be allocated as needed
Expand All @@ -147,50 +115,84 @@ tde_lwlock_enc_keys(void)
*/
#define CACHE_DSA_INITIAL_SIZE (4096 * 64)

static Size
required_shared_mem_size(void)
Size
PrincipalKeyShmemSize(void)
{
Size sz = CACHE_DSA_INITIAL_SIZE;

sz = add_size(sz, sizeof(TdePrincipalKeySharedState));
return MAXALIGN(sz);
}

/*
* Initialize the shared area for Principal key info.
* This includes locks and cache area for principal key info
*/

static Size
initialize_shared_state(void *start_address)
void
PrincipalKeyShmemInit(void)
{
TdePrincipalKeySharedState *sharedState = (TdePrincipalKeySharedState *) start_address;
bool found;
char *free_start;
Size required_shmem_size = PrincipalKeyShmemSize();

ereport(LOG, errmsg("initializing shared state for principal key"));
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);

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

principalKeyLocalState.sharedPrincipalKeyState = sharedState;
principalKeyLocalState.sharedHash = NULL;
if (!found)
{
TdePrincipalKeySharedState *sharedState;
Size sz;
Size dsa_area_size;
dsa_area *dsa;
dshash_table *dsh;

return sizeof(TdePrincipalKeySharedState);
}
/* Now place shared state structure */
sharedState = (TdePrincipalKeySharedState *) free_start;
sz = MAXALIGN(sizeof(TdePrincipalKeySharedState));
free_start += sz;
Assert(sz <= required_shmem_size);

static void
initialize_objects_in_dsa_area(dsa_area *dsa, void *raw_dsa_area)
{
dshash_table *dsh;
TdePrincipalKeySharedState *sharedState = principalKeyLocalState.sharedPrincipalKeyState;
/* Create DSA area */
dsa_area_size = required_shmem_size - sz;
Assert(dsa_area_size > 0);

ereport(LOG, errmsg("creating DSA area of size %lu", dsa_area_size));

dsa = dsa_create_in_place(free_start,
dsa_area_size,
LWLockNewTrancheId(), 0);
dsa_pin(dsa);

/* Limit area size during population to get a nice error */
dsa_set_size_limit(dsa, dsa_area_size);

principal_key_dsh_params.tranche_id = LWLockNewTrancheId();
dsh = dshash_create(dsa, &principal_key_dsh_params, NULL);

dsa_set_size_limit(dsa, -1);

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

Assert(sharedState != NULL);
principalKeyLocalState.sharedPrincipalKeyState = sharedState;
principalKeyLocalState.sharedHash = NULL;

sharedState->rawDsaArea = raw_dsa_area;
principal_key_dsh_params.tranche_id = LWLockNewTrancheId();
dsh = dshash_create(dsa, &principal_key_dsh_params, NULL);
sharedState->hashHandle = dshash_get_hash_table_handle(dsh);
dshash_detach(dsh);
dshash_detach(dsh);
}

LWLockRelease(AddinShmemInitLock);
}

/*
* Lock to guard internal/principal key. Usually, this lock has to be held until
* the caller fetches an internal_key or rotates the principal.
*/
LWLock *
tde_lwlock_enc_keys(void)
{
Assert(principalKeyLocalState.sharedPrincipalKeyState);

return &principalKeyLocalState.sharedPrincipalKeyState->Locks[TDE_LWLOCK_ENC_KEY].lock;
}

/*
Expand All @@ -217,12 +219,6 @@ principal_key_info_attach_shmem(void)
MemoryContextSwitchTo(oldcontext);
}

static void
shared_memory_shutdown(int code, Datum arg)
{
principalKeyLocalState.sharedPrincipalKeyState = NULL;
}

void
set_principal_key_with_keyring(const char *key_name, const char *provider_name,
Oid providerOid, Oid dbOid, bool ensure_new_key)
Expand Down
Loading