3 * This is the Postgres database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Wikimedia\Timestamp\ConvertibleTimestamp
;
24 use Wikimedia\WaitConditionLoop
;
25 use Wikimedia\Rdbms\Blob
;
26 use Wikimedia\Rdbms\PostgresBlob
;
27 use Wikimedia\Rdbms\PostgresField
;
32 class DatabasePostgres
extends Database
{
37 protected $mLastResult = null;
38 /** @var int The number of rows affected as an integer */
39 protected $mAffectedRows = null;
42 private $mInsertId = null;
43 /** @var float|string */
44 private $numericVersion = null;
45 /** @var string Connect string to open a PostgreSQL connection */
46 private $connectString;
49 /** @var string[] Map of (reserved table name => alternate table name) */
50 private $keywordTableMap = [];
53 * @see Database::__construct()
54 * @param array $params Additional parameters include:
55 * - keywordTableMap : Map of reserved table names to alternative table names to use
57 public function __construct( array $params ) {
58 $this->port
= isset( $params['port'] ) ?
$params['port'] : false;
59 $this->keywordTableMap
= isset( $params['keywordTableMap'] )
60 ?
$params['keywordTableMap']
63 parent
::__construct( $params );
66 public function getType() {
70 public function implicitGroupby() {
74 public function implicitOrderby() {
78 public function hasConstraint( $name ) {
79 $conn = $this->getBindingHandle();
81 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
82 "WHERE c.connamespace = n.oid AND conname = '" .
83 pg_escape_string( $conn, $name ) . "' AND n.nspname = '" .
84 pg_escape_string( $conn, $this->getCoreSchema() ) . "'";
85 $res = $this->doQuery( $sql );
87 return $this->numRows( $res );
90 public function open( $server, $user, $password, $dbName ) {
91 # Test for Postgres support, to avoid suppressed fatal error
92 if ( !function_exists( 'pg_connect' ) ) {
93 throw new DBConnectionError(
95 "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
96 "option? (Note: if you recently installed PHP, you may need to restart your\n" .
97 "webserver and database)\n"
101 $this->mServer
= $server;
102 $this->mUser
= $user;
103 $this->mPassword
= $password;
104 $this->mDBname
= $dbName;
109 'password' => $password
111 if ( $server != false && $server != '' ) {
112 $connectVars['host'] = $server;
114 if ( (int)$this->port
> 0 ) {
115 $connectVars['port'] = (int)$this->port
;
117 if ( $this->mFlags
& self
::DBO_SSL
) {
118 $connectVars['sslmode'] = 1;
121 $this->connectString
= $this->makeConnectionString( $connectVars );
123 $this->installErrorHandler();
126 // Use new connections to let LoadBalancer/LBFactory handle reuse
127 $this->mConn
= pg_connect( $this->connectString
, PGSQL_CONNECT_FORCE_NEW
);
128 } catch ( Exception
$ex ) {
129 $this->restoreErrorHandler();
133 $phpError = $this->restoreErrorHandler();
135 if ( !$this->mConn
) {
136 $this->queryLogger
->debug(
137 "DB connection error\n" .
138 "Server: $server, Database: $dbName, User: $user, Password: " .
139 substr( $password, 0, 3 ) . "...\n"
141 $this->queryLogger
->debug( $this->lastError() . "\n" );
142 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
145 $this->mOpened
= true;
147 # If called from the command-line (e.g. importDump), only show errors
148 if ( $this->cliMode
) {
149 $this->doQuery( "SET client_min_messages = 'ERROR'" );
152 $this->query( "SET client_encoding='UTF8'", __METHOD__
);
153 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__
);
154 $this->query( "SET timezone = 'GMT'", __METHOD__
);
155 $this->query( "SET standard_conforming_strings = on", __METHOD__
);
156 if ( $this->getServerVersion() >= 9.0 ) {
157 $this->query( "SET bytea_output = 'escape'", __METHOD__
); // PHP bug 53127
160 $this->determineCoreSchema( $this->mSchema
);
161 // The schema to be used is now in the search path; no need for explicit qualification
168 * Postgres doesn't support selectDB in the same way MySQL does. So if the
169 * DB name doesn't match the open connection, open a new one
173 public function selectDB( $db ) {
174 if ( $this->mDBname
!== $db ) {
175 return (bool)$this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $db );
182 * @param string[] $vars
185 private function makeConnectionString( $vars ) {
187 foreach ( $vars as $name => $value ) {
188 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
194 protected function closeConnection() {
195 return $this->mConn ?
pg_close( $this->mConn
) : true;
198 public function doQuery( $sql ) {
199 $conn = $this->getBindingHandle();
201 $sql = mb_convert_encoding( $sql, 'UTF-8' );
202 // Clear previously left over PQresult
203 while ( $res = pg_get_result( $conn ) ) {
204 pg_free_result( $res );
206 if ( pg_send_query( $conn, $sql ) === false ) {
207 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
209 $this->mLastResult
= pg_get_result( $conn );
210 $this->mAffectedRows
= null;
211 if ( pg_result_error( $this->mLastResult
) ) {
215 return $this->mLastResult
;
218 protected function dumpError() {
222 PGSQL_DIAG_MESSAGE_PRIMARY
,
223 PGSQL_DIAG_MESSAGE_DETAIL
,
224 PGSQL_DIAG_MESSAGE_HINT
,
225 PGSQL_DIAG_STATEMENT_POSITION
,
226 PGSQL_DIAG_INTERNAL_POSITION
,
227 PGSQL_DIAG_INTERNAL_QUERY
,
229 PGSQL_DIAG_SOURCE_FILE
,
230 PGSQL_DIAG_SOURCE_LINE
,
231 PGSQL_DIAG_SOURCE_FUNCTION
233 foreach ( $diags as $d ) {
234 $this->queryLogger
->debug( sprintf( "PgSQL ERROR(%d): %s\n",
235 $d, pg_result_error_field( $this->mLastResult
, $d ) ) );
239 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
241 /* Check for constraint violation */
242 if ( $errno === '23505' ) {
243 parent
::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
248 /* Transaction stays in the ERROR state until rolled back */
249 if ( $this->mTrxLevel
) {
250 $this->rollback( __METHOD__
);
252 parent
::reportQueryError( $error, $errno, $sql, $fname, false );
255 public function freeResult( $res ) {
256 if ( $res instanceof ResultWrapper
) {
259 MediaWiki\
suppressWarnings();
260 $ok = pg_free_result( $res );
261 MediaWiki\restoreWarnings
();
263 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
267 public function fetchObject( $res ) {
268 if ( $res instanceof ResultWrapper
) {
271 MediaWiki\
suppressWarnings();
272 $row = pg_fetch_object( $res );
273 MediaWiki\restoreWarnings
();
274 # @todo FIXME: HACK HACK HACK HACK debug
276 # @todo hashar: not sure if the following test really trigger if the object
278 $conn = $this->getBindingHandle();
279 if ( pg_last_error( $conn ) ) {
280 throw new DBUnexpectedError(
282 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
289 public function fetchRow( $res ) {
290 if ( $res instanceof ResultWrapper
) {
293 MediaWiki\
suppressWarnings();
294 $row = pg_fetch_array( $res );
295 MediaWiki\restoreWarnings
();
297 $conn = $this->getBindingHandle();
298 if ( pg_last_error( $conn ) ) {
299 throw new DBUnexpectedError(
301 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
308 public function numRows( $res ) {
309 if ( $res instanceof ResultWrapper
) {
312 MediaWiki\
suppressWarnings();
313 $n = pg_num_rows( $res );
314 MediaWiki\restoreWarnings
();
316 $conn = $this->getBindingHandle();
317 if ( pg_last_error( $conn ) ) {
318 throw new DBUnexpectedError(
320 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
327 public function numFields( $res ) {
328 if ( $res instanceof ResultWrapper
) {
332 return pg_num_fields( $res );
335 public function fieldName( $res, $n ) {
336 if ( $res instanceof ResultWrapper
) {
340 return pg_field_name( $res, $n );
344 * Return the result of the last call to nextSequenceValue();
345 * This must be called after nextSequenceValue().
349 public function insertId() {
350 return $this->mInsertId
;
353 public function dataSeek( $res, $row ) {
354 if ( $res instanceof ResultWrapper
) {
358 return pg_result_seek( $res, $row );
361 public function lastError() {
362 if ( $this->mConn
) {
363 if ( $this->mLastResult
) {
364 return pg_result_error( $this->mLastResult
);
366 return pg_last_error();
370 return $this->getLastPHPError() ?
: 'No database connection';
373 public function lastErrno() {
374 if ( $this->mLastResult
) {
375 return pg_result_error_field( $this->mLastResult
, PGSQL_DIAG_SQLSTATE
);
381 public function affectedRows() {
382 if ( !is_null( $this->mAffectedRows
) ) {
383 // Forced result for simulated queries
384 return $this->mAffectedRows
;
386 if ( empty( $this->mLastResult
) ) {
390 return pg_affected_rows( $this->mLastResult
);
394 * Estimate rows in dataset
395 * Returns estimated count, based on EXPLAIN output
396 * This is not necessarily an accurate estimate, so use sparingly
397 * Returns -1 if count cannot be found
398 * Takes same arguments as Database::select()
400 * @param string $table
401 * @param string $vars
402 * @param string $conds
403 * @param string $fname
404 * @param array $options
407 public function estimateRowCount( $table, $vars = '*', $conds = '',
408 $fname = __METHOD__
, $options = []
410 $options['EXPLAIN'] = true;
411 $res = $this->select( $table, $vars, $conds, $fname, $options );
414 $row = $this->fetchRow( $res );
416 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
417 $rows = (int)$count[1];
424 public function indexInfo( $table, $index, $fname = __METHOD__
) {
425 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
426 $res = $this->query( $sql, $fname );
430 foreach ( $res as $row ) {
431 if ( $row->indexname
== $this->indexName( $index ) ) {
439 public function indexAttributes( $index, $schema = false ) {
440 if ( $schema === false ) {
441 $schema = $this->getCoreSchema();
444 * A subquery would be not needed if we didn't care about the order
445 * of attributes, but we do
447 $sql = <<<__INDEXATTR__
451 i.indoption[s.g] as option,
454 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
458 ON cis.oid=isub.indexrelid
460 ON cis.relnamespace = ns.oid
461 WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
467 ON ci.oid=i.indexrelid
469 ON ct.oid = i.indrelid
471 ON ci.relnamespace = n.oid
473 ci.relname='$index' AND n.nspname='$schema'
474 AND attrelid = ct.oid
475 AND i.indkey[s.g] = attnum
476 AND i.indclass[s.g] = opcls.oid
477 AND pg_am.oid = opcls.opcmethod
479 $res = $this->query( $sql, __METHOD__ );
482 foreach ( $res as $row ) {
496 public function indexUnique( $table, $index, $fname = __METHOD__ ) {
497 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
498 " AND indexdef LIKE 'CREATE UNIQUE%(" .
499 $this->strencode( $this->indexName( $index ) ) .
501 $res = $this->query( $sql, $fname );
506 return $res->numRows() > 0;
509 public function selectSQLText(
510 $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
512 // Change the FOR UPDATE option as necessary based on the join conditions. Then pass
513 // to the parent function to get the actual SQL text.
514 // In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
515 // can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
516 // do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
518 // MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
519 if ( is_array( $options ) ) {
520 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
521 if ( $forUpdateKey !== false && $join_conds ) {
522 unset( $options[$forUpdateKey] );
524 foreach ( $join_conds as $table_cond => $join_cond ) {
525 if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
526 $options['FOR UPDATE'][] = $table_cond;
531 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
532 unset( $options['ORDER BY'] );
536 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
540 * INSERT wrapper, inserts an array into a table
542 * $args may be a single associative array, or an array of these with numeric keys,
543 * for multi-row insert (Postgres version 8.2 and above only).
545 * @param string $table Name of the table to insert to.
546 * @param array $args Items to insert into the table.
547 * @param string $fname Name of the function, for profiling
548 * @param array|string $options String or array. Valid options: IGNORE
549 * @return bool Success of insert operation. IGNORE always returns true.
551 public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
552 if ( !count( $args ) ) {
556 $table = $this->tableName( $table );
557 if ( !isset( $this->numericVersion ) ) {
558 $this->getServerVersion();
561 if ( !is_array( $options ) ) {
562 $options = [ $options ];
565 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
567 $keys = array_keys( $args[0] );
570 $keys = array_keys( $args );
573 // If IGNORE is set, we use savepoints to emulate mysql's behavior
574 $savepoint = $olde = null;
575 $numrowsinserted = 0;
576 if ( in_array( 'IGNORE', $options ) ) {
577 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
578 $olde = error_reporting( 0 );
579 // For future use, we may want to track the number of actual inserts
580 // Right now, insert (all writes) simply return true/false
583 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
586 if ( $this->numericVersion >= 8.2 && !$savepoint ) {
588 foreach ( $args as $row ) {
594 $sql .= '(' . $this->makeList( $row ) . ')';
596 $res = (bool)$this->query( $sql, $fname, $savepoint );
600 foreach ( $args as $row ) {
602 $tempsql .= '(' . $this->makeList( $row ) . ')';
605 $savepoint->savepoint();
608 $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
611 $bar = pg_result_error( $this->mLastResult );
612 if ( $bar != false ) {
613 $savepoint->rollback();
615 $savepoint->release();
620 // If any of them fail, we fail overall for this function call
621 // Note that this will be ignored if IGNORE is set
628 // Not multi, just a lone insert
630 $savepoint->savepoint();
633 $sql .= '(' . $this->makeList( $args ) . ')';
634 $res = (bool)$this->query( $sql, $fname, $savepoint );
636 $bar = pg_result_error( $this->mLastResult );
637 if ( $bar != false ) {
638 $savepoint->rollback();
640 $savepoint->release();
646 error_reporting( $olde );
647 $savepoint->commit();
649 // Set the affected row count for the whole operation
650 $this->mAffectedRows = $numrowsinserted;
652 // IGNORE always returns true
660 * INSERT SELECT wrapper
661 * $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
662 * Source items may be literals rather then field names, but strings should
663 * be quoted with Database::addQuotes()
664 * $conds may be "*" to copy the whole table
665 * srcTable may be an array of tables.
666 * @todo FIXME: Implement this a little better (seperate select/insert)?
668 * @param string $destTable
669 * @param array|string $srcTable
670 * @param array $varMap
671 * @param array $conds
672 * @param string $fname
673 * @param array $insertOptions
674 * @param array $selectOptions
677 public function nativeInsertSelect(
678 $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
679 $insertOptions = [], $selectOptions = []
681 $destTable = $this->tableName( $destTable );
683 if ( !is_array( $insertOptions ) ) {
684 $insertOptions = [ $insertOptions ];
688 * If IGNORE is set, we use savepoints to emulate mysql's behavior
689 * Ignore LOW PRIORITY option, since it is MySQL-specific
691 $savepoint = $olde = null;
692 $numrowsinserted = 0;
693 if ( in_array( 'IGNORE', $insertOptions ) ) {
694 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
695 $olde = error_reporting( 0 );
696 $savepoint->savepoint();
699 if ( !is_array( $selectOptions ) ) {
700 $selectOptions = [ $selectOptions ];
702 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) =
703 $this->makeSelectOptions( $selectOptions );
704 if ( is_array( $srcTable ) ) {
705 $srcTable = implode( ',', array_map( [ $this, 'tableName' ], $srcTable ) );
707 $srcTable = $this->tableName( $srcTable );
710 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
711 " SELECT $startOpts " . implode( ',', $varMap ) .
712 " FROM $srcTable $useIndex $ignoreIndex ";
714 if ( $conds != '*' ) {
715 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
718 $sql .= " $tailOpts";
720 $res = (bool)$this->query( $sql, $fname, $savepoint );
722 $bar = pg_result_error( $this->mLastResult );
723 if ( $bar != false ) {
724 $savepoint->rollback();
726 $savepoint->release();
729 error_reporting( $olde );
730 $savepoint->commit();
732 // Set the affected row count for the whole operation
733 $this->mAffectedRows = $numrowsinserted;
735 // IGNORE always returns true
742 public function tableName( $name, $format = 'quoted' ) {
743 // Replace reserved words with better ones
744 $name = $this->remappedTableName( $name );
746 return parent::tableName( $name, $format );
750 * @param string $name
751 * @return string Value of $name or remapped name if $name is a reserved keyword
753 public function remappedTableName( $name ) {
754 return isset( $this->keywordTableMap[$name] ) ? $this->keywordTableMap[$name] : $name;
758 * @param string $name
759 * @param string $format
760 * @return string Qualified and encoded (if requested) table name
762 public function realTableName( $name, $format = 'quoted' ) {
763 return parent::tableName( $name, $format );
766 public function nextSequenceValue( $seqName ) {
767 $safeseq = str_replace( "'", "''", $seqName );
768 $res = $this->query( "SELECT nextval('$safeseq')" );
769 $row = $this->fetchRow( $res );
770 $this->mInsertId = $row[0];
772 return $this->mInsertId;
776 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
778 * @param string $seqName
781 public function currentSequenceValue( $seqName ) {
782 $safeseq = str_replace( "'", "''", $seqName );
783 $res = $this->query( "SELECT currval('$safeseq')" );
784 $row = $this->fetchRow( $res );
790 public function textFieldSize( $table, $field ) {
791 $table = $this->tableName( $table );
792 $sql = "SELECT t.typname as ftype,a.atttypmod as size
793 FROM pg_class c, pg_attribute a, pg_type t
794 WHERE relname='$table' AND a.attrelid=c.oid AND
795 a.atttypid=t.oid and a.attname='$field'";
796 $res = $this->query( $sql );
797 $row = $this->fetchObject( $res );
798 if ( $row->ftype == 'varchar' ) {
799 $size = $row->size - 4;
807 public function limitResult( $sql, $limit, $offset = false ) {
808 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
811 public function wasDeadlock() {
812 return $this->lastErrno() == '40P01';
815 public function duplicateTableStructure(
816 $oldName, $newName, $temporary = false, $fname = __METHOD__
818 $newName = $this->addIdentifierQuotes( $newName );
819 $oldName = $this->addIdentifierQuotes( $oldName );
821 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
822 "(LIKE $oldName INCLUDING DEFAULTS)", $fname );
825 public function listTables( $prefix = null, $fname = __METHOD__ ) {
826 $eschema = $this->addQuotes( $this->getCoreSchema() );
827 $result = $this->query(
828 "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
831 foreach ( $result as $table ) {
832 $vars = get_object_vars( $table );
833 $table = array_pop( $vars );
834 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
835 $endArray[] = $table;
842 public function timestamp( $ts = 0 ) {
843 $ct = new ConvertibleTimestamp( $ts );
845 return $ct->getTimestamp( TS_POSTGRES );
849 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
850 * to https://secure.php.net/manual/en/ref.pgsql.php
852 * Parsing a postgres array can be a tricky problem, he's my
853 * take on this, it handles multi-dimensional arrays plus
854 * escaping using a nasty regexp to determine the limits of each
857 * This should really be handled by PHP PostgreSQL module
860 * @param string $text Postgreql array returned in a text form like {a,b}
861 * @param string $output
862 * @param int|bool $limit
866 private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
867 if ( false === $limit ) {
868 $limit = strlen( $text ) - 1;
871 if ( '{}' == $text ) {
875 if ( '{' != $text[$offset] ) {
876 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
877 $text, $match, 0, $offset );
878 $offset += strlen( $match[0] );
879 $output[] = ( '"' != $match[1][0]
881 : stripcslashes( substr( $match[1], 1, -1 ) ) );
882 if ( '},' == $match[3] ) {
886 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
888 } while ( $limit > $offset );
893 public function aggregateValue( $valuedata, $valuename = 'value' ) {
897 public function getSoftwareLink() {
898 return '[{{int:version-db-postgres-url}} PostgreSQL]';
902 * Return current schema (executes SELECT current_schema())
906 * @return string Default schema for the current session
908 public function getCurrentSchema() {
909 $res = $this->query( "SELECT current_schema()", __METHOD__ );
910 $row = $this->fetchRow( $res );
916 * Return list of schemas which are accessible without schema name
917 * This is list does not contain magic keywords like "$user"
920 * @see getSearchPath()
921 * @see setSearchPath()
923 * @return array List of actual schemas for the current sesson
925 public function getSchemas() {
926 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
927 $row = $this->fetchRow( $res );
930 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
932 return $this->pg_array_parse( $row[0], $schemas );
936 * Return search patch for schemas
937 * This is different from getSchemas() since it contain magic keywords
942 * @return array How to search for table names schemas for the current user
944 public function getSearchPath() {
945 $res = $this->query( "SHOW search_path", __METHOD__ );
946 $row = $this->fetchRow( $res );
948 /* PostgreSQL returns SHOW values as strings */
950 return explode( ",", $row[0] );
954 * Update search_path, values should already be sanitized
955 * Values may contain magic keywords like "$user"
958 * @param array $search_path List of schemas to be searched by default
960 private function setSearchPath( $search_path ) {
961 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
965 * Determine default schema for the current application
966 * Adjust this session schema search path if desired schema exists
967 * and is not alread there.
969 * We need to have name of the core schema stored to be able
970 * to query database metadata.
972 * This will be also called by the installer after the schema is created
976 * @param string $desiredSchema
978 public function determineCoreSchema( $desiredSchema ) {
979 $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
980 if ( $this->schemaExists( $desiredSchema ) ) {
981 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
982 $this->mCoreSchema = $desiredSchema;
983 $this->queryLogger->debug(
984 "Schema \"" . $desiredSchema . "\" already in the search path\n" );
987 * Prepend our schema (e.g. 'mediawiki') in front
991 $search_path = $this->getSearchPath();
992 array_unshift( $search_path,
993 $this->addIdentifierQuotes( $desiredSchema ) );
994 $this->setSearchPath( $search_path );
995 $this->mCoreSchema = $desiredSchema;
996 $this->queryLogger->debug(
997 "Schema \"" . $desiredSchema . "\" added to the search path\n" );
1000 $this->mCoreSchema = $this->getCurrentSchema();
1001 $this->queryLogger->debug(
1002 "Schema \"" . $desiredSchema . "\" not found, using current \"" .
1003 $this->mCoreSchema . "\"\n" );
1005 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
1006 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
1010 * Return schema name for core application tables
1013 * @return string Core schema name
1015 public function getCoreSchema() {
1016 return $this->mCoreSchema;
1019 public function getServerVersion() {
1020 if ( !isset( $this->numericVersion ) ) {
1021 $conn = $this->getBindingHandle();
1022 $versionInfo = pg_version( $conn );
1023 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1024 // Old client, abort install
1025 $this->numericVersion = '7.3 or earlier';
1026 } elseif ( isset( $versionInfo['server'] ) ) {
1028 $this->numericVersion = $versionInfo['server'];
1030 // T18937: broken pgsql extension from PHP<5.3
1031 $this->numericVersion = pg_parameter_status( $conn, 'server_version' );
1035 return $this->numericVersion;
1039 * Query whether a given relation exists (in the given schema, or the
1040 * default mw one if not given)
1041 * @param string $table
1042 * @param array|string $types
1043 * @param bool|string $schema
1046 private function relationExists( $table, $types, $schema = false ) {
1047 if ( !is_array( $types ) ) {
1048 $types = [ $types ];
1050 if ( $schema === false ) {
1051 $schema = $this->getCoreSchema();
1053 $etable = $this->addQuotes( $table );
1054 $eschema = $this->addQuotes( $schema );
1055 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1056 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1057 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1058 $res = $this->query( $sql );
1059 $count = $res ? $res->numRows() : 0;
1061 return (bool)$count;
1065 * For backward compatibility, this function checks both tables and views.
1066 * @param string $table
1067 * @param string $fname
1068 * @param bool|string $schema
1071 public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1072 return $this->relationExists( $table, [ 'r', 'v' ], $schema );
1075 public function sequenceExists( $sequence, $schema = false ) {
1076 return $this->relationExists( $sequence, 'S', $schema );
1079 public function triggerExists( $table, $trigger ) {
1081 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1082 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1083 AND tgrelid=pg_class.oid
1084 AND nspname=%s AND relname=%s AND tgname=%s
1086 $res = $this->query(
1089 $this->addQuotes( $this->getCoreSchema() ),
1090 $this->addQuotes( $table ),
1091 $this->addQuotes( $trigger )
1097 $rows = $res->numRows();
1102 public function ruleExists( $table, $rule ) {
1103 $exists = $this->selectField( 'pg_rules', 'rulename',
1105 'rulename' => $rule,
1106 'tablename' => $table,
1107 'schemaname' => $this->getCoreSchema()
1111 return $exists === $rule;
1114 public function constraintExists( $table, $constraint ) {
1115 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1116 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1117 $this->addQuotes( $this->getCoreSchema() ),
1118 $this->addQuotes( $table ),
1119 $this->addQuotes( $constraint )
1121 $res = $this->query( $sql );
1125 $rows = $res->numRows();
1131 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1132 * @param string $schema
1135 public function schemaExists( $schema ) {
1136 if ( !strlen( $schema ) ) {
1137 return false; // short-circuit
1140 $exists = $this->selectField(
1141 '"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
1143 return (bool)$exists;
1147 * Returns true if a given role (i.e. user) exists, false otherwise.
1148 * @param string $roleName
1151 public function roleExists( $roleName ) {
1152 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1153 [ 'rolname' => $roleName ], __METHOD__ );
1155 return (bool)$exists;
1159 * @var string $table
1160 * @var string $field
1161 * @return PostgresField|null
1163 public function fieldInfo( $table, $field ) {
1164 return PostgresField::fromText( $this, $table, $field );
1168 * pg_field_type() wrapper
1169 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1170 * @param int $index Field number, starting from 0
1173 public function fieldType( $res, $index ) {
1174 if ( $res instanceof ResultWrapper ) {
1175 $res = $res->result;
1178 return pg_field_type( $res, $index );
1181 public function encodeBlob( $b ) {
1182 return new PostgresBlob( pg_escape_bytea( $b ) );
1185 public function decodeBlob( $b ) {
1186 if ( $b instanceof PostgresBlob ) {
1188 } elseif ( $b instanceof Blob ) {
1192 return pg_unescape_bytea( $b );
1195 public function strencode( $s ) {
1196 // Should not be called by us
1197 return pg_escape_string( $this->getBindingHandle(), $s );
1200 public function addQuotes( $s ) {
1201 $conn = $this->getBindingHandle();
1203 if ( is_null( $s ) ) {
1205 } elseif ( is_bool( $s ) ) {
1206 return intval( $s );
1207 } elseif ( $s instanceof Blob ) {
1208 if ( $s instanceof PostgresBlob ) {
1211 $s = pg_escape_bytea( $conn, $s->fetch() );
1216 return "'" . pg_escape_string( $conn, $s ) . "'";
1220 * Postgres specific version of replaceVars.
1221 * Calls the parent version in Database.php
1223 * @param string $ins SQL string, read from a stream (usually tables.sql)
1224 * @return string SQL string
1226 protected function replaceVars( $ins ) {
1227 $ins = parent::replaceVars( $ins );
1229 if ( $this->numericVersion >= 8.3 ) {
1230 // Thanks for not providing backwards-compatibility, 8.3
1231 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1234 if ( $this->numericVersion <= 8.1 ) { // Our minimum version
1235 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1241 public function makeSelectOptions( $options ) {
1242 $preLimitTail = $postLimitTail = '';
1243 $startOpts = $useIndex = $ignoreIndex = '';
1246 foreach ( $options as $key => $option ) {
1247 if ( is_numeric( $key ) ) {
1248 $noKeyOptions[$option] = true;
1252 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1254 $preLimitTail .= $this->makeOrderBy( $options );
1256 if ( isset( $options['FOR UPDATE'] ) ) {
1257 $postLimitTail .= ' FOR UPDATE OF ' .
1258 implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
1259 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1260 $postLimitTail .= ' FOR UPDATE';
1263 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1264 $startOpts .= 'DISTINCT';
1267 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1270 public function getDBname() {
1271 return $this->mDBname;
1274 public function getServer() {
1275 return $this->mServer;
1278 public function buildConcat( $stringList ) {
1279 return implode( ' || ', $stringList );
1282 public function buildGroupConcatField(
1283 $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
1285 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1287 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1290 public function buildStringCast( $field ) {
1291 return $field . '::text';
1294 public function streamStatementEnd( &$sql, &$newLine ) {
1295 # Allow dollar quoting for function declarations
1296 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1297 if ( $this->delimiter ) {
1298 $this->delimiter = false;
1300 $this->delimiter = ';';
1304 return parent::streamStatementEnd( $sql, $newLine );
1307 public function lockIsFree( $lockName, $method ) {
1308 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1309 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1310 $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
1311 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1312 $row = $this->fetchObject( $result );
1314 return ( $row->lockstatus === 't' );
1317 public function lock( $lockName, $method, $timeout = 5 ) {
1318 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1319 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1320 $loop = new WaitConditionLoop(
1321 function () use ( $lockName, $key, $timeout, $method ) {
1322 $res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1323 $row = $this->fetchObject( $res );
1324 if ( $row->lockstatus === 't' ) {
1325 parent::lock( $lockName, $method, $timeout ); // record
1329 return WaitConditionLoop::CONDITION_CONTINUE;
1334 return ( $loop->invoke() === $loop::CONDITION_REACHED );
1337 public function unlock( $lockName, $method ) {
1338 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1339 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1340 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1341 $row = $this->fetchObject( $result );
1343 if ( $row->lockstatus === 't' ) {
1344 parent::unlock( $lockName, $method ); // record
1348 $this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
1354 * @param string $lockName
1355 * @return string Integer
1357 private function bigintFromLockName( $lockName ) {
1358 return Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );