Add tablesUsed to RevisionStoreDbTest
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabasePostgres.php
1 <?php
2 /**
3 * This is the Postgres database abstraction layer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23 namespace Wikimedia\Rdbms;
24
25 use Wikimedia\Timestamp\ConvertibleTimestamp;
26 use Wikimedia\WaitConditionLoop;
27 use MediaWiki;
28 use Exception;
29
30 /**
31 * @ingroup Database
32 */
33 class DatabasePostgres extends Database {
34 /** @var int|bool */
35 protected $port;
36
37 /** @var resource */
38 protected $mLastResult = null;
39 /** @var int The number of rows affected as an integer */
40 protected $mAffectedRows = null;
41
42 /** @var float|string */
43 private $numericVersion = null;
44 /** @var string Connect string to open a PostgreSQL connection */
45 private $connectString;
46 /** @var string */
47 private $mCoreSchema;
48 /** @var string[] Map of (reserved table name => alternate table name) */
49 private $keywordTableMap = [];
50
51 /**
52 * @see Database::__construct()
53 * @param array $params Additional parameters include:
54 * - keywordTableMap : Map of reserved table names to alternative table names to use
55 */
56 public function __construct( array $params ) {
57 $this->port = isset( $params['port'] ) ? $params['port'] : false;
58 $this->keywordTableMap = isset( $params['keywordTableMap'] )
59 ? $params['keywordTableMap']
60 : [];
61
62 parent::__construct( $params );
63 }
64
65 public function getType() {
66 return 'postgres';
67 }
68
69 public function implicitGroupby() {
70 return false;
71 }
72
73 public function implicitOrderby() {
74 return false;
75 }
76
77 public function hasConstraint( $name ) {
78 $conn = $this->getBindingHandle();
79
80 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
81 "WHERE c.connamespace = n.oid AND conname = '" .
82 pg_escape_string( $conn, $name ) . "' AND n.nspname = '" .
83 pg_escape_string( $conn, $this->getCoreSchema() ) . "'";
84 $res = $this->doQuery( $sql );
85
86 return $this->numRows( $res );
87 }
88
89 public function open( $server, $user, $password, $dbName ) {
90 # Test for Postgres support, to avoid suppressed fatal error
91 if ( !function_exists( 'pg_connect' ) ) {
92 throw new DBConnectionError(
93 $this,
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"
97 );
98 }
99
100 $this->mServer = $server;
101 $this->mUser = $user;
102 $this->mPassword = $password;
103 $this->mDBname = $dbName;
104
105 $connectVars = [
106 // pg_connect() user $user as the default database. Since a database is *required*,
107 // at least pick a "don't care" database that is more likely to exist. This case
108 // arrises when LoadBalancer::getConnection( $i, [], '' ) is used.
109 'dbname' => strlen( $dbName ) ? $dbName : 'postgres',
110 'user' => $user,
111 'password' => $password
112 ];
113 if ( $server != false && $server != '' ) {
114 $connectVars['host'] = $server;
115 }
116 if ( (int)$this->port > 0 ) {
117 $connectVars['port'] = (int)$this->port;
118 }
119 if ( $this->mFlags & self::DBO_SSL ) {
120 $connectVars['sslmode'] = 1;
121 }
122
123 $this->connectString = $this->makeConnectionString( $connectVars );
124 $this->close();
125 $this->installErrorHandler();
126
127 try {
128 // Use new connections to let LoadBalancer/LBFactory handle reuse
129 $this->mConn = pg_connect( $this->connectString, PGSQL_CONNECT_FORCE_NEW );
130 } catch ( Exception $ex ) {
131 $this->restoreErrorHandler();
132 throw $ex;
133 }
134
135 $phpError = $this->restoreErrorHandler();
136
137 if ( !$this->mConn ) {
138 $this->queryLogger->debug(
139 "DB connection error\n" .
140 "Server: $server, Database: $dbName, User: $user, Password: " .
141 substr( $password, 0, 3 ) . "...\n"
142 );
143 $this->queryLogger->debug( $this->lastError() . "\n" );
144 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
145 }
146
147 $this->mOpened = true;
148
149 # If called from the command-line (e.g. importDump), only show errors
150 if ( $this->cliMode ) {
151 $this->doQuery( "SET client_min_messages = 'ERROR'" );
152 }
153
154 $this->query( "SET client_encoding='UTF8'", __METHOD__ );
155 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__ );
156 $this->query( "SET timezone = 'GMT'", __METHOD__ );
157 $this->query( "SET standard_conforming_strings = on", __METHOD__ );
158 if ( $this->getServerVersion() >= 9.0 ) {
159 $this->query( "SET bytea_output = 'escape'", __METHOD__ ); // PHP bug 53127
160 }
161
162 $this->determineCoreSchema( $this->mSchema );
163 // The schema to be used is now in the search path; no need for explicit qualification
164 $this->mSchema = '';
165
166 return $this->mConn;
167 }
168
169 public function databasesAreIndependent() {
170 return true;
171 }
172
173 /**
174 * Postgres doesn't support selectDB in the same way MySQL does. So if the
175 * DB name doesn't match the open connection, open a new one
176 * @param string $db
177 * @return bool
178 * @throws DBUnexpectedError
179 */
180 public function selectDB( $db ) {
181 if ( $this->mDBname !== $db ) {
182 return (bool)$this->open( $this->mServer, $this->mUser, $this->mPassword, $db );
183 } else {
184 return true;
185 }
186 }
187
188 /**
189 * @param string[] $vars
190 * @return string
191 */
192 private function makeConnectionString( $vars ) {
193 $s = '';
194 foreach ( $vars as $name => $value ) {
195 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
196 }
197
198 return $s;
199 }
200
201 protected function closeConnection() {
202 return $this->mConn ? pg_close( $this->mConn ) : true;
203 }
204
205 public function doQuery( $sql ) {
206 $conn = $this->getBindingHandle();
207
208 $sql = mb_convert_encoding( $sql, 'UTF-8' );
209 // Clear previously left over PQresult
210 while ( $res = pg_get_result( $conn ) ) {
211 pg_free_result( $res );
212 }
213 if ( pg_send_query( $conn, $sql ) === false ) {
214 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
215 }
216 $this->mLastResult = pg_get_result( $conn );
217 $this->mAffectedRows = null;
218 if ( pg_result_error( $this->mLastResult ) ) {
219 return false;
220 }
221
222 return $this->mLastResult;
223 }
224
225 protected function dumpError() {
226 $diags = [
227 PGSQL_DIAG_SEVERITY,
228 PGSQL_DIAG_SQLSTATE,
229 PGSQL_DIAG_MESSAGE_PRIMARY,
230 PGSQL_DIAG_MESSAGE_DETAIL,
231 PGSQL_DIAG_MESSAGE_HINT,
232 PGSQL_DIAG_STATEMENT_POSITION,
233 PGSQL_DIAG_INTERNAL_POSITION,
234 PGSQL_DIAG_INTERNAL_QUERY,
235 PGSQL_DIAG_CONTEXT,
236 PGSQL_DIAG_SOURCE_FILE,
237 PGSQL_DIAG_SOURCE_LINE,
238 PGSQL_DIAG_SOURCE_FUNCTION
239 ];
240 foreach ( $diags as $d ) {
241 $this->queryLogger->debug( sprintf( "PgSQL ERROR(%d): %s\n",
242 $d, pg_result_error_field( $this->mLastResult, $d ) ) );
243 }
244 }
245
246 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
247 if ( $tempIgnore ) {
248 /* Check for constraint violation */
249 if ( $errno === '23505' ) {
250 parent::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
251
252 return;
253 }
254 }
255 /* Transaction stays in the ERROR state until rolled back */
256 if ( $this->mTrxLevel ) {
257 // Throw away the transaction state, then raise the error as normal.
258 // Note that if this connection is managed by LBFactory, it's already expected
259 // that the other transactions LBFactory manages will be rolled back.
260 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
261 }
262 parent::reportQueryError( $error, $errno, $sql, $fname, false );
263 }
264
265 public function freeResult( $res ) {
266 if ( $res instanceof ResultWrapper ) {
267 $res = $res->result;
268 }
269 MediaWiki\suppressWarnings();
270 $ok = pg_free_result( $res );
271 MediaWiki\restoreWarnings();
272 if ( !$ok ) {
273 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
274 }
275 }
276
277 public function fetchObject( $res ) {
278 if ( $res instanceof ResultWrapper ) {
279 $res = $res->result;
280 }
281 MediaWiki\suppressWarnings();
282 $row = pg_fetch_object( $res );
283 MediaWiki\restoreWarnings();
284 # @todo FIXME: HACK HACK HACK HACK debug
285
286 # @todo hashar: not sure if the following test really trigger if the object
287 # fetching failed.
288 $conn = $this->getBindingHandle();
289 if ( pg_last_error( $conn ) ) {
290 throw new DBUnexpectedError(
291 $this,
292 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
293 );
294 }
295
296 return $row;
297 }
298
299 public function fetchRow( $res ) {
300 if ( $res instanceof ResultWrapper ) {
301 $res = $res->result;
302 }
303 MediaWiki\suppressWarnings();
304 $row = pg_fetch_array( $res );
305 MediaWiki\restoreWarnings();
306
307 $conn = $this->getBindingHandle();
308 if ( pg_last_error( $conn ) ) {
309 throw new DBUnexpectedError(
310 $this,
311 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
312 );
313 }
314
315 return $row;
316 }
317
318 public function numRows( $res ) {
319 if ( $res instanceof ResultWrapper ) {
320 $res = $res->result;
321 }
322 MediaWiki\suppressWarnings();
323 $n = pg_num_rows( $res );
324 MediaWiki\restoreWarnings();
325
326 $conn = $this->getBindingHandle();
327 if ( pg_last_error( $conn ) ) {
328 throw new DBUnexpectedError(
329 $this,
330 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
331 );
332 }
333
334 return $n;
335 }
336
337 public function numFields( $res ) {
338 if ( $res instanceof ResultWrapper ) {
339 $res = $res->result;
340 }
341
342 return pg_num_fields( $res );
343 }
344
345 public function fieldName( $res, $n ) {
346 if ( $res instanceof ResultWrapper ) {
347 $res = $res->result;
348 }
349
350 return pg_field_name( $res, $n );
351 }
352
353 public function insertId() {
354 $res = $this->query( "SELECT lastval()" );
355 $row = $this->fetchRow( $res );
356 return is_null( $row[0] ) ? null : (int)$row[0];
357 }
358
359 public function dataSeek( $res, $row ) {
360 if ( $res instanceof ResultWrapper ) {
361 $res = $res->result;
362 }
363
364 return pg_result_seek( $res, $row );
365 }
366
367 public function lastError() {
368 if ( $this->mConn ) {
369 if ( $this->mLastResult ) {
370 return pg_result_error( $this->mLastResult );
371 } else {
372 return pg_last_error();
373 }
374 }
375
376 return $this->getLastPHPError() ?: 'No database connection';
377 }
378
379 public function lastErrno() {
380 if ( $this->mLastResult ) {
381 return pg_result_error_field( $this->mLastResult, PGSQL_DIAG_SQLSTATE );
382 } else {
383 return false;
384 }
385 }
386
387 public function affectedRows() {
388 if ( !is_null( $this->mAffectedRows ) ) {
389 // Forced result for simulated queries
390 return $this->mAffectedRows;
391 }
392 if ( empty( $this->mLastResult ) ) {
393 return 0;
394 }
395
396 return pg_affected_rows( $this->mLastResult );
397 }
398
399 /**
400 * Estimate rows in dataset
401 * Returns estimated count, based on EXPLAIN output
402 * This is not necessarily an accurate estimate, so use sparingly
403 * Returns -1 if count cannot be found
404 * Takes same arguments as Database::select()
405 *
406 * @param string $table
407 * @param string $vars
408 * @param string $conds
409 * @param string $fname
410 * @param array $options
411 * @return int
412 */
413 public function estimateRowCount( $table, $vars = '*', $conds = '',
414 $fname = __METHOD__, $options = []
415 ) {
416 $options['EXPLAIN'] = true;
417 $res = $this->select( $table, $vars, $conds, $fname, $options );
418 $rows = -1;
419 if ( $res ) {
420 $row = $this->fetchRow( $res );
421 $count = [];
422 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
423 $rows = (int)$count[1];
424 }
425 }
426
427 return $rows;
428 }
429
430 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
431 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
432 $res = $this->query( $sql, $fname );
433 if ( !$res ) {
434 return null;
435 }
436 foreach ( $res as $row ) {
437 if ( $row->indexname == $this->indexName( $index ) ) {
438 return $row;
439 }
440 }
441
442 return false;
443 }
444
445 public function indexAttributes( $index, $schema = false ) {
446 if ( $schema === false ) {
447 $schema = $this->getCoreSchema();
448 }
449 /*
450 * A subquery would be not needed if we didn't care about the order
451 * of attributes, but we do
452 */
453 $sql = <<<__INDEXATTR__
454
455 SELECT opcname,
456 attname,
457 i.indoption[s.g] as option,
458 pg_am.amname
459 FROM
460 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
461 FROM
462 pg_index isub
463 JOIN pg_class cis
464 ON cis.oid=isub.indexrelid
465 JOIN pg_namespace ns
466 ON cis.relnamespace = ns.oid
467 WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
468 pg_attribute,
469 pg_opclass opcls,
470 pg_am,
471 pg_class ci
472 JOIN pg_index i
473 ON ci.oid=i.indexrelid
474 JOIN pg_class ct
475 ON ct.oid = i.indrelid
476 JOIN pg_namespace n
477 ON ci.relnamespace = n.oid
478 WHERE
479 ci.relname='$index' AND n.nspname='$schema'
480 AND attrelid = ct.oid
481 AND i.indkey[s.g] = attnum
482 AND i.indclass[s.g] = opcls.oid
483 AND pg_am.oid = opcls.opcmethod
484 __INDEXATTR__;
485 $res = $this->query( $sql, __METHOD__ );
486 $a = [];
487 if ( $res ) {
488 foreach ( $res as $row ) {
489 $a[] = [
490 $row->attname,
491 $row->opcname,
492 $row->amname,
493 $row->option ];
494 }
495 } else {
496 return null;
497 }
498
499 return $a;
500 }
501
502 public function indexUnique( $table, $index, $fname = __METHOD__ ) {
503 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
504 " AND indexdef LIKE 'CREATE UNIQUE%(" .
505 $this->strencode( $this->indexName( $index ) ) .
506 ")'";
507 $res = $this->query( $sql, $fname );
508 if ( !$res ) {
509 return null;
510 }
511
512 return $res->numRows() > 0;
513 }
514
515 public function selectSQLText(
516 $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
517 ) {
518 if ( is_string( $options ) ) {
519 $options = [ $options ];
520 }
521
522 // Change the FOR UPDATE option as necessary based on the join conditions. Then pass
523 // to the parent function to get the actual SQL text.
524 // In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
525 // can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
526 // do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
527 // accordingly.
528 // MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
529 if ( is_array( $options ) ) {
530 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
531 if ( $forUpdateKey !== false && $join_conds ) {
532 unset( $options[$forUpdateKey] );
533 $options['FOR UPDATE'] = [];
534
535 $toCheck = $table;
536 reset( $toCheck );
537 while ( $toCheck ) {
538 $alias = key( $toCheck );
539 $name = $toCheck[$alias];
540 unset( $toCheck[$alias] );
541
542 $hasAlias = !is_numeric( $alias );
543 if ( !$hasAlias && is_string( $name ) ) {
544 $alias = $name;
545 }
546
547 if ( !isset( $join_conds[$alias] ) ||
548 !preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_conds[$alias][0] )
549 ) {
550 if ( is_array( $name ) ) {
551 // It's a parenthesized group, process all the tables inside the group.
552 $toCheck = array_merge( $toCheck, $name );
553 } else {
554 // Quote alias names so $this->tableName() won't mangle them
555 $options['FOR UPDATE'][] = $hasAlias ? $this->addIdentifierQuotes( $alias ) : $alias;
556 }
557 }
558 }
559 }
560
561 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
562 unset( $options['ORDER BY'] );
563 }
564 }
565
566 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
567 }
568
569 /**
570 * INSERT wrapper, inserts an array into a table
571 *
572 * $args may be a single associative array, or an array of these with numeric keys,
573 * for multi-row insert (Postgres version 8.2 and above only).
574 *
575 * @param string $table Name of the table to insert to.
576 * @param array $args Items to insert into the table.
577 * @param string $fname Name of the function, for profiling
578 * @param array|string $options String or array. Valid options: IGNORE
579 * @return bool Success of insert operation. IGNORE always returns true.
580 */
581 public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
582 if ( !count( $args ) ) {
583 return true;
584 }
585
586 $table = $this->tableName( $table );
587 if ( !isset( $this->numericVersion ) ) {
588 $this->getServerVersion();
589 }
590
591 if ( !is_array( $options ) ) {
592 $options = [ $options ];
593 }
594
595 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
596 $multi = true;
597 $keys = array_keys( $args[0] );
598 } else {
599 $multi = false;
600 $keys = array_keys( $args );
601 }
602
603 // If IGNORE is set, we use savepoints to emulate mysql's behavior
604 // @todo If PostgreSQL 9.5+, we could use ON CONFLICT DO NOTHING instead
605 $savepoint = $olde = null;
606 $numrowsinserted = 0;
607 if ( in_array( 'IGNORE', $options ) ) {
608 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
609 $olde = error_reporting( 0 );
610 // For future use, we may want to track the number of actual inserts
611 // Right now, insert (all writes) simply return true/false
612 }
613
614 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
615
616 if ( $multi ) {
617 if ( $this->numericVersion >= 8.2 && !$savepoint ) {
618 $first = true;
619 foreach ( $args as $row ) {
620 if ( $first ) {
621 $first = false;
622 } else {
623 $sql .= ',';
624 }
625 $sql .= '(' . $this->makeList( $row ) . ')';
626 }
627 $res = (bool)$this->query( $sql, $fname, $savepoint );
628 } else {
629 $res = true;
630 $origsql = $sql;
631 foreach ( $args as $row ) {
632 $tempsql = $origsql;
633 $tempsql .= '(' . $this->makeList( $row ) . ')';
634
635 if ( $savepoint ) {
636 $savepoint->savepoint();
637 }
638
639 $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
640
641 if ( $savepoint ) {
642 $bar = pg_result_error( $this->mLastResult );
643 if ( $bar != false ) {
644 $savepoint->rollback();
645 } else {
646 $savepoint->release();
647 $numrowsinserted++;
648 }
649 }
650
651 // If any of them fail, we fail overall for this function call
652 // Note that this will be ignored if IGNORE is set
653 if ( !$tempres ) {
654 $res = false;
655 }
656 }
657 }
658 } else {
659 // Not multi, just a lone insert
660 if ( $savepoint ) {
661 $savepoint->savepoint();
662 }
663
664 $sql .= '(' . $this->makeList( $args ) . ')';
665 $res = (bool)$this->query( $sql, $fname, $savepoint );
666 if ( $savepoint ) {
667 $bar = pg_result_error( $this->mLastResult );
668 if ( $bar != false ) {
669 $savepoint->rollback();
670 } else {
671 $savepoint->release();
672 $numrowsinserted++;
673 }
674 }
675 }
676 if ( $savepoint ) {
677 error_reporting( $olde );
678 $savepoint->commit();
679
680 // Set the affected row count for the whole operation
681 $this->mAffectedRows = $numrowsinserted;
682
683 // IGNORE always returns true
684 return true;
685 }
686
687 return $res;
688 }
689
690 /**
691 * INSERT SELECT wrapper
692 * $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
693 * Source items may be literals rather then field names, but strings should
694 * be quoted with Database::addQuotes()
695 * $conds may be "*" to copy the whole table
696 * srcTable may be an array of tables.
697 * @todo FIXME: Implement this a little better (seperate select/insert)?
698 *
699 * @param string $destTable
700 * @param array|string $srcTable
701 * @param array $varMap
702 * @param array $conds
703 * @param string $fname
704 * @param array $insertOptions
705 * @param array $selectOptions
706 * @param array $selectJoinConds
707 * @return bool
708 */
709 public function nativeInsertSelect(
710 $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
711 $insertOptions = [], $selectOptions = [], $selectJoinConds = []
712 ) {
713 if ( !is_array( $insertOptions ) ) {
714 $insertOptions = [ $insertOptions ];
715 }
716
717 /*
718 * If IGNORE is set, use the non-native version.
719 * @todo If PostgreSQL 9.5+, we could use ON CONFLICT DO NOTHING
720 */
721 if ( in_array( 'IGNORE', $insertOptions ) ) {
722 return $this->nonNativeInsertSelect(
723 $destTable, $srcTable, $varMap, $conds, $fname, $insertOptions, $selectOptions, $selectJoinConds
724 );
725 }
726
727 return parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
728 $insertOptions, $selectOptions, $selectJoinConds );
729 }
730
731 public function tableName( $name, $format = 'quoted' ) {
732 // Replace reserved words with better ones
733 $name = $this->remappedTableName( $name );
734
735 return parent::tableName( $name, $format );
736 }
737
738 /**
739 * @param string $name
740 * @return string Value of $name or remapped name if $name is a reserved keyword
741 */
742 public function remappedTableName( $name ) {
743 return isset( $this->keywordTableMap[$name] ) ? $this->keywordTableMap[$name] : $name;
744 }
745
746 /**
747 * @param string $name
748 * @param string $format
749 * @return string Qualified and encoded (if requested) table name
750 */
751 public function realTableName( $name, $format = 'quoted' ) {
752 return parent::tableName( $name, $format );
753 }
754
755 public function nextSequenceValue( $seqName ) {
756 return new NextSequenceValue;
757 }
758
759 /**
760 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
761 *
762 * @param string $seqName
763 * @return int
764 */
765 public function currentSequenceValue( $seqName ) {
766 $safeseq = str_replace( "'", "''", $seqName );
767 $res = $this->query( "SELECT currval('$safeseq')" );
768 $row = $this->fetchRow( $res );
769 $currval = $row[0];
770
771 return $currval;
772 }
773
774 public function textFieldSize( $table, $field ) {
775 $table = $this->tableName( $table );
776 $sql = "SELECT t.typname as ftype,a.atttypmod as size
777 FROM pg_class c, pg_attribute a, pg_type t
778 WHERE relname='$table' AND a.attrelid=c.oid AND
779 a.atttypid=t.oid and a.attname='$field'";
780 $res = $this->query( $sql );
781 $row = $this->fetchObject( $res );
782 if ( $row->ftype == 'varchar' ) {
783 $size = $row->size - 4;
784 } else {
785 $size = $row->size;
786 }
787
788 return $size;
789 }
790
791 public function limitResult( $sql, $limit, $offset = false ) {
792 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
793 }
794
795 public function wasDeadlock() {
796 return $this->lastErrno() == '40P01';
797 }
798
799 public function duplicateTableStructure(
800 $oldName, $newName, $temporary = false, $fname = __METHOD__
801 ) {
802 $newName = $this->addIdentifierQuotes( $newName );
803 $oldName = $this->addIdentifierQuotes( $oldName );
804
805 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
806 "(LIKE $oldName INCLUDING DEFAULTS INCLUDING INDEXES)", $fname );
807 }
808
809 public function listTables( $prefix = null, $fname = __METHOD__ ) {
810 $eschema = $this->addQuotes( $this->getCoreSchema() );
811 $result = $this->query(
812 "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
813 $endArray = [];
814
815 foreach ( $result as $table ) {
816 $vars = get_object_vars( $table );
817 $table = array_pop( $vars );
818 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
819 $endArray[] = $table;
820 }
821 }
822
823 return $endArray;
824 }
825
826 public function timestamp( $ts = 0 ) {
827 $ct = new ConvertibleTimestamp( $ts );
828
829 return $ct->getTimestamp( TS_POSTGRES );
830 }
831
832 /**
833 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
834 * to https://secure.php.net/manual/en/ref.pgsql.php
835 *
836 * Parsing a postgres array can be a tricky problem, he's my
837 * take on this, it handles multi-dimensional arrays plus
838 * escaping using a nasty regexp to determine the limits of each
839 * data-item.
840 *
841 * This should really be handled by PHP PostgreSQL module
842 *
843 * @since 1.19
844 * @param string $text Postgreql array returned in a text form like {a,b}
845 * @param string[] $output
846 * @param int|bool $limit
847 * @param int $offset
848 * @return string[]
849 */
850 private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
851 if ( false === $limit ) {
852 $limit = strlen( $text ) - 1;
853 $output = [];
854 }
855 if ( '{}' == $text ) {
856 return $output;
857 }
858 do {
859 if ( '{' != $text[$offset] ) {
860 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
861 $text, $match, 0, $offset );
862 $offset += strlen( $match[0] );
863 $output[] = ( '"' != $match[1][0]
864 ? $match[1]
865 : stripcslashes( substr( $match[1], 1, -1 ) ) );
866 if ( '},' == $match[3] ) {
867 return $output;
868 }
869 } else {
870 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
871 }
872 } while ( $limit > $offset );
873
874 return $output;
875 }
876
877 public function aggregateValue( $valuedata, $valuename = 'value' ) {
878 return $valuedata;
879 }
880
881 public function getSoftwareLink() {
882 return '[{{int:version-db-postgres-url}} PostgreSQL]';
883 }
884
885 /**
886 * Return current schema (executes SELECT current_schema())
887 * Needs transaction
888 *
889 * @since 1.19
890 * @return string Default schema for the current session
891 */
892 public function getCurrentSchema() {
893 $res = $this->query( "SELECT current_schema()", __METHOD__ );
894 $row = $this->fetchRow( $res );
895
896 return $row[0];
897 }
898
899 /**
900 * Return list of schemas which are accessible without schema name
901 * This is list does not contain magic keywords like "$user"
902 * Needs transaction
903 *
904 * @see getSearchPath()
905 * @see setSearchPath()
906 * @since 1.19
907 * @return array List of actual schemas for the current sesson
908 */
909 public function getSchemas() {
910 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
911 $row = $this->fetchRow( $res );
912 $schemas = [];
913
914 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
915
916 return $this->pg_array_parse( $row[0], $schemas );
917 }
918
919 /**
920 * Return search patch for schemas
921 * This is different from getSchemas() since it contain magic keywords
922 * (like "$user").
923 * Needs transaction
924 *
925 * @since 1.19
926 * @return array How to search for table names schemas for the current user
927 */
928 public function getSearchPath() {
929 $res = $this->query( "SHOW search_path", __METHOD__ );
930 $row = $this->fetchRow( $res );
931
932 /* PostgreSQL returns SHOW values as strings */
933
934 return explode( ",", $row[0] );
935 }
936
937 /**
938 * Update search_path, values should already be sanitized
939 * Values may contain magic keywords like "$user"
940 * @since 1.19
941 *
942 * @param array $search_path List of schemas to be searched by default
943 */
944 private function setSearchPath( $search_path ) {
945 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
946 }
947
948 /**
949 * Determine default schema for the current application
950 * Adjust this session schema search path if desired schema exists
951 * and is not alread there.
952 *
953 * We need to have name of the core schema stored to be able
954 * to query database metadata.
955 *
956 * This will be also called by the installer after the schema is created
957 *
958 * @since 1.19
959 *
960 * @param string $desiredSchema
961 */
962 public function determineCoreSchema( $desiredSchema ) {
963 $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
964 if ( $this->schemaExists( $desiredSchema ) ) {
965 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
966 $this->mCoreSchema = $desiredSchema;
967 $this->queryLogger->debug(
968 "Schema \"" . $desiredSchema . "\" already in the search path\n" );
969 } else {
970 /**
971 * Prepend our schema (e.g. 'mediawiki') in front
972 * of the search path
973 * Fixes T17816
974 */
975 $search_path = $this->getSearchPath();
976 array_unshift( $search_path,
977 $this->addIdentifierQuotes( $desiredSchema ) );
978 $this->setSearchPath( $search_path );
979 $this->mCoreSchema = $desiredSchema;
980 $this->queryLogger->debug(
981 "Schema \"" . $desiredSchema . "\" added to the search path\n" );
982 }
983 } else {
984 $this->mCoreSchema = $this->getCurrentSchema();
985 $this->queryLogger->debug(
986 "Schema \"" . $desiredSchema . "\" not found, using current \"" .
987 $this->mCoreSchema . "\"\n" );
988 }
989 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
990 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
991 }
992
993 /**
994 * Return schema name for core application tables
995 *
996 * @since 1.19
997 * @return string Core schema name
998 */
999 public function getCoreSchema() {
1000 return $this->mCoreSchema;
1001 }
1002
1003 public function getServerVersion() {
1004 if ( !isset( $this->numericVersion ) ) {
1005 $conn = $this->getBindingHandle();
1006 $versionInfo = pg_version( $conn );
1007 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1008 // Old client, abort install
1009 $this->numericVersion = '7.3 or earlier';
1010 } elseif ( isset( $versionInfo['server'] ) ) {
1011 // Normal client
1012 $this->numericVersion = $versionInfo['server'];
1013 } else {
1014 // T18937: broken pgsql extension from PHP<5.3
1015 $this->numericVersion = pg_parameter_status( $conn, 'server_version' );
1016 }
1017 }
1018
1019 return $this->numericVersion;
1020 }
1021
1022 /**
1023 * Query whether a given relation exists (in the given schema, or the
1024 * default mw one if not given)
1025 * @param string $table
1026 * @param array|string $types
1027 * @param bool|string $schema
1028 * @return bool
1029 */
1030 private function relationExists( $table, $types, $schema = false ) {
1031 if ( !is_array( $types ) ) {
1032 $types = [ $types ];
1033 }
1034 if ( $schema === false ) {
1035 $schema = $this->getCoreSchema();
1036 }
1037 $table = $this->realTableName( $table, 'raw' );
1038 $etable = $this->addQuotes( $table );
1039 $eschema = $this->addQuotes( $schema );
1040 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1041 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1042 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1043 $res = $this->query( $sql );
1044 $count = $res ? $res->numRows() : 0;
1045
1046 return (bool)$count;
1047 }
1048
1049 /**
1050 * For backward compatibility, this function checks both tables and views.
1051 * @param string $table
1052 * @param string $fname
1053 * @param bool|string $schema
1054 * @return bool
1055 */
1056 public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1057 return $this->relationExists( $table, [ 'r', 'v' ], $schema );
1058 }
1059
1060 public function sequenceExists( $sequence, $schema = false ) {
1061 return $this->relationExists( $sequence, 'S', $schema );
1062 }
1063
1064 public function triggerExists( $table, $trigger ) {
1065 $q = <<<SQL
1066 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1067 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1068 AND tgrelid=pg_class.oid
1069 AND nspname=%s AND relname=%s AND tgname=%s
1070 SQL;
1071 $res = $this->query(
1072 sprintf(
1073 $q,
1074 $this->addQuotes( $this->getCoreSchema() ),
1075 $this->addQuotes( $table ),
1076 $this->addQuotes( $trigger )
1077 )
1078 );
1079 if ( !$res ) {
1080 return null;
1081 }
1082 $rows = $res->numRows();
1083
1084 return $rows;
1085 }
1086
1087 public function ruleExists( $table, $rule ) {
1088 $exists = $this->selectField( 'pg_rules', 'rulename',
1089 [
1090 'rulename' => $rule,
1091 'tablename' => $table,
1092 'schemaname' => $this->getCoreSchema()
1093 ]
1094 );
1095
1096 return $exists === $rule;
1097 }
1098
1099 public function constraintExists( $table, $constraint ) {
1100 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1101 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1102 $this->addQuotes( $this->getCoreSchema() ),
1103 $this->addQuotes( $table ),
1104 $this->addQuotes( $constraint )
1105 );
1106 $res = $this->query( $sql );
1107 if ( !$res ) {
1108 return null;
1109 }
1110 $rows = $res->numRows();
1111
1112 return $rows;
1113 }
1114
1115 /**
1116 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1117 * @param string $schema
1118 * @return bool
1119 */
1120 public function schemaExists( $schema ) {
1121 if ( !strlen( $schema ) ) {
1122 return false; // short-circuit
1123 }
1124
1125 $exists = $this->selectField(
1126 '"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
1127
1128 return (bool)$exists;
1129 }
1130
1131 /**
1132 * Returns true if a given role (i.e. user) exists, false otherwise.
1133 * @param string $roleName
1134 * @return bool
1135 */
1136 public function roleExists( $roleName ) {
1137 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1138 [ 'rolname' => $roleName ], __METHOD__ );
1139
1140 return (bool)$exists;
1141 }
1142
1143 /**
1144 * @param string $table
1145 * @param string $field
1146 * @return PostgresField|null
1147 */
1148 public function fieldInfo( $table, $field ) {
1149 return PostgresField::fromText( $this, $table, $field );
1150 }
1151
1152 /**
1153 * pg_field_type() wrapper
1154 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1155 * @param int $index Field number, starting from 0
1156 * @return string
1157 */
1158 public function fieldType( $res, $index ) {
1159 if ( $res instanceof ResultWrapper ) {
1160 $res = $res->result;
1161 }
1162
1163 return pg_field_type( $res, $index );
1164 }
1165
1166 public function encodeBlob( $b ) {
1167 return new PostgresBlob( pg_escape_bytea( $b ) );
1168 }
1169
1170 public function decodeBlob( $b ) {
1171 if ( $b instanceof PostgresBlob ) {
1172 $b = $b->fetch();
1173 } elseif ( $b instanceof Blob ) {
1174 return $b->fetch();
1175 }
1176
1177 return pg_unescape_bytea( $b );
1178 }
1179
1180 public function strencode( $s ) {
1181 // Should not be called by us
1182 return pg_escape_string( $this->getBindingHandle(), (string)$s );
1183 }
1184
1185 public function addQuotes( $s ) {
1186 $conn = $this->getBindingHandle();
1187
1188 if ( is_null( $s ) ) {
1189 return 'NULL';
1190 } elseif ( is_bool( $s ) ) {
1191 return intval( $s );
1192 } elseif ( $s instanceof Blob ) {
1193 if ( $s instanceof PostgresBlob ) {
1194 $s = $s->fetch();
1195 } else {
1196 $s = pg_escape_bytea( $conn, $s->fetch() );
1197 }
1198 return "'$s'";
1199 } elseif ( $s instanceof NextSequenceValue ) {
1200 return 'DEFAULT';
1201 }
1202
1203 return "'" . pg_escape_string( $conn, (string)$s ) . "'";
1204 }
1205
1206 /**
1207 * Postgres specific version of replaceVars.
1208 * Calls the parent version in Database.php
1209 *
1210 * @param string $ins SQL string, read from a stream (usually tables.sql)
1211 * @return string SQL string
1212 */
1213 protected function replaceVars( $ins ) {
1214 $ins = parent::replaceVars( $ins );
1215
1216 if ( $this->numericVersion >= 8.3 ) {
1217 // Thanks for not providing backwards-compatibility, 8.3
1218 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1219 }
1220
1221 if ( $this->numericVersion <= 8.1 ) { // Our minimum version
1222 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1223 }
1224
1225 return $ins;
1226 }
1227
1228 public function makeSelectOptions( $options ) {
1229 $preLimitTail = $postLimitTail = '';
1230 $startOpts = $useIndex = $ignoreIndex = '';
1231
1232 $noKeyOptions = [];
1233 foreach ( $options as $key => $option ) {
1234 if ( is_numeric( $key ) ) {
1235 $noKeyOptions[$option] = true;
1236 }
1237 }
1238
1239 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1240
1241 $preLimitTail .= $this->makeOrderBy( $options );
1242
1243 if ( isset( $options['FOR UPDATE'] ) ) {
1244 $postLimitTail .= ' FOR UPDATE OF ' .
1245 implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
1246 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1247 $postLimitTail .= ' FOR UPDATE';
1248 }
1249
1250 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1251 $startOpts .= 'DISTINCT';
1252 }
1253
1254 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1255 }
1256
1257 public function getDBname() {
1258 return $this->mDBname;
1259 }
1260
1261 public function getServer() {
1262 return $this->mServer;
1263 }
1264
1265 public function buildConcat( $stringList ) {
1266 return implode( ' || ', $stringList );
1267 }
1268
1269 public function buildGroupConcatField(
1270 $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
1271 ) {
1272 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1273
1274 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1275 }
1276
1277 public function buildStringCast( $field ) {
1278 return $field . '::text';
1279 }
1280
1281 public function streamStatementEnd( &$sql, &$newLine ) {
1282 # Allow dollar quoting for function declarations
1283 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1284 if ( $this->delimiter ) {
1285 $this->delimiter = false;
1286 } else {
1287 $this->delimiter = ';';
1288 }
1289 }
1290
1291 return parent::streamStatementEnd( $sql, $newLine );
1292 }
1293
1294 public function doLockTables( array $read, array $write, $method ) {
1295 $tablesWrite = [];
1296 foreach ( $write as $table ) {
1297 $tablesWrite[] = $this->tableName( $table );
1298 }
1299 $tablesRead = [];
1300 foreach ( $read as $table ) {
1301 $tablesRead[] = $this->tableName( $table );
1302 }
1303
1304 // Acquire locks for the duration of the current transaction...
1305 if ( $tablesWrite ) {
1306 $this->query(
1307 'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
1308 $method
1309 );
1310 }
1311 if ( $tablesRead ) {
1312 $this->query(
1313 'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
1314 $method
1315 );
1316 }
1317
1318 return true;
1319 }
1320
1321 public function lockIsFree( $lockName, $method ) {
1322 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1323 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1324 $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
1325 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1326 $row = $this->fetchObject( $result );
1327
1328 return ( $row->lockstatus === 't' );
1329 }
1330
1331 public function lock( $lockName, $method, $timeout = 5 ) {
1332 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1333 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1334 $loop = new WaitConditionLoop(
1335 function () use ( $lockName, $key, $timeout, $method ) {
1336 $res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1337 $row = $this->fetchObject( $res );
1338 if ( $row->lockstatus === 't' ) {
1339 parent::lock( $lockName, $method, $timeout ); // record
1340 return true;
1341 }
1342
1343 return WaitConditionLoop::CONDITION_CONTINUE;
1344 },
1345 $timeout
1346 );
1347
1348 return ( $loop->invoke() === $loop::CONDITION_REACHED );
1349 }
1350
1351 public function unlock( $lockName, $method ) {
1352 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1353 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1354 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1355 $row = $this->fetchObject( $result );
1356
1357 if ( $row->lockstatus === 't' ) {
1358 parent::unlock( $lockName, $method ); // record
1359 return true;
1360 }
1361
1362 $this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
1363
1364 return false;
1365 }
1366
1367 public function serverIsReadOnly() {
1368 $res = $this->query( "SHOW default_transaction_read_only", __METHOD__ );
1369 $row = $this->fetchObject( $res );
1370
1371 return $row ? ( strtolower( $row->default_transaction_read_only ) === 'on' ) : false;
1372 }
1373
1374 /**
1375 * @param string $lockName
1376 * @return string Integer
1377 */
1378 private function bigintFromLockName( $lockName ) {
1379 return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
1380 }
1381 }
1382
1383 class_alias( DatabasePostgres::class, 'DatabasePostgres' );