统计信息函数
pg_stat_get_activity(integer)
描述:返回一个关于带有特殊PID的后台进程的记录信息,当参数为NULL时,则返回每个活动的后台进程的记录。返回结果是PG_STAT_ACTIVITY视图中的一个子集,不包含connection_info列。
返回值类型:setof record
gs_stack()
描述:获取CN或DN进程的堆栈信息,函数入参actor_name、tid均需要通过视图或其他函数进行获取,不支持lwtid。该函数仅25.5.0及以上集群版本支持。
gs_stack()提供了比gdb、gstack等命令更灵活、更快速的堆栈获得手段。仅建议管理员用户在问题分析定位过程中使用,不建议作为日常的监控工具使用。
- actor_name:线程所在的actor名称。
- tid:线程ID。
- lwtid:轻量级线程ID。
- stack:tid或lwtid线程对应的堆栈信息。
gs_stack()函数有三种使用方式:
- 方式一:gs_stack()函数入参为空,表示打印CN和DN所有线程的堆栈。
获取CN和所有DN上所有线程的运行堆栈,运行时间长,对正常业务运行有影响,不建议频繁使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
select * from gs_stack(); actor_name | tid | lwtid | stack --------------+-----------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------- coordinator1 | 106927505866816 | 186762 | GsStackBacktrace(int) + 0x1a + | | | 0x7ffb45f6d1f0 + | | | __poll + 0x4f + | | | poll + 0xa0 + | | | poll(pollfd*, unsigned long, int, void (*)()) + 0x19e + | | | ServerLoop() + 0x570 + | | | PmStartupThreads() + 0x167 + | | | PostmasterMain(int, char**) + 0x22f | | | coordinator1 | 106927505998400 | 186773 | GsStackBacktrace(int) + 0x1a + | | | 0x7ffb45f6d1f0 + | | | 0x7ffb45fb41aa + | | | pthread_cond_clockwait + 0x1e2 + | | | std::__condvar::wait_until(std::mutex&, int, timespec&) + 0x39
- 方式二:gs_stack(actor_name)函数入参值为指定的actor_name,表示打印名称为actor_name的actor上所有线程的堆栈。
- actor_name必须为CN或DN的名字,否则会报错“invalid actor_name”。
- actor_name需通过其他视图或函数获取(例如函数pgxc_query_wait_status)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
select * from gs_stack('coordinator1'); actor_name | tid | lwtid | stack --------------+-----------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------- coordinator1 | 106927505866816 | 186762 | GsStackBacktrace(int) + 0x1a + | | | 0x7ffb45f6d1f0 + | | | __poll + 0x4f + | | | poll + 0xa0 + | | | poll(pollfd*, unsigned long, int, void (*)()) + 0x19e + | | | ServerLoop() + 0x570 + | | | PmStartupThreads() + 0x167 + | | | PostmasterMain(int, char**) + 0x22f
- 方式三:gs_stack(actor_name,tid)函数入参值为指定的actor_name和tid,表示打印名称为actor_name的actor上指定tid线程的堆栈。
- actor_name必须为CN或DN的名字,否则会报错“invalid actorName”。
- actor_name和tid需通过其他视图或函数获取(例如函数pgxc_query_wait_status)。
- 获取指定CN或DN进程内指定线程的运行堆栈,运行时间短,对正常业务运行无影响,推荐使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
select * from gs_stack('coordinator1',106927505998400); actor_name | tid | lwtid | stack --------------+-----------------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------- coordinator1 | 106927505998400 | 186773 | GsStackBacktrace(int) + 0x1a + | | | 0x7ffb45f6d1f0 + | | | 0x7ffb45fb41aa + | | | pthread_cond_clockwait + 0x1e2 + | | | std::__condvar::wait_until(std::mutex&, int, timespec&) + 0x39 + | | (1 row)
gs_wlm_session_statistics()
描述:获取执行中SQL语句的监控信息,无法获取执行结束的SQL语句监控信息,返回结果每一行代表一个语句。
函数返回字段:
| 字段名称 | 类型 | 描述 |
|---|---|---|
| session_id | text | 语句的Session ID。 |
| statement_id | text | 语句ID。 |
| start_time | timestamp with time zone | 语句起始时间。 |
| duration | bigint | 语句运行时间。 单位:ms |
| estimate_total_time | bigint | 估计的语句运行时长。 单位:ms |
| estimate_left_time | bigint | 估计语句剩余运行时长 单位:ms |
| cn_actor_info | text | CN的Serverless信息。 |
| dn_actor_info | text | DN的Serverless信息。 |
| startup_coordinator_duration | bigint | 启动CN时长。 单位:ms |
| create_session_duration | bigint | 创建Session时长。 单位:ms |
| enqueue_statement_duration | bigint | 语句加入任务队列时长。 单位:ms |
| queue_duration | bigint | 语句在任务队列中排队时长。 单位:ms |
| write_resultset_duration | bigint | 语句写结果集时长。 单位:ms |
| queryid | bigint | Debug Query的ID。 |
| schemaname | text | 语句执行时的Schema名称。 |
| query | text | 语句。 |
| query_plan | text | explain信息 |
| tid | bigint | 线程ID。 |
| parse_time | bigint | 从解析到执行开始之前的时间。 单位:ms |
| estimate_memory | int | 语句估计内存。 单位:MB |
| min_dn_time | bigint | 最小DN时长。 单位:ms |
| max_dn_time | bigint | 最大DN时长。 单位:ms |
| average_dn_time | bigint | 平均DN时长。 单位:ms |
| dn_time_skew_percent | int | DN时长偏斜百分比。 单位:% |
| dn_min_cpu_time | bigint | 最小DN CPU时长。 单位:ms |
| dn_max_cpu_time | bigint | 最大DN CPU时长。 单位:ms |
| dn_average_cpu_time | bigint | 平均DN CPU时长。 单位:ms |
| dn_total_cpu_time | bigint | DN CPU总时长。 单位:ms |
| dn_cpu_skew_percent | int | DN CPU时长偏斜百分比。 单位:% |
| dn_min_peak_memory | int | 所有DN最小峰值内存。 单位:MB |
| dn_max_peak_memory | int | 所有DN最大峰值内存。 单位:MB |
| dn_average_peak_memory | int | 所有DN平均峰值内存。 单位:MB |
| dn_memory_skew_percent | int | 内存倾斜率。 单位:% |
| dn_spill_num | int | 下盘DN数量 |
| dn_spill_count | int | 下盘DN次数总量 |
| dn_min_spill_size | bigint | 最大下盘Size。 单位:MB |
| dn_max_spill_size | bigint | 最小下盘Size。 单位:MB |
| dn_average_spill_size | bigint | 平均下盘Size。 单位:MB |
| dn_spill_skew_percent | int | 下盘倾斜率。 单位:% |
| dn_write_disk_total_size | bigint | dn下盘写磁盘总量。 单位:MB |
| dn_write_disk_max_size | bigint | dn最大下盘写磁盘量。 单位:MB |
| dn_write_disk_min_size | bigint | dn最小下盘写磁盘量。 单位:MB |
| dn_spill_obs_total_size | bigint | dn下盘写OBS总量。 单位:MB |
| dn_spill_obs_max_size | bigint | dn最大下盘写OBS量。 单位:MB |
| dn_spill_obs_min_size | bigint | dn最小下盘写OBS量。 单位:MB |
| dn_write_obs_total_size | bigint | dn下盘直写OBS总量。 单位:MB |
| dn_write_obs_max_size | bigint | dn下盘最大直写OBS量。 单位:MB |
| dn_write_obs_min_size | bigint | dn下盘最小直写OBS量。 单位:MB |
| cn_spill_count | int | cn下盘次数 |
| cn_spill_size | bigint | cn下盘总量。 单位:MB |
| cn_spill_obs_size | bigint | cn下盘写OBS量。 单位:MB |
| cn_write_obs_size | bigint | cn下盘直写OBS量。 单位:MB |
| cn_write_obs_disk_size | bigint | cn下盘写本地磁盘量。 单位:MB |
| disk_cache_hit_ratio | numeric | 磁盘缓存命中率。 |
| disk_cache_disk_read_size | bigint | 磁盘缓存读取大小。 单位:B |
| disk_cache_disk_write_size | bigint | 磁盘缓存写入大小。 单位:B |
| disk_cache_remote_read_size | bigint | 磁盘缓存远程读取大小。 单位:B |
| disk_cache_remote_read_time | bigint | 磁盘缓存远程读取次数。 单位:次 |
| vfs_scan_bytes | bigint | OBS 文件系统扫描字节数。 单位:B |
| vfs_remote_read_bytes | bigint | OBS 文件系统远程读取字节数。 单位:B |
| disk_cache_load_time | bigint | 磁盘缓存本地加载时间。 单位:us |
| disk_cache_conflict_count | bigint | 磁盘缓存块哈希冲突次数。 单位:次 |
| disk_cache_error_count | bigint | 磁盘缓存错误次数。 单位:次 |
| disk_cache_error_code | bigint | 磁盘缓存错误码。 |
| obs_io_req_avg_rtt | bigint | OBS IO请求平均往返时间。 单位:us |
| obs_io_req_avg_latency | bigint | OBS IO请求平均延迟。 单位:us |
| obs_io_req_latency_gt_1s | bigint | OBS IO请求延迟大于1秒的次数。 单位:次 |
| obs_io_req_latency_gt_10s | bigint | 查询的OBS IO请求延迟大于10秒的次数。 单位:次 |
| obs_io_req_count | bigint | OBS IO请求次数。 单位:次 |
| obs_io_req_count | bigint | OBS IO请求重试次数。 单位:次 |
| obs_io_req_rate_limit_count | bigint | OBS IO请求速率限制次数。 单位:次 |
pg_get_actor_stat()
描述:获取一些监控事项的统计信息,监控事项包括创建CN,启动CN,创建会话,语句加入队列,执行语句,写结果集,获取执行状态,获取执行结果。每发生一次上述事项,就记录一次统计信息。
返回值类型:
| 字段名称 | 类型 | 描述 |
|---|---|---|
| event_name | text | 监控事项名称。 |
| object_name | text | 标识监控事项ID。 |
| start_time | timestamp with time zone | 监控事项开始时间。 |
| end_time | timestamp with time zone | 监控事项结束时间。 |
| comment | text | 具体内容。 |
obs_get_access_label()
描述:入参为OBS路径,用于获取指定OBS路径的权限信息。
返回值类型:
| 字段名称 | 类型 | 说明 |
|---|---|---|
| obs_path | text | 指定的OBS路径。 |
| access_label | text | 对应OBS路径的权限信息。 |
read_external_table_file()
描述:用于解析ORC和Parquet格式的外部表文件。当文件因版本差异或数据生产者问题无法正常SELECT查询时,可使用此函数进行诊断分析。
入参说明:
| 字段名称 | 类型 | 说明 |
|---|---|---|
| tablename | text | 目标外表表名,format必须为parquet或orc或iceberg。 |
| filename | text | 目标文件路径(末尾需要包含.orc或.parquet) |
| target | text | 决定获取文件的哪部分信息,取值:meta/m(元数据)、statistics/s(统计信息)、contents/c(文件内容)、meta -v/m -v(详细元数据)、statistics -i/s -i(含行索引统计) |
| num | int | 第三个参数的辅助参数,用于指定获取全部(0)还是部分。 |
返回值类型:text
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | select * from read_external_table_file('test0508','/******/30f16dfe-a7dc-487b-a285-752aeaebde72.parquet','m -v',0); read_external_table_file ---------------------------------------------------------------------------------------------------------------- { + "FileName": "/******/30f16dfe-a7dc-487b-a285-752aeaebde72.parquet", + "Version": "2.6", + "CreatedBy": "parquet-cpp-arrow version 17.0.0", + "TotalRows": "1", + "NumberOfRowGroups": "1", + "NumberOfRealColumns": "1", + "NumberOfColumns": "1", + "Columns": [ + { + "Id": "0", + "Name": "a", + "PhysicalType": "INT32", + "ConvertedType": "INT_32", + "LogicalType": {"Type": "Int", "bitWidth": 32, "isSigned": true} + } + ], + "RowGroups": [ + { + "Id": "0", + "Rows": "1", + "TotalBytes": "44", + "TotalCompressedBytes": "48", + "FileOffset": "4", + "ColumnChunks": [ + {"Id": "0", + "Values": "1", + "Compression": "SNAPPY", + "Encodings": "PLAIN RLE RLE_DICTIONARY ", + "UncompressedSize": "44", + "CompressedSize": "48", + } + ] + } + ] + } + (1 row) |
pg_get_statistics()
描述:查询表级统计信息。
入参说明:表名(可以指定schema)。
返回值说明:
| 字段名称 | 类型 | 说明 |
|---|---|---|
| column_name | text | 列名。 |
| null_fraction | float4 | NULL 值占比,范围 0~1。例如 0.25 表示该列 25% 的值为 NULL。 |
| avg_width | int4 | 列的平均字节宽度。例如 int4 类型通常为 4,text 类型取决于实际内容。 |
| distinct | float4 | 去重值数量。正数表示绝对数量,负数表示占总行数的比例(如 -0.5 表示约 50% 的值互不相同)。 |
| mcv_values | text | 最常见值列表(Most Common Values),无数据时为 NULL。 |
| mcv_freqs | text | 最常见值的出现频率列表,与 mcv_values一一对应,无数据时为 NULL。 |
| histogram_bounds | text | 直方图边界值列表,将列值域划分为等频桶,无数据时为 NULL。 |
pg_get_partition_statistics()
描述:查询分区级统计信息。
入参说明:
| 字段名称 | 类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
| table_name | text | 是 | - | 分区表的表名(可以指定schema)。 |
| partition_values | ARRAY | 是 | - | 分区键值数组。 |
| spec_id | int8 | 否 | 0 | iceberg表的分区标识。 |
返回值说明:
| 字段名称 | 类型 | 说明 |
|---|---|---|
| column_name | text | 列名。 |
| null_fraction | float4 | NULL值占比,范围 0~1。例如 0.25 表示该列 25% 的值为NULL。 |
| avg_width | int4 | 列的平均字节宽度。例如 int4 类型通常为 4,text 类型取决于实际内容。 |
| distinct | float4 | 去重值数量。正数表示绝对数量,负数表示占总行数的比例(如 -0.5 表示约 50% 的值互不相同)。 |
| mcv_values | text | 最常见值列表(Most Common Values),无数据时为 NULL。 |
| mcv_freqs | text | 最常见值的出现频率列表,与 mcv_values一一对应,无数据时为 NULL。 |
| histogram_bounds | text | 直方图边界值列表,将列值域划分为等频桶,无数据时为 NULL。 |
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1. SELECT * FROM pg_get_statistics('my_table'); column_name | null_fraction | avg_width | distinct | mcv_values | mcv_freqs | histogram_bounds -------------+---------------+-----------+----------+-------------+-----------------+------------------ id | 0 | 4 | -1 | NULL | NULL | 1, 50, 100 name | 0.1 | 16 | -0.5 | Alice, Bob | 0.3, 0.2 | NULL status | 0 | 4 | 3 | active | 0.6 | NULL (1 row) 2. SELECT * FROM pg_get_partition_statistics('iceberg_table', ARRAY['2026-01-01', '3'], 0); column_name | null_fraction | avg_width | distinct | mcv_values | mcv_freqs | histogram_bounds -------------+---------------+-----------+----------+------------+-----------+------------------ x | 0 | 4 | -1 | NULL | NULL | 1, 2, 3 a | 0 | 10 | -0.8 | 123456789 | 0.4 | NULL b | 0 | 4 | 1 | NULL | NULL | NULL (1 row) |