int         i;
    RangeTblEntry *childrte;
    Index       childRTindex;
-   bool        has_child = false;
    PartitionDesc partdesc = RelationGetPartitionDesc(parentrel);
 
    check_stack_depth();
                                    top_parentrc, parentrel,
                                    appinfos, &childrte, &childRTindex);
 
+   /*
+    * If the partitioned table has no partitions, treat this as the
+    * non-inheritance case.
+    */
+   if (partdesc->nparts == 0)
+   {
+       parentrte->inh = false;
+       return;
+   }
+
    for (i = 0; i < partdesc->nparts; i++)
    {
        Oid         childOID = partdesc->oids[i];
        /* Open rel; we already have required locks */
        childrel = heap_open(childOID, NoLock);
 
-       /* As in expand_inherited_rtentry, skip non-local temp tables */
+       /*
+        * Temporary partitions belonging to other sessions should have been
+        * disallowed at definition, but for paranoia's sake, let's double
+        * check.
+        */
        if (RELATION_IS_OTHER_TEMP(childrel))
-       {
-           heap_close(childrel, lockmode);
-           continue;
-       }
-
-       /* We have a real partition. */
-       has_child = true;
+           elog(ERROR, "temporary relation from another session found as partition");
 
        expand_single_inheritance_child(root, parentrte, parentRTindex,
                                        parentrel, top_parentrc, childrel,
        /* Close child relation, but keep locks */
        heap_close(childrel, NoLock);
    }
-
-   /*
-    * If the partitioned table has no partitions or all the partitions are
-    * temporary tables from other backends, treat this as non-inheritance
-    * case.
-    */
-   if (!has_child)
-       parentrte->inh = false;
 }
 
 /*
 
  1
 (2 rows)
 
+-- Test partitioned tables with no partitions, which should be handled the
+-- same as the non-inheritance case when expanding its RTE.
+create table list_parted_tbl (a int,b int) partition by list (a);
+create table list_parted_tbl1 partition of list_parted_tbl
+  for values in (1) partition by list(b);
+explain (costs off) select * from list_parted_tbl;
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
+drop table list_parted_tbl;
 
 -- (see bug #5084)
 select * from (values (2),(null),(1)) v(k) where k = k order by k;
 select * from (values (2),(null),(1)) v(k) where k = k;
+
+-- Test partitioned tables with no partitions, which should be handled the
+-- same as the non-inheritance case when expanding its RTE.
+create table list_parted_tbl (a int,b int) partition by list (a);
+create table list_parted_tbl1 partition of list_parted_tbl
+  for values in (1) partition by list(b);
+explain (costs off) select * from list_parted_tbl;
+drop table list_parted_tbl;