Skip to content

Commit 884dcbd

Browse files
committed
ext/pgsql: add meta_cache per-link metadata caching
1 parent 3bbec1b commit 884dcbd

1 file changed

Lines changed: 63 additions & 2 deletions

File tree

ext/pgsql/pgsql.c

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ PHP_FUNCTION(pg_query)
11951195
FREE_HASHTABLE(link->meta_cache);
11961196
link->meta_cache = NULL;
11971197
}
1198+
11981199
while ((pgsql_result = PQgetResult(pgsql))) {
11991200
PQclear(pgsql_result);
12001201
leftover = true;
@@ -1323,6 +1324,13 @@ PHP_FUNCTION(pg_query_params)
13231324
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
13241325
RETURN_FALSE;
13251326
}
1327+
1328+
if (link->meta_cache) {
1329+
zend_hash_destroy(link->meta_cache);
1330+
FREE_HASHTABLE(link->meta_cache);
1331+
link->meta_cache = NULL;
1332+
}
1333+
13261334
while ((pgsql_result = PQgetResult(pgsql))) {
13271335
PQclear(pgsql_result);
13281336
leftover = true;
@@ -1418,6 +1426,11 @@ PHP_FUNCTION(pg_prepare)
14181426
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
14191427
RETURN_FALSE;
14201428
}
1429+
if (link->meta_cache) {
1430+
zend_hash_destroy(link->meta_cache);
1431+
FREE_HASHTABLE(link->meta_cache);
1432+
link->meta_cache = NULL;
1433+
}
14211434
while ((pgsql_result = PQgetResult(pgsql))) {
14221435
PQclear(pgsql_result);
14231436
leftover = true;
@@ -1506,6 +1519,11 @@ PHP_FUNCTION(pg_execute)
15061519
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
15071520
RETURN_FALSE;
15081521
}
1522+
if (link->meta_cache) {
1523+
zend_hash_destroy(link->meta_cache);
1524+
FREE_HASHTABLE(link->meta_cache);
1525+
link->meta_cache = NULL;
1526+
}
15091527
while ((pgsql_result = PQgetResult(pgsql))) {
15101528
PQclear(pgsql_result);
15111529
leftover = true;
@@ -1712,6 +1730,25 @@ static zend_string *get_field_name(PGconn *pgsql, Oid oid)
17121730
return ret;
17131731
}
17141732

1733+
static pgsql_link_handle *pgsql_get_link_from_conn(PGconn *conn)
1734+
{
1735+
zval *pgsql_link;
1736+
1737+
if (!conn) {
1738+
return NULL;
1739+
}
1740+
1741+
ZEND_HASH_FOREACH_VAL(&PGG(connections), pgsql_link) {
1742+
pgsql_link_handle *link = Z_PGSQL_LINK_P(pgsql_link);
1743+
1744+
if (link && link->conn == conn) {
1745+
return link;
1746+
}
1747+
} ZEND_HASH_FOREACH_END();
1748+
1749+
return NULL;
1750+
}
1751+
17151752
/* Returns the name of the table field belongs to, or table's oid if oid_only is true */
17161753
PHP_FUNCTION(pg_field_table)
17171754
{
@@ -3992,6 +4029,12 @@ PHP_FUNCTION(pg_send_query)
39924029
RETURN_FALSE;
39934030
}
39944031

4032+
if (link->meta_cache) {
4033+
zend_hash_destroy(link->meta_cache);
4034+
FREE_HASHTABLE(link->meta_cache);
4035+
link->meta_cache = NULL;
4036+
}
4037+
39954038
if (_php_pgsql_link_has_results(pgsql)) {
39964039
php_error_docref(NULL, E_NOTICE,
39974040
"There are results on this connection. Call pg_get_result() until it returns FALSE");
@@ -4066,6 +4109,12 @@ PHP_FUNCTION(pg_send_query_params)
40664109
RETURN_FALSE;
40674110
}
40684111

4112+
if (link->meta_cache) {
4113+
zend_hash_destroy(link->meta_cache);
4114+
FREE_HASHTABLE(link->meta_cache);
4115+
link->meta_cache = NULL;
4116+
}
4117+
40694118
if (_php_pgsql_link_has_results(pgsql)) {
40704119
php_error_docref(NULL, E_NOTICE,
40714120
"There are results on this connection. Call pg_get_result() until it returns FALSE");
@@ -4146,6 +4195,12 @@ PHP_FUNCTION(pg_send_prepare)
41464195
RETURN_FALSE;
41474196
}
41484197

4198+
if (link->meta_cache) {
4199+
zend_hash_destroy(link->meta_cache);
4200+
FREE_HASHTABLE(link->meta_cache);
4201+
link->meta_cache = NULL;
4202+
}
4203+
41494204
if (_php_pgsql_link_has_results(pgsql)) {
41504205
php_error_docref(NULL, E_NOTICE,
41514206
"There are results on this connection. Call pg_get_result() until it returns FALSE");
@@ -4221,6 +4276,12 @@ PHP_FUNCTION(pg_send_execute)
42214276
RETURN_FALSE;
42224277
}
42234278

4279+
if (link->meta_cache) {
4280+
zend_hash_destroy(link->meta_cache);
4281+
FREE_HASHTABLE(link->meta_cache);
4282+
link->meta_cache = NULL;
4283+
}
4284+
42244285
if (_php_pgsql_link_has_results(pgsql)) {
42254286
php_error_docref(NULL, E_NOTICE,
42264287
"There are results on this connection. Call pg_get_result() until it returns FALSE");
@@ -4552,7 +4613,6 @@ PHP_FUNCTION(pg_flush)
45524613

45534614
/* {{{ php_pgsql_meta_data
45544615
* table_name must not be empty
4555-
* TODO: Add meta_data cache for better performance
45564616
*/
45574617
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended)
45584618
{
@@ -4564,7 +4624,6 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
45644624
int i, num_rows, err;
45654625
zval elem;
45664626
pgsql_link_handle *link;
4567-
link = FETCH_DEFAULT_LINK_NO_WARNING();
45684627

45694628
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
45704629

@@ -4635,6 +4694,8 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
46354694
smart_str_0(&querystr);
46364695
efree(src);
46374696

4697+
link = pgsql_get_link_from_conn(pg_link);
4698+
46384699
if (link && link->meta_cache) {
46394700
zval *meta_cache = zend_hash_find(link->meta_cache, querystr.s);
46404701

0 commit comments

Comments
 (0)