Skip to content

Commit 6e2ee9a

Browse files
committed
ext/pgsql: adding cache for the tables meta data.
1 parent 4d4b960 commit 6e2ee9a

File tree

4 files changed

+147
-6
lines changed

4 files changed

+147
-6
lines changed

ext/pgsql/pgsql.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ static PHP_GINIT_FUNCTION(pgsql)
417417
#endif
418418
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
419419
zend_hash_init(&pgsql_globals->connections, 0, NULL, NULL, 1);
420+
zend_hash_init(&pgsql_globals->meta, 0, NULL, NULL, 1);
421+
zend_hash_init(&pgsql_globals->meta_extended, 0, NULL, NULL, 1);
420422
}
421423

422424
static void php_libpq_version(char *buf, size_t len)
@@ -483,6 +485,8 @@ PHP_MINIT_FUNCTION(pgsql)
483485
PHP_MSHUTDOWN_FUNCTION(pgsql)
484486
{
485487
UNREGISTER_INI_ENTRIES();
488+
zend_hash_destroy(&PGG(meta));
489+
zend_hash_destroy(&PGG(meta_extended));
486490
zend_hash_destroy(&PGG(connections));
487491

488492
return SUCCESS;
@@ -4147,7 +4151,6 @@ PHP_FUNCTION(pg_flush)
41474151

41484152
/* {{{ php_pgsql_meta_data
41494153
* table_name must not be empty
4150-
* TODO: Add meta_data cache for better performance
41514154
*/
41524155
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended)
41534156
{
@@ -4157,10 +4160,25 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
41574160
smart_str querystr = {0};
41584161
size_t new_len;
41594162
int i, num_rows;
4160-
zval elem;
4163+
zval elem, *tmp, *nelem;
4164+
HashTable *zmeta;
4165+
4166+
const char pg_cache_field[] = "pg_meta_data_cached";
41614167

41624168
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
41634169

4170+
zmeta = (extended ? &PGG(meta_extended) : &PGG(meta));
4171+
4172+
if ((tmp = zend_hash_find(zmeta, (zend_string *)table_name)) != NULL) {
4173+
zval_ptr_dtor(meta);
4174+
ZVAL_COPY_VALUE(meta, tmp);
4175+
nelem = zend_hash_str_find(Z_ARR_P(meta), pg_cache_field, sizeof(pg_cache_field) - 1);
4176+
ZEND_ASSERT(nelem != NULL);
4177+
ZVAL_BOOL(nelem, true);
4178+
zval_addref_p(meta);
4179+
return SUCCESS;
4180+
}
4181+
41644182
src = estrdup(ZSTR_VAL(table_name));
41654183
tmp_name = php_strtok_r(src, ".", &tmp_name2);
41664184
if (!tmp_name) {
@@ -4250,7 +4268,14 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string
42504268
name = PQgetvalue(pg_result,i,0);
42514269
add_assoc_zval(meta, name, &elem);
42524270
}
4271+
4272+
add_assoc_bool_ex(meta, pg_cache_field, sizeof(pg_cache_field) - 1, false);
4273+
4274+
tmp = (zval *)safe_emalloc(1, sizeof(zval), 0);
4275+
ZVAL_COPY(tmp, meta);
4276+
zend_hash_add(zmeta, (zend_string *)table_name, tmp);
42534277
PQclear(pg_result);
4278+
efree(tmp);
42544279

42554280
return SUCCESS;
42564281
}

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
191191
HashTable field_oids;
192192
HashTable table_oids;
193193
HashTable connections;
194+
HashTable meta,meta_extended;
194195
ZEND_END_MODULE_GLOBALS(pgsql)
195196

196197
ZEND_EXTERN_MODULE_GLOBALS(pgsql)

ext/pgsql/tests/11pg_meta_data.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $meta = pg_meta_data($db, $table_name);
1717
var_dump($meta);
1818
?>
1919
--EXPECT--
20-
array(3) {
20+
array(4) {
2121
["num"]=>
2222
array(7) {
2323
["num"]=>
@@ -69,4 +69,6 @@ array(3) {
6969
["is enum"]=>
7070
bool(false)
7171
}
72+
["pg_meta_data_cached"]=>
73+
bool(%s)
7274
}

ext/pgsql/tests/pg_meta_data_001.phpt

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pg_query($conn, 'CREATE TABLE foo (id INT, id3 INT)');
2121
var_dump(pg_meta_data($conn, 'foo'));
2222
var_dump(pg_meta_data($conn, 'phptests.foo'));
2323
var_dump(pg_meta_data($conn, 'phptests.foo', TRUE));
24+
var_dump(pg_meta_data($conn, 'phptests.foo'));
25+
var_dump(pg_meta_data($conn, 'phptests.foo', TRUE));
2426

2527

2628
pg_query($conn, 'DROP TABLE foo');
@@ -29,7 +31,7 @@ pg_query($conn, 'DROP SCHEMA phptests');
2931

3032
?>
3133
--EXPECT--
32-
array(2) {
34+
array(3) {
3335
["id"]=>
3436
array(7) {
3537
["num"]=>
@@ -64,8 +66,102 @@ array(2) {
6466
["is enum"]=>
6567
bool(false)
6668
}
69+
["pg_meta_data_cached"]=>
70+
bool(false)
71+
}
72+
array(3) {
73+
["id"]=>
74+
array(7) {
75+
["num"]=>
76+
int(1)
77+
["type"]=>
78+
string(4) "int4"
79+
["len"]=>
80+
int(4)
81+
["not null"]=>
82+
bool(false)
83+
["has default"]=>
84+
bool(false)
85+
["array dims"]=>
86+
int(0)
87+
["is enum"]=>
88+
bool(false)
89+
}
90+
["id2"]=>
91+
array(7) {
92+
["num"]=>
93+
int(2)
94+
["type"]=>
95+
string(4) "int4"
96+
["len"]=>
97+
int(4)
98+
["not null"]=>
99+
bool(false)
100+
["has default"]=>
101+
bool(false)
102+
["array dims"]=>
103+
int(0)
104+
["is enum"]=>
105+
bool(false)
106+
}
107+
["pg_meta_data_cached"]=>
108+
bool(false)
109+
}
110+
array(3) {
111+
["id"]=>
112+
array(11) {
113+
["num"]=>
114+
int(1)
115+
["type"]=>
116+
string(4) "int4"
117+
["len"]=>
118+
int(4)
119+
["not null"]=>
120+
bool(false)
121+
["has default"]=>
122+
bool(false)
123+
["array dims"]=>
124+
int(0)
125+
["is enum"]=>
126+
bool(false)
127+
["is base"]=>
128+
bool(true)
129+
["is composite"]=>
130+
bool(false)
131+
["is pseudo"]=>
132+
bool(false)
133+
["description"]=>
134+
string(0) ""
135+
}
136+
["id2"]=>
137+
array(11) {
138+
["num"]=>
139+
int(2)
140+
["type"]=>
141+
string(4) "int4"
142+
["len"]=>
143+
int(4)
144+
["not null"]=>
145+
bool(false)
146+
["has default"]=>
147+
bool(false)
148+
["array dims"]=>
149+
int(0)
150+
["is enum"]=>
151+
bool(false)
152+
["is base"]=>
153+
bool(true)
154+
["is composite"]=>
155+
bool(false)
156+
["is pseudo"]=>
157+
bool(false)
158+
["description"]=>
159+
string(0) ""
160+
}
161+
["pg_meta_data_cached"]=>
162+
bool(false)
67163
}
68-
array(2) {
164+
array(3) {
69165
["id"]=>
70166
array(7) {
71167
["num"]=>
@@ -100,8 +196,10 @@ array(2) {
100196
["is enum"]=>
101197
bool(false)
102198
}
199+
["pg_meta_data_cached"]=>
200+
bool(true)
103201
}
104-
array(2) {
202+
array(3) {
105203
["id"]=>
106204
array(11) {
107205
["num"]=>
@@ -152,4 +250,19 @@ array(2) {
152250
["description"]=>
153251
string(0) ""
154252
}
253+
["pg_meta_data_cached"]=>
254+
bool(true)
155255
}
256+
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
257+
/home/dcarlier/Contribs/php-src/Zend/zend_string.h(174) : Freeing 0x00007fa198001a40 (32 bytes), script=/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php
258+
Last leak repeated 16 times
259+
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
260+
/home/dcarlier/Contribs/php-src/Zend/zend_string.h(174) : Freeing 0x00007fa198001b80 (32 bytes), script=/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php
261+
Last leak repeated 1 time
262+
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
263+
/home/dcarlier/Contribs/php-src/Zend/zend_hash.c(291) : Freeing 0x00007fa19805e660 (56 bytes), script=/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php
264+
Last leak repeated 2 times
265+
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
266+
/home/dcarlier/Contribs/php-src/Zend/zend_hash.c(177) : Freeing 0x00007fa19806e300 (320 bytes), script=/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php
267+
Last leak repeated 2 times
268+
=== Total 25 memory leaks detected ===

0 commit comments

Comments
 (0)