From defeeee9b04fca7f376a7a62fcdfa0fbd46cccef Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 20 Feb 2022 13:39:40 -0800
Subject: [PATCH v1 5/5] initdb: call isatty() only once in bootparse.y.

Causes a not insignificant amount of syscalls...
---
 src/backend/bootstrap/bootparse.y | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 142433f63f3..dff86f71583 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -62,11 +62,17 @@ do_start(void)
 static void
 do_end(void)
 {
+	static int isatty_cached = -1;
+
 	/* Reclaim memory allocated while processing this line */
 	MemoryContextSwitchTo(CurTransactionContext);
 	MemoryContextReset(per_line_ctx);
 	CHECK_FOR_INTERRUPTS();		/* allow SIGINT to kill bootstrap run */
-	if (isatty(0))
+
+	if (isatty_cached == -1)
+		isatty_cached = isatty(0);
+
+	if (isatty_cached)
 	{
 		printf("bootstrap> ");
 		fflush(stdout);
-- 
2.34.0

