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 namespace Wikimedia\Rdbms
;
25 use Wikimedia\Timestamp\ConvertibleTimestamp
;
26 use Wikimedia\WaitConditionLoop
;
28 use DBUnexpectedError
;
29 use DBConnectionError
;
35 class DatabasePostgres
extends Database
{
40 protected $mLastResult = null;
41 /** @var int The number of rows affected as an integer */
42 protected $mAffectedRows = null;
45 private $mInsertId = null;
46 /** @var float|string */
47 private $numericVersion = null;
48 /** @var string Connect string to open a PostgreSQL connection */
49 private $connectString;
52 /** @var string[] Map of (reserved table name => alternate table name) */
53 private $keywordTableMap = [];
56 * @see Database::__construct()
57 * @param array $params Additional parameters include:
58 * - keywordTableMap : Map of reserved table names to alternative table names to use
60 public function __construct( array $params ) {
61 $this->port
= isset( $params['port'] ) ?
$params['port'] : false;
62 $this->keywordTableMap
= isset( $params['keywordTableMap'] )
63 ?
$params['keywordTableMap']
66 parent
::__construct( $params );
69 public function getType() {
73 public function implicitGroupby() {
77 public function implicitOrderby() {
81 public function hasConstraint( $name ) {
82 $conn = $this->getBindingHandle();
84 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
85 "WHERE c.connamespace = n.oid AND conname = '" .
86 pg_escape_string( $conn, $name ) . "' AND n.nspname = '" .
87 pg_escape_string( $conn, $this->getCoreSchema() ) . "'";
88 $res = $this->doQuery( $sql );
90 return $this->numRows( $res );
93 public function open( $server, $user, $password, $dbName ) {
94 # Test for Postgres support, to avoid suppressed fatal error
95 if ( !function_exists( 'pg_connect' ) ) {
96 throw new DBConnectionError(
98 "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
99 "option? (Note: if you recently installed PHP, you may need to restart your\n" .
100 "webserver and database)\n"
104 $this->mServer
= $server;
105 $this->mUser
= $user;
106 $this->mPassword
= $password;
107 $this->mDBname
= $dbName;
112 'password' => $password
114 if ( $server != false && $server != '' ) {
115 $connectVars['host'] = $server;
117 if ( (int)$this->port
> 0 ) {
118 $connectVars['port'] = (int)$this->port
;
120 if ( $this->mFlags
& self
::DBO_SSL
) {
121 $connectVars['sslmode'] = 1;
124 $this->connectString
= $this->makeConnectionString( $connectVars );
126 $this->installErrorHandler();
129 // Use new connections to let LoadBalancer/LBFactory handle reuse
130 $this->mConn
= pg_connect( $this->connectString
, PGSQL_CONNECT_FORCE_NEW
);
131 } catch ( Exception
$ex ) {
132 $this->restoreErrorHandler();
136 $phpError = $this->restoreErrorHandler();
138 if ( !$this->mConn
) {
139 $this->queryLogger
->debug(
140 "DB connection error\n" .
141 "Server: $server, Database: $dbName, User: $user, Password: " .
142 substr( $password, 0, 3 ) . "...\n"
144 $this->queryLogger
->debug( $this->lastError() . "\n" );
145 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
148 $this->mOpened
= true;
150 # If called from the command-line (e.g. importDump), only show errors
151 if ( $this->cliMode
) {
152 $this->doQuery( "SET client_min_messages = 'ERROR'" );
155 $this->query( "SET client_encoding='UTF8'", __METHOD__
);
156 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__
);
157 $this->query( "SET timezone = 'GMT'", __METHOD__
);
158 $this->query( "SET standard_conforming_strings = on", __METHOD__
);
159 if ( $this->getServerVersion() >= 9.0 ) {
160 $this->query( "SET bytea_output = 'escape'", __METHOD__
); // PHP bug 53127
163 $this->determineCoreSchema( $this->mSchema
);
164 // The schema to be used is now in the search path; no need for explicit qualification
171 * Postgres doesn't support selectDB in the same way MySQL does. So if the
172 * DB name doesn't match the open connection, open a new one
176 public function selectDB( $db ) {
177 if ( $this->mDBname
!== $db ) {
178 return (bool)$this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $db );
185 * @param string[] $vars
188 private function makeConnectionString( $vars ) {
190 foreach ( $vars as $name => $value ) {
191 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
197 protected function closeConnection() {
198 return $this->mConn ?
pg_close( $this->mConn
) : true;
201 public function doQuery( $sql ) {
202 $conn = $this->getBindingHandle();
204 $sql = mb_convert_encoding( $sql, 'UTF-8' );
205 // Clear previously left over PQresult
206 while ( $res = pg_get_result( $conn ) ) {
207 pg_free_result( $res );
209 if ( pg_send_query( $conn, $sql ) === false ) {
210 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
212 $this->mLastResult
= pg_get_result( $conn );
213 $this->mAffectedRows
= null;
214 if ( pg_result_error( $this->mLastResult
) ) {
218 return $this->mLastResult
;
221 protected function dumpError() {
225 PGSQL_DIAG_MESSAGE_PRIMARY
,
226 PGSQL_DIAG_MESSAGE_DETAIL
,
227 PGSQL_DIAG_MESSAGE_HINT
,
228 PGSQL_DIAG_STATEMENT_POSITION
,
229 PGSQL_DIAG_INTERNAL_POSITION
,
230 PGSQL_DIAG_INTERNAL_QUERY
,
232 PGSQL_DIAG_SOURCE_FILE
,
233 PGSQL_DIAG_SOURCE_LINE
,
234 PGSQL_DIAG_SOURCE_FUNCTION
236 foreach ( $diags as $d ) {
237 $this->queryLogger
->debug( sprintf( "PgSQL ERROR(%d): %s\n",
238 $d, pg_result_error_field( $this->mLastResult
, $d ) ) );
242 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
244 /* Check for constraint violation */
245 if ( $errno === '23505' ) {
246 parent
::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
251 /* Transaction stays in the ERROR state until rolled back */
252 if ( $this->mTrxLevel
) {
253 $this->rollback( __METHOD__
);
255 parent
::reportQueryError( $error, $errno, $sql, $fname, false );
258 public function freeResult( $res ) {
259 if ( $res instanceof ResultWrapper
) {
262 MediaWiki\
suppressWarnings();
263 $ok = pg_free_result( $res );
264 MediaWiki\restoreWarnings
();
266 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
270 public function fetchObject( $res ) {
271 if ( $res instanceof ResultWrapper
) {
274 MediaWiki\
suppressWarnings();
275 $row = pg_fetch_object( $res );
276 MediaWiki\restoreWarnings
();
277 # @todo FIXME: HACK HACK HACK HACK debug
279 # @todo hashar: not sure if the following test really trigger if the object
281 $conn = $this->getBindingHandle();
282 if ( pg_last_error( $conn ) ) {
283 throw new DBUnexpectedError(
285 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
292 public function fetchRow( $res ) {
293 if ( $res instanceof ResultWrapper
) {
296 MediaWiki\
suppressWarnings();
297 $row = pg_fetch_array( $res );
298 MediaWiki\restoreWarnings
();
300 $conn = $this->getBindingHandle();
301 if ( pg_last_error( $conn ) ) {
302 throw new DBUnexpectedError(
304 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
311 public function numRows( $res ) {
312 if ( $res instanceof ResultWrapper
) {
315 MediaWiki\
suppressWarnings();
316 $n = pg_num_rows( $res );
317 MediaWiki\restoreWarnings
();
319 $conn = $this->getBindingHandle();
320 if ( pg_last_error( $conn ) ) {
321 throw new DBUnexpectedError(
323 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
330 public function numFields( $res ) {
331 if ( $res instanceof ResultWrapper
) {
335 return pg_num_fields( $res );
338 public function fieldName( $res, $n ) {
339 if ( $res instanceof ResultWrapper
) {
343 return pg_field_name( $res, $n );
347 * Return the result of the last call to nextSequenceValue();
348 * This must be called after nextSequenceValue().
352 public function insertId() {
353 return $this->mInsertId
;
356 public function dataSeek( $res, $row ) {
357 if ( $res instanceof ResultWrapper
) {
361 return pg_result_seek( $res, $row );
364 public function lastError() {
365 if ( $this->mConn
) {
366 if ( $this->mLastResult
) {
367 return pg_result_error( $this->mLastResult
);
369 return pg_last_error();
373 return $this->getLastPHPError() ?
: 'No database connection';
376 public function lastErrno() {
377 if ( $this->mLastResult
) {
378 return pg_result_error_field( $this->mLastResult
, PGSQL_DIAG_SQLSTATE
);
384 public function affectedRows() {
385 if ( !is_null( $this->mAffectedRows
) ) {
386 // Forced result for simulated queries
387 return $this->mAffectedRows
;
389 if ( empty( $this->mLastResult
) ) {
393 return pg_affected_rows( $this->mLastResult
);
397 * Estimate rows in dataset
398 * Returns estimated count, based on EXPLAIN output
399 * This is not necessarily an accurate estimate, so use sparingly
400 * Returns -1 if count cannot be found
401 * Takes same arguments as Database::select()
403 * @param string $table
404 * @param string $vars
405 * @param string $conds
406 * @param string $fname
407 * @param array $options
410 public function estimateRowCount( $table, $vars = '*', $conds = '',
411 $fname = __METHOD__
, $options = []
413 $options['EXPLAIN'] = true;
414 $res = $this->select( $table, $vars, $conds, $fname, $options );
417 $row = $this->fetchRow( $res );
419 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
420 $rows = (int)$count[1];
427 public function indexInfo( $table, $index, $fname = __METHOD__
) {
428 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
429 $res = $this->query( $sql, $fname );
433 foreach ( $res as $row ) {
434 if ( $row->indexname
== $this->indexName( $index ) ) {
442 public function indexAttributes( $index, $schema = false ) {
443 if ( $schema === false ) {
444 $schema = $this->getCoreSchema();
447 * A subquery would be not needed if we didn't care about the order
448 * of attributes, but we do
450 $sql = <<<__INDEXATTR__
454 i.indoption[s.g] as option,
457 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
461 ON cis.oid=isub.indexrelid
463 ON cis.relnamespace = ns.oid
464 WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
470 ON ci.oid=i.indexrelid
472 ON ct.oid = i.indrelid
474 ON ci.relnamespace = n.oid
476 ci.relname='$index' AND n.nspname='$schema'
477 AND attrelid = ct.oid
478 AND i.indkey[s.g] = attnum
479 AND i.indclass[s.g] = opcls.oid
480 AND pg_am.oid = opcls.opcmethod
482 $res = $this->query( $sql, __METHOD__ );
485 foreach ( $res as $row ) {
499 public function indexUnique( $table, $index, $fname = __METHOD__ ) {
500 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
501 " AND indexdef LIKE 'CREATE UNIQUE%(" .
502 $this->strencode( $this->indexName( $index ) ) .
504 $res = $this->query( $sql, $fname );
509 return $res->numRows() > 0;
512 public function selectSQLText(
513 $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
515 // Change the FOR UPDATE option as necessary based on the join conditions. Then pass
516 // to the parent function to get the actual SQL text.
517 // In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
518 // can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
519 // do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
521 // MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
522 if ( is_array( $options ) ) {
523 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
524 if ( $forUpdateKey !== false && $join_conds ) {
525 unset( $options[$forUpdateKey] );
527 foreach ( $join_conds as $table_cond => $join_cond ) {
528 if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
529 $options['FOR UPDATE'][] = $table_cond;
534 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
535 unset( $options['ORDER BY'] );
539 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
543 * INSERT wrapper, inserts an array into a table
545 * $args may be a single associative array, or an array of these with numeric keys,
546 * for multi-row insert (Postgres version 8.2 and above only).
548 * @param string $table Name of the table to insert to.
549 * @param array $args Items to insert into the table.
550 * @param string $fname Name of the function, for profiling
551 * @param array|string $options String or array. Valid options: IGNORE
552 * @return bool Success of insert operation. IGNORE always returns true.
554 public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
555 if ( !count( $args ) ) {
559 $table = $this->tableName( $table );
560 if ( !isset( $this->numericVersion ) ) {
561 $this->getServerVersion();
564 if ( !is_array( $options ) ) {
565 $options = [ $options ];
568 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
570 $keys = array_keys( $args[0] );
573 $keys = array_keys( $args );
576 // If IGNORE is set, we use savepoints to emulate mysql's behavior
577 $savepoint = $olde = null;
578 $numrowsinserted = 0;
579 if ( in_array( 'IGNORE', $options ) ) {
580 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
581 $olde = error_reporting( 0 );
582 // For future use, we may want to track the number of actual inserts
583 // Right now, insert (all writes) simply return true/false
586 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
589 if ( $this->numericVersion >= 8.2 && !$savepoint ) {
591 foreach ( $args as $row ) {
597 $sql .= '(' . $this->makeList( $row ) . ')';
599 $res = (bool)$this->query( $sql, $fname, $savepoint );
603 foreach ( $args as $row ) {
605 $tempsql .= '(' . $this->makeList( $row ) . ')';
608 $savepoint->savepoint();
611 $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
614 $bar = pg_result_error( $this->mLastResult );
615 if ( $bar != false ) {
616 $savepoint->rollback();
618 $savepoint->release();
623 // If any of them fail, we fail overall for this function call
624 // Note that this will be ignored if IGNORE is set
631 // Not multi, just a lone insert
633 $savepoint->savepoint();
636 $sql .= '(' . $this->makeList( $args ) . ')';
637 $res = (bool)$this->query( $sql, $fname, $savepoint );
639 $bar = pg_result_error( $this->mLastResult );
640 if ( $bar != false ) {
641 $savepoint->rollback();
643 $savepoint->release();
649 error_reporting( $olde );
650 $savepoint->commit();
652 // Set the affected row count for the whole operation
653 $this->mAffectedRows = $numrowsinserted;
655 // IGNORE always returns true
663 * INSERT SELECT wrapper
664 * $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
665 * Source items may be literals rather then field names, but strings should
666 * be quoted with Database::addQuotes()
667 * $conds may be "*" to copy the whole table
668 * srcTable may be an array of tables.
669 * @todo FIXME: Implement this a little better (seperate select/insert)?
671 * @param string $destTable
672 * @param array|string $srcTable
673 * @param array $varMap
674 * @param array $conds
675 * @param string $fname
676 * @param array $insertOptions
677 * @param array $selectOptions
680 public function nativeInsertSelect(
681 $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
682 $insertOptions = [], $selectOptions = []
684 $destTable = $this->tableName( $destTable );
686 if ( !is_array( $insertOptions ) ) {
687 $insertOptions = [ $insertOptions ];
691 * If IGNORE is set, we use savepoints to emulate mysql's behavior
692 * Ignore LOW PRIORITY option, since it is MySQL-specific
694 $savepoint = $olde = null;
695 $numrowsinserted = 0;
696 if ( in_array( 'IGNORE', $insertOptions ) ) {
697 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
698 $olde = error_reporting( 0 );
699 $savepoint->savepoint();
702 if ( !is_array( $selectOptions ) ) {
703 $selectOptions = [ $selectOptions ];
705 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) =
706 $this->makeSelectOptions( $selectOptions );
707 if ( is_array( $srcTable ) ) {
708 $srcTable = implode( ',', array_map( [ $this, 'tableName' ], $srcTable ) );
710 $srcTable = $this->tableName( $srcTable );
713 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
714 " SELECT $startOpts " . implode( ',', $varMap ) .
715 " FROM $srcTable $useIndex $ignoreIndex ";
717 if ( $conds != '*' ) {
718 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
721 $sql .= " $tailOpts";
723 $res = (bool)$this->query( $sql, $fname, $savepoint );
725 $bar = pg_result_error( $this->mLastResult );
726 if ( $bar != false ) {
727 $savepoint->rollback();
729 $savepoint->release();
732 error_reporting( $olde );
733 $savepoint->commit();
735 // Set the affected row count for the whole operation
736 $this->mAffectedRows = $numrowsinserted;
738 // IGNORE always returns true
745 public function tableName( $name, $format = 'quoted' ) {
746 // Replace reserved words with better ones
747 $name = $this->remappedTableName( $name );
749 return parent::tableName( $name, $format );
753 * @param string $name
754 * @return string Value of $name or remapped name if $name is a reserved keyword
756 public function remappedTableName( $name ) {
757 return isset( $this->keywordTableMap[$name] ) ? $this->keywordTableMap[$name] : $name;
761 * @param string $name
762 * @param string $format
763 * @return string Qualified and encoded (if requested) table name
765 public function realTableName( $name, $format = 'quoted' ) {
766 return parent::tableName( $name, $format );
769 public function nextSequenceValue( $seqName ) {
770 $safeseq = str_replace( "'", "''", $seqName );
771 $res = $this->query( "SELECT nextval('$safeseq')" );
772 $row = $this->fetchRow( $res );
773 $this->mInsertId = $row[0];
775 return $this->mInsertId;
779 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
781 * @param string $seqName
784 public function currentSequenceValue( $seqName ) {
785 $safeseq = str_replace( "'", "''", $seqName );
786 $res = $this->query( "SELECT currval('$safeseq')" );
787 $row = $this->fetchRow( $res );
793 public function textFieldSize( $table, $field ) {
794 $table = $this->tableName( $table );
795 $sql = "SELECT t.typname as ftype,a.atttypmod as size
796 FROM pg_class c, pg_attribute a, pg_type t
797 WHERE relname='$table' AND a.attrelid=c.oid AND
798 a.atttypid=t.oid and a.attname='$field'";
799 $res = $this->query( $sql );
800 $row = $this->fetchObject( $res );
801 if ( $row->ftype == 'varchar' ) {
802 $size = $row->size - 4;
810 public function limitResult( $sql, $limit, $offset = false ) {
811 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
814 public function wasDeadlock() {
815 return $this->lastErrno() == '40P01';
818 public function duplicateTableStructure(
819 $oldName, $newName, $temporary = false, $fname = __METHOD__
821 $newName = $this->addIdentifierQuotes( $newName );
822 $oldName = $this->addIdentifierQuotes( $oldName );
824 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
825 "(LIKE $oldName INCLUDING DEFAULTS)", $fname );
828 public function listTables( $prefix = null, $fname = __METHOD__ ) {
829 $eschema = $this->addQuotes( $this->getCoreSchema() );
830 $result = $this->query(
831 "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
834 foreach ( $result as $table ) {
835 $vars = get_object_vars( $table );
836 $table = array_pop( $vars );
837 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
838 $endArray[] = $table;
845 public function timestamp( $ts = 0 ) {
846 $ct = new ConvertibleTimestamp( $ts );
848 return $ct->getTimestamp( TS_POSTGRES );
852 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
853 * to https://secure.php.net/manual/en/ref.pgsql.php
855 * Parsing a postgres array can be a tricky problem, he's my
856 * take on this, it handles multi-dimensional arrays plus
857 * escaping using a nasty regexp to determine the limits of each
860 * This should really be handled by PHP PostgreSQL module
863 * @param string $text Postgreql array returned in a text form like {a,b}
864 * @param string[] $output
865 * @param int|bool $limit
869 private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
870 if ( false === $limit ) {
871 $limit = strlen( $text ) - 1;
874 if ( '{}' == $text ) {
878 if ( '{' != $text[$offset] ) {
879 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
880 $text, $match, 0, $offset );
881 $offset += strlen( $match[0] );
882 $output[] = ( '"' != $match[1][0]
884 : stripcslashes( substr( $match[1], 1, -1 ) ) );
885 if ( '},' == $match[3] ) {
889 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
891 } while ( $limit > $offset );
896 public function aggregateValue( $valuedata, $valuename = 'value' ) {
900 public function getSoftwareLink() {
901 return '[{{int:version-db-postgres-url}} PostgreSQL]';
905 * Return current schema (executes SELECT current_schema())
909 * @return string Default schema for the current session
911 public function getCurrentSchema() {
912 $res = $this->query( "SELECT current_schema()", __METHOD__ );
913 $row = $this->fetchRow( $res );
919 * Return list of schemas which are accessible without schema name
920 * This is list does not contain magic keywords like "$user"
923 * @see getSearchPath()
924 * @see setSearchPath()
926 * @return array List of actual schemas for the current sesson
928 public function getSchemas() {
929 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
930 $row = $this->fetchRow( $res );
933 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
935 return $this->pg_array_parse( $row[0], $schemas );
939 * Return search patch for schemas
940 * This is different from getSchemas() since it contain magic keywords
945 * @return array How to search for table names schemas for the current user
947 public function getSearchPath() {
948 $res = $this->query( "SHOW search_path", __METHOD__ );
949 $row = $this->fetchRow( $res );
951 /* PostgreSQL returns SHOW values as strings */
953 return explode( ",", $row[0] );
957 * Update search_path, values should already be sanitized
958 * Values may contain magic keywords like "$user"
961 * @param array $search_path List of schemas to be searched by default
963 private function setSearchPath( $search_path ) {
964 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
968 * Determine default schema for the current application
969 * Adjust this session schema search path if desired schema exists
970 * and is not alread there.
972 * We need to have name of the core schema stored to be able
973 * to query database metadata.
975 * This will be also called by the installer after the schema is created
979 * @param string $desiredSchema
981 public function determineCoreSchema( $desiredSchema ) {
982 $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
983 if ( $this->schemaExists( $desiredSchema ) ) {
984 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
985 $this->mCoreSchema = $desiredSchema;
986 $this->queryLogger->debug(
987 "Schema \"" . $desiredSchema . "\" already in the search path\n" );
990 * Prepend our schema (e.g. 'mediawiki') in front
994 $search_path = $this->getSearchPath();
995 array_unshift( $search_path,
996 $this->addIdentifierQuotes( $desiredSchema ) );
997 $this->setSearchPath( $search_path );
998 $this->mCoreSchema = $desiredSchema;
999 $this->queryLogger->debug(
1000 "Schema \"" . $desiredSchema . "\" added to the search path\n" );
1003 $this->mCoreSchema = $this->getCurrentSchema();
1004 $this->queryLogger->debug(
1005 "Schema \"" . $desiredSchema . "\" not found, using current \"" .
1006 $this->mCoreSchema . "\"\n" );
1008 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
1009 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
1013 * Return schema name for core application tables
1016 * @return string Core schema name
1018 public function getCoreSchema() {
1019 return $this->mCoreSchema;
1022 public function getServerVersion() {
1023 if ( !isset( $this->numericVersion ) ) {
1024 $conn = $this->getBindingHandle();
1025 $versionInfo = pg_version( $conn );
1026 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1027 // Old client, abort install
1028 $this->numericVersion = '7.3 or earlier';
1029 } elseif ( isset( $versionInfo['server'] ) ) {
1031 $this->numericVersion = $versionInfo['server'];
1033 // T18937: broken pgsql extension from PHP<5.3
1034 $this->numericVersion = pg_parameter_status( $conn, 'server_version' );
1038 return $this->numericVersion;
1042 * Query whether a given relation exists (in the given schema, or the
1043 * default mw one if not given)
1044 * @param string $table
1045 * @param array|string $types
1046 * @param bool|string $schema
1049 private function relationExists( $table, $types, $schema = false ) {
1050 if ( !is_array( $types ) ) {
1051 $types = [ $types ];
1053 if ( $schema === false ) {
1054 $schema = $this->getCoreSchema();
1056 $etable = $this->addQuotes( $table );
1057 $eschema = $this->addQuotes( $schema );
1058 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1059 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1060 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1061 $res = $this->query( $sql );
1062 $count = $res ? $res->numRows() : 0;
1064 return (bool)$count;
1068 * For backward compatibility, this function checks both tables and views.
1069 * @param string $table
1070 * @param string $fname
1071 * @param bool|string $schema
1074 public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1075 return $this->relationExists( $table, [ 'r', 'v' ], $schema );
1078 public function sequenceExists( $sequence, $schema = false ) {
1079 return $this->relationExists( $sequence, 'S', $schema );
1082 public function triggerExists( $table, $trigger ) {
1084 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1085 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1086 AND tgrelid=pg_class.oid
1087 AND nspname=%s AND relname=%s AND tgname=%s
1089 $res = $this->query(
1092 $this->addQuotes( $this->getCoreSchema() ),
1093 $this->addQuotes( $table ),
1094 $this->addQuotes( $trigger )
1100 $rows = $res->numRows();
1105 public function ruleExists( $table, $rule ) {
1106 $exists = $this->selectField( 'pg_rules', 'rulename',
1108 'rulename' => $rule,
1109 'tablename' => $table,
1110 'schemaname' => $this->getCoreSchema()
1114 return $exists === $rule;
1117 public function constraintExists( $table, $constraint ) {
1118 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1119 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1120 $this->addQuotes( $this->getCoreSchema() ),
1121 $this->addQuotes( $table ),
1122 $this->addQuotes( $constraint )
1124 $res = $this->query( $sql );
1128 $rows = $res->numRows();
1134 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1135 * @param string $schema
1138 public function schemaExists( $schema ) {
1139 if ( !strlen( $schema ) ) {
1140 return false; // short-circuit
1143 $exists = $this->selectField(
1144 '"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
1146 return (bool)$exists;
1150 * Returns true if a given role (i.e. user) exists, false otherwise.
1151 * @param string $roleName
1154 public function roleExists( $roleName ) {
1155 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1156 [ 'rolname' => $roleName ], __METHOD__ );
1158 return (bool)$exists;
1162 * @var string $table
1163 * @var string $field
1164 * @return PostgresField|null
1166 public function fieldInfo( $table, $field ) {
1167 return PostgresField::fromText( $this, $table, $field );
1171 * pg_field_type() wrapper
1172 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1173 * @param int $index Field number, starting from 0
1176 public function fieldType( $res, $index ) {
1177 if ( $res instanceof ResultWrapper ) {
1178 $res = $res->result;
1181 return pg_field_type( $res, $index );
1184 public function encodeBlob( $b ) {
1185 return new PostgresBlob( pg_escape_bytea( $b ) );
1188 public function decodeBlob( $b ) {
1189 if ( $b instanceof PostgresBlob ) {
1191 } elseif ( $b instanceof Blob ) {
1195 return pg_unescape_bytea( $b );
1198 public function strencode( $s ) {
1199 // Should not be called by us
1200 return pg_escape_string( $this->getBindingHandle(), $s );
1203 public function addQuotes( $s ) {
1204 $conn = $this->getBindingHandle();
1206 if ( is_null( $s ) ) {
1208 } elseif ( is_bool( $s ) ) {
1209 return intval( $s );
1210 } elseif ( $s instanceof Blob ) {
1211 if ( $s instanceof PostgresBlob ) {
1214 $s = pg_escape_bytea( $conn, $s->fetch() );
1219 return "'" . pg_escape_string( $conn, $s ) . "'";
1223 * Postgres specific version of replaceVars.
1224 * Calls the parent version in Database.php
1226 * @param string $ins SQL string, read from a stream (usually tables.sql)
1227 * @return string SQL string
1229 protected function replaceVars( $ins ) {
1230 $ins = parent::replaceVars( $ins );
1232 if ( $this->numericVersion >= 8.3 ) {
1233 // Thanks for not providing backwards-compatibility, 8.3
1234 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1237 if ( $this->numericVersion <= 8.1 ) { // Our minimum version
1238 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1244 public function makeSelectOptions( $options ) {
1245 $preLimitTail = $postLimitTail = '';
1246 $startOpts = $useIndex = $ignoreIndex = '';
1249 foreach ( $options as $key => $option ) {
1250 if ( is_numeric( $key ) ) {
1251 $noKeyOptions[$option] = true;
1255 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1257 $preLimitTail .= $this->makeOrderBy( $options );
1259 if ( isset( $options['FOR UPDATE'] ) ) {
1260 $postLimitTail .= ' FOR UPDATE OF ' .
1261 implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
1262 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1263 $postLimitTail .= ' FOR UPDATE';
1266 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1267 $startOpts .= 'DISTINCT';
1270 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1273 public function getDBname() {
1274 return $this->mDBname;
1277 public function getServer() {
1278 return $this->mServer;
1281 public function buildConcat( $stringList ) {
1282 return implode( ' || ', $stringList );
1285 public function buildGroupConcatField(
1286 $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
1288 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1290 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1293 public function buildStringCast( $field ) {
1294 return $field . '::text';
1297 public function streamStatementEnd( &$sql, &$newLine ) {
1298 # Allow dollar quoting for function declarations
1299 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1300 if ( $this->delimiter ) {
1301 $this->delimiter = false;
1303 $this->delimiter = ';';
1307 return parent::streamStatementEnd( $sql, $newLine );
1310 public function doLockTables( array $read, array $write, $method ) {
1312 foreach ( $write as $table ) {
1313 $tablesWrite[] = $this->tableName( $table );
1316 foreach ( $read as $table ) {
1317 $tablesRead[] = $this->tableName( $table );
1320 // Acquire locks for the duration of the current transaction...
1321 if ( $tablesWrite ) {
1323 'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
1327 if ( $tablesRead ) {
1329 'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
1337 public function lockIsFree( $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 (CASE(pg_try_advisory_lock($key))
1341 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1342 $row = $this->fetchObject( $result );
1344 return ( $row->lockstatus === 't' );
1347 public function lock( $lockName, $method, $timeout = 5 ) {
1348 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1349 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1350 $loop = new WaitConditionLoop(
1351 function () use ( $lockName, $key, $timeout, $method ) {
1352 $res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1353 $row = $this->fetchObject( $res );
1354 if ( $row->lockstatus === 't' ) {
1355 parent::lock( $lockName, $method, $timeout ); // record
1359 return WaitConditionLoop::CONDITION_CONTINUE;
1364 return ( $loop->invoke() === $loop::CONDITION_REACHED );
1367 public function unlock( $lockName, $method ) {
1368 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1369 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1370 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1371 $row = $this->fetchObject( $result );
1373 if ( $row->lockstatus === 't' ) {
1374 parent::unlock( $lockName, $method ); // record
1378 $this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
1384 * @param string $lockName
1385 * @return string Integer
1387 private function bigintFromLockName( $lockName ) {
1388 return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
1392 class_alias( DatabasePostgres::class, 'DatabasePostgres' );