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
;
33 class DatabasePostgres
extends Database
{
38 protected $lastResultHandle = null;
40 /** @var float|string */
41 private $numericVersion = null;
42 /** @var string Connect string to open a PostgreSQL connection */
43 private $connectString;
48 /** @var string[] Map of (reserved table name => alternate table name) */
49 private $keywordTableMap = [];
52 * @see Database::__construct()
53 * @param array $params Additional parameters include:
54 * - keywordTableMap : Map of reserved table names to alternative table names to use
56 public function __construct( array $params ) {
57 $this->port
= $params['port'] ??
false;
58 $this->keywordTableMap
= $params['keywordTableMap'] ??
[];
60 parent
::__construct( $params );
63 public function getType() {
67 public function implicitGroupby() {
71 public function implicitOrderby() {
75 public function hasConstraint( $name ) {
76 foreach ( $this->getCoreSchemas() as $schema ) {
77 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
78 "WHERE c.connamespace = n.oid AND conname = " .
79 $this->addQuotes( $name ) . " AND n.nspname = " .
80 $this->addQuotes( $schema );
81 $res = $this->doQuery( $sql );
82 if ( $res && $this->numRows( $res ) ) {
89 protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
90 # Test for Postgres support, to avoid suppressed fatal error
91 if ( !function_exists( 'pg_connect' ) ) {
92 throw new DBConnectionError(
94 "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
95 "option? (Note: if you recently installed PHP, you may need to restart your\n" .
96 "webserver and database)\n"
100 $this->server
= $server;
102 $this->password
= $password;
105 // pg_connect() user $user as the default database. Since a database is *required*,
106 // at least pick a "don't care" database that is more likely to exist. This case
107 // arrises when LoadBalancer::getConnection( $i, [], '' ) is used.
108 'dbname' => strlen( $dbName ) ?
$dbName : 'postgres',
110 'password' => $password
112 if ( $server != false && $server != '' ) {
113 $connectVars['host'] = $server;
115 if ( (int)$this->port
> 0 ) {
116 $connectVars['port'] = (int)$this->port
;
118 if ( $this->flags
& self
::DBO_SSL
) {
119 $connectVars['sslmode'] = 1;
122 $this->connectString
= $this->makeConnectionString( $connectVars );
124 $this->installErrorHandler();
127 // Use new connections to let LoadBalancer/LBFactory handle reuse
128 $this->conn
= pg_connect( $this->connectString
, PGSQL_CONNECT_FORCE_NEW
);
129 } catch ( Exception
$ex ) {
130 $this->restoreErrorHandler();
134 $phpError = $this->restoreErrorHandler();
136 if ( !$this->conn
) {
137 $this->queryLogger
->debug(
138 "DB connection error\n" .
139 "Server: $server, Database: $dbName, User: $user, Password: " .
140 substr( $password, 0, 3 ) . "...\n"
142 $this->queryLogger
->debug( $this->lastError() . "\n" );
143 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
146 $this->opened
= true;
148 # If called from the command-line (e.g. importDump), only show errors
149 if ( $this->cliMode
) {
150 $this->doQuery( "SET client_min_messages = 'ERROR'" );
153 $this->query( "SET client_encoding='UTF8'", __METHOD__
);
154 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__
);
155 $this->query( "SET timezone = 'GMT'", __METHOD__
);
156 $this->query( "SET standard_conforming_strings = on", __METHOD__
);
157 $this->query( "SET bytea_output = 'escape'", __METHOD__
); // PHP bug 53127
159 $this->determineCoreSchema( $schema );
160 $this->currentDomain
= new DatabaseDomain( $dbName, $schema, $tablePrefix );
162 return (bool)$this->conn
;
165 protected function relationSchemaQualifier() {
166 if ( $this->coreSchema
=== $this->currentDomain
->getSchema() ) {
167 // The schema to be used is now in the search path; no need for explicit qualification
171 return parent
::relationSchemaQualifier();
174 public function databasesAreIndependent() {
178 public function doSelectDomain( DatabaseDomain
$domain ) {
179 if ( $this->getDBname() !== $domain->getDatabase() ) {
180 // Postgres doesn't support selectDB in the same way MySQL does.
181 // So if the DB name doesn't match the open connection, open a new one
186 $domain->getDatabase(),
187 $domain->getSchema(),
188 $domain->getTablePrefix()
191 $this->currentDomain
= $domain;
198 * @param string[] $vars
201 private function makeConnectionString( $vars ) {
203 foreach ( $vars as $name => $value ) {
204 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
210 protected function closeConnection() {
211 return $this->conn ?
pg_close( $this->conn
) : true;
214 protected function isTransactableQuery( $sql ) {
215 return parent
::isTransactableQuery( $sql ) &&
216 !preg_match( '/^SELECT\s+pg_(try_|)advisory_\w+\(/', $sql );
219 public function doQuery( $sql ) {
220 $conn = $this->getBindingHandle();
222 $sql = mb_convert_encoding( $sql, 'UTF-8' );
223 // Clear previously left over PQresult
224 while ( $res = pg_get_result( $conn ) ) {
225 pg_free_result( $res );
227 if ( pg_send_query( $conn, $sql ) === false ) {
228 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
230 $this->lastResultHandle
= pg_get_result( $conn );
231 if ( pg_result_error( $this->lastResultHandle
) ) {
235 return $this->lastResultHandle
;
238 protected function dumpError() {
242 PGSQL_DIAG_MESSAGE_PRIMARY
,
243 PGSQL_DIAG_MESSAGE_DETAIL
,
244 PGSQL_DIAG_MESSAGE_HINT
,
245 PGSQL_DIAG_STATEMENT_POSITION
,
246 PGSQL_DIAG_INTERNAL_POSITION
,
247 PGSQL_DIAG_INTERNAL_QUERY
,
249 PGSQL_DIAG_SOURCE_FILE
,
250 PGSQL_DIAG_SOURCE_LINE
,
251 PGSQL_DIAG_SOURCE_FUNCTION
253 foreach ( $diags as $d ) {
254 $this->queryLogger
->debug( sprintf( "PgSQL ERROR(%d): %s\n",
255 $d, pg_result_error_field( $this->lastResultHandle
, $d ) ) );
259 public function freeResult( $res ) {
260 if ( $res instanceof ResultWrapper
) {
263 Wikimedia\
suppressWarnings();
264 $ok = pg_free_result( $res );
265 Wikimedia\restoreWarnings
();
267 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
271 public function fetchObject( $res ) {
272 if ( $res instanceof ResultWrapper
) {
275 Wikimedia\
suppressWarnings();
276 $row = pg_fetch_object( $res );
277 Wikimedia\restoreWarnings
();
278 # @todo FIXME: HACK HACK HACK HACK debug
280 # @todo hashar: not sure if the following test really trigger if the object
282 $conn = $this->getBindingHandle();
283 if ( pg_last_error( $conn ) ) {
284 throw new DBUnexpectedError(
286 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
293 public function fetchRow( $res ) {
294 if ( $res instanceof ResultWrapper
) {
297 Wikimedia\
suppressWarnings();
298 $row = pg_fetch_array( $res );
299 Wikimedia\restoreWarnings
();
301 $conn = $this->getBindingHandle();
302 if ( pg_last_error( $conn ) ) {
303 throw new DBUnexpectedError(
305 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
312 public function numRows( $res ) {
313 if ( $res === false ) {
317 if ( $res instanceof ResultWrapper
) {
320 Wikimedia\
suppressWarnings();
321 $n = pg_num_rows( $res );
322 Wikimedia\restoreWarnings
();
324 $conn = $this->getBindingHandle();
325 if ( pg_last_error( $conn ) ) {
326 throw new DBUnexpectedError(
328 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
335 public function numFields( $res ) {
336 if ( $res instanceof ResultWrapper
) {
340 return pg_num_fields( $res );
343 public function fieldName( $res, $n ) {
344 if ( $res instanceof ResultWrapper
) {
348 return pg_field_name( $res, $n );
351 public function insertId() {
352 $res = $this->query( "SELECT lastval()" );
353 $row = $this->fetchRow( $res );
354 return is_null( $row[0] ) ?
null : (int)$row[0];
357 public function dataSeek( $res, $row ) {
358 if ( $res instanceof ResultWrapper
) {
362 return pg_result_seek( $res, $row );
365 public function lastError() {
367 if ( $this->lastResultHandle
) {
368 return pg_result_error( $this->lastResultHandle
);
370 return pg_last_error();
374 return $this->getLastPHPError() ?
: 'No database connection';
377 public function lastErrno() {
378 if ( $this->lastResultHandle
) {
379 return pg_result_error_field( $this->lastResultHandle
, PGSQL_DIAG_SQLSTATE
);
385 protected function fetchAffectedRowCount() {
386 if ( !$this->lastResultHandle
) {
390 return pg_affected_rows( $this->lastResultHandle
);
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
402 * @param string $conds
403 * @param string $fname
404 * @param array $options
405 * @param array $join_conds
408 public function estimateRowCount( $table, $var = '*', $conds = '',
409 $fname = __METHOD__
, $options = [], $join_conds = []
411 $conds = $this->normalizeConditions( $conds, $fname );
412 $column = $this->extractSingleFieldFromList( $var );
413 if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
414 $conds[] = "$column IS NOT NULL";
417 $options['EXPLAIN'] = true;
418 $res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
421 $row = $this->fetchRow( $res );
423 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
424 $rows = (int)$count[1];
431 public function indexInfo( $table, $index, $fname = __METHOD__
) {
432 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
433 $res = $this->query( $sql, $fname );
437 foreach ( $res as $row ) {
438 if ( $row->indexname
== $this->indexName( $index ) ) {
446 public function indexAttributes( $index, $schema = false ) {
447 if ( $schema === false ) {
448 $schemas = $this->getCoreSchemas();
450 $schemas = [ $schema ];
453 $eindex = $this->addQuotes( $index );
455 foreach ( $schemas as $schema ) {
456 $eschema = $this->addQuotes( $schema );
458 * A subquery would be not needed if we didn't care about the order
459 * of attributes, but we do
461 $sql = <<<__INDEXATTR__
465 i.indoption[s.g] as option,
468 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
472 ON cis.oid=isub.indexrelid
474 ON cis.relnamespace = ns.oid
475 WHERE cis.relname=$eindex AND ns.nspname=$eschema) AS s,
481 ON ci.oid=i.indexrelid
483 ON ct.oid = i.indrelid
485 ON ci.relnamespace = n.oid
487 ci.relname=$eindex AND n.nspname=$eschema
488 AND attrelid = ct.oid
489 AND i.indkey[s.g] = attnum
490 AND i.indclass[s.g] = opcls.oid
491 AND pg_am.oid = opcls.opcmethod
493 $res = $this->query( $sql, __METHOD__ );
496 foreach ( $res as $row ) {
509 public function indexUnique( $table, $index, $fname = __METHOD__ ) {
510 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
511 " AND indexdef LIKE 'CREATE UNIQUE%(" .
512 $this->strencode( $this->indexName( $index ) ) .
514 $res = $this->query( $sql, $fname );
519 return $res->numRows() > 0;
522 public function selectSQLText(
523 $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
525 if ( is_string( $options ) ) {
526 $options = [ $options ];
529 // Change the FOR UPDATE option as necessary based on the join conditions. Then pass
530 // to the parent function to get the actual SQL text.
531 // In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
532 // can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
533 // do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
535 // MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
536 if ( is_array( $options ) ) {
537 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
538 if ( $forUpdateKey !== false && $join_conds ) {
539 unset( $options[$forUpdateKey] );
540 $options['FOR UPDATE'] = [];
545 $alias = key( $toCheck );
546 $name = $toCheck[$alias];
547 unset( $toCheck[$alias] );
549 $hasAlias = !is_numeric( $alias );
550 if ( !$hasAlias && is_string( $name ) ) {
554 if ( !isset( $join_conds[$alias] ) ||
555 !preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_conds[$alias][0] )
557 if ( is_array( $name ) ) {
558 // It's a parenthesized group, process all the tables inside the group.
559 $toCheck = array_merge( $toCheck, $name );
561 // Quote alias names so $this->tableName() won't mangle them
562 $options['FOR UPDATE'][] = $hasAlias ? $this->addIdentifierQuotes( $alias ) : $alias;
568 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
569 unset( $options['ORDER BY'] );
573 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
577 public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
578 if ( !count( $args ) ) {
582 $table = $this->tableName( $table );
583 if ( !isset( $this->numericVersion ) ) {
584 $this->getServerVersion();
587 if ( !is_array( $options ) ) {
588 $options = [ $options ];
591 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
593 $keys = array_keys( $args[0] );
596 $keys = array_keys( $args );
599 $ignore = in_array( 'IGNORE', $options );
601 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
603 if ( $this->numericVersion >= 9.5 || !$ignore ) {
604 // No IGNORE or our PG has "ON CONFLICT DO NOTHING"
606 foreach ( $rows as $row ) {
612 $sql .= '(' . $this->makeList( $row ) . ')';
615 $sql .= ' ON CONFLICT DO NOTHING';
617 $this->query( $sql, $fname );
619 // Emulate IGNORE by doing each row individually, with savepoints
620 // to roll back as necessary.
621 $numrowsinserted = 0;
623 $tok = $this->startAtomic( "$fname (outer)", self::ATOMIC_CANCELABLE );
625 foreach ( $rows as $row ) {
627 $tempsql .= '(' . $this->makeList( $row ) . ')';
629 $this->startAtomic( "$fname (inner)", self::ATOMIC_CANCELABLE );
631 $this->query( $tempsql, $fname );
632 $this->endAtomic( "$fname (inner)" );
634 } catch ( DBQueryError $e ) {
635 $this->cancelAtomic( "$fname (inner)" );
636 // Our IGNORE is supposed to ignore duplicate key errors, but not others.
637 // (even though MySQL's version apparently ignores all errors)
638 if ( $e->errno !== '23505' ) {
643 } catch ( Exception $e ) {
644 $this->cancelAtomic( "$fname (outer)", $tok );
647 $this->endAtomic( "$fname (outer)" );
649 // Set the affected row count for the whole operation
650 $this->affectedRowCount = $numrowsinserted;
657 * INSERT SELECT wrapper
658 * $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
659 * Source items may be literals rather then field names, but strings should
660 * be quoted with Database::addQuotes()
661 * $conds may be "*" to copy the whole table
662 * srcTable may be an array of tables.
663 * @todo FIXME: Implement this a little better (separate select/insert)?
665 * @param string $destTable
666 * @param array|string $srcTable
667 * @param array $varMap
668 * @param array $conds
669 * @param string $fname
670 * @param array $insertOptions
671 * @param array $selectOptions
672 * @param array $selectJoinConds
674 protected function nativeInsertSelect(
675 $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
676 $insertOptions = [], $selectOptions = [], $selectJoinConds = []
678 if ( !is_array( $insertOptions ) ) {
679 $insertOptions = [ $insertOptions ];
682 if ( in_array( 'IGNORE', $insertOptions ) ) {
683 if ( $this->getServerVersion() >= 9.5 ) {
684 // Use ON CONFLICT DO NOTHING if we have it for IGNORE
685 $destTable = $this->tableName( $destTable );
687 $selectSql = $this->selectSQLText(
689 array_values( $varMap ),
696 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ') ' .
697 $selectSql . ' ON CONFLICT DO NOTHING';
699 $this->query( $sql, $fname );
701 // IGNORE and we don't have ON CONFLICT DO NOTHING, so just use the non-native version
702 $this->nonNativeInsertSelect(
703 $destTable, $srcTable, $varMap, $conds, $fname,
704 $insertOptions, $selectOptions, $selectJoinConds
708 parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
709 $insertOptions, $selectOptions, $selectJoinConds );
713 public function tableName( $name, $format = 'quoted' ) {
714 // Replace reserved words with better ones
715 $name = $this->remappedTableName( $name );
717 return parent::tableName( $name, $format );
721 * @param string $name
722 * @return string Value of $name or remapped name if $name is a reserved keyword
724 public function remappedTableName( $name ) {
725 return $this->keywordTableMap[$name] ?? $name;
729 * @param string $name
730 * @param string $format
731 * @return string Qualified and encoded (if requested) table name
733 public function realTableName( $name, $format = 'quoted' ) {
734 return parent::tableName( $name, $format );
737 public function nextSequenceValue( $seqName ) {
738 return new NextSequenceValue;
742 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
744 * @param string $seqName
747 public function currentSequenceValue( $seqName ) {
748 $safeseq = str_replace( "'", "''", $seqName );
749 $res = $this->query( "SELECT currval('$safeseq')" );
750 $row = $this->fetchRow( $res );
756 public function textFieldSize( $table, $field ) {
757 $table = $this->tableName( $table );
758 $sql = "SELECT t.typname as ftype,a.atttypmod as size
759 FROM pg_class c, pg_attribute a, pg_type t
760 WHERE relname='$table' AND a.attrelid=c.oid AND
761 a.atttypid=t.oid and a.attname='$field'";
762 $res = $this->query( $sql );
763 $row = $this->fetchObject( $res );
764 if ( $row->ftype == 'varchar' ) {
765 $size = $row->size - 4;
773 public function limitResult( $sql, $limit, $offset = false ) {
774 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
777 public function wasDeadlock() {
778 // https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
779 return $this->lastErrno() === '40P01';
782 public function wasLockTimeout() {
783 // https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
784 return $this->lastErrno() === '55P03';
787 public function wasConnectionError( $errno ) {
788 // https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
789 static $codes = [ '08000', '08003', '08006', '08001', '08004', '57P01', '57P03', '53300' ];
791 return in_array( $errno, $codes, true );
794 protected function wasKnownStatementRollbackError() {
795 return false; // transaction has to be rolled-back from error state
798 public function duplicateTableStructure(
799 $oldName, $newName, $temporary = false, $fname = __METHOD__
801 $newNameE = $this->addIdentifierQuotes( $newName );
802 $oldNameE = $this->addIdentifierQuotes( $oldName );
804 $temporary = $temporary ? 'TEMPORARY' : '';
806 $ret = $this->query( "CREATE $temporary TABLE $newNameE " .
807 "(LIKE $oldNameE INCLUDING DEFAULTS INCLUDING INDEXES)", $fname );
812 $res = $this->query( 'SELECT attname FROM pg_class c'
813 . ' JOIN pg_namespace n ON (n.oid = c.relnamespace)'
814 . ' JOIN pg_attribute a ON (a.attrelid = c.oid)'
815 . ' JOIN pg_attrdef d ON (c.oid=d.adrelid and a.attnum=d.adnum)'
816 . ' WHERE relkind = \'r\''
817 . ' AND nspname = ' . $this->addQuotes( $this->getCoreSchema() )
818 . ' AND relname = ' . $this->addQuotes( $oldName )
819 . ' AND pg_get_expr(adbin, adrelid) LIKE \'nextval(%\'',
822 $row = $this->fetchObject( $res );
824 $field = $row->attname;
825 $newSeq = "{$newName}_{$field}_seq";
826 $fieldE = $this->addIdentifierQuotes( $field );
827 $newSeqE = $this->addIdentifierQuotes( $newSeq );
828 $newSeqQ = $this->addQuotes( $newSeq );
829 $this->query( "CREATE $temporary SEQUENCE $newSeqE OWNED BY $newNameE.$fieldE", $fname );
831 "ALTER TABLE $newNameE ALTER COLUMN $fieldE SET DEFAULT nextval({$newSeqQ}::regclass)",
839 public function resetSequenceForTable( $table, $fname = __METHOD__ ) {
840 $table = $this->tableName( $table, 'raw' );
841 foreach ( $this->getCoreSchemas() as $schema ) {
843 'SELECT c.oid FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace)'
844 . ' WHERE relkind = \'r\''
845 . ' AND nspname = ' . $this->addQuotes( $schema )
846 . ' AND relname = ' . $this->addQuotes( $table ),
849 if ( !$res || !$this->numRows( $res ) ) {
853 $oid = $this->fetchObject( $res )->oid;
854 $res = $this->query( 'SELECT pg_get_expr(adbin, adrelid) AS adsrc FROM pg_attribute a'
855 . ' JOIN pg_attrdef d ON (a.attrelid=d.adrelid and a.attnum=d.adnum)'
856 . " WHERE a.attrelid = $oid"
857 . ' AND pg_get_expr(adbin, adrelid) LIKE \'nextval(%\'',
860 $row = $this->fetchObject( $res );
863 'SELECT ' . preg_replace( '/^nextval\((.+)\)$/', 'setval($1,1,false)', $row->adsrc ),
875 * @suppress SecurityCheck-SQLInjection array_map not recognized T204911
877 public function listTables( $prefix = null, $fname = __METHOD__ ) {
878 $eschemas = implode( ',', array_map( [ $this, 'addQuotes' ], $this->getCoreSchemas() ) );
879 $result = $this->query(
880 "SELECT DISTINCT tablename FROM pg_tables WHERE schemaname IN ($eschemas)", $fname );
883 foreach ( $result as $table ) {
884 $vars = get_object_vars( $table );
885 $table = array_pop( $vars );
886 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
887 $endArray[] = $table;
894 public function timestamp( $ts = 0 ) {
895 $ct = new ConvertibleTimestamp( $ts );
897 return $ct->getTimestamp( TS_POSTGRES );
901 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
902 * to https://secure.php.net/manual/en/ref.pgsql.php
904 * Parsing a postgres array can be a tricky problem, he's my
905 * take on this, it handles multi-dimensional arrays plus
906 * escaping using a nasty regexp to determine the limits of each
909 * This should really be handled by PHP PostgreSQL module
912 * @param string $text Postgreql array returned in a text form like {a,b}
913 * @param string[] $output
914 * @param int|bool $limit
918 private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
919 if ( $limit === false ) {
920 $limit = strlen( $text ) - 1;
923 if ( $text == '{}' ) {
927 if ( $text[$offset] != '{' ) {
928 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
929 $text, $match, 0, $offset );
930 $offset += strlen( $match[0] );
931 $output[] = ( $match[1][0] != '"'
933 : stripcslashes( substr( $match[1], 1, -1 ) ) );
934 if ( $match[3] == '},' ) {
938 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
940 } while ( $limit > $offset );
945 public function aggregateValue( $valuedata, $valuename = 'value' ) {
949 public function getSoftwareLink() {
950 return '[{{int:version-db-postgres-url}} PostgreSQL]';
954 * Return current schema (executes SELECT current_schema())
958 * @return string Default schema for the current session
960 public function getCurrentSchema() {
961 $res = $this->query( "SELECT current_schema()", __METHOD__ );
962 $row = $this->fetchRow( $res );
968 * Return list of schemas which are accessible without schema name
969 * This is list does not contain magic keywords like "$user"
972 * @see getSearchPath()
973 * @see setSearchPath()
975 * @return array List of actual schemas for the current sesson
977 public function getSchemas() {
978 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
979 $row = $this->fetchRow( $res );
982 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
984 return $this->pg_array_parse( $row[0], $schemas );
988 * Return search patch for schemas
989 * This is different from getSchemas() since it contain magic keywords
994 * @return array How to search for table names schemas for the current user
996 public function getSearchPath() {
997 $res = $this->query( "SHOW search_path", __METHOD__ );
998 $row = $this->fetchRow( $res );
1000 /* PostgreSQL returns SHOW values as strings */
1002 return explode( ",", $row[0] );
1006 * Update search_path, values should already be sanitized
1007 * Values may contain magic keywords like "$user"
1010 * @param array $search_path List of schemas to be searched by default
1012 private function setSearchPath( $search_path ) {
1013 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
1017 * Determine default schema for the current application
1018 * Adjust this session schema search path if desired schema exists
1019 * and is not alread there.
1021 * We need to have name of the core schema stored to be able
1022 * to query database metadata.
1024 * This will be also called by the installer after the schema is created
1028 * @param string $desiredSchema
1030 public function determineCoreSchema( $desiredSchema ) {
1031 $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
1032 if ( $this->schemaExists( $desiredSchema ) ) {
1033 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
1034 $this->coreSchema = $desiredSchema;
1035 $this->queryLogger->debug(
1036 "Schema \"" . $desiredSchema . "\" already in the search path\n" );
1039 * Prepend our schema (e.g. 'mediawiki') in front
1040 * of the search path
1043 $search_path = $this->getSearchPath();
1044 array_unshift( $search_path,
1045 $this->addIdentifierQuotes( $desiredSchema ) );
1046 $this->setSearchPath( $search_path );
1047 $this->coreSchema = $desiredSchema;
1048 $this->queryLogger->debug(
1049 "Schema \"" . $desiredSchema . "\" added to the search path\n" );
1052 $this->coreSchema = $this->getCurrentSchema();
1053 $this->queryLogger->debug(
1054 "Schema \"" . $desiredSchema . "\" not found, using current \"" .
1055 $this->coreSchema . "\"\n" );
1057 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
1058 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
1062 * Return schema name for core application tables
1065 * @return string Core schema name
1067 public function getCoreSchema() {
1068 return $this->coreSchema;
1072 * Return schema names for temporary tables and core application tables
1075 * @return string[] schema names
1077 public function getCoreSchemas() {
1078 if ( $this->tempSchema ) {
1079 return [ $this->tempSchema, $this->getCoreSchema() ];
1082 $res = $this->query(
1083 "SELECT nspname FROM pg_catalog.pg_namespace n WHERE n.oid = pg_my_temp_schema()", __METHOD__
1085 $row = $this->fetchObject( $res );
1087 $this->tempSchema = $row->nspname;
1088 return [ $this->tempSchema, $this->getCoreSchema() ];
1091 return [ $this->getCoreSchema() ];
1094 public function getServerVersion() {
1095 if ( !isset( $this->numericVersion ) ) {
1096 $conn = $this->getBindingHandle();
1097 $versionInfo = pg_version( $conn );
1098 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1099 // Old client, abort install
1100 $this->numericVersion = '7.3 or earlier';
1101 } elseif ( isset( $versionInfo['server'] ) ) {
1103 $this->numericVersion = $versionInfo['server'];
1105 // T18937: broken pgsql extension from PHP<5.3
1106 $this->numericVersion = pg_parameter_status( $conn, 'server_version' );
1110 return $this->numericVersion;
1114 * Query whether a given relation exists (in the given schema, or the
1115 * default mw one if not given)
1116 * @param string $table
1117 * @param array|string $types
1118 * @param bool|string $schema
1121 private function relationExists( $table, $types, $schema = false ) {
1122 if ( !is_array( $types ) ) {
1123 $types = [ $types ];
1125 if ( $schema === false ) {
1126 $schemas = $this->getCoreSchemas();
1128 $schemas = [ $schema ];
1130 $table = $this->realTableName( $table, 'raw' );
1131 $etable = $this->addQuotes( $table );
1132 foreach ( $schemas as $schema ) {
1133 $eschema = $this->addQuotes( $schema );
1134 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1135 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1136 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1137 $res = $this->query( $sql );
1138 if ( $res && $res->numRows() ) {
1147 * For backward compatibility, this function checks both tables and views.
1148 * @param string $table
1149 * @param string $fname
1150 * @param bool|string $schema
1153 public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1154 return $this->relationExists( $table, [ 'r', 'v' ], $schema );
1157 public function sequenceExists( $sequence, $schema = false ) {
1158 return $this->relationExists( $sequence, 'S', $schema );
1161 public function triggerExists( $table, $trigger ) {
1163 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1164 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1165 AND tgrelid=pg_class.oid
1166 AND nspname=%s AND relname=%s AND tgname=%s
1168 foreach ( $this->getCoreSchemas() as $schema ) {
1169 $res = $this->query(
1172 $this->addQuotes( $schema ),
1173 $this->addQuotes( $table ),
1174 $this->addQuotes( $trigger )
1177 if ( $res && $res->numRows() ) {
1185 public function ruleExists( $table, $rule ) {
1186 $exists = $this->selectField( 'pg_rules', 'rulename',
1188 'rulename' => $rule,
1189 'tablename' => $table,
1190 'schemaname' => $this->getCoreSchemas()
1194 return $exists === $rule;
1197 public function constraintExists( $table, $constraint ) {
1198 foreach ( $this->getCoreSchemas() as $schema ) {
1199 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1200 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1201 $this->addQuotes( $schema ),
1202 $this->addQuotes( $table ),
1203 $this->addQuotes( $constraint )
1205 $res = $this->query( $sql );
1206 if ( $res && $res->numRows() ) {
1214 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1215 * @param string $schema
1218 public function schemaExists( $schema ) {
1219 if ( !strlen( $schema ) ) {
1220 return false; // short-circuit
1223 $exists = $this->selectField(
1224 '"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
1226 return (bool)$exists;
1230 * Returns true if a given role (i.e. user) exists, false otherwise.
1231 * @param string $roleName
1234 public function roleExists( $roleName ) {
1235 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1236 [ 'rolname' => $roleName ], __METHOD__ );
1238 return (bool)$exists;
1242 * @param string $table
1243 * @param string $field
1244 * @return PostgresField|null
1246 public function fieldInfo( $table, $field ) {
1247 return PostgresField::fromText( $this, $table, $field );
1251 * pg_field_type() wrapper
1252 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1253 * @param int $index Field number, starting from 0
1256 public function fieldType( $res, $index ) {
1257 if ( $res instanceof ResultWrapper ) {
1258 $res = $res->result;
1261 return pg_field_type( $res, $index );
1264 public function encodeBlob( $b ) {
1265 return new PostgresBlob( pg_escape_bytea( $b ) );
1268 public function decodeBlob( $b ) {
1269 if ( $b instanceof PostgresBlob ) {
1271 } elseif ( $b instanceof Blob ) {
1275 return pg_unescape_bytea( $b );
1278 public function strencode( $s ) {
1279 // Should not be called by us
1280 return pg_escape_string( $this->getBindingHandle(), (string)$s );
1283 public function addQuotes( $s ) {
1284 $conn = $this->getBindingHandle();
1286 if ( is_null( $s ) ) {
1288 } elseif ( is_bool( $s ) ) {
1289 return intval( $s );
1290 } elseif ( $s instanceof Blob ) {
1291 if ( $s instanceof PostgresBlob ) {
1294 $s = pg_escape_bytea( $conn, $s->fetch() );
1297 } elseif ( $s instanceof NextSequenceValue ) {
1301 return "'" . pg_escape_string( $conn, (string)$s ) . "'";
1304 public function makeSelectOptions( $options ) {
1305 $preLimitTail = $postLimitTail = '';
1306 $startOpts = $useIndex = $ignoreIndex = '';
1309 foreach ( $options as $key => $option ) {
1310 if ( is_numeric( $key ) ) {
1311 $noKeyOptions[$option] = true;
1315 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1317 $preLimitTail .= $this->makeOrderBy( $options );
1319 if ( isset( $options['FOR UPDATE'] ) ) {
1320 $postLimitTail .= ' FOR UPDATE OF ' .
1321 implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
1322 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1323 $postLimitTail .= ' FOR UPDATE';
1326 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1327 $startOpts .= 'DISTINCT';
1330 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1333 public function getServer() {
1334 return $this->server;
1337 public function buildConcat( $stringList ) {
1338 return implode( ' || ', $stringList );
1341 public function buildGroupConcatField(
1342 $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
1344 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1346 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1349 public function buildStringCast( $field ) {
1350 return $field . '::text';
1353 public function streamStatementEnd( &$sql, &$newLine ) {
1354 # Allow dollar quoting for function declarations
1355 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1356 if ( $this->delimiter ) {
1357 $this->delimiter = false;
1359 $this->delimiter = ';';
1363 return parent::streamStatementEnd( $sql, $newLine );
1366 public function doLockTables( array $read, array $write, $method ) {
1368 foreach ( $write as $table ) {
1369 $tablesWrite[] = $this->tableName( $table );
1372 foreach ( $read as $table ) {
1373 $tablesRead[] = $this->tableName( $table );
1376 // Acquire locks for the duration of the current transaction...
1377 if ( $tablesWrite ) {
1379 'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
1383 if ( $tablesRead ) {
1385 'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
1393 public function lockIsFree( $lockName, $method ) {
1394 if ( !parent::lockIsFree( $lockName, $method ) ) {
1395 return false; // already held
1397 // http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1398 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1399 $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
1400 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1401 $row = $this->fetchObject( $result );
1403 return ( $row->lockstatus === 't' );
1406 public function lock( $lockName, $method, $timeout = 5 ) {
1407 // http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1408 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1409 $loop = new WaitConditionLoop(
1410 function () use ( $lockName, $key, $timeout, $method ) {
1411 $res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1412 $row = $this->fetchObject( $res );
1413 if ( $row->lockstatus === 't' ) {
1414 parent::lock( $lockName, $method, $timeout ); // record
1418 return WaitConditionLoop::CONDITION_CONTINUE;
1423 return ( $loop->invoke() === $loop::CONDITION_REACHED );
1426 public function unlock( $lockName, $method ) {
1427 // http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1428 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1429 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1430 $row = $this->fetchObject( $result );
1432 if ( $row->lockstatus === 't' ) {
1433 parent::unlock( $lockName, $method ); // record
1437 $this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
1442 public function serverIsReadOnly() {
1443 $res = $this->query( "SHOW default_transaction_read_only", __METHOD__ );
1444 $row = $this->fetchObject( $res );
1446 return $row ? ( strtolower( $row->default_transaction_read_only ) === 'on' ) : false;
1450 * @param string $lockName
1451 * @return string Integer
1453 private function bigintFromLockName( $lockName ) {
1454 return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
1459 * @deprecated since 1.29
1461 class_alias( DatabasePostgres::class, 'DatabasePostgres' );