@@ -408,16 +408,13 @@ process_remote_message(StringInfo s)
408
408
if (MtmVacuumStmt != NULL ) {
409
409
ExecVacuum (MtmVacuumStmt , 1 );
410
410
} else if (MtmIndexStmt != NULL ) {
411
- MemoryContext oldContext = MemoryContextSwitchTo (MtmApplyContext );
412
411
Oid relid = RangeVarGetRelidExtended (MtmIndexStmt -> relation , ShareUpdateExclusiveLock ,
413
412
false, false,
414
413
NULL ,
415
414
NULL );
416
415
/* Run parse analysis ... */
417
416
MtmIndexStmt = transformIndexStmt (relid , MtmIndexStmt , messageBody );
418
417
419
- MemoryContextSwitchTo (oldContext );
420
-
421
418
DefineIndex (relid , /* OID of heap relation */
422
419
MtmIndexStmt ,
423
420
InvalidOid , /* no predefined OID */
@@ -618,6 +615,7 @@ read_rel(StringInfo s, LOCKMODE mode)
618
615
RangeVar * rv ;
619
616
Oid remote_relid = pq_getmsgint (s , 4 );
620
617
Oid local_relid ;
618
+ MemoryContext old_context ;
621
619
622
620
local_relid = pglogical_relid_map_get (remote_relid );
623
621
if (local_relid == InvalidOid ) {
@@ -630,7 +628,9 @@ read_rel(StringInfo s, LOCKMODE mode)
630
628
rv -> relname = (char * ) pq_getmsgbytes (s , relnamelen );
631
629
632
630
local_relid = RangeVarGetRelidExtended (rv , mode , false, false, NULL , NULL );
631
+ old_context = MemoryContextSwitchTo (TopMemoryContext );
633
632
pglogical_relid_map_put (remote_relid , local_relid );
633
+ MemoryContextSwitchTo (old_context );
634
634
} else {
635
635
nspnamelen = pq_getmsgbyte (s );
636
636
s -> cursor += nspnamelen ;
@@ -1060,7 +1060,8 @@ void MtmExecutor(void* work, size_t size)
1060
1060
int spill_file = -1 ;
1061
1061
int save_cursor = 0 ;
1062
1062
int save_len = 0 ;
1063
- MemoryContext topContext ;
1063
+ MemoryContext old_context ;
1064
+ MemoryContext top_context ;
1064
1065
1065
1066
s .data = work ;
1066
1067
s .len = size ;
@@ -1074,13 +1075,15 @@ void MtmExecutor(void* work, size_t size)
1074
1075
ALLOCSET_DEFAULT_INITSIZE ,
1075
1076
ALLOCSET_DEFAULT_MAXSIZE );
1076
1077
}
1077
- topContext = MemoryContextSwitchTo (MtmApplyContext );
1078
-
1078
+ top_context = MemoryContextSwitchTo (MtmApplyContext );
1079
1079
replorigin_session_origin = InvalidRepOriginId ;
1080
1080
PG_TRY ();
1081
1081
{
1082
- while (true) {
1082
+ bool inside_transaction = true;
1083
+ do {
1083
1084
char action = pq_getmsgbyte (& s );
1085
+ old_context = MemoryContextSwitchTo (MtmApplyContext );
1086
+
1084
1087
MTM_LOG2 ("%d: REMOTE process action %c" , MyProcPid , action );
1085
1088
#if 0
1086
1089
if (Mtm -> status == MTM_RECOVERY ) {
@@ -1091,84 +1094,81 @@ void MtmExecutor(void* work, size_t size)
1091
1094
switch (action ) {
1092
1095
/* BEGIN */
1093
1096
case 'B' :
1094
- if (process_remote_begin (& s )) {
1095
- continue ;
1096
- } else {
1097
- break ;
1098
- }
1097
+ inside_transaction = process_remote_begin (& s );
1098
+ break ;
1099
1099
/* COMMIT */
1100
1100
case 'C' :
1101
1101
close_rel (rel );
1102
1102
process_remote_commit (& s );
1103
+ inside_transaction = false;
1103
1104
break ;
1104
1105
/* INSERT */
1105
1106
case 'I' :
1106
- process_remote_insert (& s , rel );
1107
- continue ;
1107
+ process_remote_insert (& s , rel );
1108
+ break ;
1108
1109
/* UPDATE */
1109
1110
case 'U' :
1110
1111
process_remote_update (& s , rel );
1111
- continue ;
1112
+ break ;
1112
1113
/* DELETE */
1113
1114
case 'D' :
1114
1115
process_remote_delete (& s , rel );
1115
- continue ;
1116
+ break ;
1116
1117
case 'R' :
1117
1118
close_rel (rel );
1118
1119
rel = read_rel (& s , RowExclusiveLock );
1119
- continue ;
1120
+ break ;
1120
1121
case 'F' :
1121
1122
{
1122
1123
int node_id = pq_getmsgint (& s , 4 );
1123
1124
int file_id = pq_getmsgint (& s , 4 );
1124
1125
Assert (spill_file < 0 );
1125
1126
spill_file = MtmOpenSpillFile (node_id , file_id );
1126
- continue ;
1127
+ break ;
1127
1128
}
1128
1129
case '(' :
1129
1130
{
1130
1131
size_t size = pq_getmsgint (& s , 4 );
1131
- s .data = palloc ( size );
1132
+ s .data = MemoryContextAlloc ( TopMemoryContext , size );
1132
1133
save_cursor = s .cursor ;
1133
1134
save_len = s .len ;
1134
1135
s .cursor = 0 ;
1135
1136
s .len = size ;
1136
1137
MtmReadSpillFile (spill_file , s .data , size );
1137
- continue ;
1138
+ break ;
1138
1139
}
1139
1140
case ')' :
1140
1141
{
1141
1142
pfree (s .data );
1142
1143
s .data = work ;
1143
1144
s .cursor = save_cursor ;
1144
1145
s .len = save_len ;
1145
- continue ;
1146
+ break ;
1146
1147
}
1147
1148
case 'M' :
1148
1149
{
1149
- if (process_remote_message (& s )) {
1150
- break ;
1151
- }
1152
- continue ;
1150
+ inside_transaction = !process_remote_message (& s );
1151
+ break ;
1153
1152
}
1154
1153
case 'Z' :
1155
1154
{
1156
1155
MtmRecoveryCompleted ();
1156
+ inside_transaction = false;
1157
1157
break ;
1158
1158
}
1159
1159
default :
1160
1160
MTM_ELOG (ERROR , "unknown action of type %c" , action );
1161
1161
}
1162
- break ;
1163
- }
1162
+ MemoryContextSwitchTo (old_context );
1163
+ MemoryContextResetAndDeleteChildren (MtmApplyContext );
1164
+ } while (inside_transaction );
1164
1165
}
1165
1166
PG_CATCH ();
1166
1167
{
1167
- MemoryContext oldcontext ;
1168
1168
MtmReleaseLock ();
1169
- oldcontext = MemoryContextSwitchTo (MtmApplyContext );
1169
+ old_context = MemoryContextSwitchTo (MtmApplyContext );
1170
1170
MtmHandleApplyError ();
1171
- MemoryContextSwitchTo (oldcontext );
1171
+ MemoryContextSwitchTo (old_context );
1172
1172
EmitErrorReport ();
1173
1173
FlushErrorState ();
1174
1174
MTM_LOG1 ("%d: REMOTE begin abort transaction %llu" , MyProcPid , (long64 )MtmGetCurrentTransactionId ());
@@ -1178,12 +1178,15 @@ void MtmExecutor(void* work, size_t size)
1178
1178
MTM_LOG2 ("%d: REMOTE end abort transaction %llu" , MyProcPid , (long64 )MtmGetCurrentTransactionId ());
1179
1179
}
1180
1180
PG_END_TRY ();
1181
+ if (s .data != work ) {
1182
+ pfree (s .data );
1183
+ }
1181
1184
#if 0 /* spill file is expecrted to be closed by tranaction commit or rollback */
1182
1185
if (spill_file >= 0 ) {
1183
1186
MtmCloseSpillFile (spill_file );
1184
1187
}
1185
1188
#endif
1189
+ MemoryContextSwitchTo (top_context );
1186
1190
MemoryContextResetAndDeleteChildren (MtmApplyContext );
1187
- MemoryContextSwitchTo (topContext );
1188
1191
}
1189
1192
0 commit comments