--- Begin Message ---




all the time you use pg_exec you have to put the result of this function
into a 'PGresult *' and after the execution you have clear the pointer.
In big trafic your apache can take more memory. We can save more 1k per query !!!
there is the current code of pg_exec

(L.714, file : ext/pgsql.c)

 PHP_FUNCTION(pg_exec)
{
    zval **query, **pgsql_link = NULL;
    int id = -1;
    PGconn *pgsql;
    PGresult *pgsql_result;
    ExecStatusType status;
    pgsql_result_handle *pg_result;
[...]
    convert_to_string_ex(query);
    pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query)); /* <- well with have free that buffer */

    if (pgsql_result) {
        status = PQresultStatus(pgsql_result);
    } else {
        status = (ExecStatusType) PQstatus(pgsql);
    }
[...]
    switch (status) {
        case PGRES_EMPTY_QUERY:
        case PGRES_BAD_RESPONSE:
        case PGRES_NONFATAL_ERROR:
        case PGRES_FATAL_ERROR:
            php_error(E_WARNING, "PostgreSQL query failed:  %s", PQerrorMessage(pgsql));
            RETURN_FALSE;
            break;
        case PGRES_COMMAND_OK: /* well the query has be excuted, and the pg_result is in memory, but we don't need it */
        default:
[...]

/*------------------------------Fixed code---------------------------------*/

/* {{{ proto int pg_exec([int connection,] string query)
   Execute a query */
PHP_FUNCTION(pg_exec)
{
    zval **query, **pgsql_link = NULL;
    int id = -1;
    PGconn *pgsql;
    PGresult *pgsql_result;
    ExecStatusType status;
    pgsql_result_handle *pg_result;

    switch(ZEND_NUM_ARGS()) {
        case 1:
            if (zend_get_parameters_ex(1, &query)==FAILURE) {
                RETURN_FALSE;
            }
            id = PGG(default_link);
            CHECK_DEFAULT_LINK(id);
            break;
        case 2:
            if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) {
                RETURN_FALSE;
            }
            break;
        default:
            WRONG_PARAM_COUNT;
            break;
    }

    ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);

    convert_to_string_ex(query);
    pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query));

    if (pgsql_result) {
        status = PQresultStatus(pgsql_result);
    } else {
        status = (ExecStatusType) PQstatus(pgsql);
    }
    switch (status) {
        case PGRES_EMPTY_QUERY:
        case PGRES_BAD_RESPONSE:
        case PGRES_NONFATAL_ERROR:
        case PGRES_FATAL_ERROR:
            php_error(E_WARNING, "PostgreSQL query failed:  %s", PQerrorMessage(pgsql));
            RETURN_FALSE;
            break;
        case PGRES_COMMAND_OK:
            PQclear(pgsql_result);      /* vacum the current execution and save 1k ! :) */
            /* successful command that did not return rows */
        default:
            if (pgsql_result) {
                pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
                pg_result->conn = pgsql;
                pg_result->result = pgsql_result;
                pg_result->row = -1;
                ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
                /*
                return_value->value.lval = zend_list_insert(pg_result,le_result);
                return_value->type = IS_LONG;
                */
            } else {
                RETURN_FALSE;
            }
            break;
    }
}
/* }}} */

--- End Message ---
-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to