diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index e5e636b..84c3ce9 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -835,6 +835,7 @@ function comment_get_thread($node, $mode, $comments_per_page) {
     ->condition('c.nid', $node->nid)
     ->addTag('node_access')
     ->addTag('comment_filter')
+    ->addTag('comment_pager')
     ->addMetaData('node', $node);
 
   if (!user_access('administer comments')) {
@@ -2447,3 +2448,19 @@ function comment_file_download_access($field, $entity_type, $entity) {
     return FALSE;
   }
 }
+
+/**
+ * Implements hook_query_TAG_alter().
+ *
+ * Adds a GROUP BY clause to node access queries so that the pager is rendered
+ * properly when the user has access from multiple groups within a given
+ * grant realm.
+ *
+ * @see comment_get_thread()
+ */
+function comment_query_node_access_alter(QueryAlterableInterface $query) {
+  if ($query->hasTag('comment_pager')) {
+    $query->groupBy('na.realm');
+    $query->groupBy('na.gid');
+  }
+}
diff --git a/core/modules/node/node.test b/core/modules/node/node.test
index 2e9b075..d97474d 100644
--- a/core/modules/node/node.test
+++ b/core/modules/node/node.test
@@ -2300,3 +2300,55 @@ class NodeTokenReplaceTestCase extends DrupalWebTestCase {
     }
   }
 }
+
+/**
+ * Tests comment pagination with a node access module enabled.
+ */
+class NodeAccessCommentPagerTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Node comment pagination',
+      'description' => 'Test access controlled nodes have the right amount of comment pages.',
+      'group' => 'Node',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp('node_access_test', 'comment');
+    node_access_rebuild();
+    $this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'node test view'));
+  }
+
+  /**
+   * Tests the comment pager for nodes with multiple grants per realm.
+   */
+  public function testPageCount() {
+    // Create a node.
+    $node = $this->drupalCreateNode();
+
+    // Create 60 comments.
+    for ($i = 0; $i < 60; $i++) {
+      $comment = entity_create('comment', array(
+        'nid' => $node->nid,
+        'subject' => $this->randomName(),
+        'comment_body' => array(
+          LANGUAGE_NONE => array(
+            array('value' => $this->randomName()),
+          ),
+        ),
+      ));
+      $comment->save();
+    }
+
+    $this->drupalLogin($this->web_user);
+
+    // View the node page. With the default 50 comments per page there should
+    // be two pages (0, 1) but no third (2) page.
+    $this->drupalGet('node/' . $node->nid);
+    $this->assertText($node->title, t('Node title found.'));
+    $this->assertText(t('Comments'), t('Has a comments section.'));
+    $this->assertRaw('page=1', t('Secound page exists.'));
+    $this->assertNoRaw('page=2', t('No third page exists.'));
+  }
+}
diff --git a/core/modules/node/tests/node_access_test.module b/core/modules/node/tests/node_access_test.module
index f946573..02c3257 100644
--- a/core/modules/node/tests/node_access_test.module
+++ b/core/modules/node/tests/node_access_test.module
@@ -15,7 +15,7 @@ function node_access_test_node_grants($account, $op) {
   // First grant a grant to the author for own content.
   $grants['node_access_test_author'] = array($account->uid);
   if ($op == 'view' && user_access('node test view', $account)) {
-    $grants['node_access_test'] = array(8888);
+    $grants['node_access_test'] = array(8888, 8889);
   }
   if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
     $grants['node_access_all'] = array(0);
@@ -38,6 +38,14 @@ function node_access_test_node_access_records($node) {
       'grant_delete' => 0,
       'priority' => 0,
     );
+    $grants[] = array(
+      'realm' => 'node_access_test',
+      'gid' => 8889,
+      'grant_view' => 1,
+      'grant_update' => 0,
+      'grant_delete' => 0,
+      'priority' => 0,
+    );
     // For the author realm, the GID is equivalent to a UID, which
     // means there are many many groups of just 1 user.
     $grants[] = array(
