From 412ef97d925c54d6135afb4c22cceccb6421d8a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Wed, 9 Sep 2026 12:58:00 +0200 Subject: [PATCH] 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 upfront, alongside the existing checks for system catalogs and TOAST tables, and document the limitation. Author: Bharath Rupireddy Reviewed-by: Kiran Kaki Backpatch-through: 19 Discussion: https://postgr.es/m/CALj2ACXPvy4_LkQ0nHKgbZjAGK9fkDqRYUvXA3hgwxPm_X7Eng@mail.gmail.com --- contrib/test_decoding/expected/repack.out | 6 ++++++ contrib/test_decoding/sql/repack.sql | 5 +++++ doc/src/sgml/ref/repack.sgml | 6 ++++++ src/backend/commands/repack.c | 8 ++++++++ 4 files changed, 25 insertions(+) diff --git a/contrib/test_decoding/expected/repack.out b/contrib/test_decoding/expected/repack.out index ce2cadfa222..2dd3891b869 100644 --- a/contrib/test_decoding/expected/repack.out +++ b/contrib/test_decoding/expected/repack.out @@ -87,6 +87,12 @@ REPACK (CONCURRENTLY) repack_conc_unlogged; ERROR: cannot execute REPACK (CONCURRENTLY) on relation "repack_conc_unlogged" HINT: REPACK (CONCURRENTLY) is only allowed for permanent relations. DROP TABLE repack_conc_unlogged; +-- Doesn't support materialized views +CREATE MATERIALIZED VIEW repack_conc_matview AS SELECT 1 AS i; +REPACK (CONCURRENTLY) repack_conc_matview; +ERROR: cannot execute REPACK (CONCURRENTLY) on relation "repack_conc_matview" +DETAIL: This operation is not supported for materialized views. +DROP MATERIALIZED VIEW repack_conc_matview; -- Doesn't support tables with REPLICA IDENTITY NOTHING, even if they have a primary key CREATE TABLE repack_conc_replident (i int PRIMARY KEY); ALTER TABLE repack_conc_replident REPLICA IDENTITY NOTHING; diff --git a/contrib/test_decoding/sql/repack.sql b/contrib/test_decoding/sql/repack.sql index eb891009baf..e995c72d28d 100644 --- a/contrib/test_decoding/sql/repack.sql +++ b/contrib/test_decoding/sql/repack.sql @@ -64,6 +64,11 @@ CREATE UNLOGGED TABLE repack_conc_unlogged (i int PRIMARY KEY); REPACK (CONCURRENTLY) repack_conc_unlogged; DROP TABLE repack_conc_unlogged; +-- Doesn't support materialized views +CREATE MATERIALIZED VIEW repack_conc_matview AS SELECT 1 AS i; +REPACK (CONCURRENTLY) repack_conc_matview; +DROP MATERIALIZED VIEW repack_conc_matview; + -- Doesn't support tables with REPLICA IDENTITY NOTHING, even if they have a primary key CREATE TABLE repack_conc_replident (i int PRIMARY KEY); ALTER TABLE repack_conc_replident REPLICA IDENTITY NOTHING; diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml index f9fc45b9575..20426cda4ca 100644 --- a/doc/src/sgml/ref/repack.sgml +++ b/doc/src/sgml/ref/repack.sgml @@ -261,6 +261,12 @@ REPACK [ ( option [, ...] ) ] USING following cases: + + + The relation is not a table (e.g., it is a materialized view). + + + The table is UNLOGGED. diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index 57f28f1a420..4521ccd036f 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -945,6 +945,14 @@ check_concurrent_repack_requirements(Relation rel, Oid *ident_idx_p) errhint("%s is only allowed for permanent relations.", "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)), + errdetail_relkind_not_supported(rel->rd_rel->relkind)); + /* * With NOTHING, WAL does not contain the old tuple; FULL is not yet * supported.