Skip to content

Commit 5c0e4a9

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

1 file changed

Lines changed: 27 additions & 51 deletions

File tree

ext/pgsql/pgsql.c

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ static zend_function *pgsql_link_get_constructor(zend_object *object) {
171171
return NULL;
172172
}
173173

174+
static inline void pgsql_meta_cache_destroy(HashTable **cache)
175+
{
176+
if (*cache) {
177+
zend_hash_destroy(*cache);
178+
FREE_HASHTABLE(*cache);
179+
*cache = NULL;
180+
}
181+
}
182+
174183
static void pgsql_link_free(pgsql_link_handle *link)
175184
{
176185
PGresult *res;
@@ -194,11 +203,9 @@ static void pgsql_link_free(pgsql_link_handle *link)
194203
FREE_HASHTABLE(link->notices);
195204
link->notices = NULL;
196205
}
197-
if (link->meta_cache) {
198-
zend_hash_destroy(link->meta_cache);
199-
FREE_HASHTABLE(link->meta_cache);
200-
link->meta_cache = NULL;
201-
}
206+
207+
pgsql_meta_cache_destroy(&link->meta_cache);
208+
202209
}
203210

204211
static void pgsql_link_free_obj(zend_object *obj)
@@ -1190,11 +1197,7 @@ PHP_FUNCTION(pg_query)
11901197
RETURN_FALSE;
11911198
}
11921199

1193-
if (link->meta_cache) {
1194-
zend_hash_destroy(link->meta_cache);
1195-
FREE_HASHTABLE(link->meta_cache);
1196-
link->meta_cache = NULL;
1197-
}
1200+
pgsql_meta_cache_destroy(&link->meta_cache);
11981201

11991202
while ((pgsql_result = PQgetResult(pgsql))) {
12001203
PQclear(pgsql_result);
@@ -1325,11 +1328,7 @@ PHP_FUNCTION(pg_query_params)
13251328
RETURN_FALSE;
13261329
}
13271330

1328-
if (link->meta_cache) {
1329-
zend_hash_destroy(link->meta_cache);
1330-
FREE_HASHTABLE(link->meta_cache);
1331-
link->meta_cache = NULL;
1332-
}
1331+
pgsql_meta_cache_destroy(&link->meta_cache);
13331332

13341333
while ((pgsql_result = PQgetResult(pgsql))) {
13351334
PQclear(pgsql_result);
@@ -1426,11 +1425,9 @@ PHP_FUNCTION(pg_prepare)
14261425
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
14271426
RETURN_FALSE;
14281427
}
1429-
if (link->meta_cache) {
1430-
zend_hash_destroy(link->meta_cache);
1431-
FREE_HASHTABLE(link->meta_cache);
1432-
link->meta_cache = NULL;
1433-
}
1428+
1429+
pgsql_meta_cache_destroy(&link->meta_cache);
1430+
14341431
while ((pgsql_result = PQgetResult(pgsql))) {
14351432
PQclear(pgsql_result);
14361433
leftover = true;
@@ -1519,11 +1516,9 @@ PHP_FUNCTION(pg_execute)
15191516
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
15201517
RETURN_FALSE;
15211518
}
1522-
if (link->meta_cache) {
1523-
zend_hash_destroy(link->meta_cache);
1524-
FREE_HASHTABLE(link->meta_cache);
1525-
link->meta_cache = NULL;
1526-
}
1519+
1520+
pgsql_meta_cache_destroy(&link->meta_cache);
1521+
15271522
while ((pgsql_result = PQgetResult(pgsql))) {
15281523
PQclear(pgsql_result);
15291524
leftover = true;
@@ -4029,11 +4024,7 @@ PHP_FUNCTION(pg_send_query)
40294024
RETURN_FALSE;
40304025
}
40314026

4032-
if (link->meta_cache) {
4033-
zend_hash_destroy(link->meta_cache);
4034-
FREE_HASHTABLE(link->meta_cache);
4035-
link->meta_cache = NULL;
4036-
}
4027+
pgsql_meta_cache_destroy(&link->meta_cache);
40374028

40384029
if (_php_pgsql_link_has_results(pgsql)) {
40394030
php_error_docref(NULL, E_NOTICE,
@@ -4108,12 +4099,8 @@ PHP_FUNCTION(pg_send_query_params)
41084099
php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
41094100
RETURN_FALSE;
41104101
}
4111-
4112-
if (link->meta_cache) {
4113-
zend_hash_destroy(link->meta_cache);
4114-
FREE_HASHTABLE(link->meta_cache);
4115-
link->meta_cache = NULL;
4116-
}
4102+
4103+
pgsql_meta_cache_destroy(&link->meta_cache);
41174104

41184105
if (_php_pgsql_link_has_results(pgsql)) {
41194106
php_error_docref(NULL, E_NOTICE,
@@ -4195,11 +4182,7 @@ PHP_FUNCTION(pg_send_prepare)
41954182
RETURN_FALSE;
41964183
}
41974184

4198-
if (link->meta_cache) {
4199-
zend_hash_destroy(link->meta_cache);
4200-
FREE_HASHTABLE(link->meta_cache);
4201-
link->meta_cache = NULL;
4202-
}
4185+
pgsql_meta_cache_destroy(&link->meta_cache);
42034186

42044187
if (_php_pgsql_link_has_results(pgsql)) {
42054188
php_error_docref(NULL, E_NOTICE,
@@ -4276,11 +4259,7 @@ PHP_FUNCTION(pg_send_execute)
42764259
RETURN_FALSE;
42774260
}
42784261

4279-
if (link->meta_cache) {
4280-
zend_hash_destroy(link->meta_cache);
4281-
FREE_HASHTABLE(link->meta_cache);
4282-
link->meta_cache = NULL;
4283-
}
4262+
pgsql_meta_cache_destroy(&link->meta_cache);
42844263

42854264
if (_php_pgsql_link_has_results(pgsql)) {
42864265
php_error_docref(NULL, E_NOTICE,
@@ -6186,17 +6165,14 @@ PHP_FUNCTION(pg_delete)
61866165
CHECK_PGSQL_LINK(link);
61876166
pg_link = link->conn;
61886167

6168+
pgsql_meta_cache_destroy(&link->meta_cache);
6169+
61896170
if (php_pgsql_flush_query(pg_link)) {
61906171
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
61916172
}
61926173
if (php_pgsql_delete(pg_link, table, ids, option, &sql) == FAILURE) {
61936174
RETURN_FALSE;
61946175
}
6195-
if (link->meta_cache) {
6196-
zend_hash_destroy(link->meta_cache);
6197-
FREE_HASHTABLE(link->meta_cache);
6198-
link->meta_cache = NULL;
6199-
}
62006176
if (option & PGSQL_DML_STRING) {
62016177
RETURN_STR(sql);
62026178
}

0 commit comments

Comments
 (0)