From c434e073f99ae048d2a148c7fc008cbf7ebfc69e Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Thu, 19 Nov 2026 09:42:47 -0500
Subject: [PATCH v4] Add a table AM callback to report supported features.

REPACK (CONCURRENTLY) loses the changes made during its rewrite if the
table's AM can't be logically decoded.  Commit e8636f2bed2 restricted it
to heap for want of a way to ask the AM, which also refuses AMs that do
support decoding.

Add an optional relation_supports_features callback, which is passed a
mask of TABLEAM_SUPPORTS_* flags and returns the ones the relation
supports, rather than a callback per capability.  Define
TABLEAM_SUPPORTS_LOGICAL_DECODING, have heap support it, and have REPACK
ask for it.  An AM without the callback supports no features, as does
one that predates a flag, so existing AMs are unaffected.  The callback
is not asked to work out bits that were not requested, and what it
returns is cut down to the bits that were.

An AM that returns the logical decoding flag must have a resource manager
with an rm_decode callback that also calls change_useless_for_repack(), as
heap's does, because the output plugin used by REPACK (CONCURRENTLY)
expects only changes to the table being rebuilt and its TOAST table.
Say so in the comment on the flag and in the documentation.

Add test_tableam_logical, with AMs that copy heap's routine, one without
the callback and one whose callback claims every bit, and a function to
show what the core makes of the answers.

Discussion: https://postgr.es/m/apBkixO4xAGFoiT3@nathan
---
 doc/src/sgml/ref/repack.sgml                  |   4 +-
 doc/src/sgml/tableam.sgml                     |  25 ++++
 src/backend/access/heap/heapam_handler.c      |  12 +-
 src/backend/commands/repack.c                 |   7 +-
 src/include/access/tableam.h                  |  60 ++++++++++
 src/test/modules/Makefile                     |   1 +
 src/test/modules/meson.build                  |   1 +
 .../modules/test_tableam_logical/Makefile     |  26 +++++
 .../test_tableam_logical/expected/repack.out  |  75 ++++++++++++
 .../modules/test_tableam_logical/meson.build  |  37 ++++++
 .../test_tableam_logical/sql/repack.sql       |  53 +++++++++
 .../test_tableam_logical/tableam_logical.conf |   1 +
 .../test_tableam_logical--1.0.sql             |  31 +++++
 .../test_tableam_logical.c                    | 109 ++++++++++++++++++
 .../test_tableam_logical.control              |   5 +
 15 files changed, 440 insertions(+), 7 deletions(-)
 create mode 100644 src/test/modules/test_tableam_logical/Makefile
 create mode 100644 src/test/modules/test_tableam_logical/expected/repack.out
 create mode 100644 src/test/modules/test_tableam_logical/meson.build
 create mode 100644 src/test/modules/test_tableam_logical/sql/repack.sql
 create mode 100644 src/test/modules/test_tableam_logical/tableam_logical.conf
 create mode 100644 src/test/modules/test_tableam_logical/test_tableam_logical--1.0.sql
 create mode 100644 src/test/modules/test_tableam_logical/test_tableam_logical.c
 create mode 100644 src/test/modules/test_tableam_logical/test_tableam_logical.control

diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml
index 4cd3cfa0b11..ab26cb5917e 100644
--- a/doc/src/sgml/ref/repack.sgml
+++ b/doc/src/sgml/ref/repack.sgml
@@ -417,7 +417,9 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
 
      <listitem>
       <para>
-       The table's access method is not <literal>heap</literal>.
+       The table's access method does not support logical decoding of its
+       changes (see <xref linkend="tableam"/>).  The
+       <literal>heap</literal> access method does.
       </para>
      </listitem>
 
diff --git a/doc/src/sgml/tableam.sgml b/doc/src/sgml/tableam.sgml
index 9ccf5b739ed..8913e8c2807 100644
--- a/doc/src/sgml/tableam.sgml
+++ b/doc/src/sgml/tableam.sgml
@@ -138,6 +138,31 @@ my_tableam_handler(PG_FUNCTION_ARGS)
   implemented.
  </para>
 
+ <para>
+  An access method can tell the server which optional features it supports
+  by providing the <structfield>relation_supports_features</structfield>
+  callback.  It is passed a mask of the <literal>TABLEAM_SUPPORTS_</literal>
+  flags the caller is asking about, and returns the ones the relation
+  supports; an access method without the callback supports none of them.
+  Flags that the access method does not know are simply not returned, so
+  adding a flag to the server does not affect existing access methods.
+  Currently there is one:
+  <literal>TABLEAM_SUPPORTS_LOGICAL_DECODING</literal>.  Changes to a table can be
+  <link linkend="logicaldecoding">logically decoded</link> only if the access
+  method's resource manager provides an <function>rm_decode</function>
+  callback that turns its WAL records into row changes, and an access method
+  that does so should return this flag.  That callback must also call
+  <function>change_useless_for_repack</function> (declared in
+  <filename>replication/decode.h</filename>) for each change it decodes, and
+  skip the change if it returns true, as the one for
+  <literal>heap</literal> does: while <command>REPACK (CONCURRENTLY)</command>
+  runs, its decoding expects only changes to the table being rebuilt and its
+  TOAST table.  Commands that need to see every
+  change made to a table while they work, such as
+  <link linkend="sql-repack"><command>REPACK (CONCURRENTLY)</command></link>,
+  refuse to run on a table of an access method that doesn't.
+ </para>
+
  <para>
   To implement transactional support in a manner that allows different table
   access methods be accessed within a single transaction, it likely is
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 1437274b0c6..ce73e654bff 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -2658,6 +2658,14 @@ BitmapHeapScanNextBlock(TableScanDesc scan,
  * ------------------------------------------------------------------------
  */
 
+/* Heap's resource managers support logical decoding. */
+static uint32
+heapam_relation_supports_features(Relation rel, uint32 features)
+{
+	return features & TABLEAM_SUPPORTS_LOGICAL_DECODING;
+}
+
+
 static const TableAmRoutine heapam_methods = {
 	.type = T_TableAmRoutine,
 
@@ -2713,7 +2721,9 @@ static const TableAmRoutine heapam_methods = {
 
 	.scan_bitmap_next_tuple = heapam_scan_bitmap_next_tuple,
 	.scan_sample_next_block = heapam_scan_sample_next_block,
-	.scan_sample_next_tuple = heapam_scan_sample_next_tuple
+	.scan_sample_next_tuple = heapam_scan_sample_next_tuple,
+
+	.relation_supports_features = heapam_relation_supports_features
 };
 
 
diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 8cf339a9cc7..f71706afc0b 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -987,16 +987,13 @@ check_concurrent_repack_requirements(Relation rel, Oid *ident_idx_p)
 	/*
 	 * A table AM that doesn't support logical decoding would cause REPACK
 	 * (CONCURRENTLY) to silently lose the changes made during the rewrite.
-	 * Nothing in TableAmRoutine tells us whether it does, so for now restrict
-	 * to heap. Check the routine rather than the AM OID, so that an AM
-	 * reusing the heap handler still works.
 	 */
-	if (rel->rd_tableam != GetHeapamTableAmRoutine())
+	if (!table_relation_supports_feature(rel, TABLEAM_SUPPORTS_LOGICAL_DECODING))
 		ereport(ERROR,
 				errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				errmsg("cannot execute %s on relation \"%s\"",
 					   "REPACK (CONCURRENTLY)", RelationGetRelationName(rel)),
-				errdetail("This operation is only supported for the \"heap\" access method."));
+				errdetail("The table's access method does not support logical decoding."));
 
 	/* Data changes in system relations are not logically decoded. */
 	if (IsCatalogRelation(rel))
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 9d2fc7f9acb..1d8672ac6d2 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -878,9 +878,43 @@ typedef struct TableAmRoutine
 										   SampleScanState *scanstate,
 										   TupleTableSlot *slot);
 
+	/* ------------------------------------------------------------------------
+	 * Optional capabilities.
+	 * ------------------------------------------------------------------------
+	 */
+
+	/*
+	 * See table_relation_supports_features().
+	 *
+	 * Optional; if NULL, the AM is taken to support none of the features.
+	 * 'features' is a mask of the TABLEAM_SUPPORTS_* flags the caller is
+	 * asking about.  Return the subset of them that the relation supports,
+	 * and do not spend time on the bits that were not asked for, as some may
+	 * be expensive to determine.  A flag the AM has never heard of, because
+	 * it is newer than the AM, is simply not returned, which reads as
+	 * unsupported.
+	 */
+	uint32		(*relation_supports_features) (Relation rel, uint32 features);
+
 } TableAmRoutine;
 
 
+/*
+ * Features a table AM can say it supports, through its
+ * relation_supports_features callback.  Values are never reused.
+ *
+ * TABLEAM_SUPPORTS_LOGICAL_DECODING: every change to the relation's rows can
+ * be logically decoded, which needs the AM's resource manager to have an
+ * rm_decode callback.  REPACK (CONCURRENTLY) checks this, as it would
+ * otherwise lose the changes made while it runs.  That rm_decode must also
+ * call change_useless_for_repack() (replication/decode.h) for each change
+ * it decodes, and skip the change if it returns true, as heap's does: while
+ * REPACK (CONCURRENTLY) runs, its output plugin expects only changes to the
+ * table being rebuilt and its TOAST table.
+ */
+#define TABLEAM_SUPPORTS_LOGICAL_DECODING	(1U << 0)
+
+
 /* ----------------------------------------------------------------------------
  * Slot functions.
  * ----------------------------------------------------------------------------
@@ -2040,6 +2074,32 @@ table_relation_estimate_size(Relation rel, int32 *attr_widths,
 											allvisfrac);
 }
 
+/*
+ * table_relation_supports_features - which of the TABLEAM_SUPPORTS_* flags in
+ * 'features' does the relation's table AM support?  An AM without the
+ * callback supports none.  Only bits that were asked about are returned,
+ * whatever the AM says.
+ */
+static inline uint32
+table_relation_supports_features(Relation rel, uint32 features)
+{
+	Assert(rel->rd_tableam != NULL);
+	if (rel->rd_tableam->relation_supports_features == NULL)
+		return 0;
+	return rel->rd_tableam->relation_supports_features(rel, features) &
+		features;
+}
+
+/*
+ * table_relation_supports_feature - the same for a single flag.
+ */
+static inline bool
+table_relation_supports_feature(Relation rel, uint32 feature)
+{
+	Assert(feature != 0 && (feature & (feature - 1)) == 0);
+	return table_relation_supports_features(rel, feature) != 0;
+}
+
 
 /* ----------------------------------------------------------------------------
  * Executor related functionality
diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index 6c1504ac932..ebf9d86ac3c 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -53,6 +53,7 @@ SUBDIRS = \
 		  test_shmem \
 		  test_shm_mq \
 		  test_slru \
+		  test_tableam_logical \
 		  test_tidstore \
 		  test_wait_lsn \
 		  unsafe_tests \
diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build
index ef3cdb8faf9..9dc5cc18c88 100644
--- a/src/test/modules/meson.build
+++ b/src/test/modules/meson.build
@@ -55,6 +55,7 @@ subdir('test_saslprep')
 subdir('test_shmem')
 subdir('test_shm_mq')
 subdir('test_slru')
+subdir('test_tableam_logical')
 subdir('test_tidstore')
 subdir('test_wait_lsn')
 subdir('typcache')
diff --git a/src/test/modules/test_tableam_logical/Makefile b/src/test/modules/test_tableam_logical/Makefile
new file mode 100644
index 00000000000..4cbb7ce4aba
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/Makefile
@@ -0,0 +1,26 @@
+# src/test/modules/test_tableam_logical/Makefile
+
+MODULES = test_tableam_logical
+
+EXTENSION = test_tableam_logical
+DATA = test_tableam_logical--1.0.sql
+PGFILEDESC = "test_tableam_logical - table access methods with and without logical decoding support"
+
+REGRESS = repack
+
+REGRESS_OPTS = --temp-config $(top_srcdir)/src/test/modules/test_tableam_logical/tableam_logical.conf
+
+# Disabled because these tests require "wal_level=replica", which
+# some installcheck users do not have (e.g. buildfarm clients).
+NO_INSTALLCHECK = 1
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_tableam_logical
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_tableam_logical/expected/repack.out b/src/test/modules/test_tableam_logical/expected/repack.out
new file mode 100644
index 00000000000..da42f44fd42
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/expected/repack.out
@@ -0,0 +1,75 @@
+--
+-- REPACK (CONCURRENTLY) needs the table AM to say it supports logical
+-- decoding, through its relation_supports_features callback.  The test AMs
+-- copy heap's routine, which heap's index builds and TOAST code reject, so
+-- their tables have no index and no TOAST table.
+--
+CREATE EXTENSION test_tableam_logical;
+CREATE TABLE repack_heap (i int PRIMARY KEY, j int);
+CREATE TABLE repack_decode (i int, j int) USING test_decode;
+CREATE TABLE repack_nodecode (i int, j int) USING test_nodecode;
+INSERT INTO repack_heap SELECT g, g * 2 FROM generate_series(1, 100) g;
+INSERT INTO repack_decode SELECT g, g * 2 FROM generate_series(1, 100) g;
+INSERT INTO repack_nodecode SELECT g, g * 2 FROM generate_series(1, 100) g;
+-- What the callback returns is cut down to the bits asked for: bit 0 is
+-- TABLEAM_SUPPORTS_LOGICAL_DECODING, and 2 is a flag no AM has heard of.
+-- No callback, none; asking for nothing, nothing.
+CREATE TABLE repack_overclaim (i int, j int) USING test_overclaim;
+SELECT c.relname, test_tableam_supports_features(c.oid, 1) AS asked_1,
+       test_tableam_supports_features(c.oid, 3) AS asked_3,
+       test_tableam_supports_features(c.oid, 2) AS asked_2,
+       test_tableam_supports_features(c.oid, 0) AS asked_0
+  FROM pg_class c WHERE c.relkind = 'r' AND c.relname LIKE 'repack\_%' ORDER BY c.relname;
+     relname      | asked_1 | asked_3 | asked_2 | asked_0 
+------------------+---------+---------+---------+---------
+ repack_decode    |       1 |       1 |       0 |       0
+ repack_heap      |       1 |       1 |       0 |       0
+ repack_nodecode  |       0 |       0 |       0 |       0
+ repack_overclaim |       1 |       3 |       2 |       0
+(4 rows)
+
+-- Not every relation has a table AM to ask.
+CREATE TABLE repack_part (i int) PARTITION BY RANGE (i);
+CREATE VIEW repack_view AS SELECT 1 AS i;
+SELECT test_tableam_supports_features('repack_part'::regclass, 1);
+ERROR:  "repack_part" is not a table
+SELECT test_tableam_supports_features('repack_view'::regclass, 1);
+ERROR:  "repack_view" is not a table
+SELECT test_tableam_supports_features('repack_heap_pkey'::regclass, 1);
+ERROR:  cannot open relation "repack_heap_pkey"
+DETAIL:  This operation is not supported for indexes.
+-- heap has it
+REPACK (CONCURRENTLY) repack_heap;
+-- an AM without it is refused
+REPACK (CONCURRENTLY) repack_nodecode;
+ERROR:  cannot execute REPACK (CONCURRENTLY) on relation "repack_nodecode"
+DETAIL:  The table's access method does not support logical decoding.
+-- one with it passes that check, then fails for want of an identity index
+REPACK (CONCURRENTLY) repack_decode;
+ERROR:  cannot execute REPACK (CONCURRENTLY) on relation "repack_decode"
+DETAIL:  Relation "repack_decode" has no identity index.
+-- the rest of REPACK doesn't need it
+REPACK repack_nodecode;
+VACUUM FULL repack_nodecode;
+SELECT count(*) FROM repack_heap;
+ count 
+-------
+   100
+(1 row)
+
+SELECT count(*) FROM repack_decode;
+ count 
+-------
+   100
+(1 row)
+
+SELECT count(*) FROM repack_nodecode;
+ count 
+-------
+   100
+(1 row)
+
+DROP VIEW repack_view;
+DROP TABLE repack_part;
+DROP TABLE repack_heap, repack_decode, repack_nodecode, repack_overclaim;
+DROP EXTENSION test_tableam_logical;
diff --git a/src/test/modules/test_tableam_logical/meson.build b/src/test/modules/test_tableam_logical/meson.build
new file mode 100644
index 00000000000..2f62c58b642
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/meson.build
@@ -0,0 +1,37 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+test_tableam_logical_sources = files(
+  'test_tableam_logical.c',
+)
+
+if host_system == 'windows'
+  test_tableam_logical_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
+    '--NAME', 'test_tableam_logical',
+    '--FILEDESC', 'test_tableam_logical - table access methods with and without logical decoding support',])
+endif
+
+test_tableam_logical = shared_module('test_tableam_logical',
+  test_tableam_logical_sources,
+  kwargs: pg_test_mod_args,
+)
+test_install_libs += test_tableam_logical
+
+test_install_data += files(
+  'test_tableam_logical.control',
+  'test_tableam_logical--1.0.sql',
+)
+
+tests += {
+  'name': 'test_tableam_logical',
+  'sd': meson.current_source_dir(),
+  'bd': meson.current_build_dir(),
+  'regress': {
+    'sql': [
+      'repack',
+    ],
+    # Disabled because these tests require "wal_level=replica", which
+    # some runningcheck users do not have (e.g. buildfarm clients).
+    'regress_args': ['--temp-config', files('tableam_logical.conf')],
+    'runningcheck': false,
+  },
+}
diff --git a/src/test/modules/test_tableam_logical/sql/repack.sql b/src/test/modules/test_tableam_logical/sql/repack.sql
new file mode 100644
index 00000000000..b3ab1c1d65c
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/sql/repack.sql
@@ -0,0 +1,53 @@
+--
+-- REPACK (CONCURRENTLY) needs the table AM to say it supports logical
+-- decoding, through its relation_supports_features callback.  The test AMs
+-- copy heap's routine, which heap's index builds and TOAST code reject, so
+-- their tables have no index and no TOAST table.
+--
+CREATE EXTENSION test_tableam_logical;
+
+CREATE TABLE repack_heap (i int PRIMARY KEY, j int);
+CREATE TABLE repack_decode (i int, j int) USING test_decode;
+CREATE TABLE repack_nodecode (i int, j int) USING test_nodecode;
+INSERT INTO repack_heap SELECT g, g * 2 FROM generate_series(1, 100) g;
+INSERT INTO repack_decode SELECT g, g * 2 FROM generate_series(1, 100) g;
+INSERT INTO repack_nodecode SELECT g, g * 2 FROM generate_series(1, 100) g;
+
+-- What the callback returns is cut down to the bits asked for: bit 0 is
+-- TABLEAM_SUPPORTS_LOGICAL_DECODING, and 2 is a flag no AM has heard of.
+-- No callback, none; asking for nothing, nothing.
+CREATE TABLE repack_overclaim (i int, j int) USING test_overclaim;
+SELECT c.relname, test_tableam_supports_features(c.oid, 1) AS asked_1,
+       test_tableam_supports_features(c.oid, 3) AS asked_3,
+       test_tableam_supports_features(c.oid, 2) AS asked_2,
+       test_tableam_supports_features(c.oid, 0) AS asked_0
+  FROM pg_class c WHERE c.relkind = 'r' AND c.relname LIKE 'repack\_%' ORDER BY c.relname;
+
+-- Not every relation has a table AM to ask.
+CREATE TABLE repack_part (i int) PARTITION BY RANGE (i);
+CREATE VIEW repack_view AS SELECT 1 AS i;
+SELECT test_tableam_supports_features('repack_part'::regclass, 1);
+SELECT test_tableam_supports_features('repack_view'::regclass, 1);
+SELECT test_tableam_supports_features('repack_heap_pkey'::regclass, 1);
+
+-- heap has it
+REPACK (CONCURRENTLY) repack_heap;
+
+-- an AM without it is refused
+REPACK (CONCURRENTLY) repack_nodecode;
+
+-- one with it passes that check, then fails for want of an identity index
+REPACK (CONCURRENTLY) repack_decode;
+
+-- the rest of REPACK doesn't need it
+REPACK repack_nodecode;
+VACUUM FULL repack_nodecode;
+
+SELECT count(*) FROM repack_heap;
+SELECT count(*) FROM repack_decode;
+SELECT count(*) FROM repack_nodecode;
+
+DROP VIEW repack_view;
+DROP TABLE repack_part;
+DROP TABLE repack_heap, repack_decode, repack_nodecode, repack_overclaim;
+DROP EXTENSION test_tableam_logical;
diff --git a/src/test/modules/test_tableam_logical/tableam_logical.conf b/src/test/modules/test_tableam_logical/tableam_logical.conf
new file mode 100644
index 00000000000..88f3e0aa6c0
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/tableam_logical.conf
@@ -0,0 +1 @@
+wal_level = replica
diff --git a/src/test/modules/test_tableam_logical/test_tableam_logical--1.0.sql b/src/test/modules/test_tableam_logical/test_tableam_logical--1.0.sql
new file mode 100644
index 00000000000..fa5c2c49c15
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/test_tableam_logical--1.0.sql
@@ -0,0 +1,31 @@
+/* src/test/modules/test_tableam_logical/test_tableam_logical--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_tableam_logical" to load this file. \quit
+
+CREATE FUNCTION test_tableam_decode_handler(internal)
+RETURNS table_am_handler
+AS 'MODULE_PATHNAME'
+LANGUAGE C;
+
+CREATE FUNCTION test_tableam_nodecode_handler(internal)
+RETURNS table_am_handler
+AS 'MODULE_PATHNAME'
+LANGUAGE C;
+
+CREATE FUNCTION test_tableam_overclaim_handler(internal)
+RETURNS table_am_handler
+AS 'MODULE_PATHNAME'
+LANGUAGE C;
+
+CREATE FUNCTION test_tableam_supports_features(regclass, int)
+RETURNS int
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT;
+
+CREATE ACCESS METHOD test_decode TYPE TABLE
+  HANDLER test_tableam_decode_handler;
+CREATE ACCESS METHOD test_nodecode TYPE TABLE
+  HANDLER test_tableam_nodecode_handler;
+CREATE ACCESS METHOD test_overclaim TYPE TABLE
+  HANDLER test_tableam_overclaim_handler;
diff --git a/src/test/modules/test_tableam_logical/test_tableam_logical.c b/src/test/modules/test_tableam_logical/test_tableam_logical.c
new file mode 100644
index 00000000000..f8c101ed6ed
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/test_tableam_logical.c
@@ -0,0 +1,109 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_tableam_logical.c
+ *		Table AMs that reuse heap's code, with and without the
+ *		relation_supports_features callback.
+ *
+ * Copyright (c) 2026, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		src/test/modules/test_tableam_logical/test_tableam_logical.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/table.h"
+#include "access/tableam.h"
+#include "catalog/pg_class.h"
+#include "fmgr.h"
+#include "utils/rel.h"
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(test_tableam_decode_handler);
+PG_FUNCTION_INFO_V1(test_tableam_nodecode_handler);
+PG_FUNCTION_INFO_V1(test_tableam_overclaim_handler);
+PG_FUNCTION_INFO_V1(test_tableam_supports_features);
+
+static TableAmRoutine decode_methods;
+static TableAmRoutine nodecode_methods;
+static TableAmRoutine overclaim_methods;
+static bool methods_initialized = false;
+
+static void init_methods(void);
+static uint32 overclaim_features(Relation rel, uint32 features);
+
+/* An AM that says it supports everything, whatever it is asked. */
+static uint32
+overclaim_features(Relation rel, uint32 features)
+{
+	return ~(uint32) 0;
+}
+
+/*
+ * Copies of heap's routine: the first as it is, the second without the
+ * callback, the third with one that returns bits it was not asked about, to
+ * show that the caller keeps only the ones it asked for.
+ */
+static void
+init_methods(void)
+{
+	memcpy(&decode_methods, GetHeapamTableAmRoutine(), sizeof(TableAmRoutine));
+
+	memcpy(&nodecode_methods, GetHeapamTableAmRoutine(), sizeof(TableAmRoutine));
+	nodecode_methods.relation_supports_features = NULL;
+
+	memcpy(&overclaim_methods, GetHeapamTableAmRoutine(), sizeof(TableAmRoutine));
+	overclaim_methods.relation_supports_features = overclaim_features;
+
+	methods_initialized = true;
+}
+
+Datum
+test_tableam_decode_handler(PG_FUNCTION_ARGS)
+{
+	if (!methods_initialized)
+		init_methods();
+	PG_RETURN_POINTER(&decode_methods);
+}
+
+Datum
+test_tableam_overclaim_handler(PG_FUNCTION_ARGS)
+{
+	if (!methods_initialized)
+		init_methods();
+	PG_RETURN_POINTER(&overclaim_methods);
+}
+
+Datum
+test_tableam_nodecode_handler(PG_FUNCTION_ARGS)
+{
+	if (!methods_initialized)
+		init_methods();
+	PG_RETURN_POINTER(&nodecode_methods);
+}
+
+/* The features of rel's table AM among those in the mask, as the core asks. */
+Datum
+test_tableam_supports_features(PG_FUNCTION_ARGS)
+{
+	Oid			relid = PG_GETARG_OID(0);
+	uint32		features = (uint32) PG_GETARG_INT32(1);
+	Relation	rel;
+	uint32		result;
+
+	rel = table_open(relid, AccessShareLock);
+
+	/* a partitioned table or a view has no table AM to ask */
+	if (!RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("\"%s\" is not a table",
+						RelationGetRelationName(rel))));
+
+	result = table_relation_supports_features(rel, features);
+	table_close(rel, AccessShareLock);
+
+	PG_RETURN_INT32((int32) result);
+}
diff --git a/src/test/modules/test_tableam_logical/test_tableam_logical.control b/src/test/modules/test_tableam_logical/test_tableam_logical.control
new file mode 100644
index 00000000000..f38ab722948
--- /dev/null
+++ b/src/test/modules/test_tableam_logical/test_tableam_logical.control
@@ -0,0 +1,5 @@
+# test_tableam_logical extension
+comment = 'test_tableam_logical - table access methods with and without logical decoding support'
+default_version = '1.0'
+module_pathname = '$libdir/test_tableam_logical'
+relocatable = true
-- 
2.43.0

