Merge "Avoid locking aggregated SELECT in Category::refresh"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 23 Apr 2018 20:00:14 +0000 (20:00 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 23 Apr 2018 20:00:14 +0000 (20:00 +0000)
1  2 
includes/Category.php

diff --combined includes/Category.php
@@@ -328,25 -328,35 +328,35 @@@ class Category 
                $dbw = wfGetDB( DB_MASTER );
                # Avoid excess contention on the same category (T162121)
                $name = __METHOD__ . ':' . md5( $this->mName );
 -              $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__, 1 );
 +              $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__, 0 );
                if ( !$scopedLock ) {
                        return false;
                }
  
                $dbw->startAtomic( __METHOD__ );
  
-               $cond1 = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY ], 1, 'NULL' );
-               $cond2 = $dbw->conditional( [ 'page_namespace' => NS_FILE ], 1, 'NULL' );
-               $result = $dbw->selectRow(
+               // Lock all the `categorylinks` records and gaps for this category;
+               // this is a separate query due to postgres/oracle limitations
+               $dbw->selectRowCount(
                        [ 'categorylinks', 'page' ],
-                       [ 'pages' => 'COUNT(*)',
-                               'subcats' => "COUNT($cond1)",
-                               'files' => "COUNT($cond2)"
-                       ],
+                       '*',
                        [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
                        __METHOD__,
                        [ 'LOCK IN SHARE MODE' ]
                );
+               // Get the aggregate `categorylinks` row counts for this category
+               $catCond = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY ], 1, 'NULL' );
+               $fileCond = $dbw->conditional( [ 'page_namespace' => NS_FILE ], 1, 'NULL' );
+               $result = $dbw->selectRow(
+                       [ 'categorylinks', 'page' ],
+                       [
+                               'pages' => 'COUNT(*)',
+                               'subcats' => "COUNT($catCond)",
+                               'files' => "COUNT($fileCond)"
+                       ],
+                       [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
+                       __METHOD__
+               );
  
                $shouldExist = $result->pages > 0 || $this->getTitle()->exists();