 includes/database/database.inc              |    6 ++
 includes/database/select.inc                |   20 ++++++++
 includes/pager.inc                          |   68 ++++++++++----------------
 modules/node/node.module                    |    6 --
 modules/search/search.extender.inc          |   27 +++++++++++
 modules/simpletest/tests/database_test.test |    4 +-
 6 files changed, 81 insertions(+), 50 deletions(-)

diff --git includes/database/database.inc includes/database/database.inc
index b01ac8d..ef0bc98 100644
--- includes/database/database.inc
+++ includes/database/database.inc
@@ -1673,6 +1673,12 @@ class FieldsOverlapException extends Exception {}
 class NoFieldsException extends Exception {}
 
 /**
+ * Exception thrown if an extended query that overrides execute() is used as a
+ * subquery.
+ */
+class DatabaseInvalidSubqueryException extends Exception {}
+
+/**
  * A wrapper class for creating and managing database transactions.
  *
  * Not all databases or database configurations support transactions. For
diff --git includes/database/select.inc includes/database/select.inc
index fd3efd3..7d4e545 100644
--- includes/database/select.inc
+++ includes/database/select.inc
@@ -605,6 +605,23 @@ class SelectQueryExtender implements SelectQueryInterface {
     return $this->query->isPrepared();
   }
 
+  /**
+   * Extenders that override the execute() method are supposed to return TRUE.
+   * 
+   * Only extended queries that don't are supported as subqueries, others can't
+   * be handled because subqueries are casted to a string without calling
+   * execute().
+   *
+   * @return
+   *   TRUE if the extended query can not be used as a subquery.
+   */
+  public function hasExecute() {
+    if ($this->query instanceof SelectQueryExtender) {
+      return $this->query->hasExecute();
+    }
+    return FALSE;
+  }
+
   public function preExecute(SelectQueryInterface $query = NULL) {
     // If no query object is passed in, use $this.
     if (is_null($query)) {
@@ -1178,6 +1195,9 @@ class SelectQuery extends Query implements SelectQueryInterface {
 
     if (empty($alias)) {
       if ($table instanceof SelectQueryInterface) {
+        if ($table instanceof SelectQueryExtender && $table->hasExecute()) {
+          throw new DatabaseInvalidSubqueryException(t('Extender %classname not allowed to be used as subquery', array('%classname' => get_class($table))));
+        }
         $alias = 'subquery';
       }
       else {
diff --git includes/pager.inc includes/pager.inc
index 12c9eed..fbcb79b 100644
--- includes/pager.inc
+++ includes/pager.inc
@@ -52,48 +52,6 @@ class PagerDefault extends SelectQueryExtender {
   }
 
   /**
-   * Override the execute method.
-   *
-   * Before we run the query, we need to add pager-based range() instructions
-   * to it.
-   */
-  public function execute() {
-    global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
-
-    // Add convenience tag to mark that this is an extended query. We have to
-    // do this in the constructor to ensure that it is set before preExecute()
-    // gets called.
-    if (!$this->preExecute($this)) {
-      return NULL;
-    }
-
-    // A NULL limit is the "kill switch" for pager queries.
-    if (empty($this->limit)) {
-      return;
-    }
-    $this->ensureElement();
-
-    $page = isset($_GET['page']) ? $_GET['page'] : '';
-
-    // Convert comma-separated $page to an array, used by other functions.
-    $pager_page_array = explode(',', $page);
-
-    if (!isset($pager_page_array[$this->element])) {
-      $pager_page_array[$this->element] = 0;
-    }
-
-    // We calculate the total of pages as ceil(items / limit).
-    $pager_total_items[$this->element] = $this->getCountQuery()->execute()->fetchField();
-    $pager_total[$this->element] = ceil($pager_total_items[$this->element] / $this->limit);
-    $pager_page_array[$this->element] = max(0, min((int)$pager_page_array[$this->element], ((int)$pager_total[$this->element]) - 1));
-    $pager_limits[$this->element] = $this->limit;
-    $this->range($pager_page_array[$this->element] * $this->limit, $this->limit);
-
-    // Now that we've added our pager-based range instructions, run the query normally.
-    return $this->query->execute();
-  }
-
-  /**
    * Ensure that there is an element associated with this query.
    *
    * After running this query, access $this->element to get the element for this
@@ -149,7 +107,33 @@ class PagerDefault extends SelectQueryExtender {
    *   value (FALSE, 0, NULL), the pager is disabled.
    */
   public function limit($limit = 10) {
+    global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
     $this->limit = $limit;
+    
+    // A NULL limit is the "kill switch" for pager queries.
+    if (empty($this->limit)) {
+      $this->range();
+      return;
+    }
+
+    $this->ensureElement();
+
+    $page = isset($_GET['page']) ? $_GET['page'] : '';
+
+    // Convert comma-separated $page to an array, used by other functions.
+    $pager_page_array = explode(',', $page);
+
+    if (!isset($pager_page_array[$this->element])) {
+      $pager_page_array[$this->element] = 0;
+    }
+
+    // We calculate the total of pages as ceil(items / limit).
+    $pager_total_items[$this->element] = $this->getCountQuery()->execute()->fetchField();
+    $pager_total[$this->element] = ceil($pager_total_items[$this->element] / $this->limit);
+    $pager_page_array[$this->element] = max(0, min((int)$pager_page_array[$this->element], ((int)$pager_total[$this->element]) - 1));
+    $pager_limits[$this->element] = $this->limit;
+    $this->range($pager_page_array[$this->element] * $this->limit, $this->limit);
+
     return $this;
   }
 
diff --git modules/node/node.module modules/node/node.module
index d13a294..56792b2 100644
--- modules/node/node.module
+++ modules/node/node.module
@@ -1558,12 +1558,6 @@ function node_search_execute($keys = NULL) {
 
   // Add the ranking expressions.
   _node_rankings($query);
-
-  // Add a count query.
-  $inner_query = clone $query;
-  $count_query = db_select($inner_query->fields('i', array('sid')), NULL, array('target' => 'slave'));
-  $count_query->addExpression('COUNT(*)');
-  $query->setCountQuery($count_query);
   $find = $query
     ->limit(10)
     ->execute();
diff --git modules/search/search.extender.inc modules/search/search.extender.inc
index a38eec1..08a252c 100644
--- modules/search/search.extender.inc
+++ modules/search/search.extender.inc
@@ -442,4 +442,31 @@ class SearchQuery extends SelectQueryExtender {
 
     return $this->query->execute();
   }
+
+  /**
+   * Queries extended by SearchQuery must not be used as subqueries.
+   */
+  function hasExecute() {
+    return TRUE;
+  }
+
+  /**
+   * Count search queries always needs a subquery due to the group by.
+   */
+  public function countQuery() {
+    // Clone the inner query.
+    $count = clone $this->query;
+
+    // Zero-out existing fields.
+    $fields =& $count->getFields();
+    $fields = array();
+
+    // Add the sid as the only field and count them as a subquery.
+    $count = db_select($count->fields('i', array('sid')), NULL, array('target' => 'slave'));
+
+    // Add the COUNT() expression.
+    $count->addExpression('COUNT(*)');
+
+    return $count;
+  }
 }
diff --git modules/simpletest/tests/database_test.test modules/simpletest/tests/database_test.test
index 215e20c..2319d6c 100644
--- modules/simpletest/tests/database_test.test
+++ modules/simpletest/tests/database_test.test
@@ -2062,7 +2062,7 @@ class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase {
     $query
       ->fields('t', array('age'))
       ->orderBy('age')
-      ->limit(5);
+      ->limit(3);
 
     $outer_query = db_select($query);
     $outer_query->addField('subquery', 'age');
@@ -2070,7 +2070,7 @@ class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase {
     $ages = $outer_query
       ->execute()
       ->fetchCol();
-    $this->assertEqual($ages, array(25, 26, 27, 28), t('Inner pager query returned the correct ages.'));
+    $this->assertEqual($ages, array(25, 26, 27), t('Inner pager query returned the correct ages.'));
   }
 
   /**
