php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70092 pg_field_type() is too slow
Submitted: 2015-07-17 21:16 UTC Modified: 2015-07-25 20:17 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: yohgaki@php.net Assigned: yohgaki (profile)
Status: Closed Package: PostgreSQL related
PHP Version: master-Git-2015-07-17 (Git) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: yohgaki@php.net
New email:
PHP Version: OS:

 

 [2015-07-17 21:16 UTC] yohgaki@php.net
Description:
------------
Somehow pg_field_type() became very slow. Other functions that share the same code pg_field_name()/etc is not affected.



Test script:
---------------
<?php
$db = pg_connect('host=localhost') or die('Cannot connect db');
$res = pg_query('SELECT * FROM test');

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    pg_field_type($res, 1);
}
echo 'pg_field_type(): '.(microtime(true)-$start)."\n";


$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    pg_field_name($res, 1);
}
echo 'pg_field_name(): '.(microtime(true)-$start)."\n";

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    pg_field_size($res, 1);
}
echo 'pg_field_type(): '.(microtime(true)-$start)."\n";






Expected result:
----------------
pg_filed_type() should be as fast as pg_filed_name()/etc

Actual result:
--------------
PHP 7 and master
[yohgaki@dev PHP-master]$ ./php-bin ../t.php
pg_field_type(): 3.1048510074615
pg_field_name(): 0.0023491382598877
pg_field_type(): 0.0014290809631348

PHP 5.6
[yohgaki@dev PHP-master]$ php ../t.php
pg_field_type(): 0.0019080638885498
pg_field_name(): 0.0013489723205566
pg_field_type(): 0.0010430812835693



Patches

pg_field_type (last revision 2015-07-25 14:04 UTC by cmb@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-17 21:17 UTC] yohgaki@php.net
-Assigned To: +Assigned To: yohgaki
 [2015-07-25 12:41 UTC] cmb@php.net
-Status: Assigned +Status: Verified
 [2015-07-25 12:41 UTC] cmb@php.net
For some reason the buffering of the field names in
get_field_name() does not work, and so for each call to
pg_field_type() the else clause ("hash all oids")[1] is executed
again.

[1] <https://github.com/php/php-src/blob/php-7.0.0beta2/ext/pgsql/pgsql.c#L2349>
 [2015-07-25 14:04 UTC] cmb@php.net
The following patch has been added/updated:

Patch Name: pg_field_type
Revision:   1437833083
URL:        https://bugs.php.net/patch-display.php?bug=70092&patch=pg_field_type&revision=1437833083
 [2015-07-25 14:05 UTC] cmb@php.net
-Status: Verified +Status: Analyzed
 [2015-07-25 14:05 UTC] cmb@php.net
Simply setting the length of the smart_string's zend_string to
zero[1] won't work, because that causes the same zend_string to be
used as key of the list hashtable again and again. Instead it
seems that the smart_string's zend_string has to be free (see the
attached patch "pg_field_type").

Yasuo, please have a look at this issue.

[1] <https://github.com/php/php-src/blob/php-7.0.0beta2/ext/pgsql/pgsql.c#L2371>
 [2015-07-25 19:28 UTC] yohgaki@php.net
@cmb

Thanks! I thought it seems freeing is required, too. I'll check the behavior using gdb later.
 [2015-07-25 20:17 UTC] yohgaki@php.net
Simply freed smart_str instead of setting length to 0.

PHP7/master without --enable-debug
[yohgaki@dev PHP-master]$ ./php-bin ../t.php
pg_field_type(): 0.0013771057128906
pg_field_name(): 0.00064301490783691
pg_field_type(): 0.00048398971557617

Fedora22's PHP 5.6
[yohgaki@dev PHP-master]$ php ../t.php
pg_field_type(): 0.0018699169158936
pg_field_name(): 0.0012152194976807
pg_field_type(): 0.0011649131774902
 [2015-07-25 20:29 UTC] yohgaki@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=62515a1dbb6132051d657ce4df7cc5da32b1a039
Log: Fixed Bug #70092 pg_field_type() is too slow
 [2015-07-25 20:29 UTC] yohgaki@php.net
-Status: Analyzed +Status: Closed
 [2015-08-04 20:54 UTC] ab@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=62515a1dbb6132051d657ce4df7cc5da32b1a039
Log: Fixed Bug #70092 pg_field_type() is too slow
 [2016-07-20 11:37 UTC] davey@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=62515a1dbb6132051d657ce4df7cc5da32b1a039
Log: Fixed Bug #70092 pg_field_type() is too slow
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Sep 16 13:00:01 2025 UTC