Skip to content

Increase readability of GetPrincipalKey() #359

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
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
57 changes: 25 additions & 32 deletions contrib/pg_tde/src/catalog/tde_principal_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,47 +819,40 @@ TDEPrincipalKey *
GetPrincipalKey(Oid dbOid, LWLockMode lockMode)
{
TDEPrincipalKey *principalKey = GetPrincipalKeyNoDefault(dbOid, lockMode);
#ifndef FRONTEND
TDEPrincipalKey *newPrincipalKey = NULL;
#endif

if (principalKey != NULL)
{
return principalKey;
}

#ifndef FRONTEND

/*
* If database doesn't have dedicated principal key we should try to
* fallback to default principal key.
*/

/* Lock is already updated to exclusive at this point */
principalKey = GetPrincipalKeyNoDefault(DEFAULT_DATA_TDE_OID, LW_EXCLUSIVE);

if (principalKey == NULL)
{
return NULL;
}
/*
* If database doesn't have dedicated principal key we should try to
* fallback to default principal key.
*/
TDEPrincipalKey *newPrincipalKey;

newPrincipalKey = palloc_object(TDEPrincipalKey);
*newPrincipalKey = *principalKey;
newPrincipalKey->keyInfo.databaseId = dbOid;
/* Lock is already updated to exclusive at this point */
principalKey = GetPrincipalKeyNoDefault(DEFAULT_DATA_TDE_OID, LW_EXCLUSIVE);

/*
* We have to write default principal key info to database keys file.
* However we cannot write XLOG records about this operation as current
* funcion may be invoked during server startup/recovery where WAL writes
* forbidden.
*/
pg_tde_save_principal_key(newPrincipalKey, false);
if (principalKey == NULL)
return NULL;

push_principal_key_to_cache(newPrincipalKey);
newPrincipalKey = palloc_object(TDEPrincipalKey);
*newPrincipalKey = *principalKey;
newPrincipalKey->keyInfo.databaseId = dbOid;

pfree(newPrincipalKey);
/*
* We have to write default principal key info to database keys file.
* However we cannot write XLOG records about this operation as
* current funcion may be invoked during server startup/recovery where
* WAL writes forbidden.
*/
pg_tde_save_principal_key(newPrincipalKey, false);

push_principal_key_to_cache(newPrincipalKey);

principalKey = GetPrincipalKeyNoDefault(dbOid, LW_EXCLUSIVE);
pfree(newPrincipalKey);

principalKey = GetPrincipalKeyNoDefault(dbOid, LW_EXCLUSIVE);
}
#endif

return principalKey;
Expand Down