From f2b4426bafd0314fc390ca95ac05a61b1d529941 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Mon, 2 Nov 2026 10:18:29 +0100
Subject: [PATCH v3 5/5] pg_visibility: Reject temporary tables of other
 sessions

pg_visibility cannot see the pages of another session's temporary
table, but it did not reject such tables, so some of its functions
returned wrong results or modified that session's visibility map.
Report an error instead.

Author: Jim Jones <jim.jones@uni-muenster.de>
Reported-by: Michael Paquier <michael@paquier.xyz>
---
 contrib/pg_visibility/pg_visibility.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c
index 4195aeacc2a..86528cacaa2 100644
--- a/contrib/pg_visibility/pg_visibility.c
+++ b/contrib/pg_visibility/pg_visibility.c
@@ -931,6 +931,16 @@ check_relation_relkind(Relation rel)
 						RelationGetRelationName(rel)),
 				 errdetail_relkind_not_supported(rel->rd_rel->relkind)));
 
+	/*
+	 * Reject attempts to read non-local temporary relations; we would be
+	 * likely to get wrong data since we have no visibility into the owning
+	 * session's local buffers.
+	 */
+	if (RELATION_IS_OTHER_TEMP(rel))
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot access temporary tables of other sessions")));
+
 	/* Temporary and unlogged relations are inaccessible during recovery. */
 	if (!RelationIsPermanent(rel) && RecoveryInProgress())
 		ereport(ERROR,
-- 
2.56.0

