<?php
private function mapping($q, $result)
    {
        $types = [];
        for ($i = 0, $count = \pg_num_fields($q); $i < $count; ++$i) {
            $types[$i] = \pg_field_type_oid($q, $i);
        }
        foreach ($result as $k => &$row) {
            $i = -1;
            foreach ($row as $name => &$value) {
                ++$i;
                if ($value === null) {
                    continue;
                }
                switch ($types[$i]) {
                    case 20:
                    case 21:
                    case 23: $value = (int)$value; break;
                    case 16507: break;
                    default:
                        throw new \RuntimeException(
                            \pg_field_type($q, $i) .' type. Need mapping ' . \pg_field_type_oid($q, $i)
                        );
                }
            }
        }
        unset($value, $row);
        return $result;
    }
?>