Skip to content

Commit 701cf1e

Browse files
author
Amit Kapila
committed
Change the misleading local end_lsn for prepared transactions.
The apply worker was using XactLastCommitEnd as local end_lsn for applying prepare and rollback_prepare. The XactLastCommitEnd value is the end lsn of the last commit applied before the prepare transaction which makes no sense. This LSN is used to decide whether we can send the acknowledgment of the corresponding remote LSN to the server. It is okay not to set the local_end LSN with the actual WAL position for the prepare because we always flush the prepare record. So, we can send the acknowledgment of the remote_end LSN as soon as prepare is finished. The current code is misleading but as such doesn't create any problem, so decided not to backpatch. Author: Hayato Kuroda Reviewed-by: Shveta Malik, Amit Kapila Discussion: https://postgr.es/m/TYAPR01MB5692FA4926754B91E9D7B5F0F5AA2@TYAPR01MB5692.jpnprd01.prod.outlook.com
1 parent 4eb179e commit 701cf1e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/backend/replication/logical/worker.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,17 @@ apply_handle_prepare(StringInfo s)
11331133
CommitTransactionCommand();
11341134
pgstat_report_stat(false);
11351135

1136-
store_flush_position(prepare_data.end_lsn, XactLastCommitEnd);
1136+
/*
1137+
* It is okay not to set the local_end LSN for the prepare because we
1138+
* always flush the prepare record. So, we can send the acknowledgment of
1139+
* the remote_end LSN as soon as prepare is finished.
1140+
*
1141+
* XXX For the sake of consistency with commit, we could have set it with
1142+
* the LSN of prepare but as of now we don't track that value similar to
1143+
* XactLastCommitEnd, and adding it for this purpose doesn't seems worth
1144+
* it.
1145+
*/
1146+
store_flush_position(prepare_data.end_lsn, InvalidXLogRecPtr);
11371147

11381148
in_remote_transaction = false;
11391149

@@ -1251,7 +1261,12 @@ apply_handle_rollback_prepared(StringInfo s)
12511261

12521262
pgstat_report_stat(false);
12531263

1254-
store_flush_position(rollback_data.rollback_end_lsn, XactLastCommitEnd);
1264+
/*
1265+
* It is okay not to set the local_end LSN for the rollback of prepared
1266+
* transaction because we always flush the WAL record for it. See
1267+
* apply_handle_prepare.
1268+
*/
1269+
store_flush_position(rollback_data.rollback_end_lsn, InvalidXLogRecPtr);
12551270
in_remote_transaction = false;
12561271

12571272
/* Process any tables that are being synchronized in parallel. */
@@ -1306,7 +1321,11 @@ apply_handle_stream_prepare(StringInfo s)
13061321

13071322
CommitTransactionCommand();
13081323

1309-
store_flush_position(prepare_data.end_lsn, XactLastCommitEnd);
1324+
/*
1325+
* It is okay not to set the local_end LSN for the prepare because
1326+
* we always flush the prepare record. See apply_handle_prepare.
1327+
*/
1328+
store_flush_position(prepare_data.end_lsn, InvalidXLogRecPtr);
13101329

13111330
in_remote_transaction = false;
13121331

@@ -1364,7 +1383,11 @@ apply_handle_stream_prepare(StringInfo s)
13641383

13651384
CommitTransactionCommand();
13661385

1367-
MyParallelShared->last_commit_end = XactLastCommitEnd;
1386+
/*
1387+
* It is okay not to set the local_end LSN for the prepare because
1388+
* we always flush the prepare record. See apply_handle_prepare.
1389+
*/
1390+
MyParallelShared->last_commit_end = InvalidXLogRecPtr;
13681391

13691392
pa_set_xact_state(MyParallelShared, PARALLEL_TRANS_FINISHED);
13701393
pa_unlock_transaction(MyParallelShared->xid, AccessExclusiveLock);

0 commit comments

Comments
 (0)