protected function query_insert_id ( $tableName, $primaryKey = null )
    {
        $sequenceName = $tableName;
        if ($primaryKey) {
            $sequenceName .= "_$primaryKey";
        }
        $sequenceName .= '_seq';

        $sql = "
            SELECT
                CURRVAL('%s')
                ";
        $args = array($sql, $sequenceName);
        $sql = $this->sql_printf($args);
        if (!($res = pg_query($this->conn, $sql))) {
            return 0;
        }
        $val = pg_fetch_row($res, 0);
        if ($val && isset($val[0])) {
            $val = $val[0];
        }

        pg_free_result($res);
        return $val;
    }