1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
// Suppress UnusedPluginSuppression because Phan on PHP 7.4 and PHP 8.1 need different suppressions
// @phan-file-suppress UnusedPluginSuppression,UnusedPluginFileSuppression
namespace Wikimedia\Rdbms;
use RuntimeException;
use Wikimedia\Rdbms\Platform\PostgresPlatform;
use Wikimedia\Rdbms\Replication\ReplicationReporter;
use Wikimedia\WaitConditionLoop;
/**
* Postgres database abstraction layer.
*
* @ingroup Database
*/
class DatabasePostgres extends Database {
/** @var int */
private $port;
/** @var string */
private $tempSchema;
/** @var float|string|null */
private $numericVersion;
/** @var resource|null */
private $lastResultHandle;
/** @var PostgresPlatform */
protected $platform;
/**
* @see Database::__construct()
* @param array $params Additional parameters include:
* - port: A port to append to the hostname
*/
public function __construct( array $params ) {
$this->port = intval( $params['port'] ?? null );
parent::__construct( $params );
$this->platform = new PostgresPlatform(
$this,
$this->logger,
$this->currentDomain,
$this->errorLogger
);
$this->replicationReporter = new ReplicationReporter(
$params['topologyRole'],
$this->logger,
$params['srvCache']
);
}
public function getType() {
return 'postgres';
}
protected function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
if ( !function_exists( 'pg_connect' ) ) {
throw $this->newExceptionAfterConnectError(
"Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
"option? (Note: if you recently installed PHP, you may need to restart your\n" .
"webserver and database)"
);
}
$this->close( __METHOD__ );
$connectVars = [
// A database must be specified in order to connect to Postgres. If $dbName is not
// specified, then use the standard "postgres" database that should exist by default.
'dbname' => ( $db !== null && $db !== '' ) ? $db : 'postgres',
'user' => $user,
'password' => $password
];
if ( $server !== null && $server !== '' ) {
$connectVars['host'] = $server;
}
if ( $this->port > 0 ) {
$connectVars['port'] = $this->port;
}
if ( $this->ssl ) {
$connectVars['sslmode'] = 'require';
}
$connectString = $this->makeConnectionString( $connectVars );
$this->installErrorHandler();
try {
$this->conn = pg_connect( $connectString, PGSQL_CONNECT_FORCE_NEW ) ?: null;
} catch ( RuntimeException $e ) {
$this->restoreErrorHandler();
throw $this->newExceptionAfterConnectError( $e->getMessage() );
}
$error = $this->restoreErrorHandler();
if ( !$this->conn ) {
throw $this->newExceptionAfterConnectError( $error ?: $this->lastError() );
}
try {
// Since no transaction is active at this point, any SET commands should apply
// for the entire session (e.g. will not be reverted on transaction rollback).
// See https://www.postgresql.org/docs/8.3/sql-set.html
$variables = [
'client_encoding' => 'UTF8',
'datestyle' => 'ISO, YMD',
'timezone' => 'GMT',
'standard_conforming_strings' => 'on',
'bytea_output' => 'escape',
'client_min_messages' => 'ERROR'
];
foreach ( $variables as $var => $val ) {
$sql = 'SET ' . $this->platform->addIdentifierQuotes( $var ) . ' = ' . $this->addQuotes( $val );
$query = new Query( $sql, self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX, 'SET' );
$this->query( $query, __METHOD__ );
}
$this->determineCoreSchema( $schema );
$this->currentDomain = new DatabaseDomain( $db, $schema, $tablePrefix );
$this->platform->setCurrentDomain( $this->currentDomain );
} catch ( RuntimeException $e ) {
throw $this->newExceptionAfterConnectError( $e->getMessage() );
}
}
public function databasesAreIndependent() {
return true;
}
public function doSelectDomain( DatabaseDomain $domain ) {
$database = $domain->getDatabase();
if ( $database === null ) {
// A null database means "don't care" so leave it as is and update the table prefix
$this->currentDomain = new DatabaseDomain(
$this->currentDomain->getDatabase(),
$domain->getSchema() ?? $this->currentDomain->getSchema(),
$domain->getTablePrefix()
);
$this->platform->setCurrentDomain( $this->currentDomain );
} elseif ( $this->getDBname() !== $database ) {
// Postgres doesn't support selectDB in the same way MySQL does.
// So if the DB name doesn't match the open connection, open a new one
$this->open(
$this->connectionParams[self::CONN_HOST],
$this->connectionParams[self::CONN_USER],
$this->connectionParams[self::CONN_PASSWORD],
$database,
$domain->getSchema(),
$domain->getTablePrefix()
);
} else {
$this->currentDomain = $domain;
$this->platform->setCurrentDomain( $domain );
}
return true;
}
/**
* @param string[] $vars
* @return string
*/
private function makeConnectionString( $vars ) {
$s = '';
foreach ( $vars as $name => $value ) {
$s .= "$name='" . str_replace( [ "\\", "'" ], [ "\\\\", "\\'" ], $value ) . "' ";
}
return $s;
}
protected function closeConnection() {
return $this->conn ? pg_close( $this->conn ) : true;
}
public function doSingleStatementQuery( string $sql ): QueryStatus {
$conn = $this->getBindingHandle();
$sql = mb_convert_encoding( $sql, 'UTF-8' );
// Clear any previously left over result
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( $priorRes = pg_get_result( $conn ) ) {
pg_free_result( $priorRes );
}
if ( pg_send_query( $conn, $sql ) === false ) {
throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
}
// Newer PHP versions use PgSql\Result instead of resource variables
// https://www.php.net/manual/en/function.pg-get-result.php
$pgRes = pg_get_result( $conn );
// Phan on PHP 7.4 and PHP 8.1 need different suppressions
// @phan-suppress-next-line PhanTypeMismatchProperty,PhanTypeMismatchPropertyProbablyReal
$this->lastResultHandle = $pgRes;
$res = pg_result_error( $pgRes ) ? false : $pgRes;
return new QueryStatus(
// @phan-suppress-next-line PhanTypeMismatchArgument
is_bool( $res ) ? $res : new PostgresResultWrapper( $this, $conn, $res ),
$pgRes ? pg_affected_rows( $pgRes ) : 0,
$this->lastError(),
$this->lastErrno()
);
}
protected function dumpError() {
$diags = [
PGSQL_DIAG_SEVERITY,
PGSQL_DIAG_SQLSTATE,
PGSQL_DIAG_MESSAGE_PRIMARY,
PGSQL_DIAG_MESSAGE_DETAIL,
PGSQL_DIAG_MESSAGE_HINT,
PGSQL_DIAG_STATEMENT_POSITION,
PGSQL_DIAG_INTERNAL_POSITION,
PGSQL_DIAG_INTERNAL_QUERY,
PGSQL_DIAG_CONTEXT,
PGSQL_DIAG_SOURCE_FILE,
PGSQL_DIAG_SOURCE_LINE,
PGSQL_DIAG_SOURCE_FUNCTION
];
foreach ( $diags as $d ) {
$this->logger->debug( sprintf( "PgSQL ERROR(%d): %s",
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
$d, pg_result_error_field( $this->lastResultHandle, $d ) ) );
}
}
protected function lastInsertId() {
// Avoid using query() to prevent unwanted side-effects like changing affected
// row counts or connection retries. Note that lastval() is connection-specific.
// Note that this causes "lastval is not yet defined in this session" errors if
// nextval() was never directly or implicitly triggered (error out any transaction).
$qs = $this->doSingleStatementQuery( "SELECT lastval() AS id" );
return $qs->res ? (int)$qs->res->fetchRow()['id'] : 0;
}
public function lastError() {
if ( $this->conn ) {
if ( $this->lastResultHandle ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
return pg_result_error( $this->lastResultHandle );
} else {
return pg_last_error() ?: $this->lastConnectError;
}
}
return $this->getLastPHPError() ?: 'No database connection';
}
public function lastErrno() {
if ( $this->lastResultHandle ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
$lastErrno = pg_result_error_field( $this->lastResultHandle, PGSQL_DIAG_SQLSTATE );
if ( $lastErrno !== false ) {
return $lastErrno;
}
}
return '00000';
}
public function estimateRowCount( $table, $var = '*', $conds = '',
$fname = __METHOD__, $options = [], $join_conds = []
): int {
$conds = $this->platform->normalizeConditions( $conds, $fname );
$column = $this->platform->extractSingleFieldFromList( $var );
if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
$conds[] = "$column IS NOT NULL";
}
$options['EXPLAIN'] = true;
$res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
$rows = -1;
if ( $res ) {
$row = $res->fetchRow();
$count = [];
if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
$rows = (int)$count[1];
}
}
return $rows;
}
public function indexInfo( $table, $index, $fname = __METHOD__ ) {
$components = $this->platform->qualifiedTableComponents( $table );
if ( count( $components ) === 1 ) {
$schemas = $this->getCoreSchemas();
$tableComponent = $components[0];
} elseif ( count( $components ) === 2 ) {
[ $schema, $tableComponent ] = $components;
$schemas = [ $schema ];
} else {
[ , $schema, $tableComponent ] = $components;
$schemas = [ $schema ];
}
foreach ( $schemas as $schema ) {
$encSchema = $this->addQuotes( $schema );
$encTable = $this->addQuotes( $tableComponent );
$encIndex = $this->addQuotes( $this->platform->indexName( $index ) );
$query = new Query(
"SELECT indexname,indexdef FROM pg_indexes " .
"WHERE schemaname=$encSchema AND tablename=$encTable AND indexname=$encIndex",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query );
$row = $res->fetchObject();
if ( $row ) {
return [ 'unique' => ( strpos( $row->indexdef, 'CREATE UNIQUE ' ) === 0 ) ];
}
}
return false;
}
public function indexAttributes( $index, $schema = false ) {
if ( $schema === false ) {
$schemas = $this->getCoreSchemas();
} else {
$schemas = [ $schema ];
}
$eindex = $this->addQuotes( $index );
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
foreach ( $schemas as $schema ) {
$eschema = $this->addQuotes( $schema );
/*
* A subquery would be not needed if we didn't care about the order
* of attributes, but we do
*/
$sql = <<<__INDEXATTR__
SELECT opcname,
attname,
i.indoption[s.g] as option,
pg_am.amname
FROM
(SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
FROM
pg_index isub
JOIN pg_class cis
ON cis.oid=isub.indexrelid
JOIN pg_namespace ns
ON cis.relnamespace = ns.oid
WHERE cis.relname=$eindex AND ns.nspname=$eschema) AS s,
pg_attribute,
pg_opclass opcls,
pg_am,
pg_class ci
JOIN pg_index i
ON ci.oid=i.indexrelid
JOIN pg_class ct
ON ct.oid = i.indrelid
JOIN pg_namespace n
ON ci.relnamespace = n.oid
WHERE
ci.relname=$eindex AND n.nspname=$eschema
AND attrelid = ct.oid
AND i.indkey[s.g] = attnum
AND i.indclass[s.g] = opcls.oid
AND pg_am.oid = opcls.opcmethod
__INDEXATTR__;
$query = new Query( $sql, $flags, 'SELECT' );
$res = $this->query( $query, __METHOD__ );
$a = [];
if ( $res ) {
foreach ( $res as $row ) {
$a[] = [
$row->attname,
$row->opcname,
$row->amname,
$row->option ];
}
return $a;
}
}
return null;
}
protected function doInsertSelectNative(
$destTable,
$srcTable,
array $varMap,
$conds,
$fname,
array $insertOptions,
array $selectOptions,
$selectJoinConds
) {
if ( in_array( 'IGNORE', $insertOptions ) ) {
// Use "ON CONFLICT DO" if we have it for IGNORE
$destTableEnc = $this->tableName( $destTable );
$selectSql = $this->selectSQLText(
$srcTable,
array_values( $varMap ),
$conds,
$fname,
$selectOptions,
$selectJoinConds
);
$sql = "INSERT INTO $destTableEnc (" . implode( ',', array_keys( $varMap ) ) . ') ' .
$selectSql . ' ON CONFLICT DO NOTHING';
$query = new Query( $sql, self::QUERY_CHANGE_ROWS, 'INSERT', $destTable );
$this->query( $query, $fname );
} else {
parent::doInsertSelectNative( $destTable, $srcTable, $varMap, $conds, $fname,
$insertOptions, $selectOptions, $selectJoinConds );
}
}
public function getValueTypesForWithClause( $table ) {
$typesByColumn = [];
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
$encTable = $this->addQuotes( $table );
foreach ( $this->getCoreSchemas() as $schema ) {
$encSchema = $this->addQuotes( $schema );
$sql = "SELECT column_name,udt_name " .
"FROM information_schema.columns " .
"WHERE table_name = $encTable AND table_schema = $encSchema";
$query = new Query( $sql, $flags, 'SELECT' );
$res = $this->query( $query, __METHOD__ );
if ( $res->numRows() ) {
foreach ( $res as $row ) {
$typesByColumn[$row->column_name] = $row->udt_name;
}
break;
}
}
return $typesByColumn;
}
protected function isConnectionError( $errno ) {
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
static $codes = [ '08000', '08003', '08006', '08001', '08004', '57P01', '57P03', '53300' ];
return in_array( $errno, $codes, true );
}
protected function isQueryTimeoutError( $errno ) {
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
return ( $errno === '57014' );
}
protected function isKnownStatementRollbackError( $errno ) {
return false; // transaction has to be rolled-back from error state
}
public function duplicateTableStructure(
$oldName, $newName, $temporary = false, $fname = __METHOD__
) {
$newNameE = $this->platform->addIdentifierQuotes( $newName );
$oldNameE = $this->platform->addIdentifierQuotes( $oldName );
$temporary = $temporary ? 'TEMPORARY' : '';
$query = new Query(
"CREATE $temporary TABLE $newNameE " .
"(LIKE $oldNameE INCLUDING DEFAULTS INCLUDING INDEXES)",
self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA,
$temporary ? 'CREATE TEMPORARY' : 'CREATE',
// Use a dot to avoid double-prefixing in Database::getTempTableWrites()
'.' . $newName
);
$ret = $this->query( $query, $fname );
if ( !$ret ) {
return $ret;
}
$sql = 'SELECT attname FROM pg_class c'
. ' JOIN pg_namespace n ON (n.oid = c.relnamespace)'
. ' JOIN pg_attribute a ON (a.attrelid = c.oid)'
. ' JOIN pg_attrdef d ON (c.oid=d.adrelid and a.attnum=d.adnum)'
. ' WHERE relkind = \'r\''
. ' AND nspname = ' . $this->addQuotes( $this->getCoreSchema() )
. ' AND relname = ' . $this->addQuotes( $oldName )
. ' AND pg_get_expr(adbin, adrelid) LIKE \'nextval(%\'';
$query = new Query(
$sql,
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, $fname );
$row = $res->fetchObject();
if ( $row ) {
$field = $row->attname;
$newSeq = "{$newName}_{$field}_seq";
$fieldE = $this->platform->addIdentifierQuotes( $field );
$newSeqE = $this->platform->addIdentifierQuotes( $newSeq );
$newSeqQ = $this->addQuotes( $newSeq );
$query = new Query(
"CREATE $temporary SEQUENCE $newSeqE OWNED BY $newNameE.$fieldE",
self::QUERY_CHANGE_SCHEMA,
'CREATE',
// Do not treat this is as a table modification on top of the CREATE above.
null
);
$this->query( $query, $fname );
$query = new Query(
"ALTER TABLE $newNameE ALTER COLUMN $fieldE SET DEFAULT nextval({$newSeqQ}::regclass)",
self::QUERY_CHANGE_SCHEMA,
'ALTER',
// Do not treat this is as a table modification on top of the CREATE above.
null
);
$this->query( $query, $fname );
}
return $ret;
}
public function truncateTable( $table, $fname = __METHOD__ ) {
$sql = "TRUNCATE TABLE " . $this->tableName( $table ) . " RESTART IDENTITY";
$query = new Query( $sql, self::QUERY_CHANGE_SCHEMA, 'TRUNCATE', $table );
$this->query( $query, $fname );
}
/**
* @param string $prefix Only show tables with this prefix, e.g. mw_
* @param string $fname Calling function name
* @return string[]
* @suppress SecurityCheck-SQLInjection array_map not recognized T204911
*/
public function listTables( $prefix = '', $fname = __METHOD__ ) {
$eschemas = implode( ',', array_map( [ $this, 'addQuotes' ], $this->getCoreSchemas() ) );
$query = new Query(
"SELECT DISTINCT tablename FROM pg_tables WHERE schemaname IN ($eschemas)",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$result = $this->query( $query, $fname );
$endArray = [];
foreach ( $result as $table ) {
$vars = get_object_vars( $table );
$table = array_pop( $vars );
if ( $prefix == '' || strpos( $table, $prefix ) === 0 ) {
$endArray[] = $table;
}
}
return $endArray;
}
/**
* Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
* to https://www.php.net/manual/en/ref.pgsql.php
*
* Parsing a postgres array can be a tricky problem, he's my
* take on this, it handles multi-dimensional arrays plus
* escaping using a nasty regexp to determine the limits of each
* data-item.
*
* This should really be handled by PHP PostgreSQL module
*
* @since 1.19
* @param string $text Postgreql array returned in a text form like {a,b}
* @param string[] &$output
* @param int|false $limit
* @param int $offset
* @return string[]
*/
private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
if ( $limit === false ) {
$limit = strlen( $text ) - 1;
$output = [];
}
if ( $text == '{}' ) {
return $output;
}
do {
if ( $text[$offset] != '{' ) {
preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
$text, $match, 0, $offset );
$offset += strlen( $match[0] );
$output[] = ( $match[1][0] != '"'
? $match[1]
: stripcslashes( substr( $match[1], 1, -1 ) ) );
if ( $match[3] == '},' ) {
return $output;
}
} else {
$offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
}
} while ( $limit > $offset );
return $output;
}
public function getSoftwareLink() {
return '[{{int:version-db-postgres-url}} PostgreSQL]';
}
/**
* Return current schema (executes SELECT current_schema())
* Needs transaction
*
* @since 1.19
* @return string Default schema for the current session
*/
public function getCurrentSchema() {
$query = new Query(
"SELECT current_schema()",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
$row = $res->fetchRow();
return $row[0];
}
/**
* Return list of schemas which are accessible without schema name
* This is list does not contain magic keywords like "$user"
* Needs transaction
*
* @see getSearchPath()
* @see setSearchPath()
* @since 1.19
* @return array List of actual schemas for the current session
*/
public function getSchemas() {
$query = new Query(
"SELECT current_schemas(false)",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
$row = $res->fetchRow();
$schemas = [];
/* PHP pgsql support does not support array type, "{a,b}" string is returned */
return $this->pg_array_parse( $row[0], $schemas );
}
/**
* Return search patch for schemas
* This is different from getSchemas() since it contain magic keywords
* (like "$user").
* Needs transaction
*
* @since 1.19
* @return array How to search for table names schemas for the current user
*/
public function getSearchPath() {
$query = new Query(
"SHOW search_path",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SHOW'
);
$res = $this->query( $query, __METHOD__ );
$row = $res->fetchRow();
/* PostgreSQL returns SHOW values as strings */
return explode( ",", $row[0] );
}
/**
* Update search_path, values should already be sanitized
* Values may contain magic keywords like "$user"
* @since 1.19
*
* @param string[] $search_path List of schemas to be searched by default
*/
private function setSearchPath( $search_path ) {
$query = new Query(
"SET search_path = " . implode( ", ", $search_path ),
self::QUERY_CHANGE_TRX,
'SET'
);
$this->query( $query, __METHOD__ );
}
/**
* Determine default schema for the current application
* Adjust this session schema search path if desired schema exists
* and is not already there.
*
* We need to have name of the core schema stored to be able
* to query database metadata.
*
* This will be also called by the installer after the schema is created
*
* @since 1.19
*
* @param string|null $desiredSchema
*/
public function determineCoreSchema( $desiredSchema ) {
if ( $this->trxLevel() ) {
// We do not want the schema selection to change on ROLLBACK or INSERT SELECT.
// See https://www.postgresql.org/docs/8.3/sql-set.html
throw new DBUnexpectedError(
$this,
__METHOD__ . ": a transaction is currently active"
);
}
if ( $this->schemaExists( $desiredSchema ) ) {
if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
$this->platform->setCoreSchema( $desiredSchema );
$this->logger->debug(
"Schema \"" . $desiredSchema . "\" already in the search path\n" );
} else {
// Prepend the desired schema to the search path (T17816)
$search_path = $this->getSearchPath();
array_unshift( $search_path, $this->platform->addIdentifierQuotes( $desiredSchema ) );
$this->setSearchPath( $search_path );
$this->platform->setCoreSchema( $desiredSchema );
$this->logger->debug(
"Schema \"" . $desiredSchema . "\" added to the search path\n" );
}
} else {
$this->platform->setCoreSchema( $this->getCurrentSchema() );
$this->logger->debug(
"Schema \"" . $desiredSchema . "\" not found, using current \"" .
$this->getCoreSchema() . "\"\n" );
}
}
/**
* Return schema name for core application tables
*
* @since 1.19
* @return string Core schema name
*/
public function getCoreSchema() {
return $this->platform->getCoreSchema();
}
/**
* Return schema names for temporary tables and core application tables
*
* @since 1.31
* @return string[] schema names
*/
public function getCoreSchemas() {
if ( $this->tempSchema ) {
return [ $this->tempSchema, $this->getCoreSchema() ];
}
$query = new Query(
"SELECT nspname FROM pg_catalog.pg_namespace n WHERE n.oid = pg_my_temp_schema()",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
$row = $res->fetchObject();
if ( $row ) {
$this->tempSchema = $row->nspname;
return [ $this->tempSchema, $this->getCoreSchema() ];
}
return [ $this->getCoreSchema() ];
}
public function getServerVersion() {
if ( $this->numericVersion === null ) {
// Works on PG 7.4+
$this->numericVersion = pg_version( $this->getBindingHandle() )['server'];
}
return $this->numericVersion;
}
/**
* Query whether a given relation exists (in the given schema, or the
* default mw one if not given)
* @param string $table
* @param array|string $types
* @return bool
*/
private function relationExists( $table, $types ) {
if ( !is_array( $types ) ) {
$types = [ $types ];
}
$schemas = $this->getCoreSchemas();
$components = $this->platform->qualifiedTableComponents( $table );
$etable = $this->addQuotes( end( $components ) );
foreach ( $schemas as $schema ) {
$eschema = $this->addQuotes( $schema );
$sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
. "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
. "AND c.relkind IN ('" . implode( "','", $types ) . "')";
$query = new Query(
$sql,
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
if ( $res && $res->numRows() ) {
return true;
}
}
return false;
}
public function tableExists( $table, $fname = __METHOD__ ) {
return $this->relationExists( $table, [ 'r', 'v' ] );
}
public function sequenceExists( $sequence ) {
return $this->relationExists( $sequence, 'S' );
}
public function constraintExists( $table, $constraint ) {
foreach ( $this->getCoreSchemas() as $schema ) {
$sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
"WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
$this->addQuotes( $schema ),
$this->addQuotes( $table ),
$this->addQuotes( $constraint )
);
$query = new Query(
$sql,
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
if ( $res && $res->numRows() ) {
return true;
}
}
return false;
}
/**
* Query whether a given schema exists. Returns true if it does, false if it doesn't.
* @param string|null $schema
* @return bool
*/
public function schemaExists( $schema ) {
if ( !strlen( $schema ?? '' ) ) {
return false; // short-circuit
}
$query = new Query(
"SELECT 1 FROM pg_catalog.pg_namespace " .
"WHERE nspname = " . $this->addQuotes( $schema ) . " LIMIT 1",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
return ( $res->numRows() > 0 );
}
/**
* Returns true if a given role (i.e. user) exists, false otherwise.
* @param string $roleName
* @return bool
*/
public function roleExists( $roleName ) {
$query = new Query(
"SELECT 1 FROM pg_catalog.pg_roles " .
"WHERE rolname = " . $this->addQuotes( $roleName ) . " LIMIT 1",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
return ( $res->numRows() > 0 );
}
/**
* @param string $table
* @param string $field
* @return PostgresField|null
*/
public function fieldInfo( $table, $field ) {
return PostgresField::fromText( $this, $table, $field );
}
public function encodeBlob( $b ) {
$conn = $this->getBindingHandle();
return new PostgresBlob( pg_escape_bytea( $conn, $b ) );
}
public function decodeBlob( $b ) {
if ( $b instanceof PostgresBlob ) {
$b = $b->fetch();
} elseif ( $b instanceof Blob ) {
return $b->fetch();
}
return pg_unescape_bytea( $b );
}
public function strencode( $s ) {
// Should not be called by us
return pg_escape_string( $this->getBindingHandle(), (string)$s );
}
public function addQuotes( $s ) {
if ( $s instanceof RawSQLValue ) {
return $s->toSql();
}
$conn = $this->getBindingHandle();
if ( $s === null ) {
return 'NULL';
} elseif ( is_bool( $s ) ) {
return (string)intval( $s );
} elseif ( is_int( $s ) ) {
return (string)$s;
} elseif ( $s instanceof Blob ) {
if ( $s instanceof PostgresBlob ) {
$s = $s->fetch();
} else {
$s = pg_escape_bytea( $conn, $s->fetch() );
}
return "'$s'";
}
return "'" . pg_escape_string( $conn, (string)$s ) . "'";
}
public function streamStatementEnd( &$sql, &$newLine ) {
# Allow dollar quoting for function declarations
if ( str_starts_with( $newLine, '$mw$' ) ) {
if ( $this->delimiter ) {
$this->delimiter = false;
} else {
$this->delimiter = ';';
}
}
return parent::streamStatementEnd( $sql, $newLine );
}
public function doLockIsFree( string $lockName, string $method ) {
$query = new Query(
$this->platform->lockIsFreeSQLText( $lockName ),
self::QUERY_CHANGE_LOCKS,
'SELECT'
);
$res = $this->query( $query, $method );
$row = $res->fetchObject();
return (bool)$row->unlocked;
}
public function doLock( string $lockName, string $method, int $timeout ) {
$query = new Query(
$this->platform->lockSQLText( $lockName, $timeout ),
self::QUERY_CHANGE_LOCKS,
'SELECT'
);
$acquired = null;
$loop = new WaitConditionLoop(
function () use ( $query, $method, &$acquired ) {
$res = $this->query( $query, $method );
$row = $res->fetchObject();
if ( $row->acquired !== null ) {
$acquired = (float)$row->acquired;
return WaitConditionLoop::CONDITION_REACHED;
}
return WaitConditionLoop::CONDITION_CONTINUE;
},
$timeout
);
$loop->invoke();
return $acquired;
}
public function doUnlock( string $lockName, string $method ) {
$query = new Query(
$this->platform->unlockSQLText( $lockName ),
self::QUERY_CHANGE_LOCKS,
'SELECT'
);
$result = $this->query( $query, $method );
$row = $result->fetchObject();
return (bool)$row->released;
}
protected function doFlushSession( $fname ) {
$flags = self::QUERY_CHANGE_LOCKS | self::QUERY_NO_RETRY;
// https://www.postgresql.org/docs/9.1/functions-admin.html
$sql = "SELECT pg_advisory_unlock_all()";
$query = new Query( $sql, $flags, 'UNLOCK' );
$qs = $this->executeQuery( $query, __METHOD__, $flags );
if ( $qs->res === false ) {
$this->reportQueryError( $qs->message, $qs->code, $sql, $fname, true );
}
}
public function serverIsReadOnly() {
$query = new Query(
"SHOW default_transaction_read_only",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SHOW'
);
$res = $this->query( $query, __METHOD__ );
$row = $res->fetchObject();
return $row && strtolower( $row->default_transaction_read_only ) === 'on';
}
protected function getInsertIdColumnForUpsert( $table ) {
$column = null;
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
$components = $this->platform->qualifiedTableComponents( $table );
$encTable = $this->addQuotes( end( $components ) );
foreach ( $this->getCoreSchemas() as $schema ) {
$encSchema = $this->addQuotes( $schema );
$query = new Query(
"SELECT column_name,data_type,column_default " .
"FROM information_schema.columns " .
"WHERE table_name = $encTable AND table_schema = $encSchema",
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
'SELECT'
);
$res = $this->query( $query, __METHOD__ );
if ( $res->numRows() ) {
foreach ( $res as $row ) {
if (
$row->column_default !== null &&
str_starts_with( $row->column_default, "nextval(" ) &&
in_array( $row->data_type, [ 'integer', 'bigint' ], true )
) {
$column = $row->column_name;
}
}
break;
}
}
return $column;
}
public static function getAttributes() {
return [ self::ATTR_SCHEMAS_AS_TABLE_GROUPS => true ];
}
}
|