Skip to content

ext/pgsql: adding cache for the tables meta data. #11193

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 33 additions & 8 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ static PHP_GINIT_FUNCTION(pgsql)
#endif
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
zend_hash_init(&pgsql_globals->connections, 0, NULL, NULL, 1);
zend_hash_init(&pgsql_globals->meta, 0, NULL, NULL, 1);
zend_hash_init(&pgsql_globals->meta_extended, 0, NULL, NULL, 1);
}

static void php_libpq_version(char *buf, size_t len)
Expand Down Expand Up @@ -483,6 +485,8 @@ PHP_MINIT_FUNCTION(pgsql)
PHP_MSHUTDOWN_FUNCTION(pgsql)
{
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(&PGG(meta));
zend_hash_destroy(&PGG(meta_extended));
zend_hash_destroy(&PGG(connections));

return SUCCESS;
Expand Down Expand Up @@ -4147,20 +4151,34 @@ 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)
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, zend_string *table_name, zval *meta, bool extended)
{
PGresult *pg_result;
char *src, *tmp_name, *tmp_name2 = NULL;
char *escaped;
smart_str querystr = {0};
size_t new_len;
int i, num_rows;
zval elem;
zval elem, *tmp, *nelem;
HashTable *zmeta;

const char pg_cache_field[] = "pg_meta_data_cached";

ZEND_ASSERT(ZSTR_LEN(table_name) != 0);

zmeta = (extended ? &PGG(meta_extended) : &PGG(meta));

if ((tmp = zend_hash_find(zmeta, table_name)) != NULL) {
ZVAL_COPY_VALUE(meta, tmp);
nelem = zend_hash_str_find(Z_ARR_P(meta), pg_cache_field, sizeof(pg_cache_field) - 1);
ZEND_ASSERT(nelem != NULL);
if (Z_TYPE_INFO_P(nelem) == IS_FALSE) {
ZVAL_BOOL(nelem, true);
}
return SUCCESS;
}

src = estrdup(ZSTR_VAL(table_name));
tmp_name = php_strtok_r(src, ".", &tmp_name2);
if (!tmp_name) {
Expand Down Expand Up @@ -4250,7 +4268,14 @@ 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);
}

add_assoc_bool_ex(meta, pg_cache_field, sizeof(pg_cache_field) - 1, false);

tmp = (zval *)safe_emalloc(1, sizeof(zval), 0);
ZVAL_COPY(tmp, meta);
zend_hash_add(zmeta, table_name, tmp);
Copy link
Member

@iluuu1994 iluuu1994 May 11, 2023

Choose a reason for hiding this comment

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

This leaks because meta here is still emalloced. You'd need to copy the entire array to "process-space" recursively. May I suggest making the cache request-bound? This problem goes away, along with potential cache-invalidation issues. Even with request-bound cache I'm wondering, if the table changes during the request (could be a CLI process) we'd need a way to clear the cache.

PQclear(pg_result);
efree(tmp);

return SUCCESS;
}
Expand Down Expand Up @@ -4459,7 +4484,7 @@ static zend_string *php_pgsql_add_quotes(zend_string *src)
/* {{{ php_pgsql_convert
* check and convert array values (fieldname=>value pair) for sql
*/
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt)
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, zend_string *table_name, const zval *values, zval *result, zend_ulong opt)
{
zend_string *field = NULL;
zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
Expand Down Expand Up @@ -5198,7 +5223,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const z
/* }}} */

/* {{{ php_pgsql_insert */
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *var_array, zend_ulong opt, zend_string **sql)
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, zend_string *table, zval *var_array, zend_ulong opt, zend_string **sql)
{
zval *val, converted;
char buf[256];
Expand Down Expand Up @@ -5466,7 +5491,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
/* }}} */

/* {{{ php_pgsql_update */
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql)
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, zend_string *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql)
{
zval var_converted, ids_converted;
smart_str querystr = {0};
Expand Down Expand Up @@ -5577,7 +5602,7 @@ PHP_FUNCTION(pg_update)
/* }}} */

/* {{{ php_pgsql_delete */
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids_array, zend_ulong opt, zend_string **sql)
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, zend_string *table, zval *ids_array, zend_ulong opt, zend_string **sql)
{
zval ids_converted;
smart_str querystr = {0};
Expand Down Expand Up @@ -5715,7 +5740,7 @@ PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array,
/* }}} */

/* {{{ php_pgsql_select */
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids_array, zval *ret_array, zend_ulong opt, long result_type, zend_string **sql)
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, zend_string *table, zval *ids_array, zval *ret_array, zend_ulong opt, long result_type, zend_string **sql)
{
zval ids_converted;
smart_str querystr = {0};
Expand Down
13 changes: 7 additions & 6 deletions ext/pgsql/php_pgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ PHP_MINFO_FUNCTION(pgsql);
#define PGSQL_DML_ESCAPE (1<<12) /* No convert, but escape only */

/* exported functions */
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended);
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt);
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *values, zend_ulong opt, zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids, zend_ulong opt, zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, zend_string *table_name, zval *meta, bool extended);
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, zend_string *table_name, const zval *values, zval *result, zend_ulong opt);
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, zend_string *table, zval *values, zend_ulong opt, zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, zend_string *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, zend_string *table, zval *ids, zend_ulong opt, zend_string **sql);
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, zend_string *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);

/* internal functions */
Expand Down Expand Up @@ -191,6 +191,7 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
HashTable field_oids;
HashTable table_oids;
HashTable connections;
HashTable meta,meta_extended;
ZEND_END_MODULE_GLOBALS(pgsql)

ZEND_EXTERN_MODULE_GLOBALS(pgsql)
Expand Down
6 changes: 4 additions & 2 deletions ext/pgsql/tests/11pg_meta_data.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ $meta = pg_meta_data($db, $table_name);

var_dump($meta);
?>
--EXPECT--
array(3) {
--EXPECTF--
array(4) {
["num"]=>
array(7) {
["num"]=>
Expand Down Expand Up @@ -69,4 +69,6 @@ array(3) {
["is enum"]=>
bool(false)
}
["pg_meta_data_cached"]=>
bool(%s)
}
119 changes: 116 additions & 3 deletions ext/pgsql/tests/pg_meta_data_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pg_query($conn, 'CREATE TABLE foo (id INT, id3 INT)');
var_dump(pg_meta_data($conn, 'foo'));
var_dump(pg_meta_data($conn, 'phptests.foo'));
var_dump(pg_meta_data($conn, 'phptests.foo', TRUE));
var_dump(pg_meta_data($conn, 'phptests.foo'));
var_dump(pg_meta_data($conn, 'phptests.foo', TRUE));


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

?>
--EXPECT--
array(2) {
array(3) {
["id"]=>
array(7) {
["num"]=>
Expand Down Expand Up @@ -64,8 +66,102 @@ array(2) {
["is enum"]=>
bool(false)
}
["pg_meta_data_cached"]=>
bool(false)
}
array(3) {
["id"]=>
array(7) {
["num"]=>
int(1)
["type"]=>
string(4) "int4"
["len"]=>
int(4)
["not null"]=>
bool(false)
["has default"]=>
bool(false)
["array dims"]=>
int(0)
["is enum"]=>
bool(false)
}
["id2"]=>
array(7) {
["num"]=>
int(2)
["type"]=>
string(4) "int4"
["len"]=>
int(4)
["not null"]=>
bool(false)
["has default"]=>
bool(false)
["array dims"]=>
int(0)
["is enum"]=>
bool(false)
}
["pg_meta_data_cached"]=>
bool(false)
}
array(3) {
["id"]=>
array(11) {
["num"]=>
int(1)
["type"]=>
string(4) "int4"
["len"]=>
int(4)
["not null"]=>
bool(false)
["has default"]=>
bool(false)
["array dims"]=>
int(0)
["is enum"]=>
bool(false)
["is base"]=>
bool(true)
["is composite"]=>
bool(false)
["is pseudo"]=>
bool(false)
["description"]=>
string(0) ""
}
["id2"]=>
array(11) {
["num"]=>
int(2)
["type"]=>
string(4) "int4"
["len"]=>
int(4)
["not null"]=>
bool(false)
["has default"]=>
bool(false)
["array dims"]=>
int(0)
["is enum"]=>
bool(false)
["is base"]=>
bool(true)
["is composite"]=>
bool(false)
["is pseudo"]=>
bool(false)
["description"]=>
string(0) ""
}
["pg_meta_data_cached"]=>
bool(false)
}
array(2) {
array(3) {
["id"]=>
array(7) {
["num"]=>
Expand Down Expand Up @@ -100,8 +196,10 @@ array(2) {
["is enum"]=>
bool(false)
}
["pg_meta_data_cached"]=>
bool(true)
}
array(2) {
array(3) {
["id"]=>
array(11) {
["num"]=>
Expand Down Expand Up @@ -152,4 +250,19 @@ array(2) {
["description"]=>
string(0) ""
}
["pg_meta_data_cached"]=>
bool(true)
}
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
/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
Last leak repeated 16 times
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
/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
Last leak repeated 1 time
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
/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
Last leak repeated 2 times
[Sat May 6 22:56:16 2023] Script: '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
/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
Last leak repeated 2 times
=== Total 25 memory leaks detected ===