diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c
index 9085a409db..67a3f2783d 100644
--- a/contrib/pg_prewarm/autoprewarm.c
+++ b/contrib/pg_prewarm/autoprewarm.c
@@ -511,7 +511,8 @@ autoprewarm_database_main(Datum main_arg)
 
 			Assert(rel == NULL);
 			StartTransactionCommand();
-			reloid = RelidByRelfilenumber(blk->tablespace, blk->filenumber);
+			reloid = RelidByRelfilenumber(blk->tablespace, blk->filenumber,
+										  RELPERSISTENCE_PERMANENT);
 			if (OidIsValid(reloid))
 				rel = try_relation_open(reloid, AccessShareLock);
 
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index f315fecf18..73978c7756 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -404,6 +404,11 @@ CREATE OR REPLACE FUNCTION
   RETURNS boolean STRICT VOLATILE LANGUAGE INTERNAL AS 'pg_terminate_backend'
   PARALLEL SAFE;
 
+CREATE OR REPLACE FUNCTION
+  pg_filenode_relation(tablespace oid, relfilenumber oid, persistence "char" DEFAULT NULL)
+  RETURNS regclass VOLATILE LANGUAGE internal AS 'pg_filenode_relation'
+  PARALLEL SAFE;
+
 -- legacy definition for compatibility with 9.3
 CREATE OR REPLACE FUNCTION
   json_populate_record(base anyelement, from_json json, use_json_as_text boolean DEFAULT false)
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index d1334ffb55..5a0bcb9f59 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -2185,7 +2185,8 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
 					Assert(snapshot_now);
 
 					reloid = RelidByRelfilenumber(change->data.tp.rlocator.spcOid,
-												  change->data.tp.rlocator.relNumber);
+												  change->data.tp.rlocator.relNumber,
+												  RELPERSISTENCE_PERMANENT);
 
 					/*
 					 * Mapped catalog tuple without data, emitted while
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 2df025d50a..efabffa6e6 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -928,10 +928,26 @@ pg_filenode_relation(PG_FUNCTION_ARGS)
 	Oid			heaprel;
 
 	/* test needed so RelidByRelfilenumber doesn't misbehave */
-	if (!RelFileNumberIsValid(relfilenumber))
+	if (PG_ARGISNULL(0) || PG_ARGISNULL(1) ||
+		!RelFileNumberIsValid(relfilenumber))
 		PG_RETURN_NULL();
 
-	heaprel = RelidByRelfilenumber(reltablespace, relfilenumber);
+	if (!PG_ARGISNULL(2))
+		heaprel = RelidByRelfilenumber(reltablespace, relfilenumber,
+									   PG_GETARG_CHAR(2));
+	else
+	{
+		/* search for any kind of persistence. for backward compatibility */
+		char persistences[] = {'p', 'u', 't'};
+		
+		for (int i = 0 ; i < 3 ; i++)
+		{
+			heaprel = RelidByRelfilenumber(reltablespace, relfilenumber,
+										   persistences[i]);
+			if (OidIsValid(heaprel))
+				break;
+		}
+	}
 
 	if (!OidIsValid(heaprel))
 		PG_RETURN_NULL();
diff --git a/src/backend/utils/cache/relfilenumbermap.c b/src/backend/utils/cache/relfilenumbermap.c
index b7caa84341..3c4b94c434 100644
--- a/src/backend/utils/cache/relfilenumbermap.c
+++ b/src/backend/utils/cache/relfilenumbermap.c
@@ -38,6 +38,7 @@ typedef struct
 {
 	Oid			reltablespace;
 	RelFileNumber relfilenumber;
+	char		persistence;
 } RelfilenumberMapKey;
 
 typedef struct
@@ -135,7 +136,8 @@ InitializeRelfilenumberMap(void)
  * Returns InvalidOid if no relation matching the criteria could be found.
  */
 Oid
-RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
+RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber,
+					 char persistence)
 {
 	RelfilenumberMapKey key;
 	RelfilenumberMapEntry *entry;
@@ -156,6 +158,7 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 	MemSet(&key, 0, sizeof(key));
 	key.reltablespace = reltablespace;
 	key.relfilenumber = relfilenumber;
+	key.persistence   = persistence;
 
 	/*
 	 * Check cache and return entry if one is found. Even if no target
@@ -211,10 +214,16 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 		{
 			Form_pg_class classform = (Form_pg_class) GETSTRUCT(ntp);
 
+			if (classform->relpersistence != persistence)
+				continue;
+
 			if (found)
+			{
 				elog(ERROR,
-					 "unexpected duplicate for tablespace %u, relfilenumber %u",
-					 reltablespace, relfilenumber);
+					 "unexpected duplicate for tablespace %u, relfilenumber %u, persistence %c",
+					 reltablespace, relfilenumber, persistence);
+
+			}
 			found = true;
 
 			Assert(classform->reltablespace == reltablespace);
@@ -226,7 +235,7 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 		table_close(relation, AccessShareLock);
 
 		/* check for tables that are mapped but not shared */
-		if (!found)
+		if (!found && persistence == RELPERSISTENCE_PERMANENT)
 			relid = RelationMapFilenumberToOid(relfilenumber, false);
 	}
 
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 7979392776..2cf326185e 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -7505,8 +7505,8 @@
   proname => 'pg_relation_filenode', provolatile => 's', prorettype => 'oid',
   proargtypes => 'regclass', prosrc => 'pg_relation_filenode' },
 { oid => '3454', descr => 'relation OID for filenode and tablespace',
-  proname => 'pg_filenode_relation', provolatile => 's',
-  prorettype => 'regclass', proargtypes => 'oid oid',
+  proname => 'pg_filenode_relation', proisstrict => 'f', provolatile => 'v',
+  prorettype => 'regclass', proargtypes => 'oid oid char',
   prosrc => 'pg_filenode_relation' },
 { oid => '3034', descr => 'file path of relation',
   proname => 'pg_relation_filepath', provolatile => 's', prorettype => 'text',
diff --git a/src/include/utils/relfilenumbermap.h b/src/include/utils/relfilenumbermap.h
index 0ca89e452a..5b98e02eaa 100644
--- a/src/include/utils/relfilenumbermap.h
+++ b/src/include/utils/relfilenumbermap.h
@@ -16,6 +16,7 @@
 #include "common/relpath.h"
 
 extern Oid	RelidByRelfilenumber(Oid reltablespace,
-								 RelFileNumber relfilenumber);
+								 RelFileNumber relfilenumber,
+								 char persistence);
 
 #endif							/* RELFILENUMBERMAP_H */
