From 33ea3cd2834d5061ab8ab971dcdc3002801bfefd Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sat, 29 Aug 2026 15:40:46 +0000
Subject: [PATCH v1] Fix error message for concurrent repack on materialized
 views.

Previously, running REPACK (CONCURRENTLY) on a materialized view
failed with a confusing "has no identity index" error.

REPACK (CONCURRENTLY) replays changes decoded from WAL, but a
materialized view produces none, since it cannot be modified by
DML and REFRESH rewrites it through a transient heap that is not
logically decoded.

Fix this by reporting a clear error for materialized views up
front, alongside the existing checks for system catalogs and
TOAST tables, and document the limitation.

Author: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Discussion: https://postgr.es/m/
Backpatch-through: 19
---
 doc/src/sgml/ref/repack.sgml  | 6 ++++++
 src/backend/commands/repack.c | 9 +++++++++
 2 files changed, 15 insertions(+)

diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml
index 0cb72b6b289..777681252ef 100644
--- a/doc/src/sgml/ref/repack.sgml
+++ b/doc/src/sgml/ref/repack.sgml
@@ -279,6 +279,12 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
         </para>
        </listitem>
 
+       <listitem>
+        <para>
+          The relation is a materialized view.
+        </para>
+       </listitem>
+
        <listitem>
         <para>
           The table is a system catalog or a <acronym>TOAST</acronym> table.
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 477c86b2ba6..73a42af3f4d 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -877,6 +877,15 @@ check_concurrent_repack_requirements(Relation rel, Oid *ident_idx_p)
 				errdetail("%s requires \"wal_level\" to be set to \"replica\" or higher.",
 						  "REPACK (CONCURRENTLY)"));
 
+	/* A materialized view produces no logically decoded changes. */
+	if (rel->rd_rel->relkind == RELKIND_MATVIEW)
+		ereport(ERROR,
+				errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				errmsg("cannot execute %s on relation \"%s\"",
+					   "REPACK (CONCURRENTLY)", RelationGetRelationName(rel)),
+				errhint("%s is not supported for materialized views.",
+						"REPACK (CONCURRENTLY)"));
+
 	/* Data changes in system relations are not logically decoded. */
 	if (IsCatalogRelation(rel))
 		ereport(ERROR,
-- 
2.47.3

