From f63ca9cb6869452e85aec7b09d4ae471ab1f3030 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Fri, 13 Mar 2026 00:21:04 +0100
Subject: [PATCH v3 2/8] pgindent: Always clean up .BAK files from
 pg_bsd_indent

The previous commit let pgindent clean up File::Temp files on SIGINT.
This extends that to also cleaning up the .BAK files, created by
pg_bsd_indent.
---
 src/tools/pgindent/pgindent | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index b96891b2252..6ce695895eb 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -27,6 +27,12 @@ use Getopt::Long;
 $SIG{INT} = sub { exit 130; };     # 128 + 2 (SIGINT)
 $SIG{TERM} = sub { exit 143; };    # 128 + 15 (SIGTERM)
 
+# pg_bsd_indent creates a .BAK file that File::Temp doesn't know about. This
+# END block makes sure that that file is cleaned up in case someone presses
+# Ctrl+C during pgindent.
+my $bak_to_cleanup;
+END { unlink $bak_to_cleanup if defined $bak_to_cleanup; }
+
 # Update for pg_bsd_indent version
 my $INDENT_VERSION = "2.1.2";
 
@@ -295,11 +301,14 @@ sub run_indent
 	print $tmp_fh $source;
 	$tmp_fh->close();
 
+	$bak_to_cleanup = "$filename.BAK";
+
 	$$error_message = `$cmd $filename 2>&1`;
 
 	return "" if ($? || length($$error_message) > 0);
 
-	unlink "$filename.BAK";
+	unlink $bak_to_cleanup;
+	$bak_to_cleanup = undef;
 
 	open(my $src_out, '<', $filename) || die $!;
 	local ($/) = undef;
-- 
2.53.0

