13
13
#include "postgres.h"
14
14
#include "access/pg_tde_tdemap.h"
15
15
#include "common/file_perm.h"
16
- #include "transam/pg_tde_xact_handler.h"
17
16
#include "storage/fd.h"
18
17
#include "utils/wait_event.h"
19
18
#include "utils/memutils.h"
@@ -129,7 +128,6 @@ static int pg_tde_file_header_write(const char *tde_filename, int fd, const TDES
129
128
static void pg_tde_sign_principal_key_info (TDESignedPrincipalKeyInfo * signed_key_info , const TDEPrincipalKey * principal_key );
130
129
static off_t pg_tde_write_one_map_entry (int fd , const TDEMapEntry * map_entry , off_t * offset , const char * db_map_path );
131
130
static void pg_tde_write_key_map_entry (const RelFileLocator * rlocator , InternalKey * rel_key_data , TDEPrincipalKey * principal_key , bool write_xlog );
132
- static bool pg_tde_delete_map_entry (const RelFileLocator * rlocator , char * db_map_path , off_t offset );
133
131
static int keyrotation_init_file (const TDESignedPrincipalKeyInfo * signed_key_info , char * rotated_filename , const char * filename , off_t * curr_pos );
134
132
static void finalize_key_rotation (const char * path_old , const char * path_new );
135
133
static int pg_tde_open_file_write (const char * tde_filename , const TDESignedPrincipalKeyInfo * signed_key_info , bool truncate , off_t * curr_pos );
@@ -486,9 +484,6 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
486
484
487
485
/* Let's close the file. */
488
486
close (map_fd );
489
-
490
- /* Register the entry to be freed in case the transaction aborts */
491
- RegisterEntryForDeletion (rlocator , curr_pos , false);
492
487
}
493
488
494
489
/*
@@ -548,51 +543,40 @@ pg_tde_write_key_map_entry_redo(const TDEMapEntry *write_map_entry, TDESignedPri
548
543
LWLockRelease (tde_lwlock_enc_keys ());
549
544
}
550
545
551
- static bool
552
- pg_tde_delete_map_entry (const RelFileLocator * rlocator , char * db_map_path , off_t offset )
546
+ /*
547
+ * Mark relation map entry as free and overwrite the key
548
+ *
549
+ * This fucntion is called by the pg_tde SMGR when storage is unlinked on
550
+ * transaction commit/abort.
551
+ */
552
+ void
553
+ pg_tde_free_key_map_entry (const RelFileLocator * rlocator )
553
554
{
555
+ char db_map_path [MAXPGPATH ];
554
556
File map_fd ;
555
- bool found = false;
556
557
off_t curr_pos = 0 ;
557
558
558
- /* Open and validate file for basic correctness. */
559
- map_fd = pg_tde_open_file_write (db_map_path , NULL , false, & curr_pos );
559
+ Assert (rlocator );
560
560
561
- /*
562
- * If we need to delete an entry, we expect an offset value to the start
563
- * of the entry to speed up the operation. Otherwise, we'd be sequentially
564
- * scanning the entire map file.
565
- */
566
- if (offset > 0 )
567
- {
568
- curr_pos = lseek (map_fd , offset , SEEK_SET );
561
+ pg_tde_set_db_file_path (rlocator -> dbOid , db_map_path );
569
562
570
- if (curr_pos == -1 )
571
- {
572
- ereport (ERROR ,
573
- errcode_for_file_access (),
574
- errmsg ("could not seek in tde map file \"%s\": %m" ,
575
- db_map_path ));
576
- }
577
- }
563
+ LWLockAcquire (tde_lwlock_enc_keys (), LW_EXCLUSIVE );
564
+
565
+ /* Open and validate file for basic correctness. */
566
+ map_fd = pg_tde_open_file_write (db_map_path , NULL , false, & curr_pos );
578
567
579
- /*
580
- * Read until we find an empty slot. Otherwise, read until end. This seems
581
- * to be less frequent than vacuum. So let's keep this function here
582
- * rather than overloading the vacuum process.
583
- */
584
568
while (1 )
585
569
{
586
570
TDEMapEntry read_map_entry ;
587
571
off_t prev_pos = curr_pos ;
572
+ bool found ;
588
573
589
574
found = pg_tde_read_one_map_entry (map_fd , rlocator , MAP_ENTRY_VALID , & read_map_entry , & curr_pos );
590
575
591
576
/* We've reached EOF */
592
577
if (curr_pos == prev_pos )
593
578
break ;
594
579
595
- /* We found a valid entry for the relation */
596
580
if (found )
597
581
{
598
582
TDEMapEntry empty_map_entry = {
@@ -607,52 +591,9 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
607
591
}
608
592
}
609
593
610
- /* Let's close the file. */
611
594
close (map_fd );
612
595
613
- /* Return -1 indicating that no entry was removed */
614
- return found ;
615
- }
616
-
617
- /*
618
- * Called when transaction is being completed; either committed or aborted.
619
- * By default, when a transaction creates an entry, we mark it as MAP_ENTRY_VALID.
620
- * Only during the abort phase of the transaction that we are proceed on with
621
- * marking the entry as MAP_ENTRY_FREE. This optimistic strategy that assumes
622
- * that transaction will commit more often then getting aborted avoids
623
- * unnecessary locking.
624
- *
625
- * The offset allows us to simply seek to the desired location and mark the entry
626
- * as MAP_ENTRY_FREE without needing any further processing.
627
- */
628
- void
629
- pg_tde_free_key_map_entry (const RelFileLocator * rlocator , off_t offset )
630
- {
631
- bool found ;
632
- char db_map_path [MAXPGPATH ] = {0 };
633
-
634
- Assert (rlocator );
635
-
636
- /* Get the file paths */
637
- pg_tde_set_db_file_path (rlocator -> dbOid , db_map_path );
638
-
639
- LWLockAcquire (tde_lwlock_enc_keys (), LW_EXCLUSIVE );
640
-
641
- /* Remove the map entry if found */
642
- found = pg_tde_delete_map_entry (rlocator , db_map_path , offset );
643
-
644
596
LWLockRelease (tde_lwlock_enc_keys ());
645
-
646
- if (!found )
647
- {
648
- ereport (WARNING ,
649
- errcode (ERRCODE_NO_DATA_FOUND ),
650
- errmsg ("could not find the required map entry for deletion of relation %d in tablespace %d in tde map file \"%s\": %m" ,
651
- rlocator -> relNumber ,
652
- rlocator -> spcOid ,
653
- db_map_path ));
654
-
655
- }
656
597
}
657
598
658
599
/*
0 commit comments