Skip to content

Commit 243827b

Browse files
committed
Fix GH-13519: another attempt after the faulty fix.
Close GH-14055
1 parent 8e4363d commit 243827b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ PHP NEWS
174174

175175
- PGSQL:
176176
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
177+
. Persistent connections support the PGSQL_CONNECT_FORCE_RENEW flag.
178+
(David Carlier)
177179

178180
- Phar:
179181
. Fixed bug GH-12532 (PharData created from zip has incorrect timestamp).

ext/pgsql/pgsql.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
560560

561561
/* try to find if we already have this link in our persistent list */
562562
if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
563+
newpconn:
563564
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
564565
php_error_docref(NULL, E_WARNING,
565566
"Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
@@ -591,6 +592,18 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
591592
if (le->type != le_plink) {
592593
goto err;
593594
}
595+
if (connect_type & PGSQL_CONNECT_FORCE_NEW) {
596+
PGresult *pg_result;
597+
598+
while ((pg_result = PQgetResult(le->ptr))) {
599+
PQclear(pg_result);
600+
}
601+
PQfinish(le->ptr);
602+
le->ptr = NULL;
603+
PGG(num_links)--;
604+
PGG(num_persistent)--;
605+
goto newpconn;
606+
}
594607
/* ensure that the link did not die */
595608
if (PGG(auto_reset_persistent) & 1) {
596609
/* need to send & get something from backend to

ext/pgsql/tests/gh13519.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-13519 - PGSQL_CONNECT_FORCE_NEW with persistent connections.
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("inc/skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
include 'inc/config.inc';
10+
11+
$db1 = pg_pconnect($conn_str);
12+
$pid1 = pg_get_pid($db1);
13+
for ($i = 0; $i < 3; $i ++) {
14+
$db2 = pg_pconnect($conn_str);
15+
var_dump($pid1 === pg_get_pid($db2));
16+
}
17+
for ($i = 0; $i < 3; $i ++) {
18+
$db2 = pg_pconnect($conn_str, PGSQL_CONNECT_FORCE_NEW);
19+
var_dump($pid1 === pg_get_pid($db2));
20+
pg_close($db2);
21+
}
22+
pg_close($db1);
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(false)
29+
bool(false)
30+
bool(false)

0 commit comments

Comments
 (0)