Skip to content

Commit a3d2a24

Browse files
committed
PG-1866 Reset WAL key cache on shmem init
It seems like there are cases when the postmaster have "restarted" after a backend crash where the wal cache inherited from the postmaster is wrong. I'm not at all sure exactly how and why this happens, but this patch fixes a bug with this and allows recovery/013_crash_restart to pass with WAL encryption enabled.
1 parent 621c3f8 commit a3d2a24

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

contrib/pg_tde/src/access/pg_tde_xlog_keys.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ get_wal_key_file_path(void)
7272
return wal_key_file_path;
7373
}
7474

75+
void
76+
pg_tde_free_wal_key_cache(void)
77+
{
78+
WALKeyCacheRec *rec = tde_wal_key_cache;
79+
80+
while (rec != NULL)
81+
{
82+
WALKeyCacheRec *next = rec->next;
83+
84+
pfree(rec);
85+
rec = next;
86+
}
87+
88+
tde_wal_key_cache = NULL;
89+
tde_wal_key_last_rec = NULL;
90+
}
91+
7592
void
7693
pg_tde_wal_last_key_set_location(WalLocation loc)
7794
{

contrib/pg_tde/src/access/pg_tde_xlog_smgr.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,18 @@ TDEXLogSmgrInit()
220220
void
221221
TDEXLogSmgrInitWrite(bool encrypt_xlog)
222222
{
223-
WalEncryptionKey *key = pg_tde_read_last_wal_key();
223+
WalEncryptionKey *key;
224224
WALKeyCacheRec *keys;
225225

226+
/*
227+
* If the postmaster have done a "soft" restart after a backend crash, we
228+
* may have inherited the cache in a weird state. Clearing the cache here
229+
* ensures we reinitialize all keys from disk.
230+
*/
231+
pg_tde_free_wal_key_cache();
232+
233+
key = pg_tde_read_last_wal_key();
234+
226235
/*
227236
* Always generate a new key on starting PostgreSQL to protect against
228237
* attacks on CTR ciphers based on comparing the WAL generated by two

contrib/pg_tde/src/include/access/pg_tde_xlog_keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern int pg_tde_count_wal_keys_in_file(void);
7474
extern void pg_tde_create_wal_key(WalEncryptionKey *rel_key_data, WalEncryptionKeyType entry_type);
7575
extern void pg_tde_delete_server_key(void);
7676
extern WALKeyCacheRec *pg_tde_fetch_wal_keys(WalLocation start);
77+
extern void pg_tde_free_wal_key_cache(void);
7778
extern WALKeyCacheRec *pg_tde_get_last_wal_key(void);
7879
extern TDESignedPrincipalKeyInfo *pg_tde_get_server_key_info(void);
7980
extern WALKeyCacheRec *pg_tde_get_wal_cache_keys(void);

0 commit comments

Comments
 (0)