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