php / php/php-src

pg_field_type first call is very slow

Open
#9,692 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: pgsql Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

Problem: function pg_field_type first call (after run php script) is very slow.
The following code:

$connection = pg_connect("...") or die('Cannot connect db');
$res = pg_query($connection, 'SELECT 100');

$time0 = microtime(true); pg_field_name($res, 0);
$out[] = '(query 1) pg_field_name() first call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

$time0 = microtime(true); pg_field_size($res, 0);
$out[] = '(query 1) pg_field_size() first call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

$time0 = microtime(true); pg_field_type($res, 0);
$out[] = '(query 1) pg_field_type() first call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

$time0 = microtime(true); pg_field_type($res, 0);
$out[] = '(query 1) pg_field_type() second call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

$res = pg_query($connection, 'SELECT 200');

$time0 = microtime(true); pg_field_type($res, 0);
$out[] = '(query 2) pg_field_type() first call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

$time0 = microtime(true); pg_field_type($res, 0);
$out[] = '(query 2) pg_field_type() second call = '.number_format((microtime(true) - $time0) * 1000, 4).' мс';

print_r($out);
<?php

Resulted in this output:

Array (
    [0] => "(query 1) pg_field_name() first call = 0.0069 мс"
    [1] => "(query 1) pg_field_size() first call = 0.0031 мс"
    [2] => "(query 1) pg_field_type() first call = 46.6821 мс"  // FIRST CALL IS VERY SLOW (pg_field_type)
    [3] => "(query 1) pg_field_type() second call = 0.0050 мс"
    [4] => "(query 2) pg_field_type() first call = 0.0069 мс"
    [5] => "(query 2) pg_field_type() second call = 0.0029 мс"
)

But I expected this output instead:

Array (
    [0] => "(query 1) pg_field_name() first call = 0.0069 мс"
    [1] => "(query 1) pg_field_size() first call = 0.0031 мс"
    [2] => "(query 1) pg_field_type() first call = 0.0050-0.0069 мс"  // MUST BE
    [3] => "(query 1) pg_field_type() second call = 0.0050 мс"
    [4] => "(query 2) pg_field_type() first call = 0.0069 мс"
    [5] => "(query 2) pg_field_type() second call = 0.0029 мс"
)
PHP Version

PHP 8.1

Operating System

Debian 11 (bullseye)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied PHP reproduction using pg_connect(), pg_query(), and pg_field_type() on PHP 8.1 with PostgreSQL, comparing the first and subsequent calls. Trace the PostgreSQL field-type lookup path to identify why the first call takes about 46 ms; done means the first call no longer has the unexplained delay and the observed timing is covered by a regression check.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.