@@ -915,94 +915,42 @@ where
915
915
S : SchemaStore + Clone + Send + ' static ,
916
916
T : ApplyLoopHook ,
917
917
{
918
- let commit_lsn = get_commit_lsn ( state, & message) ?;
919
-
920
918
state. current_tx_events += 1 ;
921
919
922
920
match & message {
923
921
LogicalReplicationMessage :: Begin ( begin_body) => {
924
- handle_begin_message ( state, start_lsn, commit_lsn , begin_body) . await
922
+ handle_begin_message ( state, start_lsn, begin_body) . await
925
923
}
926
924
LogicalReplicationMessage :: Commit ( commit_body) => {
927
- handle_commit_message ( state, start_lsn, commit_lsn, commit_body, hook, pipeline_id)
928
- . await
925
+ handle_commit_message ( state, start_lsn, commit_body, hook, pipeline_id) . await
929
926
}
930
927
LogicalReplicationMessage :: Relation ( relation_body) => {
931
- handle_relation_message (
932
- state,
933
- start_lsn,
934
- commit_lsn,
935
- relation_body,
936
- schema_store,
937
- hook,
938
- )
939
- . await
928
+ handle_relation_message ( state, start_lsn, relation_body, schema_store, hook) . await
940
929
}
941
930
LogicalReplicationMessage :: Insert ( insert_body) => {
942
- handle_insert_message (
943
- state,
944
- start_lsn,
945
- commit_lsn,
946
- insert_body,
947
- hook,
948
- schema_store,
949
- )
950
- . await
931
+ handle_insert_message ( state, start_lsn, insert_body, hook, schema_store) . await
951
932
}
952
933
LogicalReplicationMessage :: Update ( update_body) => {
953
- handle_update_message (
954
- state,
955
- start_lsn,
956
- commit_lsn,
957
- update_body,
958
- hook,
959
- schema_store,
960
- )
961
- . await
934
+ handle_update_message ( state, start_lsn, update_body, hook, schema_store) . await
962
935
}
963
936
LogicalReplicationMessage :: Delete ( delete_body) => {
964
- handle_delete_message (
965
- state,
966
- start_lsn,
967
- commit_lsn,
968
- delete_body,
969
- hook,
970
- schema_store,
971
- )
972
- . await
937
+ handle_delete_message ( state, start_lsn, delete_body, hook, schema_store) . await
973
938
}
974
939
LogicalReplicationMessage :: Truncate ( truncate_body) => {
975
- handle_truncate_message ( state, start_lsn, commit_lsn, truncate_body, hook) . await
940
+ handle_truncate_message ( state, start_lsn, truncate_body, hook) . await
941
+ }
942
+ LogicalReplicationMessage :: Origin ( _) => {
943
+ debug ! ( "received unsupported ORIGIN message" ) ;
944
+ Ok ( HandleMessageResult :: default ( ) )
945
+ }
946
+ LogicalReplicationMessage :: Type ( _) => {
947
+ debug ! ( "received unsupported TYPE message" ) ;
948
+ Ok ( HandleMessageResult :: default ( ) )
976
949
}
977
- LogicalReplicationMessage :: Origin ( _) => Ok ( HandleMessageResult :: default ( ) ) ,
978
- LogicalReplicationMessage :: Type ( _) => Ok ( HandleMessageResult :: default ( ) ) ,
979
950
_ => Ok ( HandleMessageResult :: default ( ) ) ,
980
951
}
981
952
}
982
953
983
- /// Determines the commit LSN for a replication message based on transaction state.
984
- ///
985
- /// This function extracts the appropriate commit LSN depending on the message type.
986
- /// For `BEGIN` messages, it uses the final LSN from the message payload. For all
987
- /// other message types, it retrieves the previously stored `remote_final_lsn`
988
- /// that was set when the transaction began.
989
- fn get_commit_lsn ( state : & ApplyLoopState , message : & LogicalReplicationMessage ) -> EtlResult < PgLsn > {
990
- // If we are in a `Begin` message, the `commit_lsn` is the `final_lsn` of the payload, in all the
991
- // other cases we read the `remote_final_lsn` which should be always set in case we are within or
992
- // at the end of a transaction (meaning that the event type is different from `Begin`).
993
- if let LogicalReplicationMessage :: Begin ( message) = message {
994
- Ok ( PgLsn :: from ( message. final_lsn ( ) ) )
995
- } else {
996
- state. remote_final_lsn . ok_or_else ( || {
997
- etl_error ! (
998
- ErrorKind :: InvalidState ,
999
- "Invalid transaction" ,
1000
- "A transaction should have started for get_commit_lsn to be performed"
1001
- )
1002
- } )
1003
- }
1004
- }
1005
-
1006
954
/// Handles Postgres BEGIN messages that mark transaction boundaries.
1007
955
///
1008
956
/// This function processes transaction start events by validating the event type
@@ -1015,7 +963,6 @@ fn get_commit_lsn(state: &ApplyLoopState, message: &LogicalReplicationMessage) -
1015
963
async fn handle_begin_message (
1016
964
state : & mut ApplyLoopState ,
1017
965
start_lsn : PgLsn ,
1018
- commit_lsn : PgLsn ,
1019
966
message : & protocol:: BeginBody ,
1020
967
) -> EtlResult < HandleMessageResult > {
1021
968
// We track the final lsn of this transaction, which should be equal to the `commit_lsn` of the
@@ -1028,7 +975,7 @@ async fn handle_begin_message(
1028
975
state. current_tx_events = 0 ;
1029
976
1030
977
// Convert event from the protocol message.
1031
- let event = parse_event_from_begin_message ( start_lsn, commit_lsn , message) ;
978
+ let event = parse_event_from_begin_message ( start_lsn, final_lsn , message) ;
1032
979
1033
980
Ok ( HandleMessageResult :: return_event ( Event :: Begin ( event) ) )
1034
981
}
@@ -1046,7 +993,6 @@ async fn handle_begin_message(
1046
993
async fn handle_commit_message < T > (
1047
994
state : & mut ApplyLoopState ,
1048
995
start_lsn : PgLsn ,
1049
- commit_lsn : PgLsn ,
1050
996
message : & protocol:: CommitBody ,
1051
997
hook : & T ,
1052
998
pipeline_id : PipelineId ,
@@ -1068,14 +1014,14 @@ where
1068
1014
// If the commit lsn of the message is different from the remote final lsn, it means that the
1069
1015
// transaction that was started expect a different commit lsn in the commit message. In this case,
1070
1016
// we want to bail assuming we are in an inconsistent state.
1071
- let commit_lsn_msg = PgLsn :: from ( message. commit_lsn ( ) ) ;
1072
- if commit_lsn_msg != remote_final_lsn {
1017
+ let commit_lsn = PgLsn :: from ( message. commit_lsn ( ) ) ;
1018
+ if commit_lsn != remote_final_lsn {
1073
1019
bail ! (
1074
1020
ErrorKind :: ValidationError ,
1075
1021
"Invalid commit LSN" ,
1076
1022
format!(
1077
1023
"Incorrect commit LSN {} in COMMIT message (expected {})" ,
1078
- commit_lsn_msg , remote_final_lsn
1024
+ commit_lsn , remote_final_lsn
1079
1025
)
1080
1026
) ;
1081
1027
}
@@ -1155,7 +1101,6 @@ where
1155
1101
async fn handle_relation_message < S , T > (
1156
1102
state : & mut ApplyLoopState ,
1157
1103
start_lsn : PgLsn ,
1158
- commit_lsn : PgLsn ,
1159
1104
message : & protocol:: RelationBody ,
1160
1105
schema_store : & S ,
1161
1106
hook : & T ,
@@ -1196,7 +1141,7 @@ where
1196
1141
} ) ?;
1197
1142
1198
1143
// Convert event from the protocol message.
1199
- let event = parse_event_from_relation_message ( start_lsn, commit_lsn , message) ?;
1144
+ let event = parse_event_from_relation_message ( start_lsn, remote_final_lsn , message) ?;
1200
1145
1201
1146
// We compare the table schema from the relation message with the existing schema (if any).
1202
1147
// The purpose of this comparison is that we want to throw an error and stop the processing
@@ -1219,7 +1164,6 @@ where
1219
1164
async fn handle_insert_message < S , T > (
1220
1165
state : & mut ApplyLoopState ,
1221
1166
start_lsn : PgLsn ,
1222
- commit_lsn : PgLsn ,
1223
1167
message : & protocol:: InsertBody ,
1224
1168
hook : & T ,
1225
1169
schema_store : & S ,
@@ -1245,7 +1189,7 @@ where
1245
1189
1246
1190
// Convert event from the protocol message.
1247
1191
let event =
1248
- parse_event_from_insert_message ( schema_store, start_lsn, commit_lsn , message) . await ?;
1192
+ parse_event_from_insert_message ( schema_store, start_lsn, remote_final_lsn , message) . await ?;
1249
1193
1250
1194
Ok ( HandleMessageResult :: return_event ( Event :: Insert ( event) ) )
1251
1195
}
@@ -1254,7 +1198,6 @@ where
1254
1198
async fn handle_update_message < S , T > (
1255
1199
state : & mut ApplyLoopState ,
1256
1200
start_lsn : PgLsn ,
1257
- commit_lsn : PgLsn ,
1258
1201
message : & protocol:: UpdateBody ,
1259
1202
hook : & T ,
1260
1203
schema_store : & S ,
@@ -1280,7 +1223,7 @@ where
1280
1223
1281
1224
// Convert event from the protocol message.
1282
1225
let event =
1283
- parse_event_from_update_message ( schema_store, start_lsn, commit_lsn , message) . await ?;
1226
+ parse_event_from_update_message ( schema_store, start_lsn, remote_final_lsn , message) . await ?;
1284
1227
1285
1228
Ok ( HandleMessageResult :: return_event ( Event :: Update ( event) ) )
1286
1229
}
@@ -1289,7 +1232,6 @@ where
1289
1232
async fn handle_delete_message < S , T > (
1290
1233
state : & mut ApplyLoopState ,
1291
1234
start_lsn : PgLsn ,
1292
- commit_lsn : PgLsn ,
1293
1235
message : & protocol:: DeleteBody ,
1294
1236
hook : & T ,
1295
1237
schema_store : & S ,
@@ -1315,7 +1257,7 @@ where
1315
1257
1316
1258
// Convert event from the protocol message.
1317
1259
let event =
1318
- parse_event_from_delete_message ( schema_store, start_lsn, commit_lsn , message) . await ?;
1260
+ parse_event_from_delete_message ( schema_store, start_lsn, remote_final_lsn , message) . await ?;
1319
1261
1320
1262
Ok ( HandleMessageResult :: return_event ( Event :: Delete ( event) ) )
1321
1263
}
@@ -1329,7 +1271,6 @@ where
1329
1271
async fn handle_truncate_message < T > (
1330
1272
state : & mut ApplyLoopState ,
1331
1273
start_lsn : PgLsn ,
1332
- commit_lsn : PgLsn ,
1333
1274
message : & protocol:: TruncateBody ,
1334
1275
hook : & T ,
1335
1276
) -> EtlResult < HandleMessageResult >
@@ -1361,7 +1302,7 @@ where
1361
1302
}
1362
1303
1363
1304
// Convert event from the protocol message.
1364
- let event = parse_event_from_truncate_message ( start_lsn, commit_lsn , message, rel_ids) ;
1305
+ let event = parse_event_from_truncate_message ( start_lsn, remote_final_lsn , message, rel_ids) ;
1365
1306
1366
1307
Ok ( HandleMessageResult :: return_event ( Event :: Truncate ( event) ) )
1367
1308
}
0 commit comments