identify LogPager queries for load balancing
[lhc/web/wiklou.git] / includes / db / DatabasePostgres.php
index 72371a2..cfa2074 100644 (file)
@@ -79,6 +79,7 @@ SQL;
                $n->conname = $row->conname;
                $n->has_default = ( $row->atthasdef === 't' );
                $n->default = $row->adsrc;
+
                return $n;
        }
 
@@ -113,6 +114,7 @@ SQL;
        function conname() {
                return $this->conname;
        }
+
        /**
         * @since 1.19
         */
@@ -123,7 +125,6 @@ SQL;
                        return false;
                }
        }
-
 }
 
 /**
@@ -181,7 +182,6 @@ class PostgresTransactionState {
                                        }
                                        $old = next( $this->mCurrentState );
                                        $new = next( $this->mNewState );
-
                                }
                        }
                }
@@ -224,8 +224,8 @@ class SavepointPostgres {
                $this->didbegin = false;
                /* If we are not in a transaction, we need to be for savepoint trickery */
                if ( !$dbw->trxLevel() ) {
-                               $dbw->begin( "FOR SAVEPOINT" );
-                               $this->didbegin = true;
+                       $dbw->begin( "FOR SAVEPOINT" );
+                       $this->didbegin = true;
                }
        }
 
@@ -247,10 +247,10 @@ class SavepointPostgres {
                global $wgDebugDBTransactions;
                if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== false ) {
                        if ( $wgDebugDBTransactions ) {
-                               wfDebug( sprintf ( $msg_ok, $this->id ) );
+                               wfDebug( sprintf( $msg_ok, $this->id ) );
                        }
                } else {
-                       wfDebug( sprintf ( $msg_failed, $this->id ) );
+                       wfDebug( sprintf( $msg_failed, $this->id ) );
                }
        }
 
@@ -296,32 +296,42 @@ class DatabasePostgres extends DatabaseBase {
        function cascadingDeletes() {
                return true;
        }
+
        function cleanupTriggers() {
                return true;
        }
+
        function strictIPs() {
                return true;
        }
+
        function realTimestamps() {
                return true;
        }
+
        function implicitGroupby() {
                return false;
        }
+
        function implicitOrderby() {
                return false;
        }
+
        function searchableIPs() {
                return true;
        }
+
        function functionalIndexes() {
                return true;
        }
 
        function hasConstraint( $name ) {
-               $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = '" .
-                               pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" . pg_escape_string( $this->mConn, $this->getCoreSchema() ) . "'";
+               $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
+                       "WHERE c.connamespace = n.oid AND conname = '" .
+                       pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" .
+                       pg_escape_string( $this->mConn, $this->getCoreSchema() ) . "'";
                $res = $this->doQuery( $SQL );
+
                return $this->numRows( $res );
        }
 
@@ -337,7 +347,12 @@ class DatabasePostgres extends DatabaseBase {
        function open( $server, $user, $password, $dbName ) {
                # Test for Postgres support, to avoid suppressed fatal error
                if ( !function_exists( 'pg_connect' ) ) {
-                       throw new DBConnectionError( $this, "Postgres functions missing, have you compiled PHP with the --with-pgsql option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
+                       throw new DBConnectionError(
+                               $this,
+                               "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
+                               "option? (Note: if you recently installed PHP, you may need to restart your\n" .
+                               "webserver and database)\n"
+                       );
                }
 
                global $wgDBport;
@@ -382,7 +397,8 @@ class DatabasePostgres extends DatabaseBase {
 
                if ( !$this->mConn ) {
                        wfDebug( "DB connection error\n" );
-                       wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
+                       wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " .
+                               substr( $password, 0, 3 ) . "...\n" );
                        wfDebug( $this->lastError() . "\n" );
                        throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
                }
@@ -428,6 +444,7 @@ class DatabasePostgres extends DatabaseBase {
                foreach ( $vars as $name => $value ) {
                        $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
                }
+
                return $s;
        }
 
@@ -454,24 +471,28 @@ class DatabasePostgres extends DatabaseBase {
                if ( pg_result_error( $this->mLastResult ) ) {
                        return false;
                }
+
                return $this->mLastResult;
        }
 
        protected function dumpError() {
-               $diags = array( PGSQL_DIAG_SEVERITY,
-                               PGSQL_DIAG_SQLSTATE,
-                               PGSQL_DIAG_MESSAGE_PRIMARY,
-                               PGSQL_DIAG_MESSAGE_DETAIL,
-                               PGSQL_DIAG_MESSAGE_HINT,
-                               PGSQL_DIAG_STATEMENT_POSITION,
-                               PGSQL_DIAG_INTERNAL_POSITION,
-                               PGSQL_DIAG_INTERNAL_QUERY,
-                               PGSQL_DIAG_CONTEXT,
-                               PGSQL_DIAG_SOURCE_FILE,
-                               PGSQL_DIAG_SOURCE_LINE,
-                               PGSQL_DIAG_SOURCE_FUNCTION );
+               $diags = array(
+                       PGSQL_DIAG_SEVERITY,
+                       PGSQL_DIAG_SQLSTATE,
+                       PGSQL_DIAG_MESSAGE_PRIMARY,
+                       PGSQL_DIAG_MESSAGE_DETAIL,
+                       PGSQL_DIAG_MESSAGE_HINT,
+                       PGSQL_DIAG_STATEMENT_POSITION,
+                       PGSQL_DIAG_INTERNAL_POSITION,
+                       PGSQL_DIAG_INTERNAL_QUERY,
+                       PGSQL_DIAG_CONTEXT,
+                       PGSQL_DIAG_SOURCE_FILE,
+                       PGSQL_DIAG_SOURCE_LINE,
+                       PGSQL_DIAG_SOURCE_FUNCTION
+               );
                foreach ( $diags as $d ) {
-                       wfDebug( sprintf( "PgSQL ERROR(%d): %s\n", $d, pg_result_error_field( $this->mLastResult, $d ) ) );
+                       wfDebug( sprintf( "PgSQL ERROR(%d): %s\n",
+                               $d, pg_result_error_field( $this->mLastResult, $d ) ) );
                }
        }
 
@@ -481,6 +502,7 @@ class DatabasePostgres extends DatabaseBase {
                        /* Check for constraint violation */
                        if ( $errno === '23505' ) {
                                parent::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
+
                                return;
                        }
                }
@@ -517,8 +539,12 @@ class DatabasePostgres extends DatabaseBase {
                # @todo hashar: not sure if the following test really trigger if the object
                #          fetching failed.
                if ( pg_last_error( $this->mConn ) ) {
-                       throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
+                       throw new DBUnexpectedError(
+                               $this,
+                               'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
+                       );
                }
+
                return $row;
        }
 
@@ -530,8 +556,12 @@ class DatabasePostgres extends DatabaseBase {
                $row = pg_fetch_array( $res );
                wfRestoreWarnings();
                if ( pg_last_error( $this->mConn ) ) {
-                       throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
+                       throw new DBUnexpectedError(
+                               $this,
+                               'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
+                       );
                }
+
                return $row;
        }
 
@@ -543,8 +573,12 @@ class DatabasePostgres extends DatabaseBase {
                $n = pg_num_rows( $res );
                wfRestoreWarnings();
                if ( pg_last_error( $this->mConn ) ) {
-                       throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
+                       throw new DBUnexpectedError(
+                               $this,
+                               'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
+                       );
                }
+
                return $n;
        }
 
@@ -552,6 +586,7 @@ class DatabasePostgres extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
+
                return pg_num_fields( $res );
        }
 
@@ -559,6 +594,7 @@ class DatabasePostgres extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
+
                return pg_field_name( $res, $n );
        }
 
@@ -576,6 +612,7 @@ class DatabasePostgres extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
+
                return pg_result_seek( $res, $row );
        }
 
@@ -590,6 +627,7 @@ class DatabasePostgres extends DatabaseBase {
                        return 'No database connection';
                }
        }
+
        function lastErrno() {
                if ( $this->mLastResult ) {
                        return pg_result_error_field( $this->mLastResult, PGSQL_DIAG_SQLSTATE );
@@ -606,6 +644,7 @@ class DatabasePostgres extends DatabaseBase {
                if ( empty( $this->mLastResult ) ) {
                        return 0;
                }
+
                return pg_affected_rows( $this->mLastResult );
        }
 
@@ -617,7 +656,9 @@ class DatabasePostgres extends DatabaseBase {
         * Takes same arguments as Database::select()
         * @return int
         */
-       function estimateRowCount( $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = array() ) {
+       function estimateRowCount( $table, $vars = '*', $conds = '',
+               $fname = __METHOD__, $options = array()
+       ) {
                $options['EXPLAIN'] = true;
                $res = $this->select( $table, $vars, $conds, $fname, $options );
                $rows = -1;
@@ -628,6 +669,7 @@ class DatabasePostgres extends DatabaseBase {
                                $rows = $count[1];
                        }
                }
+
                return $rows;
        }
 
@@ -647,6 +689,7 @@ class DatabasePostgres extends DatabaseBase {
                                return $row;
                        }
                }
+
                return false;
        }
 
@@ -709,6 +752,7 @@ __INDEXATTR__;
                } else {
                        return null;
                }
+
                return $a;
        }
 
@@ -724,6 +768,7 @@ __INDEXATTR__;
                foreach ( $res as $row ) {
                        return true;
                }
+
                return false;
        }
 
@@ -851,15 +896,15 @@ __INDEXATTR__;
        /**
         * INSERT SELECT wrapper
         * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
-        * Source items may be literals rather then field names, but strings should be quoted with Database::addQuotes()
+        * Source items may be literals rather then field names, but strings should
+        * be quoted with Database::addQuotes()
         * $conds may be "*" to copy the whole table
         * srcTable may be an array of tables.
         * @todo FIXME: Implement this a little better (seperate select/insert)?
         * @return bool
         */
        function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
-               $insertOptions = array(), $selectOptions = array() )
-       {
+               $insertOptions = array(), $selectOptions = array() ) {
                $destTable = $this->tableName( $destTable );
 
                if ( !is_array( $insertOptions ) ) {
@@ -889,8 +934,8 @@ __INDEXATTR__;
                }
 
                $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
-                               " SELECT $startOpts " . implode( ',', $varMap ) .
-                               " FROM $srcTable $useIndex";
+                       " SELECT $startOpts " . implode( ',', $varMap ) .
+                       " FROM $srcTable $useIndex";
 
                if ( $conds != '*' ) {
                        $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
@@ -946,6 +991,7 @@ __INDEXATTR__;
                $res = $this->query( "SELECT nextval('$safeseq')" );
                $row = $this->fetchRow( $res );
                $this->mInsertId = $row[0];
+
                return $this->mInsertId;
        }
 
@@ -958,6 +1004,7 @@ __INDEXATTR__;
                $res = $this->query( "SELECT currval('$safeseq')" );
                $row = $this->fetchRow( $res );
                $currval = $row[0];
+
                return $currval;
        }
 
@@ -975,6 +1022,7 @@ __INDEXATTR__;
                } else {
                        $size = $row->size;
                }
+
                return $size;
        }
 
@@ -989,7 +1037,9 @@ __INDEXATTR__;
        function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
                $newName = $this->addIdentifierQuotes( $newName );
                $oldName = $this->addIdentifierQuotes( $oldName );
-               return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS)", $fname );
+
+               return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
+                       "(LIKE $oldName INCLUDING DEFAULTS)", $fname );
        }
 
        function listTables( $prefix = null, $fname = __METHOD__ ) {
@@ -1044,8 +1094,8 @@ __INDEXATTR__;
                                        $text, $match, 0, $offset );
                                $offset += strlen( $match[0] );
                                $output[] = ( '"' != $match[1][0]
-                                               ? $match[1]
-                                               : stripcslashes( substr( $match[1], 1, -1 ) ) );
+                                       ? $match[1]
+                                       : stripcslashes( substr( $match[1], 1, -1 ) ) );
                                if ( '},' == $match[3] ) {
                                        return $output;
                                }
@@ -1053,6 +1103,7 @@ __INDEXATTR__;
                                $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
                        }
                } while ( $limit > $offset );
+
                return $output;
        }
 
@@ -1080,6 +1131,7 @@ __INDEXATTR__;
        function getCurrentSchema() {
                $res = $this->query( "SELECT current_schema()", __METHOD__ );
                $row = $this->fetchRow( $res );
+
                return $row[0];
        }
 
@@ -1097,7 +1149,9 @@ __INDEXATTR__;
                $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
                $row = $this->fetchRow( $res );
                $schemas = array();
+
                /* PHP pgsql support does not support array type, "{a,b}" string is returned */
+
                return $this->pg_array_parse( $row[0], $schemas );
        }
 
@@ -1113,7 +1167,9 @@ __INDEXATTR__;
        function getSearchPath() {
                $res = $this->query( "SHOW search_path", __METHOD__ );
                $row = $this->fetchRow( $res );
+
                /* PostgreSQL returns SHOW values as strings */
+
                return explode( ",", $row[0] );
        }
 
@@ -1155,14 +1211,15 @@ __INDEXATTR__;
                                 */
                                $search_path = $this->getSearchPath();
                                array_unshift( $search_path,
-                                       $this->addIdentifierQuotes( $desired_schema ));
+                                       $this->addIdentifierQuotes( $desired_schema ) );
                                $this->setSearchPath( $search_path );
                                $this->mCoreSchema = $desired_schema;
                                wfDebug( "Schema \"" . $desired_schema . "\" added to the search path\n" );
                        }
                } else {
                        $this->mCoreSchema = $this->getCurrentSchema();
-                       wfDebug( "Schema \"" . $desired_schema . "\" not found, using current \"" . $this->mCoreSchema . "\"\n" );
+                       wfDebug( "Schema \"" . $desired_schema . "\" not found, using current \"" .
+                               $this->mCoreSchema . "\"\n" );
                }
                /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
                $this->commit( __METHOD__ );
@@ -1195,6 +1252,7 @@ __INDEXATTR__;
                                $this->numeric_version = pg_parameter_status( $this->mConn, 'server_version' );
                        }
                }
+
                return $this->numeric_version;
        }
 
@@ -1218,6 +1276,7 @@ __INDEXATTR__;
                        . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
                $res = $this->query( $SQL );
                $count = $res ? $res->numRows() : 0;
+
                return (bool)$count;
        }
 
@@ -1253,6 +1312,7 @@ SQL;
                        return null;
                }
                $rows = $res->numRows();
+
                return $rows;
        }
 
@@ -1264,12 +1324,13 @@ SQL;
                                'schemaname' => $this->getCoreSchema()
                        )
                );
+
                return $exists === $rule;
        }
 
        function constraintExists( $table, $constraint ) {
                $SQL = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
-                               "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
+                       "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
                        $this->addQuotes( $this->getCoreSchema() ),
                        $this->addQuotes( $table ),
                        $this->addQuotes( $constraint )
@@ -1279,6 +1340,7 @@ SQL;
                        return null;
                }
                $rows = $res->numRows();
+
                return $rows;
        }
 
@@ -1289,6 +1351,7 @@ SQL;
        function schemaExists( $schema ) {
                $exists = $this->selectField( '"pg_catalog"."pg_namespace"', 1,
                        array( 'nspname' => $schema ), __METHOD__ );
+
                return (bool)$exists;
        }
 
@@ -1299,6 +1362,7 @@ SQL;
        function roleExists( $roleName ) {
                $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
                        array( 'rolname' => $roleName ), __METHOD__ );
+
                return (bool)$exists;
        }
 
@@ -1314,6 +1378,7 @@ SQL;
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
+
                return pg_field_type( $res, $index );
        }
 
@@ -1329,6 +1394,7 @@ SQL;
                if ( $b instanceof Blob ) {
                        $b = $b->fetch();
                }
+
                return pg_unescape_bytea( $b );
        }
 
@@ -1348,6 +1414,7 @@ SQL;
                } elseif ( $s instanceof Blob ) {
                        return "'" . $s->fetch( $s ) . "'";
                }
+
                return "'" . pg_escape_string( $this->mConn, $s ) . "'";
        }
 
@@ -1431,6 +1498,14 @@ SQL;
                return implode( ' || ', $stringList );
        }
 
+       public function buildGroupConcatField(
+               $delimiter, $table, $field, $conds = '', $options = array(), $join_conds = array()
+       ) {
+               $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
+
+               return '(' . $this->selectSQLText( $table, $fld, $conds, null, array(), $join_conds ) . ')';
+       }
+
        public function getSearchEngine() {
                return 'SearchPostgres';
        }
@@ -1440,11 +1515,11 @@ SQL;
                if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
                        if ( $this->delimiter ) {
                                $this->delimiter = false;
-                       }
-                       else {
+                       } else {
                                $this->delimiter = ';';
                        }
                }
+
                return parent::streamStatementEnd( $sql, $newLine );
        }
 
@@ -1462,6 +1537,7 @@ SQL;
                $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
                        WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
                $row = $this->fetchObject( $result );
+
                return ( $row->lockstatus === 't' );
        }
 
@@ -1485,11 +1561,13 @@ SQL;
                        }
                }
                wfDebug( __METHOD__ . " failed to acquire lock\n" );
+
                return false;
        }
 
        /**
-        * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKSFROM PG DOCS: http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
+        * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKSFROM
+        * PG DOCS: http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
         * @param $lockName string
         * @param $method string
         * @return bool
@@ -1498,6 +1576,7 @@ SQL;
                $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
                $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
                $row = $this->fetchObject( $result );
+
                return ( $row->lockstatus === 't' );
        }