8000 Refactoring · percona/postgres@17657af · GitHub
[go: up one dir, main page]

Skip to content

Commit 17657af

Browse files
committed
Refactoring
1 parent cb201a2 commit 17657af

File tree

3 files changed

+12
-50
lines changed

3 files changed

+12
-50
lines changed

contrib/pg_tde/src/access/pg_tde_tdemap.c

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -524,37 +524,6 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p
524524
}
525525
}
526526

527-
/*
528-
* Checks if the key map file for the given database has any non-empty entries.
529-
*
530-
* The caller must hold the lock on the files before calling this function.
531-
*/
532-
bool
533-
pg_tde_is_key_map_empty(Oid dbOid)
534-
{
535-
char path[MAXPGPATH];
536-
off_t pos;
537-
int fd;
538-
TDEMapEntry map_entry;
539-
540-
pg_tde_set_db_file_path(dbOid, path);
541-
542-
fd = pg_tde_open_file_read(path, false, &pos);
543-
544-
while (pg_tde_read_one_map_entry(fd, &map_entry, &pos))
545-
{
546-
if (map_entry.type != MAP_ENTRY_EMPTY)
547-
{
548-
close(fd);
549-
return false;
550-
}
551-
}
552-
553-
close(fd);
554-
return true;
555-
}
556-
557-
558527
void
559528
pg_tde_delete_principal_key_redo(Oid dbOid)
560529
{
@@ -578,20 +547,10 @@ pg_tde_delete_principal_key(Oid dbOid, bool write_xlog)
578547
char path[MAXPGPATH];
579548

580549
Assert(LWLockHeldByMeInMode(tde_lwlock_enc_keys(), LW_EXCLUSIVE));
550+
Assert(pg_tde_count_relations(dbOid) == 0);
581551

582552
pg_tde_set_db_file_path(dbOid, path);
583553

584-
/*
585-
* Check that there aro no local keys in the map file, i.e. nothing is
586-
* encrypted with the principal key.
587-
*/
588-
if (!pg_tde_is_key_map_empty(dbOid))
589-
{
590-
ereport(ERROR,
591-
errcode(ERRCODE_INTERNAL_ERROR),
592-
errmsg("could not delete key, key map file is not empty"));
593-
}
594-
595554
if (write_xlog)
596555
{
597556
XLogBeginInsert();
@@ -742,20 +701,21 @@ pg_tde_find_map_entry(const RelFileLocator *rlocator, TDEMapEntryType key_type,
742701
* positive is more harmful this might not be.
743702
*
744703
* Works even if the database has no map file.
704+
* The caller must hold a shared or exclusive lock on the
705+
* tde_lwlock_enc_keys() lock.
745706
*/
746707
int
747708
pg_tde_count_relations(Oid dbOid)
748709
{
749710
char db_map_path[MAXPGPATH];
750-
LWLock *lock_pk = tde_lwlock_enc_keys();
751711
File map_fd;
752712
off_t curr_pos = 0;
753713
TDEMapEntry map_entry;
754714
int count = 0;
755715

756-
pg_tde_set_db_file_path(dbOid, db_map_path);
716+
Assert(LWLockHeldByMeInMode(tde_lwlock_enc_keys(), LW_SHARED) || LWLockHeldByMeInMode(tde_lwlock_enc_keys(), LW_EXCLUSIVE));
757717

758-
LWLockAcquire(lock_pk, LW_SHARED);
718+
pg_tde_set_db_file_path(dbOid, db_map_path);
759719

760720
map_fd = pg_tde_open_file_read(db_map_path, true, &curr_pos);
761721
if (map_fd < 0)
@@ -769,8 +729,6 @@ pg_tde_count_relations(Oid dbOid)
769729

770730
close(map_fd);
771731

772-
LWLockRelease(lock_pk);
< 8000 code>773-
774732
return count;
775733
}
776734

contrib/pg_tde/src/catalog/tde_principal_key.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ pg_tde_delete_key(PG_FUNCTION_ARGS)
608608
* If database has something encryted, we can try to fallback to the
609609
* default principal key
610610
*/
611-
if (!pg_tde_is_key_map_empty(MyDatabaseId))
611+
if (pg_tde_count_relations(MyDatabaseId) != 0)
612612
{
613613
default_principal_key = GetPrincipalKeyNoDefault(DEFAULT_DATA_TDE_OID, LW_EXCLUSIVE);
614614
if (default_principal_key == NULL)
@@ -684,7 +684,7 @@ pg_tde_delete_default_key(PG_FUNCTION_ARGS)
684684
* If database key map is empty we should remove it befroe
685685
* deleting default principal key
686686
*/
687-
if (pg_tde_is_key_map_empty(dbOid))
687+
if (pg_tde_count_relations(dbOid) == 0)
688688
{
689689
pg_tde_delete_principal_key(dbOid, true);
690690
clear_principal_key_cache(dbOid);

contrib/pg_tde/src/pg_tde_event_capture.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,11 @@ pg_tde_proccess_utility(PlannedStmt *pstmt,
633633

634634
if (dbOid != InvalidOid)
635635
{
636-
int count = pg_tde_count_relations(dbOid);
636+
int count;
637+
638+
LWLockAcquire(tde_lwlock_enc_keys(), LW_SHARED);
639+
count = pg_tde_count_relations(dbOid);
640+
LWLockRelease(tde_lwlock_enc_keys());
637641

638642
if (count > 0)
639643
ereport(ERROR,

0 commit comments

Comments
 (0)
0