diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 346d6b9..e442076 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1202,6 +1202,14 @@ heap_beginscan_internal(Relation relation, Snapshot snapshot,
 	HeapScanDesc scan;
 
 	/*
+	 * We are unable to scan temporary or unlogged relations during recovery.
+	 */
+	if (!RelationNeedsWAL(relation) && RecoveryInProgress())
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot access temporary or unlogged relations during recovery")));
+
+	/*
 	 * increment relation ref count while scanning relation
 	 *
 	 * This is just to make really sure the relcache entry won't go away while
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index e71090f..437d74b 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -24,6 +24,7 @@
 #include <unistd.h>
 
 #include "access/clog.h"
+#include "access/xlog.h"
 #include "access/multixact.h"
 #include "access/subtrans.h"
 #include "access/transam.h"
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index fd8ea45..39659ec 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -105,8 +105,12 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
 	 * case the size will be computed later in set_append_rel_pathlist, and we
 	 * must leave it zero for now to avoid bollixing the total_table_pages
 	 * calculation.
+	 *
+	 * We also skip this step for temporary or unlogged relations, which are
+	 * inaccessible during recovery.  The exact size doesn't really matter, as
+	 * the query is guaranteed to fail at execution time anyway.
 	 */
-	if (!inhparent)
+	if (!inhparent && (RelationNeedsWAL(relation) || !RecoveryInProgress()))
 		estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
 						  &rel->pages, &rel->tuples);
 
