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