From 824f87fd6626cde48c1873997a15ed3463231e50 Mon Sep 17 00:00:00 2001
From: Baji Shaik <baji.pgdev@gmail.com>
Date: Tue, 26 May 2026 21:35:37 -0500
Subject: [PATCH 1/3] Improve REPACK (CONCURRENTLY) error when wal_level <
 replica

REPACK (CONCURRENTLY) uses logical decoding internally, so it requires
wal_level >= replica.  When wal_level is too low, the precondition is
indirectly enforced by CheckSlotRequirements(), which reports a generic
error about replication slots and a CONTEXT line for an internal worker.
That is hard to make sense of for a user who issued a REPACK command.

Add an upfront check in check_concurrent_repack_requirements() so the
error is reported in REPACK's voice before the worker is launched:

    ERROR:  cannot repack relation "X"
    DETAIL:  REPACK (CONCURRENTLY) requires "wal_level" to be set to
             "replica" or higher.
---
 src/backend/commands/repack.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 22a1307b38d..3d5bd98ad5f 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -40,6 +40,7 @@
 #include "access/toast_internals.h"
 #include "access/transam.h"
 #include "access/xact.h"
+#include "access/xlog.h"
 #include "catalog/catalog.h"
 #include "catalog/dependency.h"
 #include "catalog/heap.h"
@@ -897,6 +898,18 @@ check_concurrent_repack_requirements(Relation rel, Oid *ident_idx_p)
 				replident;
 	Oid			ident_idx;
 
+	/*
+	 * REPACK (CONCURRENTLY) uses logical decoding to capture changes that
+	 * occur during the rewrite, so it requires wal_level >= replica.
+	 */
+	if (wal_level < WAL_LEVEL_REPLICA)
+		ereport(ERROR,
+				errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+				errmsg("cannot repack relation \"%s\"",
+					   RelationGetRelationName(rel)),
+				errdetail("%s requires \"wal_level\" to be set to \"replica\" or higher.",
+						  "REPACK (CONCURRENTLY)"));
+
 	/* Data changes in system relations are not logically decoded. */
 	if (IsCatalogRelation(rel))
 		ereport(ERROR,
-- 
2.50.1 (Apple Git-155)

