Skip to content

Commit 01a8ee3

Browse files
authored
Update WAL keys pointers after resizing the key cache (#151)
After the tde_rel_key_cache resize, pointers in tde_wal_key_cache became invalid. So we have to update those pointers after the resize. For PG-1488
1 parent 8c1d77c commit 01a8ee3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

contrib/pg_tde/src/access/pg_tde_tdemap.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ RelKeyCache tde_rel_key_cache = {
120120
};
121121

122122

123+
/*
124+
* TODO: WAL should have its own RelKeyCache
125+
*/
123126
static WALKeyCacheRec *tde_wal_key_cache = NULL;
124127
static WALKeyCacheRec *tde_wal_key_last_rec = NULL;
125128

@@ -147,6 +150,7 @@ static void pg_tde_write_keydata(char *db_keydata_path, TDEPrincipalKeyInfo *pri
147150
static void pg_tde_write_one_keydata(int keydata_fd, int32 key_index, InternalKey *enc_rel_key_data);
148151
static int keyrotation_init_file(TDEPrincipalKeyInfo *new_principal_key_info, char *rotated_filename, char *filename, bool *is_new_file, off_t *curr_pos);
149152
static void finalize_key_rotation(char *m_path_old, char *k_path_old, char *m_path_new, char *k_path_new);
153+
static void update_wal_keys_cache(void);
150154

151155
InternalKey *
152156
pg_tde_create_smgr_key(const RelFileLocatorBackend *newrlocator)
@@ -1552,6 +1556,25 @@ pg_tde_get_wal_cache_keys(void)
15521556
return tde_wal_key_cache;
15531557
}
15541558

1559+
/* Updates WAL keys cache pointers */
1560+
static void
1561+
update_wal_keys_cache(void)
1562+
{
1563+
WALKeyCacheRec *wal_rec = tde_wal_key_cache;
1564+
RelFileLocator rlocator = GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID);
1565+
1566+
for (int i = 0; i < tde_rel_key_cache.len && wal_rec; i++)
1567+
{
1568+
RelKeyCacheRec *rec = tde_rel_key_cache.data + i;
1569+
1570+
if (RelFileLocatorEquals(rec->locator, rlocator))
1571+
{
1572+
wal_rec->key = &rec->key;
1573+
wal_rec = wal_rec->next;
1574+
}
1575+
}
1576+
}
1577+
15551578
InternalKey *
15561579
pg_tde_read_last_wal_key(void)
15571580
{
@@ -1789,6 +1812,9 @@ pg_tde_put_key_into_cache(const RelFileLocator *rlocator, InternalKey *key)
17891812
elog(WARNING, "could not mlock internal key cache pages: %m");
17901813

17911814
tde_rel_key_cache.cap = (size - 1) / sizeof(RelKeyCacheRec);
1815+
1816+
/* update wal key pointers after moving the cache */
1817+
update_wal_keys_cache();
17921818
}
17931819

17941820
rec = tde_rel_key_cache.data + tde_rel_key_cache.len;

0 commit comments

Comments
 (0)