1313#include "postgres.h"
1414#include "access/pg_tde_tdemap.h"
1515#include "common/file_perm.h"
16- #include "transam/pg_tde_xact_handler.h"
1716#include "storage/fd.h"
1817#include "utils/wait_event.h"
1918#include "utils/memutils.h"
@@ -129,7 +128,6 @@ static int pg_tde_file_header_write(const char *tde_filename, int fd, const TDES
129128static void pg_tde_sign_principal_key_info (TDESignedPrincipalKeyInfo * signed_key_info , const TDEPrincipalKey * principal_key );
130129static off_t pg_tde_write_one_map_entry (int fd , const TDEMapEntry * map_entry , off_t * offset , const char * db_map_path );
131130static 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 );
133131static int keyrotation_init_file (const TDESignedPrincipalKeyInfo * signed_key_info , char * rotated_filename , const char * filename , off_t * curr_pos );
134132static void finalize_key_rotation (const char * path_old , const char * path_new );
135133static 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_
486484
487485 /* Let's close the file. */
488486 close (map_fd );
489-
490- /* Register the entry to be freed in case the transaction aborts */
491- RegisterEntryForDeletion (rlocator , curr_pos , false);
492487}
493488
494489/*
@@ -548,51 +543,40 @@ pg_tde_write_key_map_entry_redo(const TDEMapEntry *write_map_entry, TDESignedPri
548543 LWLockRelease (tde_lwlock_enc_keys ());
549544}
550545
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 )
553554{
555+ char db_map_path [MAXPGPATH ];
554556 File map_fd ;
555- bool found = false;
556557 off_t curr_pos = 0 ;
557558
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 );
560560
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 );
569562
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 );
578567
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- */
584568 while (1 )
585569 {
586570 TDEMapEntry read_map_entry ;
587571 off_t prev_pos = curr_pos ;
572+ bool found ;
588573
589574 found = pg_tde_read_one_map_entry (map_fd , rlocator , MAP_ENTRY_VALID , & read_map_entry , & curr_pos );
590575
591576 /* We've reached EOF */
592577 if (curr_pos == prev_pos )
593578 break ;
594579
595- /* We found a valid entry for the relation */
596580 if (found )
597581 {
598582 TDEMapEntry empty_map_entry = {
@@ -607,52 +591,9 @@ pg_tde_delete_map_entry(const RelFileLocator *rlocator, char *db_map_path, off_t
607591 }
608592 }
609593
610- /* Let's close the file. */
611594 close (map_fd );
612595
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-
644596 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- }
656597}
657598
658599/*
0 commit comments