lyp-bobi opened a new issue, #2046:
URL: https://github.com/apache/age/issues/2046

   **Describe the bug**
   A clear and concise description of what the bug is.
   
   I found this bug when I try to build a BTree index on a vertex table and got 
a OOM. Lucky I'm able to reproduce it and locate the bug.
   
   The memory leak is during BTree building, when calling 
`compare_agtype_containers_orderability` function. Inside the function, it 
calls `agtype_iterator_next` and  then`fill_agtype_value` to grab the value of 
an iterater into a agtype_value variable. And it calls `pnstrdup`, which 
pallocs memory. 
   
   The palloced memory is not released in the 
`compare_agtype_containers_orderability`, which result in OOMs.
   
   **How are you accessing AGE (Command line, driver, etc.)?**
   - [e.g. JDBC]
   psql
   
   **What data setup do we need to do?**
   nope
   
   **What is the necessary configuration info needed?**
   - [e.g. Installed PostGIS]
   nope
   
   **What is the command that caused the error?**
   
   It is easy to reproduce this problem. Just build a btree on a large agtype 
column.
   
   ```
   CREATE TABLE test(num agtype);
   INSERT INTO test SELECT ('{"id": "'||a||'"}')::agtype FROM 
generate_series(1,100000000) a;
   CREATE INDEX ON test USING btree(num);
   ```
   
   And it will fastly take ~100G memory.
   
   **Expected behavior**
   A clear and concise description of what you expected to happen.
   
   **Environment (please complete the following information):**
   - Version: [e.g. 0.4.0]
   
   **Additional context**
   Add any other context about the problem here.
   
   I can fix this problem by modifying `agtype_utils.c` in the following steps:
   
   1. I pfree the agtype_value before exit
   ```
   switch (va.type)
   {
       case AGTV_STRING:
       case AGTV_NULL:
       case AGTV_NUMERIC:
       case AGTV_BOOL:
       case AGTV_INTEGER:
       case AGTV_FLOAT:
       case AGTV_EDGE:
       case AGTV_VERTEX:
       case AGTV_PATH:
           res = compare_agtype_scalar_values(&va, &vb);
           pfree_agtype_value_content(&va);
           pfree_agtype_value_content(&vb);
           break;
   ...
   ...
   ```
   
   2. I ask the `pfree_agtype_value_content` to pfree the duplicated string
   
   ```
   switch (value->type)
   {
       case AGTV_NUMERIC:
           pfree(value->val.numeric);
           break;
   
       case AGTV_STRING:
           /*
            * The char pointer (val.string.val) is not free'd because
            * it is not allocated by an agtype helper function.
            */
           pfree(value->val.string.val);
           break;
   ...
   ...
   ```
   
   And then the memory leak is solved. However, It causes other problems. For 
example, in `create_agtype_from_list`, it use `key_agtype = 
string_to_agtype_value("__id__");`, and the string cannot be pfreed.
   
   According to the code, i think the numeric type also have the same problem. 
I think maybe we have to create a new function called `unfill_agtype_value` to 
pfree the memory allocated for string and numeric type. But I'm not sure 
whether it have other side effects...
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@age.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to