diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 00a8327e77..f229ff700a 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -467,6 +467,13 @@ ReorderBufferReturnTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
 	/* Reset the toast hash */
 	ReorderBufferToastReset(rb, txn);
 
+	/*
+	 * Update the memory counter of the transaction, removing it from
+	 * the max-heap.
+	 */
+	ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, txn->size);
+	Assert(txn->size == 0);
+
 	pfree(txn);
 }
 
@@ -1594,9 +1601,6 @@ ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
 	if (rbtxn_is_serialized(txn))
 		ReorderBufferRestoreCleanup(rb, txn);
 
-	/* Update the memory counter */
-	ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, txn->size);
-
 	/* deallocate */
 	ReorderBufferReturnTXN(rb, txn);
 }
diff --git a/src/test/subscription/t/031_column_list.pl b/src/test/subscription/t/031_column_list.pl
index 9a97fa5020..20454ae45d 100644
--- a/src/test/subscription/t/031_column_list.pl
+++ b/src/test/subscription/t/031_column_list.pl
@@ -1255,7 +1255,7 @@ is( $node_subscriber->safe_psql(
 
 $node_publisher->safe_psql(
 	'postgres', qq(
-	CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c int);
+	CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c text);
 	CREATE PUBLICATION pub_mix_1 FOR TABLE test_mix_1 (a, b);
 	CREATE PUBLICATION pub_mix_2 FOR TABLE test_mix_1 (a, c);
 ));
@@ -1263,7 +1263,7 @@ $node_publisher->safe_psql(
 $node_subscriber->safe_psql(
 	'postgres', qq(
 	DROP SUBSCRIPTION sub1;
-	CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c int);
+	CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c text);
 ));
 
 my ($cmdret, $stdout, $stderr) = $node_subscriber->psql(
@@ -1290,10 +1290,14 @@ $node_subscriber->safe_psql(
 
 $node_publisher->wait_for_catchup('sub1');
 
+# XXX: walsender raises an error during the replay of the INSERT transaction. Then during cleanup, it frees
+# the transaction entry while having the TOAST changes in memory. Previously, since we zeroed the transaction
+# size before freeing TOAST changes, we ended up further subtracting the memory counter from zero, resulting
+# in an assertion failure (with assertion build) or SIGV (wihtout assertion build).
 $node_publisher->safe_psql(
 	'postgres', qq(
 	ALTER PUBLICATION pub_mix_1 SET TABLE test_mix_1 (a, b);
-	INSERT INTO test_mix_1 VALUES(1, 1, 1);
+	INSERT INTO test_mix_1 (a, c) SELECT 1, repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50) FROM generate_series(1, 500) g(i);
 ));
 
 $offset = $node_publisher->wait_for_log(
