Skip to content

Commit a392dbc

Browse files
committed
Do not try to delete keys or log WAL for temporary tables
We forgot to have a check against trying to delete leftover SMGR keys for temporary tables which is a useless operation since they are store in memory. Additionally we forgot to prevent WAL from being written when creating or removing a key in smgrcreate() for temporary tables.
1 parent 602cd73 commit a392dbc

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

contrib/pg_tde/src/access/pg_tde_xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ tdeheap_rmgr_redo(XLogReaderState *record)
6262
{
6363
XLogRelKey *xlrec = (XLogRelKey *) XLogRecGetData(record);
6464

65-
tde_smgr_delete_key_redo(&xlrec->rlocator);
65+
tde_smgr_remove_leftover_key_redo(&xlrec->rlocator);
6666
}
6767
else if (info == XLOG_TDE_ROTATE_PRINCIPAL_KEY)
6868
{

contrib/pg_tde/src/include/smgr/pg_tde_smgr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
extern void RegisterStorageMgr(void);
88
extern void tde_smgr_create_key_redo(const RelFileLocator *rlocator);
9-
extern void tde_smgr_delete_key_redo(const RelFileLocator *rlocator);
9+
extern void tde_smgr_remove_leftover_key_redo(const RelFileLocator *rlocator);
1010
extern bool tde_smgr_rel_is_encrypted(SMgrRelation reln);
1111

1212
#endif /* PG_TDE_SMGR_H */

contrib/pg_tde/src/smgr/pg_tde_smgr.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ static bool tde_smgr_has_temp_key(const RelFileLocator *rel);
7070
static void tde_smgr_remove_temp_key(const RelFileLocator *rel);
7171
static void CalcBlockIv(ForkNumber forknum, BlockNumber bn, const unsigned char *base_iv, unsigned char *iv);
7272

73+
static void
74+
tde_smgr_log_create_key(const RelFileLocator *rlocator)
75+
{
76+
XLogRelKey xlrec = {.rlocator = *rlocator};
77+
78+
XLogBeginInsert();
79+
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
80+
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_ADD_RELATION_KEY);
81+
}
82+
83+
static void
84+
tde_smgr_log_remove_leftover_key(const RelFileLocator *rlocator)
85+
{
86+
XLogRelKey xlrec = {.rlocator = *rlocator};
87+
88+
XLogBeginInsert();
89+
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
90+
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_DELETE_RELATION_KEY);
91+
}
92+
7393
static InternalKey *
7494
tde_smgr_create_key(const RelFileLocatorBackend *smgr_rlocator)
7595
{
@@ -80,23 +100,14 @@ tde_smgr_create_key(const RelFileLocatorBackend *smgr_rlocator)
80100
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
81101
tde_smgr_save_temp_key(&smgr_rlocator->locator, key);
82102
else
103+
{
83104
pg_tde_save_smgr_key(smgr_rlocator->locator, key);
105+
tde_smgr_log_create_key(&smgr_rlocator->locator);
106+
}
84107

85108
return key;
86109
}
87110

88-
static void
89-
tde_smgr_log_create_key(const RelFileLocatorBackend *smgr_rlocator)
90-
{
91-
XLogRelKey xlrec = {
92-
.rlocator = smgr_rlocator->locator,
93-
};
94-
95-
XLogBeginInsert();
96-
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
97-
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_ADD_RELATION_KEY);
98-
}
99-
100111
void
101112
tde_smgr_create_key_redo(const RelFileLocator *rlocator)
102113
{
@@ -111,21 +122,26 @@ tde_smgr_create_key_redo(const RelFileLocator *rlocator)
111122
}
112123

113124
static void
114-
tde_smgr_delete_key(const RelFileLocatorBackend *smgr_rlocator)
125+
tde_smgr_remove_key(const RelFileLocatorBackend *smgr_rlocator)
115126
{
116-
XLogRelKey xlrec = {
117-
.rlocator = smgr_rlocator->locator,
118-
};
119-
120-
pg_tde_free_key_map_entry(smgr_rlocator->locator);
127+
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
128+
tde_smgr_remove_temp_key(&smgr_rlocator->locator);
129+
else
130+
pg_tde_free_key_map_entry(smgr_rlocator->locator);
131+
}
121132

122-
XLogBeginInsert();
123-
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
124-
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_DELETE_RELATION_KEY);
133+
static void
134+
tde_smgr_remove_leftover_key(const RelFileLocatorBackend *smgr_rlocator)
135+
{
136+
if (!RelFileLocatorBackendIsTemp(*smgr_rlocator))
137+
{
138+
pg_tde_free_key_map_entry(smgr_rlocator->locator);
139+
tde_smgr_log_remove_leftover_key(&smgr_rlocator->locator);
140+
}
125141
}
126142

127143
void
128-
tde_smgr_delete_key_redo(const RelFileLocator *rlocator)
144+
tde_smgr_remove_leftover_key_redo(const RelFileLocator *rlocator)
129145
{
130146
pg_tde_free_key_map_entry(*rlocator);
131147
}
@@ -148,15 +164,6 @@ tde_smgr_get_key(const RelFileLocatorBackend *smgr_rlocator)
148164
return pg_tde_get_smgr_key(smgr_rlocator->locator);
149165
}
150166

151-
static void
152-
tde_smgr_remove_key(const RelFileLocatorBackend *smgr_rlocator)
153-
{
154-
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
155-
tde_smgr_remove_temp_key(&smgr_rlocator->locator);
156-
else
157-
pg_tde_free_key_map_entry(smgr_rlocator->locator);
158-
}
159-
160167
static bool
161168
tde_smgr_should_encrypt(const RelFileLocatorBackend *smgr_rlocator, RelFileLocator *old_locator)
162169
{
@@ -384,7 +391,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
384391
* If we're in redo, a separate WAL record will make sure the key is
385392
* removed.
386393
*/
387-
tde_smgr_delete_key(&reln->smgr_rlocator);
394+
tde_smgr_remove_leftover_key(&reln->smgr_rlocator);
388395

389396
if (!tde_smgr_should_encrypt(&reln->smgr_rlocator, &relold))
390397
{
@@ -393,7 +400,6 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
393400
}
394401

395402
key = tde_smgr_create_key(&reln->smgr_rlocator);
396-
tde_smgr_log_create_key(&reln->smgr_rlocator);
397403

398404
tdereln->encryption_status = RELATION_KEY_AVAILABLE;
399405
tdereln->relKey = *key;

0 commit comments

Comments
 (0)