From a7a863780553aca16d7139924a6d1c6e75a1b4ba Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Sun, 4 Oct 2026 23:48:37 -0400
Subject: [PATCH v1 2/2] Test pg_resetwal --cluster-state

Check that a bad value is rejected, and that with in-production the
state lands in pg_control and the next start empties an unlogged table.

Discussion: https://postgr.es/m/ar8mhs05SzC4hjmu@paquier.xyz
---
 src/bin/pg_resetwal/t/001_basic.pl | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/bin/pg_resetwal/t/001_basic.pl b/src/bin/pg_resetwal/t/001_basic.pl
index f54e9bb4223..11b23375ad0 100644
--- a/src/bin/pg_resetwal/t/001_basic.pl
+++ b/src/bin/pg_resetwal/t/001_basic.pl
@@ -202,6 +202,12 @@ command_fails_like(
 	qr/error: invalid argument for option --char-signedness/,
 	'fails with incorrect --char-signedness option');
 
+# --cluster-state
+command_fails_like(
+	[ 'pg_resetwal', '--cluster-state', 'foo', $node->data_dir ],
+	qr/error: invalid argument for option --cluster-state/,
+	'fails with incorrect --cluster-state option');
+
 # run with control override options
 
 my $out = (run_command([ 'pg_resetwal', '--dry-run', $node->data_dir ]))[0];
@@ -282,4 +288,20 @@ is( $node->safe_psql(
 	't',
 	'new 8-byte OID reported');
 
+# With a cluster state other than "shut-down", the next start goes through
+# crash recovery, which empties unlogged tables.
+$node->safe_psql('postgres',
+	'CREATE UNLOGGED TABLE unlogged_tab AS SELECT 1 AS a');
+$node->stop;
+command_ok(
+	[ 'pg_resetwal', '--cluster-state' => 'in-production', $node->data_dir ],
+	'runs with --cluster-state');
+command_like(
+	[ 'pg_controldata', $node->data_dir ],
+	qr/^Database cluster state: *in production$/m,
+	'cluster state set in pg_control');
+$node->start;
+is($node->safe_psql('postgres', 'SELECT count(*) FROM unlogged_tab'),
+	'0', 'unlogged table reset after --cluster-state=in-production');
+
 done_testing();
-- 
2.37.1 (Apple Git-137.1)

