From cc3f122497418703b635f484081c15f857f7308a Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Sun, 18 Oct 2026 14:58:39 +0200
Subject: [PATCH v4 2/2] Reject currtid2() on unlogged tables during recovery

Unlogged tables have no data on a standby, so reading one there must
fail.  currtid2() reads the table directly instead of going through the
planner, so on a standby it failed with a confusing low-level message
instead of the error that SELECT reports, both for a table and for a
view on top of one:

  ERROR:  could not open file "base/5/16384": No such file or directory

Check for this case and report the same error as SELECT:

  ERROR:  cannot access temporary or unlogged relations during recovery

Author: Jim Jones <jim.jones@uni-muenster.de>
Reported-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://www.postgresql.org/message-id/b6d71815-d362-4579-bf07-73eca01aee6d%40uni-muenster.de
---
 src/backend/utils/adt/tid.c           |  7 +++++++
 src/test/recovery/t/001_stream_rep.pl | 23 +++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index a97873f91ba..f730b2f07c2 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -22,6 +22,7 @@
 #include "access/sysattr.h"
 #include "access/table.h"
 #include "access/tableam.h"
+#include "access/xlog.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_type.h"
 #include "common/hashfn.h"
@@ -345,6 +346,12 @@ currtid_internal(Relation rel, const ItemPointerData *tid)
 					   get_namespace_name(RelationGetNamespace(rel)),
 					   RelationGetRelationName(rel)));
 
+	/* Temporary and unlogged relations are inaccessible during recovery. */
+	if (!RelationIsPermanent(rel) && RecoveryInProgress())
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot access temporary or unlogged relations during recovery")));
+
 	ItemPointerCopy(tid, result);
 
 	snapshot = RegisterSnapshot(GetLatestSnapshot());
diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl
index 4a3c7d71d39..e481ed8b798 100644
--- a/src/test/recovery/t/001_stream_rep.pl
+++ b/src/test/recovery/t/001_stream_rep.pl
@@ -140,6 +140,29 @@ like(
 is($node_standby_1->safe_psql('postgres', 'COPY lpart TO STDOUT'),
 	'1', 'COPY TO of partitioned table on standby 1');
 
+# Check that currtid2() rejects unlogged tables on standby, including when
+# reached through a view, but still works on permanent ones
+$node_primary->safe_psql('postgres',
+	'CREATE VIEW ultab_view AS SELECT ctid, a FROM ultab');
+$node_primary->wait_for_replay_catchup($node_standby_1);
+my ($tid_ret, $tid_stdout, $tid_stderr) =
+  $node_standby_1->psql('postgres', q{SELECT currtid2('ultab', '(0,1)')});
+like(
+	$tid_stderr,
+	qr/cannot access temporary or unlogged relations during recovery/,
+	'currtid2() on unlogged table on standby 1');
+($tid_ret, $tid_stdout, $tid_stderr) =
+  $node_standby_1->psql('postgres',
+	q{SELECT currtid2('ultab_view', '(0,1)')});
+like(
+	$tid_stderr,
+	qr/cannot access temporary or unlogged relations during recovery/,
+	'currtid2() on view over unlogged table on standby 1');
+is( $node_standby_1->safe_psql(
+		'postgres', q{SELECT currtid2('tab_int', '(0,1)')}),
+	'(0,1)',
+	'currtid2() on permanent table on standby 1');
+
 # Check that only READ-only queries can run on standbys
 is($node_standby_1->psql('postgres', 'INSERT INTO tab_int VALUES (1)'),
 	3, 'read-only queries on standby 1');
-- 
2.56.0

