@@ -70,6 +70,26 @@ static bool tde_smgr_has_temp_key(const RelFileLocator *rel);
70
70
static void tde_smgr_remove_temp_key (const RelFileLocator * rel );
71
71
static void CalcBlockIv (ForkNumber forknum , BlockNumber bn , const unsigned char * base_iv , unsigned char * iv );
72
72
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
+
73
93
static InternalKey *
74
94
tde_smgr_create_key (const RelFileLocatorBackend * smgr_rlocator )
75
95
{
@@ -80,23 +100,14 @@ tde_smgr_create_key(const RelFileLocatorBackend *smgr_rlocator)
80
100
if (RelFileLocatorBackendIsTemp (* smgr_rlocator ))
81
101
tde_smgr_save_temp_key (& smgr_rlocator -> locator , key );
82
102
else
103
+ {
83
104
pg_tde_save_smgr_key (smgr_rlocator -> locator , key );
105
+ tde_smgr_log_create_key (& smgr_rlocator -> locator );
106
+ }
84
107
85
108
return key ;
86
109
}
87
110
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
-
100
111
void
101
112
tde_smgr_create_key_redo (const RelFileLocator * rlocator )
102
113
{
@@ -111,21 +122,26 @@ tde_smgr_create_key_redo(const RelFileLocator *rlocator)
111
122
}
112
123
113
124
static void
114
- tde_smgr_delete_key (const RelFileLocatorBackend * smgr_rlocator )
125
+ tde_smgr_remove_key (const RelFileLocatorBackend * smgr_rlocator )
115
126
{
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
+ }
121
132
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
+ }
125
141
}
126
142
127
143
void
128
- tde_smgr_delete_key_redo (const RelFileLocator * rlocator )
144
+ tde_smgr_remove_leftover_key_redo (const RelFileLocator * rlocator )
129
145
{
130
146
pg_tde_free_key_map_entry (* rlocator );
131
147
}
@@ -148,15 +164,6 @@ tde_smgr_get_key(const RelFileLocatorBackend *smgr_rlocator)
148
164
return pg_tde_get_smgr_key (smgr_rlocator -> locator );
149
165
}
150
166
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
-
160
167
static bool
161
168
tde_smgr_should_encrypt (const RelFileLocatorBackend * smgr_rlocator , RelFileLocator * old_locator )
162
169
{
@@ -384,7 +391,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
384
391
* If we're in redo, a separate WAL record will make sure the key is
385
392
* removed.
386
393
*/
387
- tde_smgr_delete_key (& reln -> smgr_rlocator );
394
+ tde_smgr_remove_leftover_key (& reln -> smgr_rlocator );
388
395
389
396
if (!tde_smgr_should_encrypt (& reln -> smgr_rlocator , & relold ))
390
397
{
@@ -393,7 +400,6 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
393
400
}
394
401
395
402
key = tde_smgr_create_key (& reln -> smgr_rlocator );
396
- tde_smgr_log_create_key (& reln -> smgr_rlocator );
397
403
398
404
tdereln -> encryption_status = RELATION_KEY_AVAILABLE ;
399
405
tdereln -> relKey = * key ;
0 commit comments