@@ -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,25 @@ 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
+ pg_tde_free_key_map_entry (smgr_rlocator -> locator );
138
+ tde_smgr_log_remove_leftover_key (& smgr_rlocator -> locator );
139
+ }
125
140
}
126
141
127
142
void
128
- tde_smgr_delete_key_redo (const RelFileLocator * rlocator )
143
+ tde_smgr_remove_leftover_key_redo (const RelFileLocator * rlocator )
129
144
{
130
145
pg_tde_free_key_map_entry (* rlocator );
131
146
}
@@ -148,15 +163,6 @@ tde_smgr_get_key(const RelFileLocatorBackend *smgr_rlocator)
148
163
return pg_tde_get_smgr_key (smgr_rlocator -> locator );
149
164
}
150
165
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
166
static bool
161
167
tde_smgr_should_encrypt (const RelFileLocatorBackend * smgr_rlocator , RelFileLocator * old_locator )
162
168
{
@@ -384,7 +390,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
384
390
* If we're in redo, a separate WAL record will make sure the key is
385
391
* removed.
386
392
*/
387
- tde_smgr_delete_key (& reln -> smgr_rlocator );
393
+ tde_smgr_remove_leftover_key (& reln -> smgr_rlocator );
388
394
389
395
if (!tde_smgr_should_encrypt (& reln -> smgr_rlocator , & relold ))
390
396
{
@@ -393,7 +399,6 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
393
399
}
394
400
395
401
key = tde_smgr_create_key (& reln -> smgr_rlocator );
396
- tde_smgr_log_create_key (& reln -> smgr_rlocator );
397
402
398
403
tdereln -> encryption_status = RELATION_KEY_AVAILABLE ;
399
404
tdereln -> relKey = * key ;
0 commit comments