Skip to content

Commit c38919c

Browse files
committed
Increase readability of GetPrincipalKey()
By just having one ifdef block the function becomes easier on the eye.
1 parent 2358190 commit c38919c

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

contrib/pg_tde/src/catalog/tde_principal_key.c

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -819,47 +819,40 @@ TDEPrincipalKey *
819819
GetPrincipalKey(Oid dbOid, LWLockMode lockMode)
820820
{
821821
TDEPrincipalKey *principalKey = GetPrincipalKeyNoDefault(dbOid, lockMode);
822-
#ifndef FRONTEND
823-
TDEPrincipalKey *newPrincipalKey = NULL;
824-
#endif
825-
826-
if (principalKey != NULL)
827-
{
828-
return principalKey;
829-
}
830822

831823
#ifndef FRONTEND
832-
833-
/*
834-
* If database doesn't have dedicated principal key we should try to
835-
* fallback to default principal key.
836-
*/
837-
838-
/* Lock is already updated to exclusive at this point */
839-
principalKey = GetPrincipalKeyNoDefault(DEFAULT_DATA_TDE_OID, LW_EXCLUSIVE);
840-
841824
if (principalKey == NULL)
842825
{
843-
return NULL;
844-
}
826+
/*
827+
* If database doesn't have dedicated principal key we should try to
828+
* fallback to default principal key.
829+
*/
830+
TDEPrincipalKey *newPrincipalKey;
845831

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

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

858-
push_principal_key_to_cache(newPrincipalKey);
838+
newPrincipalKey = palloc_object(TDEPrincipalKey);
839+
*newPrincipalKey = *principalKey;
840+
newPrincipalKey->keyInfo.databaseId = dbOid;
859841

860-
pfree(newPrincipalKey);
842+
/*
843+
* We have to write default principal key info to database keys file.
844+
* However we cannot write XLOG records about this operation as
845+
* current funcion may be invoked during server startup/recovery where
846+
* WAL writes forbidden.
847+
*/
848+
pg_tde_save_principal_key(newPrincipalKey, false);
849+
850+
push_principal_key_to_cache(newPrincipalKey);
861851

862-
principalKey = GetPrincipalKeyNoDefault(dbOid, LW_EXCLUSIVE);
852+
pfree(newPrincipalKey);
853+
854+
principalKey = GetPrincipalKeyNoDefault(dbOid, LW_EXCLUSIVE);
855+
}
863856
#endif
864857

865858
return principalKey;

0 commit comments

Comments
 (0)