diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index 354eb0d4e6..d50b986709 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -187,7 +187,10 @@ static bool partkey_datum_from_expr(PartitionPruneContext *context,
  * If no non-Const expressions are being compared to the partition key in any
  * of the 'partitioned_rels', then we return NIL to indicate no run-time
  * pruning should be performed.  Run-time pruning would be useless, since the
- * pruning done during planning will have pruned everything that can be.
+ * pruning done during planning will have pruned everything that can be.  We
+ * also only build pruning steps when all 'subpaths' have leaf partition
+ * parents.  Some plan shapes, for example tablesample scans can have
+ * partitioned tables in the subpaths list.
  */
 List *
 make_partition_pruneinfo(PlannerInfo *root, List *partitioned_rels,
@@ -219,6 +222,17 @@ make_partition_pruneinfo(PlannerInfo *root, List *partitioned_rels,
 		Path	   *path = (Path *) lfirst(lc);
 		RelOptInfo *pathrel = path->parent;
 
+		/*
+		 * Don't attempt run-time pruning if not all subpath rels are
+		 * leaf partitions
+		 */
+		if (pathrel->part_scheme != NULL)
+		{
+			pfree(relid_subplan_map);
+			pfree(relid_subpart_map);
+			return NIL;
+		}
+
 		Assert(IS_SIMPLE_REL(pathrel));
 		Assert(pathrel->relid < root->simple_rel_array_size);
 		/* No duplicates please */
