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

Conversation

devnexen
Copy link
Member

@devnexen devnexen commented May 6, 2023

No description provided.

@devnexen devnexen force-pushed the psql_meta_data_cache branch 4 times, most recently from 6e2ee9a to bda8f5e Compare May 7, 2023 11:36
@devnexen
Copy link
Member Author

devnexen commented May 10, 2023

cc @Girgias any guidance about how to fix those ?

223- [Sat May  6 22:56:16 2023]  Script:  '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
224- /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
225- Last leak repeated 16 times
226- [Sat May  6 22:56:16 2023]  Script:  '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
227- /home/dcarlier/Contribs/php-src/Zend/zend_string.h(174) :  Freeing 0x00007fa1[98](https://github.com/php/php-src/actions/runs/4906957074/jobs/8761757036?pr=11193#step:8:99)001b80 (32 bytes), script=/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php
228- Last leak repeated 1 time
229- [Sat May  6 22:56:16 2023]  Script:  '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
230- /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
231- Last leak repeated 2 times
232- [Sat May  6 22:56:16 2023]  Script:  '/home/dcarlier/Contribs/php-src/ext/pgsql/tests/pg_meta_data_001.php'
233- /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
234- Last leak repeated 2 times
235- === Total 25 memory leaks detected ===

They are coming from the hash insertions, asan does not report anything but I can reproduce it locally. Cheers


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

Choose a reason for hiding this comment

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

Suggested change
zend_hash_add(zmeta, (zend_string *)table_name, tmp);
zend_hash_add(zmeta, table_name, tmp);

Copy link
Member Author

Choose a reason for hiding this comment

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

in fact locally I opted for the zend_hash_str* api to avoid this.

@@ -4147,7 +4151,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)
Copy link
Member

Choose a reason for hiding this comment

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

First let's drop the const qualifier, as the hash insertion is trying to increase the refcount of the zend_string (and also compute the hash on it).

Suggested change
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)

Copy link
Member Author

Choose a reason for hiding this comment

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

ah right, will look at it later.

@devnexen devnexen force-pushed the psql_meta_data_cache branch from bda8f5e to ed9a106 Compare May 10, 2023 17:21
@devnexen devnexen marked this pull request as ready for review May 10, 2023 19:10
@devnexen devnexen requested a review from iluuu1994 May 10, 2023 19:11

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.

@devnexen devnexen requested a review from kocsismate as a code owner April 15, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants