# 内存管理函数
内存管理函数仅9.1.0及以上集群版本支持。
#### pg_shared_chunk_detail(contextname char(64))
描述：查询指定共享内存下内存上下文申请的所有chunk信息。
参数contextname，表示内存上下文名称。
使用该函数需先使用pg_shared_chunk_dump(contextname char(64))函数将相关信息打印成文件。
返回值类型：record
示例：
```
SELECT * FROM pg_shared_chunk_detail('pgstat file hash table');
       parent        | level |  file_name   | line_number | chunk_size | requested_number | total_size 
---------------------+-------+--------------+-------------+------------+------------------+------------
 pgstat file context |     2 | dynahash.cpp |         158 |       2048 |                1 |       2048
 pgstat file context |     2 | dynahash.cpp |         158 |        256 |                1 |        256
 pgstat file context |     2 | dynahash.cpp |         158 |       4096 |                9 |      36864
 pgstat file context |     2 | dynahash.cpp |         158 |       8192 |                4 |      32768
(4 rows)
```
#### pv_session_chunk_detail(tid int, contextname char(64))
描述：查询指定线程创建的某个内存上下文申请的所有chunk信息。
参数tid，表示线程ID；参数contextname，表示内存上下文名称。
使用该函数需使用pv_session_chunk_dump(tid int, contextname char(64))函数将相关信息打印成文件。
返回值类型：record
示例：
```
SELECT * FROM pv_session_chunk_detail(140178810990936, 'Timezones');
      parent      | level |   file_name   | line_number | chunk_size | requested_number | total_size 
------------------+-------+---------------+-------------+------------+------------------+------------
 TopMemoryContext |     1 | dynahash.cpp  |         158 |       1280 |                2 |       2560
 TopMemoryContext |     1 | dynahash.cpp  |         158 |        160 |                1 |        160
 TopMemoryContext |     1 | dynahash.cpp  |         158 |       2560 |                2 |       5120
 TopMemoryContext |     1 | localtime.cpp |        1965 |        128 |                1 |        128
 TopMemoryContext |     1 | localtime.cpp |        1965 |        448 |                1 |        448
(5 rows)
```
#### pg_shared_chunk_dump(contextname char(64))
描述：将指定共享内存下内存上下文申请的所有chunk信息打印成文件。
参数contextname，表示内存上下文名称。
返回值类型：boolean
示例：
```
SELECT * FROM pg_shared_chunk_dump('pgstat file hash table');
 pg_shared_chunk_dump 
----------------------
         t
(1 row)
```
#### pv_session_chunk_dump(tid int, contextname char(64))
描述：将指定线程创建的某个内存上下文申请的所有chunk信息打印成文件。
参数tid，表示线程ID；参数contextname，表示内存上下文名称。
返回值类型：boolean
示例：
```
SELECT * FROM pv_session_chunk_dump(140472797325280, 'Timezones');
 pv_session_chunk_dump 
-----------------------
 t
(1 row)
```
#### gs_print_memory_profiling()
描述：打印内存采集详情。该函数仅9.1.1.100及以上集群版本支持。
返回值类型：boolean
示例：
```
SELECT * FROM gs_print_memory_profiling();
 gs_print_memory_profiling 
----------------------------
 t
(1 row)
```
#### gs_gsc_dbstat_info(database_id bigint DEFAULT NULL::bigint)
描述：查询当前节点全局缓存内存（Global SysCache）的统计信息，例如SysCache缓存命中情况及LRU淘汰情况。该函数仅9.1.1.100及以上集群版本支持。
参数：database_id，表示数据库OID。
- 当database_id为空时，查询所有数据库的统计信息。
- 当database_id为0时，查询共享数据库的统计信息。
- 当database_id为有效数据库OID时，查询共享数据库和该数据库的统计信息。
返回值类型：record
函数返回字段如下：
| 名称               | 类型      | 描述                       |
|:---|:---|:---|
| database_id      | BIGINT   | 数据库OID。                    |
| database_name    | TEXT     | 数据库名称。                    |
| tup_searches     | BIGINT  | 系统表缓存查找次数。                 |
| tup_hits          | BIGINT  | 系统表缓存命中次数。                  |
| tup_miss         | BIGINT   | 系统表缓存未命中触发扫描表的次数。          |
| tup_swapout_count | BIGINT    | 被LRU淘汰的元组的数量。            |
| tup_count         | BIGINT    | 缓存中元组的数量。                |
| tup_dead         | BIGINT     | 垃圾队列中元组的数量。               |
| tup_memory        | BIGINT | 缓存中元组的总内存。                |
| total_memory      | BIGINT  | Global SysCache的总内存。       |
| refcount        | BIGINT   | Global SysCache被线程引用的次数。 |
   
示例：
```
SELECT * FROM gs_gsc_dbstat_info();
 database_id | database_name | tup_searches | tup_hits | tup_miss | tup_swapout_count | tup_count | tup_dead | tup_memory | total_memory | refcount
-------------+---------------+--------------+----------+----------+-------------------+-----------+----------+------------+--------------+----------
           0 |               |        12474 |     9876 |       14 |                 0 |        16 |        0 |      12560 |       697664 |        0
       17185 | postgres      |        10156 |     5521 |     2223 |                 0 |      1398 |        0 |     752296 |      4417856 |       11
(2 rows)
```
#### gs_gsc_clean(database_id bigint DEFAULT NULL::bigint)
描述：清理指定数据库的全局缓存内存（Global SysCache）。该函数仅9.1.1.100及以上集群版本支持。
参数：database_id，表示数据库OID。
- 当database_id为空时，清理所有数据库的内存。
- 当database_id为0时，清理共享数据库的内存。
- 当database_id为有效数据库OID时，清理共享数据库和指定数据库的内存。
返回值类型：bool
示例：
```
SELECT * FROM gs_gsc_clean();
 gs_gsc_clean
--------------
 t
(1 row)
```
#### gs_table_oscache(regclass)
描述：查询表的数据文件在操作系统文件缓存中的缓存情况。该函数仅9.1.1.200及以上集群版本支持。
![](https://support.huaweicloud.com/sqlreference-911-dws/public_sys-resources/caution_3.0-zh-cn.png)
该函数涉及mmap锁操作，不建议频繁使用。
返回值类型：record
函数返回字段如下：
| 名称                    | 类型        | 描述                      |
|:---|:---|:---|
| schema_name            | TEXT     | 模式名称。                 |
| table_name             | TEXT      | 表名。                   |
| file_name             | TEXT       | 数据文件名称。              |
| file_type              | TEXT    | 文件类型。                  |
| seg_count           | INTEGER | 文件段号。                   |
| file_size              | BIGINT    | 文件大小。                |
| cache_size            | BIGINT  | 该文件被缓存的大小。             |
| con_cache_count        | BIGINT   | 连续缓存页的数量。              |
| cache_dirty_size       | BIGINT    | 缓存中脏页的大小。              |
| con_cache_dirty_count | BIGINT  | 连续缓存脏页的数量。            |
| in_fd_cache           | BOOLEAN  | 文件是否在fd缓存中，即文件是否处于打开状态。 |
   
示例：
```
SELECT * FROM gs_table_oscache('employee');
 schema_name | table_name |    file_name     | file_type | seg_count | file_size | cache_size | con_cache_count | cache_dirty_size | con_cache_dirty_count | in_fd_cache
-------------+------------+------------------+-----------+-----------+-----------+------------+-----------------+------------------+-----------------------+-------------
 public      | employee   | base/17253/17103 | RowRel    |         1 |         0 |          0 |               0 |                0 |                     0 | f
(1 row)
```
#### gs_table_freecache(regclass)
描述：清理指定表的数据文件的操作系统文件缓存。该函数无法清理仍在打开状态的数据文件。该函数仅9.1.1.200及以上集群版本支持。
返回值类型：bool
示例：
```
SELECT * FROM gs_table_freecache('employee');
 gs_table_freecache
--------------------
 t
(1 row)
```
#### gs_file_freecache(IN file_path TEXT)
描述：清理指定文件的操作系统文件缓存。该函数可以清理仍在打开状态的文件。该函数仅9.1.1.200及以上集群版本支持。
参数：file_path，表示文件路径。
返回值类型：bool
示例：
```
SELECT * FROM gs_file_freecache('base/17253/17103');
 gs_file_freecache
-------------------
 t
(1 row)
```
