From c0cbdf310752b51e2c4753bd1e81abfd83c759be Mon Sep 17 00:00:00 2001
From: Stepan Neretin <slpmcf@gmail.com>
Date: Mon, 28 Jul 2025 10:24:20 +0700
Subject: [PATCH v6 2/2] Prevent cross-session temp table access test

Adds an isolation test verifying that temp tables created in one session
cannot be accessed from another session using the temp schema name.

Session 1 creates a temp table and records its temp schema name in a regular table.
Session 2 attempts to query that temp table via the stored schema name, expecting
an error.
---
 src/test/isolation/expected/temp-access.out | 32 +++++++++++++++++++++
 src/test/isolation/isolation_schedule       |  1 +
 src/test/isolation/specs/temp-access.spec   | 32 +++++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 src/test/isolation/expected/temp-access.out
 create mode 100644 src/test/isolation/specs/temp-access.spec

diff --git a/src/test/isolation/expected/temp-access.out b/src/test/isolation/expected/temp-access.out
new file mode 100644
index 00000000000..d402add4d83
--- /dev/null
+++ b/src/test/isolation/expected/temp-access.out
@@ -0,0 +1,32 @@
+Parsed test spec with 2 sessions
+
+starting permutation: create_temp_table wait try_access_temp_table
+step create_temp_table: 
+  CREATE TEMP TABLE temp_table1(a int);
+  CREATE TABLE IF NOT EXISTS temp_schema_name(schema_name text);
+  TRUNCATE temp_schema_name;
+  INSERT INTO temp_schema_name
+  SELECT n.nspname
+  FROM pg_class c
+  JOIN pg_namespace n ON c.relnamespace = n.oid
+  WHERE c.relname = 'temp_table1';
+
+step wait: 
+  SELECT 1;
+
+?column?
+--------
+       1
+(1 row)
+
+step try_access_temp_table: 
+  DO $$
+  DECLARE
+    nspname TEXT;
+  BEGIN
+    SELECT schema_name INTO nspname FROM temp_schema_name LIMIT 1;
+    EXECUTE format('SELECT * FROM %I.temp_table1', nspname);
+  END
+  $$;
+
+ERROR:  could not access temporary relations of other sessions
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index e3c669a29c7..e2d3d330fd0 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -45,6 +45,7 @@ test: lock-update-delete
 test: lock-update-traversal
 test: inherit-temp
 test: temp-schema-cleanup
+test: temp-access
 test: insert-conflict-do-nothing
 test: insert-conflict-do-nothing-2
 test: insert-conflict-do-update
diff --git a/src/test/isolation/specs/temp-access.spec b/src/test/isolation/specs/temp-access.spec
new file mode 100644
index 00000000000..35383fe7b35
--- /dev/null
+++ b/src/test/isolation/specs/temp-access.spec
@@ -0,0 +1,32 @@
+session s1
+step create_temp_table
+{
+  CREATE TEMP TABLE temp_table1(a int);
+  CREATE TABLE IF NOT EXISTS temp_schema_name(schema_name text);
+  TRUNCATE temp_schema_name;
+  INSERT INTO temp_schema_name
+  SELECT n.nspname
+  FROM pg_class c
+  JOIN pg_namespace n ON c.relnamespace = n.oid
+  WHERE c.relname = 'temp_table1';
+}
+
+session s2
+step wait
+{
+  SELECT 1;
+}
+
+step try_access_temp_table
+{
+  DO $$
+  DECLARE
+    nspname TEXT;
+  BEGIN
+    SELECT schema_name INTO nspname FROM temp_schema_name LIMIT 1;
+    EXECUTE format('SELECT * FROM %I.temp_table1', nspname);
+  END
+  $$;
+}
+
+permutation create_temp_table wait try_access_temp_table
-- 
2.48.1

