diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 210c7c0b02..ee0b81ca74 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -28321,16 +28321,20 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
         <indexterm>
          <primary>pg_filenode_relation</primary>
         </indexterm>
-        <function>pg_filenode_relation</function> ( <parameter>tablespace</parameter> <type>oid</type>, <parameter>filenode</parameter> <type>oid</type> )
+        <function>pg_filenode_relation</function>
+        ( <parameter>tablespace</parameter> <type>oid</type>,
+        <parameter>filenode</parameter> <type>oid</type>)
         <returnvalue>regclass</returnvalue>
        </para>
        <para>
         Returns a relation's OID given the tablespace OID and filenode it is
-        stored under.  This is essentially the inverse mapping of
-        <function>pg_relation_filepath</function>.  For a relation in the
-        database's default tablespace, the tablespace can be specified as zero.
-        Returns <literal>NULL</literal> if no relation in the current database
-        is associated with the given values.
+        stored under, if the given values match a permanent relation.  This is
+        essentially the inverse mapping
+        of <function>pg_relation_filepath</function> only regarding permanent
+        relations.  For a relation in the database's default tablespace, the
+        tablespace can be specified as zero.  Returns <literal>NULL</literal>
+        if no permanent relation in the current database is associated with
+        the given values.
        </para></entry>
       </row>
      </tbody>
diff --git a/src/backend/utils/cache/relfilenumbermap.c b/src/backend/utils/cache/relfilenumbermap.c
index b7caa84341..87b903360e 100644
--- a/src/backend/utils/cache/relfilenumbermap.c
+++ b/src/backend/utils/cache/relfilenumbermap.c
@@ -129,10 +129,11 @@ InitializeRelfilenumberMap(void)
 }
 
 /*
- * Map a relation's (tablespace, relfilenumber) to a relation's oid and cache
- * the result.
+ * Map a relation's (tablespace, relfilenumber) to a permanent relation's oid
+ * and cache the result.
  *
- * Returns InvalidOid if no relation matching the criteria could be found.
+ * Returns InvalidOid if no permanent relation matching the criteria could be
+ * found.
  */
 Oid
 RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
@@ -211,6 +212,16 @@ RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
 		{
 			Form_pg_class classform = (Form_pg_class) GETSTRUCT(ntp);
 
+			/*
+			 * Temporary relations should be excluded. This exclusion is
+			 * necessary because they can lead to the wrong result; the
+			 * relfilenumber space is shared with persistent
+			 * relations. Additionally, excluding them is possible since they
+			 * are not the focus in this context.
+			 */
+			if (classform->relpersistence == RELPERSISTENCE_TEMP)
+				continue;
+
 			if (found)
 				elog(ERROR,
 					 "unexpected duplicate for tablespace %u, relfilenumber %u",
