Index: matcher/valuerangepostlist.cc
===================================================================
--- matcher/valuerangepostlist.cc	(revision 10244)
+++ matcher/valuerangepostlist.cc	(working copy)
@@ -25,10 +25,16 @@
 #include "autoptr.h"
 #include "omassert.h"
 #include "document.h"
+#include "leafpostlist.h"
 #include "utils.h"
 
 using namespace std;
 
+ValueRangePostList::~ValueRangePostList()
+{
+    delete alldocs_pl;
+}
+
 Xapian::doccount
 ValueRangePostList::get_termfreq_min() const
 {
@@ -104,17 +110,14 @@
 ValueRangePostList::next(Xapian::weight)
 {
     Assert(db);
-    AssertParanoid(lastdocid == db->get_lastdocid());
-    while (current < lastdocid) {
-	try {
-	    if (++current == 0) break;
-	    AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
-	    string v = doc->get_value(valno);
-	    if (v >= begin && v <= end) return NULL;
-	} catch (const Xapian::DocNotFoundError &) {
-	    // That document doesn't exist.
-	    // FIXME: this could throw and catch a lot of exceptions!
-	}
+    if (!alldocs_pl) alldocs_pl = db->open_post_list(string());
+    alldocs_pl->skip_to(current + 1);
+    while (!alldocs_pl->at_end()) {
+	current = alldocs_pl->get_docid();
+	AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
+	string v = doc->get_value(valno);
+	if (v >= begin && v <= end) return NULL;
+	alldocs_pl->next();
     }
     db = NULL;
     return NULL;
@@ -137,8 +140,7 @@
 	valid = true;
 	return NULL;
     }
-    AssertParanoid(lastdocid == db->get_lastdocid());
-    AssertRel(did, <=, lastdocid);
+    AssertRelParanoid(did, <=, db->get_lastdocid());
     current = did;
     AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
     string v = doc->get_value(valno);
Index: matcher/valuerangepostlist.h
===================================================================
--- matcher/valuerangepostlist.h	(revision 10221)
+++ matcher/valuerangepostlist.h	(working copy)
@@ -34,10 +34,10 @@
 
     Xapian::docid current;
 
-    Xapian::doccount lastdocid;
-
     Xapian::doccount db_size;
 
+    LeafPostList * alldocs_pl;
+
     /// Disallow copying.
     ValueRangePostList(const ValueRangePostList &);
 
@@ -49,7 +49,9 @@
 		       Xapian::valueno valno_,
 		       const std::string &begin_, const std::string &end_)
 	: db(db_), valno(valno_), begin(begin_), end(end_), current(0),
-	  lastdocid(db->get_lastdocid()), db_size(db->get_doccount()) { }
+	  db_size(db->get_doccount()), alldocs_pl(0) { }
+
+    ~ValueRangePostList();
 
     Xapian::doccount get_termfreq_min() const;
 
Index: matcher/valuegepostlist.cc
===================================================================
--- matcher/valuegepostlist.cc	(revision 10244)
+++ matcher/valuegepostlist.cc	(working copy)
@@ -26,6 +26,7 @@
 #include "autoptr.h"
 #include "omassert.h"
 #include "document.h"
+#include "leafpostlist.h"
 #include "utils.h"
 
 using namespace std;
@@ -34,17 +35,14 @@
 ValueGePostList::next(Xapian::weight)
 {
     Assert(db);
-    AssertParanoid(lastdocid == db->get_lastdocid());
-    while (current < lastdocid) {
-	try {
-	    if (++current == 0) break;
-	    AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
-	    string v = doc->get_value(valno);
-	    if (v >= begin) return NULL;
-	} catch (const Xapian::DocNotFoundError &) {
-	    // That document doesn't exist.
-	    // FIXME: this could throw and catch a lot of exceptions!
-	}
+    if (!alldocs_pl) alldocs_pl = db->open_post_list(string());
+    alldocs_pl->skip_to(current + 1);
+    while (!alldocs_pl->at_end()) {
+	current = alldocs_pl->get_docid();
+	AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
+	string v = doc->get_value(valno);
+	if (v >= begin) return NULL;
+	alldocs_pl->next();
     }
     db = NULL;
     return NULL;
@@ -67,8 +65,7 @@
 	valid = true;
 	return NULL;
     }
-    AssertParanoid(lastdocid == db->get_lastdocid());
-    AssertRel(did, <=, lastdocid);
+    AssertRelParanoid(did, <=, db->get_lastdocid());
     current = did;
     AutoPtr<Xapian::Document::Internal> doc(db->open_document(current, true));
     string v = doc->get_value(valno);
