From 7a45756d652775f5e8d3dfa51772ac63d1c6922e Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Sat, 17 Oct 2026 11:08:52 +0200
Subject: [PATCH v3 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 +++++++
 1 file changed, 7 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());
-- 
2.56.0

