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

Skip to content

Minor cleanup of the code handling the shared memory cache for principal keys #409

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 2 commits into from
Jun 10, 2025
Merged
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
22 changes: 10 additions & 12 deletions contrib/pg_tde/src/catalog/tde_principal_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ 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 inline dshash_table *get_principal_key_hash(void);
static TDEPrincipalKey *get_principal_key_from_cache(Oid dbOid);
static bool pg_tde_is_same_principal_key(TDEPrincipalKey *a, TDEPrincipalKey *b);
static void pg_tde_update_global_principal_key_everywhere(TDEPrincipalKey *oldKey, TDEPrincipalKey *newKey);
Expand Down Expand Up @@ -200,9 +200,6 @@ principal_key_info_attach_shmem(void)
MemoryContext oldcontext;
dsa_area *dsa;

if (principalKeyLocalState.sharedHash)
return;

/*
* We want the dsa to remain valid throughout the lifecycle of this
* process. so switch to TopMemoryContext before attaching
Expand Down Expand Up @@ -372,9 +369,10 @@ xl_tde_perform_rotate_key(XLogPrincipalKeyRotate *xlrec)
*/

static inline dshash_table *
get_principal_key_Hash(void)
get_principal_key_hash(void)
{
principal_key_info_attach_shmem();
if (!principalKeyLocalState.sharedHash)
principal_key_info_attach_shmem();
return principalKeyLocalState.sharedHash;
}

Expand All @@ -386,10 +384,10 @@ get_principal_key_from_cache(Oid dbOid)
{
TDEPrincipalKey *cacheEntry = NULL;

cacheEntry = (TDEPrincipalKey *) dshash_find(get_principal_key_Hash(),
cacheEntry = (TDEPrincipalKey *) dshash_find(get_principal_key_hash(),
&dbOid, false);
if (cacheEntry)
dshash_release_lock(get_principal_key_Hash(), cacheEntry);
dshash_release_lock(get_principal_key_hash(), cacheEntry);

return cacheEntry;
}
Expand All @@ -411,12 +409,12 @@ push_principal_key_to_cache(TDEPrincipalKey *principalKey)
Oid databaseId = principalKey->keyInfo.databaseId;
bool found = false;

cacheEntry = dshash_find_or_insert(get_principal_key_Hash(),
cacheEntry = dshash_find_or_insert(get_principal_key_hash(),
&databaseId, &found);

if (!found)
*cacheEntry = *principalKey;
dshash_release_lock(get_principal_key_Hash(), cacheEntry);
dshash_release_lock(get_principal_key_hash(), cacheEntry);

/* we don't want principal keys to end up paged to the swap */
if (mlock(cacheEntry, sizeof(TDEPrincipalKey)) == -1)
Expand Down Expand Up @@ -447,11 +445,11 @@ clear_principal_key_cache(Oid databaseId)
TDEPrincipalKey *cache_entry;

/* Start with deleting the cache entry for the database */
cache_entry = (TDEPrincipalKey *) dshash_find(get_principal_key_Hash(),
cache_entry = (TDEPrincipalKey *) dshash_find(get_principal_key_hash(),
&databaseId, true);
if (cache_entry)
{
dshash_delete_entry(get_principal_key_Hash(), cache_entry);
dshash_delete_entry(get_principal_key_hash(), cache_entry);
}
}

Expand Down