php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74767 pg_connect connects to another database
Submitted: 2017-06-16 14:38 UTC Modified: 2020-11-21 18:42 UTC
Votes:7
Avg. Score:4.6 ± 0.7
Reproduced:6 of 7 (85.7%)
Same Version:0 (0.0%)
Same OS:5 (83.3%)
From: ivnpro at mail dot ru Assigned:
Status: Not a bug Package: PostgreSQL related
PHP Version: 7.0.20 OS: Debian Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ivnpro at mail dot ru
New email:
PHP Version: OS:

 

 [2017-06-16 14:38 UTC] ivnpro at mail dot ru
Description:
------------
pg_connect gives connection resourse to incorrect database.
php.ini default
postgreSQL 9.5 default

using PGSQL_CONNECT_FORCE_NEW help avoid this bug, but too many connections are opened in production

php-v:
PHP 7.0.20-2~ubuntu14.04.1+deb.sury.org+1 (cli) (built: Jun 14 2017 05:55:23) ( NTS )

Test script:
---------------
<?php

try {
    $count = 0;
    while ($count < 1000) {
        $names = ['test1', 'test2', 'test3', 'test4'];
        $name = $names[mt_rand(0,3)];

        $resource = pg_connect("user=postgres dbname=$name host=db-host port=5432");
        if (is_resource($resource) && pg_dbname($resource) !==  $name) {
            /**
             * got it !!!
             */
            echo "count $count \n";
            echo "resource $resource\n";
            echo "is_resource ".is_resource($resource)."\n";
            echo "pg_dbname " .pg_dbname($resource). "\n";
            echo "name $name\n";
            die(1);
        }
        ++$count;
        usleep(1000);
    }
} catch (Exception $e) {
    echo $e . "\n";
}

Expected result:
----------------
empty output if everything correctly, or exception in case couldn't connect.


Actual result:
--------------
count 26
resource Resource id #23
is_resource 1
pg_dbname test3
name test2

that mean asking for connection to test2, but it gives test3

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-21 18:30 UTC] dev at dcolt dot org
Unable to reproduce in current version on postgresql and php

------------------------

dcolt@jewels ~ $ php -v
PHP 7.4.12 (cli) (built: Nov 17 2020 21:20:02) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.12, Copyright (c), by Zend Technologies
dcolt@jewels ~ $ psql --version
psql (PostgreSQL) 13.1
dcolt@jewels ~ $ php testing.php 
dcolt@jewels ~ $
 [2020-11-21 18:42 UTC] kalle@php.net
-Status: Open +Status: Not a bug
 [2020-11-21 18:42 UTC] kalle@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP --
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 04:01:31 2024 UTC