diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 0521956235e329236d4bb26e532547872022dfb9..c8e9309354ae45640df408d086f0e89f031512c1 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.125 2005/07/28 22:27:00 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.126 2005/08/02 20:27:45 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -22,6 +22,7 @@
 
 
 #include "access/heapam.h"
+#include "catalog/namespace.h"
 #include "catalog/pg_type.h"
 #include "nodes/makefuncs.h"
 #include "optimizer/clauses.h"
@@ -807,6 +808,16 @@ expand_inherited_rtentry(PlannerInfo *root, Index rti)
 		RangeTblEntry *childrte;
 		Index		childRTindex;
 
+		/*
+		 * It is possible that the parent table has children that are
+		 * temp tables of other backends.  We cannot safely access such
+		 * tables (because of buffering issues), and the best thing to do
+		 * seems to be to silently ignore them.
+		 */
+		if (childOID != parentOID &&
+			isOtherTempNamespace(get_rel_namespace(childOID)))
+			continue;
+
 		/*
 		 * Build an RTE for the child, and attach to query's rangetable
 		 * list. We copy most fields of the parent's RTE, but replace
@@ -820,6 +831,17 @@ expand_inherited_rtentry(PlannerInfo *root, Index rti)
 		inhRTIs = lappend_int(inhRTIs, childRTindex);
 	}
 
+	/*
+	 * If all the children were temp tables, pretend it's a non-inheritance
+	 * situation.  The duplicate RTE we added for the parent table is harmless.
+	 */
+	if (list_length(inhRTIs) < 2)
+	{
+		/* Clear flag to save repeated tests if called again */
+		rte->inh = false;
+		return NIL;
+	}
+
 	/*
 	 * The executor will check the parent table's access permissions when
 	 * it examines the parent's inheritlist entry.  There's no need to