From 197d8bb4bbc152eadd768e16025f122eb9347e6f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <alvherre@kurilemu.de>
Date: Mon, 23 Nov 2026 22:43:07 +0100
Subject: [PATCH] fix psql autocommit off for repack concurrently

---
 src/bin/psql/common.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index ebce73e1f29..d4ec2b2463b 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -2475,6 +2475,47 @@ command_no_begin(const char *query)
 		return true;			/* it's CLUSTER without arguments */
 	}
 
+	/* repack (analyze) and repack (concurrently) are also not allowed */
+	if (wordlen == 6 && pg_strncasecmp(query, "repack", 6) == 0)
+	{
+		const char *option;
+		const char *optend;
+
+		/*
+		 * XXX I think assuming that '(' or ')' (and ',' below) don't appear as
+		 * second byte of a multibyte character is flawed, so this could easily
+		 * be fooled.  How bad are the consequences of that happening, though?
+		 */
+		option = strchr(query, '(');
+		if (option == NULL)
+			return false;
+		option = skip_white_space(option);
+		optend = strchr(option, ')');
+		if (optend == NULL)
+			return false;
+		for (;;)
+		{
+			const char *nextoption;
+
+			if (pg_strncasecmp(option, "concurrently", 12))
+				/* XXX what if it's 'CONCURRENTLY OFF'? */
+				return true;
+			if (pg_strncasecmp(option, "analyze", 7))
+				/* XXX what if it's 'ANALYZE OFF'? */
+				return true;
+
+			nextoption = strchr(option, ',');
+			if (nextoption == NULL)
+				return false;
+			if (nextoption > optend)
+				return false;
+			option = nextoption;
+			option = skip_white_space(option);
+		}
+
+		return false;
+	}
+
 	if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
 	{
 		query += wordlen;
-- 
2.47.3

