diff --git a/src/test/recovery/t/038_save_logical_slots_shutdown.pl b/src/test/recovery/t/038_save_logical_slots_shutdown.pl
index 5a4f5dc1d4..bcef53187e 100644
--- a/src/test/recovery/t/038_save_logical_slots_shutdown.pl
+++ b/src/test/recovery/t/038_save_logical_slots_shutdown.pl
@@ -60,6 +60,14 @@ $node_subscriber->start;
 $node_publisher->safe_psql('postgres', "CREATE TABLE test_tbl (id int)");
 $node_subscriber->safe_psql('postgres', "CREATE TABLE test_tbl (id int)");
 
+# Switch the WAL file so that the SHUTDOWN_CHECKPOINT WAL record will not be
+# inserted as the first record of a new WAL page. If the SHUTDOWN_CHECKPOINT WAL
+# record is inserted as the first record of new page, it will also include the
+# WAL page header which will result in test failure as the confirm_flush_lsn
+# will be pointing to WAL page header and the SHUTDOWN_CHECKPOINT WAL record
+# will point to a WAL record after the WAL page header.
+$node_publisher->safe_psql('postgres', "SELECT pg_switch_wal()");
+
 # Insert some data
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO test_tbl VALUES (generate_series(1, 5));");
