Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ static void pgsql_link_free(pgsql_link_handle *link)
FREE_HASHTABLE(link->notices);
link->notices = NULL;
}
if (link->meta_cache) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty repetitive. Could be a helper.

zend_hash_destroy(link->meta_cache);
FREE_HASHTABLE(link->meta_cache);
link->meta_cache = NULL;
}
}

static void pgsql_link_free_obj(zend_object *obj)
Expand Down Expand Up @@ -765,6 +770,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->meta_cache = NULL;
link->persistent = 1;
} else { /* Non persistent connection */
zval *index_ptr;
Expand Down Expand Up @@ -816,6 +822,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
link->conn = pgsql;
link->hash = zend_string_copy(str.s);
link->notices = NULL;
link->meta_cache = NULL;
link->persistent = 0;

/* add it to the hash */
Expand Down Expand Up @@ -1182,6 +1189,12 @@ PHP_FUNCTION(pg_query)
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
RETURN_FALSE;
}

if (link->meta_cache) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalidation is incomplete. Wiping on pg_query() only — pg_query_params, pg_execute, pg_prepare, and the pg_send_* family all leave stale entries

zend_hash_destroy(link->meta_cache);
FREE_HASHTABLE(link->meta_cache);
link->meta_cache = NULL;
}
while ((pgsql_result = PQgetResult(pgsql))) {
PQclear(pgsql_result);
leftover = true;
Expand Down Expand Up @@ -4550,6 +4563,8 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
size_t new_len, len;
int i, num_rows, err;
zval elem;
pgsql_link_handle *link;
link = FETCH_DEFAULT_LINK_NO_WARNING();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FETCH_DEFAULT_LINK_NO_WARNING() in php_pgsql_meta_data is wrong. The function gets PGconn *pg_link but the cache is looked up against the default link. Those don't match with multiple connections, and OO callers (no default link set) get no caching at all. The pgsql_link_handle * needs to be plumbed through.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. How can I obtain a pgsql_link_handle *link from a PGconn *pgsql? I believe we cannot replace pgsql_link_handle with PGconn, as it would break the API. How can this be resolved?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't ; plumb the handle through instead. The usual way is to keep the existing PHP_PGSQL_API symbol for ABI and introduce an internal variant taking the link directly. In-tree callers all have it on hand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you


ZEND_ASSERT(ZSTR_LEN(table_name) != 0);

Expand Down Expand Up @@ -4620,14 +4635,26 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
smart_str_0(&querystr);
efree(src);

if (link && link->meta_cache) {
zval *meta_cache = zend_hash_find(link->meta_cache, querystr.s);

if (meta_cache) {
if (Z_TYPE_P(meta) != IS_UNDEF) {
zval_ptr_dtor(meta);
}
ZVAL_COPY(meta, meta_cache);
smart_str_free(&querystr);
return SUCCESS;
}
}

pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", ZSTR_VAL(table_name));
smart_str_free(&querystr);
PQclear(pg_result);
return FAILURE;
}
smart_str_free(&querystr);

for (i = 0; i < num_rows; i++) {
char *name;
Expand Down Expand Up @@ -4658,6 +4685,21 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
name = PQgetvalue(pg_result,i,0);
add_assoc_zval(meta, name, &elem);
}

if(link) {
if (!link->meta_cache) {
ALLOC_HASHTABLE(link->meta_cache);
zend_hash_init(link->meta_cache, 8, NULL, ZVAL_PTR_DTOR, 0);
}

zval meta_copy;
ZVAL_COPY(&meta_copy, meta);
zend_string *key = zend_string_copy(querystr.s);

zend_hash_update( link->meta_cache, key, &meta_copy);
zend_string_release(key);
}
smart_str_free(&querystr);
PQclear(pg_result);

return SUCCESS;
Expand Down Expand Up @@ -6089,6 +6131,11 @@ PHP_FUNCTION(pg_delete)
if (php_pgsql_delete(pg_link, table, ids, option, &sql) == FAILURE) {
RETURN_FALSE;
}
if (link->meta_cache) {
zend_hash_destroy(link->meta_cache);
FREE_HASHTABLE(link->meta_cache);
link->meta_cache = NULL;
}
if (option & PGSQL_DML_STRING) {
RETURN_STR(sql);
}
Expand Down
1 change: 1 addition & 0 deletions ext/pgsql/php_pgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ typedef struct pgsql_link_handle {
PGconn *conn;
zend_string *hash;
HashTable *notices;
HashTable *meta_cache;
bool persistent;
zend_object std;
} pgsql_link_handle;
Expand Down
Loading