Skip to content

Commit 91997fb

Browse files
committed
Remove RelFileLocator from the WAL key file
Since the RelFileLocator has never actually been used for WAL keys we can remove all traces of it from the new file and from the code.
1 parent ac0c58d commit 91997fb

File tree

4 files changed

+17
-46
lines changed

4 files changed

+17
-46
lines changed

contrib/pg_tde/src/access/pg_tde_xlog_keys.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ static WALKeyCacheRec *tde_wal_key_last_rec = NULL;
3131

3232
static WALKeyCacheRec *pg_tde_add_wal_key_to_cache(WalEncryptionKey *cached_key, XLogRecPtr start_lsn);
3333
static WalEncryptionKey *pg_tde_decrypt_wal_key(TDEPrincipalKey *principal_key, WalKeyFileEntry *entry);
34-
static void pg_tde_initialize_wal_key_file_entry(WalKeyFileEntry *entry, const TDEPrincipalKey *principal_key, const RelFileLocator *rlocator, const WalEncryptionKey *rel_key_data);
34+
static void pg_tde_initialize_wal_key_file_entry(WalKeyFileEntry *entry, const TDEPrincipalKey *principal_key, const WalEncryptionKey *rel_key_data);
3535
static int pg_tde_open_wal_key_file_basic(const char *filename, int flags, bool ignore_missing);
3636
static int pg_tde_open_wal_key_file_read(const char *filename, bool ignore_missing, off_t *curr_pos);
3737
static int pg_tde_open_wal_key_file_write(const char *filename, const TDESignedPrincipalKeyInfo *signed_key_info, bool truncate, off_t *curr_pos);
3838
static bool pg_tde_read_one_wal_key_file_entry(int fd, WalKeyFileEntry *entry, off_t *offset);
39-
static void pg_tde_read_one_wal_key_file_entry2(int fd, int32 key_index, WalKeyFileEntry *entry, Oid databaseId);
39+
static void pg_tde_read_one_wal_key_file_entry2(int fd, int32 key_index, WalKeyFileEntry *entry);
4040
static void pg_tde_wal_key_file_header_read(const char *filename, int fd, WalKeyFileHeader *fheader, off_t *bytes_read);
4141
static int pg_tde_wal_key_file_header_write(const char *filename, int fd, const TDESignedPrincipalKeyInfo *signed_key_info, off_t *bytes_written);
4242
static void pg_tde_write_one_wal_key_file_entry(int fd, const WalKeyFileEntry *entry, off_t *offset, const char *db_map_path);
43-
static void pg_tde_write_wal_key_file_entry(const RelFileLocator *rlocator, const WalEncryptionKey *rel_key_data, TDEPrincipalKey *principal_key);
43+
static void pg_tde_write_wal_key_file_entry(const WalEncryptionKey *rel_key_data, TDEPrincipalKey *principal_key);
4444

4545
static char *
4646
get_wal_key_file_path(void)
@@ -129,15 +129,13 @@ pg_tde_wal_last_key_set_lsn(XLogRecPtr lsn)
129129
* with the actual lsn by the first WAL write.
130130
*/
131131
void
132-
pg_tde_create_wal_key(WalEncryptionKey *rel_key_data,
133-
const RelFileLocator *newrlocator,
134-
TDEMapEntryType entry_type)
132+
pg_tde_create_wal_key(WalEncryptionKey *rel_key_data, TDEMapEntryType entry_type)
135133
{
136134
TDEPrincipalKey *principal_key;
137135

138136
LWLockAcquire(tde_lwlock_enc_keys(), LW_EXCLUSIVE);
139137

140-
principal_key = GetPrincipalKey(newrlocator->dbOid, LW_EXCLUSIVE);
138+
principal_key = GetPrincipalKey(GLOBAL_DATA_TDE_OID, LW_EXCLUSIVE);
141139
if (principal_key == NULL)
142140
{
143141
ereport(ERROR,
@@ -160,7 +158,7 @@ pg_tde_create_wal_key(WalEncryptionKey *rel_key_data,
160158
errmsg("could not generate IV for WAL encryption key: %s",
161159
ERR_error_string(ERR_get_error(), NULL)));
162160

163-
pg_tde_write_wal_key_file_entry(newrlocator, rel_key_data, principal_key);
161+
pg_tde_write_wal_key_file_entry(rel_key_data, principal_key);
164162

165163
#ifdef FRONTEND
166164
free(principal_key);
@@ -186,7 +184,6 @@ pg_tde_get_wal_cache_keys(void)
186184
WalEncryptionKey *
187185
pg_tde_read_last_wal_key(void)
188186
{
189-
RelFileLocator rlocator = GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID);
190187
off_t read_pos = 0;
191188
LWLock *lock_pk = tde_lwlock_enc_keys();
192189
TDEPrincipalKey *principal_key;
@@ -197,7 +194,7 @@ pg_tde_read_last_wal_key(void)
197194
off_t fsize;
198195

199196
LWLockAcquire(lock_pk, LW_EXCLUSIVE);
200-
principal_key = GetPrincipalKey(rlocator.dbOid, LW_EXCLUSIVE);
197+
principal_key = GetPrincipalKey(GLOBAL_DATA_TDE_OID, LW_EXCLUSIVE);
201198
if (principal_key == NULL)
202199
{
203200
LWLockRelease(lock_pk);
@@ -219,7 +216,7 @@ pg_tde_read_last_wal_key(void)
219216
}
220217

221218
file_idx = ((fsize - sizeof(WalKeyFileHeader)) / sizeof(WalKeyFileEntry)) - 1;
222-
pg_tde_read_one_wal_key_file_entry2(fd, file_idx, &entry, rlocator.dbOid);
219+
pg_tde_read_one_wal_key_file_entry2(fd, file_idx, &entry);
223220

224221
rel_key_data = pg_tde_decrypt_wal_key(principal_key, &entry);
225222
#ifdef FRONTEND
@@ -235,7 +232,6 @@ pg_tde_read_last_wal_key(void)
235232
WALKeyCacheRec *
236233
pg_tde_fetch_wal_keys(XLogRecPtr start_lsn)
237234
{
238-
RelFileLocator rlocator = GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID);
239235
off_t read_pos = 0;
240236
LWLock *lock_pk = tde_lwlock_enc_keys();
241237
TDEPrincipalKey *principal_key;
@@ -244,7 +240,7 @@ pg_tde_fetch_wal_keys(XLogRecPtr start_lsn)
244240
WALKeyCacheRec *return_wal_rec = NULL;
245241

246242
LWLockAcquire(lock_pk, LW_SHARED);
247-
principal_key = GetPrincipalKey(rlocator.dbOid, LW_SHARED);
243+
principal_key = GetPrincipalKey(GLOBAL_DATA_TDE_OID, LW_SHARED);
248244
if (principal_key == NULL)
249245
{
250246
LWLockRelease(lock_pk);
@@ -283,7 +279,7 @@ pg_tde_fetch_wal_keys(XLogRecPtr start_lsn)
283279
{
284280
WalKeyFileEntry entry;
285281

286-
pg_tde_read_one_wal_key_file_entry2(fd, file_idx, &entry, rlocator.dbOid);
282+
pg_tde_read_one_wal_key_file_entry2(fd, file_idx, &entry);
287283

288284
/*
289285
* Skip new (just created but not updated by write) and invalid keys
@@ -496,8 +492,7 @@ pg_tde_read_one_wal_key_file_entry(int fd,
496492
static void
497493
pg_tde_read_one_wal_key_file_entry2(int fd,
498494
int32 key_index,
499-
WalKeyFileEntry *entry,
500-
Oid databaseId)
495+
WalKeyFileEntry *entry)
501496
{
502497
off_t read_pos;
503498

@@ -512,17 +507,14 @@ pg_tde_read_one_wal_key_file_entry2(int fd,
512507
}
513508

514509
static void
515-
pg_tde_write_wal_key_file_entry(const RelFileLocator *rlocator,
516-
const WalEncryptionKey *rel_key_data,
510+
pg_tde_write_wal_key_file_entry(const WalEncryptionKey *rel_key_data,
517511
TDEPrincipalKey *principal_key)
518512
{
519513
int fd;
520514
off_t curr_pos = 0;
521515
WalKeyFileEntry write_entry;
522516
TDESignedPrincipalKeyInfo signed_key_Info;
523517

524-
Assert(rlocator);
525-
526518
pg_tde_sign_principal_key_info(&signed_key_Info, principal_key);
527519

528520
/* Open and validate file for basic correctness. */
@@ -552,7 +544,7 @@ pg_tde_write_wal_key_file_entry(const RelFileLocator *rlocator,
552544
}
553545

554546
/* Initialize WAL key file entry and encrypt key */
555-
pg_tde_initialize_wal_key_file_entry(&write_entry, principal_key, rlocator, rel_key_data);
547+
pg_tde_initialize_wal_key_file_entry(&write_entry, principal_key, rel_key_data);
556548

557549
/* Write the given entry at curr_pos; i.e. the free entry. */
558550
pg_tde_write_one_wal_key_file_entry(fd, &write_entry, &curr_pos, get_wal_key_file_path());
@@ -610,11 +602,8 @@ pg_tde_write_one_wal_key_file_entry(int fd,
610602
static void
611603
pg_tde_initialize_wal_key_file_entry(WalKeyFileEntry *entry,
612604
const TDEPrincipalKey *principal_key,
613-
const RelFileLocator *rlocator,
614605
const WalEncryptionKey *rel_key_data)
615606
{
616-
entry->spcOid = rlocator->spcOid;
617-
entry->relNumber = rlocator->relNumber;
618607
entry->type = rel_key_data->type;
619608
entry->enc_key = *rel_key_data;
620609

@@ -663,7 +652,6 @@ pg_tde_perform_rotate_server_key(TDEPrincipalKey *principal_key,
663652
WalEncryptionKey *key;
664653
WalKeyFileEntry read_map_entry;
665654
WalKeyFileEntry write_map_entry;
666-
RelFileLocator rloc = GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID);
667655

668656
if (!pg_tde_read_one_wal_key_file_entry(old_fd, &read_map_entry, &old_curr_pos))
669657
break;
@@ -673,7 +661,7 @@ pg_tde_perform_rotate_server_key(TDEPrincipalKey *principal_key,
673661

674662
/* Decrypt and re-encrypt key */
675663
key = pg_tde_decrypt_wal_key(principal_key, &read_map_entry);
676-
pg_tde_initialize_wal_key_file_entry(&write_map_entry, new_principal_key, &rloc, key);
664+
pg_tde_initialize_wal_key_file_entry(&write_map_entry, new_principal_key, key);
677665

678666
pg_tde_write_one_wal_key_file_entry(new_fd, &write_map_entry, &new_curr_pos, tmp_path);
679667

contrib/pg_tde/src/access/pg_tde_xlog_smgr.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,11 @@ TDEXLogSmgrInitWrite(bool encrypt_xlog)
207207
*/
208208
if (encrypt_xlog)
209209
{
210-
pg_tde_create_wal_key(&EncryptionKey, &GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID),
211-
TDE_KEY_TYPE_WAL_ENCRYPTED);
210+
pg_tde_create_wal_key(&EncryptionKey, TDE_KEY_TYPE_WAL_ENCRYPTED);
212211
}
213212
else if (key && key->type == TDE_KEY_TYPE_WAL_ENCRYPTED)
214213
{
215-
pg_tde_create_wal_key(&EncryptionKey, &GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID),
216-
TDE_KEY_TYPE_WAL_UNENCRYPTED);
214+
pg_tde_create_wal_key(&EncryptionKey, TDE_KEY_TYPE_WAL_UNENCRYPTED);
217215
}
218216
else if (key)
219217
{

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define PG_TDE_XLOG_KEYS_H
33

44
#include "access/xlog_internal.h"
5-
#include "storage/relfilelocator.h"
65

76
#include "access/pg_tde_tdemap.h"
87
#include "catalog/tde_principal_key.h"
@@ -19,8 +18,6 @@ typedef struct WalEncryptionKey
1918

2019
typedef struct WalKeyFileEntry
2120
{
22-
Oid spcOid;
23-
RelFileNumber relNumber;
2421
uint32 type;
2522
WalEncryptionKey enc_key;
2623
/* IV and tag used when encrypting the key itself */
@@ -50,7 +47,7 @@ typedef struct WALKeyCacheRec
5047
} WALKeyCacheRec;
5148

5249
extern int pg_tde_count_wal_keys_in_file(void);
53-
extern void pg_tde_create_wal_key(WalEncryptionKey *rel_key_data, const RelFileLocator *newrlocator, TDEMapEntryType entry_type);
50+
extern void pg_tde_create_wal_key(WalEncryptionKey *rel_key_data, TDEMapEntryType entry_type);
5451
extern void pg_tde_delete_server_key(void);
5552
extern WALKeyCacheRec *pg_tde_fetch_wal_keys(XLogRecPtr start_lsn);
5653
extern WALKeyCacheRec *pg_tde_get_last_wal_key(void);

contrib/pg_tde/src/include/catalog/tde_global_space.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,4 @@
1414
#define GLOBAL_DATA_TDE_OID GLOBALTABLESPACE_OID
1515
#define DEFAULT_DATA_TDE_OID DEFAULTTABLESPACE_OID
1616

17-
/*
18-
* This oid can be anything since the database oid is gauranteed to not be a
19-
* real database.
20-
*/
21-
#define XLOG_TDE_OID 1
22-
23-
#define GLOBAL_SPACE_RLOCATOR(_obj_oid) (RelFileLocator) { \
24-
GLOBALTABLESPACE_OID, \
25-
GLOBAL_DATA_TDE_OID, \
26-
_obj_oid \
27-
}
28-
2917
#endif /* TDE_GLOBAL_CATALOG_H */

0 commit comments

Comments
 (0)