@@ -267,7 +267,7 @@ TDEXLogSmgrInitWrite(bool encrypt_xlog)
267267 if (keys == NULL )
268268 {
269269 /* TODO cache is empty, try to preread keys from disk */
270- // keys = pg_tde_fetch_wal_keys(start);
270+ keys = pg_tde_fetch_wal_keys (start );
271271 }
272272
273273 elog (WARNING , "keyloading %p" , keys );
@@ -362,9 +362,9 @@ tdeheap_xlog_seg_write(int fd, const void *buf, size_t count, off_t offset,
362362 TimeLineID tli , XLogSegNo segno , int segSize )
363363{
364364 #ifdef FRONTEND
365- bool recoveryInProgress = false;
365+ bool crashRecovery = false;
366366 #else
367- bool recoveryInProgress = RecoveryInProgress () ;
367+ bool crashRecovery = GetRecoveryState () == RECOVERY_STATE_CRASH ;
368368 #endif
369369
370370 WalLocation loc = {.tli = tli };
@@ -384,27 +384,15 @@ tdeheap_xlog_seg_write(int fd, const void *buf, size_t count, off_t offset,
384384
385385 if (EncryptionKey .type != WAL_KEY_TYPE_INVALID && !lastKeyUsable )
386386 {
387- if (!recoveryInProgress || EncryptionKey .type == WAL_KEY_TYPE_UNENCRYPTED ) {
388- // TODO: detect if we have conflicting later keys in case of unencrypted, and error out
389- pg_tde_wal_last_key_set_location (loc );
390- EncryptionKey .wal_start = loc ;
391- TDEXLogSetEncKeyLocation (EncryptionKey .wal_start );
392- lastKeyUsable = true;
393- elog (WARNING , "Keyset1" );
394- } else {
395- #ifndef FRONTEND
396- WALKeyCacheRec * last_key = pg_tde_get_last_wal_key ();
397- if (last_key && GetRecoveryState () != RECOVERY_STATE_CRASH ) {
398- if (last_key -> start .lsn < loc .lsn ) {
399- elog (WARNING , "Keyset2" );
400- // If we progressed at least one more page, it is safe to switch keys
401- pg_tde_wal_last_key_set_location (loc );
402- EncryptionKey .wal_start = loc ;
403- TDEXLogSetEncKeyLocation (EncryptionKey .wal_start );
404- lastKeyUsable = true;
405- }
387+ WALKeyCacheRec * last_key = pg_tde_get_last_wal_key ();
388+ if (!crashRecovery || EncryptionKey .type == WAL_KEY_TYPE_UNENCRYPTED ) {
389+ if (last_key == NULL || last_key -> start .lsn < loc .lsn ) {
390+ elog (WARNING , "Keyset2" );
391+ pg_tde_wal_last_key_set_location (loc );
392+ EncryptionKey .wal_start = loc ;
393+ TDEXLogSetEncKeyLocation (EncryptionKey .wal_start );
394+ lastKeyUsable = true;
406395 }
407- #endif
408396 }
409397 }
410398
@@ -454,12 +442,14 @@ TDEXLogCryptBuffer(const void *buf, void *out_buf, size_t count, off_t offset,
454442 WalLocation data_end = {.tli = tli };
455443 WalLocation data_start = {.tli = tli };
456444
457- if (! keys )
445+ if (keys == NULL )
458446 {
459447 WalLocation start = {.tli = 1 ,.lsn = 0 };
460448
461449 /* cache is empty, try to read keys from disk */
462- keys = pg_tde_fetch_wal_keys (start );
450+ pg_tde_fetch_wal_keys (start );
451+
452+ keys = pg_tde_get_wal_cache_keys ();
463453 }
464454
465455 /*
0 commit comments