-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/pgsql: add meta_cache per-link metadata caching #21885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
arshidkv12
wants to merge
5
commits into
php:master
Choose a base branch
from
arshidkv12:pgsql-meta-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−2
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bbec1b
ext/pgsql: add meta_cache per-link metadata caching
arshidkv12 884dcbd
ext/pgsql: add meta_cache per-link metadata caching
arshidkv12 5c0e4a9
ext/pgsql: add meta_cache per-link metadata caching
arshidkv12 638e354
ext/pgsql: add meta_cache per-link metadata caching
arshidkv12 0bb7d6d
ext/pgsql: add meta_cache per-link metadata caching
arshidkv12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,6 +194,11 @@ static void pgsql_link_free(pgsql_link_handle *link) | |
| FREE_HASHTABLE(link->notices); | ||
| link->notices = NULL; | ||
| } | ||
| if (link->meta_cache) { | ||
| zend_hash_destroy(link->meta_cache); | ||
| FREE_HASHTABLE(link->meta_cache); | ||
| link->meta_cache = NULL; | ||
| } | ||
| } | ||
|
|
||
| static void pgsql_link_free_obj(zend_object *obj) | ||
|
|
@@ -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; | ||
|
|
@@ -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 */ | ||
|
|
@@ -1182,6 +1189,13 @@ PHP_FUNCTION(pg_query) | |
| php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode"); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -1310,6 +1324,13 @@ PHP_FUNCTION(pg_query_params) | |
| php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode"); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
| 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; | ||
|
|
@@ -1405,6 +1426,11 @@ PHP_FUNCTION(pg_prepare) | |
| php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode"); | ||
| RETURN_FALSE; | ||
| } | ||
| if (link->meta_cache) { | ||
| 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; | ||
|
|
@@ -1493,6 +1519,11 @@ PHP_FUNCTION(pg_execute) | |
| php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode"); | ||
| RETURN_FALSE; | ||
| } | ||
| if (link->meta_cache) { | ||
| 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; | ||
|
|
@@ -1699,6 +1730,25 @@ static zend_string *get_field_name(PGconn *pgsql, Oid oid) | |
| return ret; | ||
| } | ||
|
|
||
| static pgsql_link_handle *pgsql_get_link_from_conn(PGconn *conn) | ||
| { | ||
| zval *pgsql_link; | ||
|
|
||
| if (!conn) { | ||
| return NULL; | ||
| } | ||
|
|
||
| ZEND_HASH_FOREACH_VAL(&PGG(connections), pgsql_link) { | ||
| pgsql_link_handle *link = Z_PGSQL_LINK_P(pgsql_link); | ||
|
|
||
| if (link && link->conn == conn) { | ||
| return link; | ||
| } | ||
| } ZEND_HASH_FOREACH_END(); | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| /* Returns the name of the table field belongs to, or table's oid if oid_only is true */ | ||
| PHP_FUNCTION(pg_field_table) | ||
| { | ||
|
|
@@ -3979,6 +4029,12 @@ PHP_FUNCTION(pg_send_query) | |
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
| zend_hash_destroy(link->meta_cache); | ||
| FREE_HASHTABLE(link->meta_cache); | ||
| link->meta_cache = NULL; | ||
| } | ||
|
|
||
| if (_php_pgsql_link_has_results(pgsql)) { | ||
| php_error_docref(NULL, E_NOTICE, | ||
| "There are results on this connection. Call pg_get_result() until it returns FALSE"); | ||
|
|
@@ -4053,6 +4109,12 @@ PHP_FUNCTION(pg_send_query_params) | |
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
| zend_hash_destroy(link->meta_cache); | ||
| FREE_HASHTABLE(link->meta_cache); | ||
| link->meta_cache = NULL; | ||
| } | ||
|
|
||
| if (_php_pgsql_link_has_results(pgsql)) { | ||
| php_error_docref(NULL, E_NOTICE, | ||
| "There are results on this connection. Call pg_get_result() until it returns FALSE"); | ||
|
|
@@ -4133,6 +4195,12 @@ PHP_FUNCTION(pg_send_prepare) | |
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
| zend_hash_destroy(link->meta_cache); | ||
| FREE_HASHTABLE(link->meta_cache); | ||
| link->meta_cache = NULL; | ||
| } | ||
|
|
||
| if (_php_pgsql_link_has_results(pgsql)) { | ||
| php_error_docref(NULL, E_NOTICE, | ||
| "There are results on this connection. Call pg_get_result() until it returns FALSE"); | ||
|
|
@@ -4208,6 +4276,12 @@ PHP_FUNCTION(pg_send_execute) | |
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (link->meta_cache) { | ||
| zend_hash_destroy(link->meta_cache); | ||
| FREE_HASHTABLE(link->meta_cache); | ||
| link->meta_cache = NULL; | ||
| } | ||
|
|
||
| if (_php_pgsql_link_has_results(pgsql)) { | ||
| php_error_docref(NULL, E_NOTICE, | ||
| "There are results on this connection. Call pg_get_result() until it returns FALSE"); | ||
|
|
@@ -4539,7 +4613,6 @@ PHP_FUNCTION(pg_flush) | |
|
|
||
| /* {{{ php_pgsql_meta_data | ||
| * table_name must not be empty | ||
| * TODO: Add meta_data cache for better performance | ||
| */ | ||
| PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended) | ||
| { | ||
|
|
@@ -4550,6 +4623,7 @@ 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; | ||
|
|
||
| ZEND_ASSERT(ZSTR_LEN(table_name) != 0); | ||
|
|
||
|
|
@@ -4620,14 +4694,28 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string | |
| smart_str_0(&querystr); | ||
| efree(src); | ||
|
|
||
| link = pgsql_get_link_from_conn(pg_link); | ||
|
|
||
| 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; | ||
|
|
@@ -4658,6 +4746,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; | ||
|
|
@@ -6089,6 +6192,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); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.