STATEMENT_HISTORY
获得当前节点的执行语句的信息。查询系统表必须具有sysadmin权限。只可在系统库中查询到结果,用户库中无法查询。
- 必须在postgres库内查询,其它库中不存数据。
- 此系统表受track_stmt_stat_level控制,默认为"OFF,L0",第一部分控制Full SQL,第二部分控制Slow SQL,具体字段记录级别见下表。考虑性能影响,更改该参数的值时建议通过set方式设置,使该参数仅对当前会话生效。
- 对于Slow SQL,当track_stmt_stat_level的值为非OFF时,且SQL执行时间超过log_min_duration_statement,会记录为慢SQL。
      当前版本暂不支持对FOR UPDATE关键字进行识别并归一化处理。例如:SELECT * FROM table; 与SELECT * FROM table FOR UPDATE WAIT N; 会被归一化处理为相同的归一化SQL,在query字段中体现。涉及FOR UPDATE关键字的SQL,可以通过query_plan字段进行区分,执行计划中会含有'lockRows'。 
| 名称 | 类型 | 描述 | 记录级别 | 
|---|---|---|---|
| db_name | name | 数据库名称。 | L0 | 
| schema_name | name | schema名称。 | L0 | 
| origin_node | integer | 节点名称。 | L0 | 
| user_name | name | 用户名。 | L0 | 
| application_name | text | 用户发起的请求的应用程序名称。 | L0 | 
| client_addr | text | 用户发起的请求的客户端地址。 | L0 | 
| client_port | integer | 用户发起的请求的客户端端口。 | L0 | 
| unique_query_id | bigint | 归一化SQL ID。 | L0 | 
| debug_query_id | bigint | 唯一SQL ID。部分语句存在不唯一的情况,如Parse报文、DCL和TCL等语句的debug_query_id值为0。 | L0 | 
| query | text | 归一化SQL,track_stmt_parameter参数开启时,显示完整SQL。 | L0 | 
| start_time | timestamp with time zone | 语句启动的时间。 | L0 | 
| finish_time | timestamp with time zone | 语句结束的时间。 | L0 | 
| slow_sql_threshold | bigint | 语句执行时慢SQL的标准。 | L0 | 
| transaction_id | bigint | 事务ID。 | L0 | 
| thread_id | bigint | 执行线程ID。 | L0 | 
| session_id | bigint | 用户session id。 | L0 | 
| n_soft_parse | bigint | 软解析次数,n_soft_parse + n_hard_parse可能大于n_calls,因为子查询未计入n_calls。 | L0 | 
| n_hard_parse | bigint | 硬解析次数,n_soft_parse + n_hard_parse可能大于n_calls,因为子查询未计入n_calls。 | L0 | 
| query_plan | text | 语句执行计划。对于Slow SQL的query_plan,execution_time大于slow_sql_threshold时记录,发生死锁等场景query_plan有可能为空。 | L0 | 
| n_returned_rows | bigint | SELECT返回的结果集行数。 | L0 | 
| n_tuples_fetched | bigint | 随机扫描行。 | L0 | 
| n_tuples_returned | bigint | 顺序扫描行。 | L0 | 
| n_tuples_inserted | bigint | 插入行。 | L0 | 
| n_tuples_updated | bigint | 更新行。 | L0 | 
| n_tuples_deleted | bigint | 删除行。 | L0 | 
| n_blocks_fetched | bigint | buffer的块访问次数。 | L0 | 
| n_blocks_hit | bigint | buffer的块命中次数。 | L0 | 
| db_time | bigint | 有效的DB时间花费,多线程将累加(单位:微秒)。 | L0 | 
| cpu_time | bigint | CPU时间(单位:微秒)。 | L0 | 
| execution_time | bigint | 执行器内执行时间(单位:微秒)。 | L0 | 
| parse_time | bigint | SQL解析时间(单位:微秒)。 | L0 | 
| plan_time | bigint | SQL生成计划时间(单位:微秒)。 | L0 | 
| rewrite_time | bigint | SQL重写时间(单位:微秒)。 | L0 | 
| pl_execution_time | bigint | plpgsql上的执行时间(单位:微秒)。 | L0 | 
| pl_compilation_time | bigint | plpgsql上的编译时间(单位:微秒)。 | L0 | 
| data_io_time | bigint | IO上的时间花费(单位:微秒)。 | L0 | 
| net_send_info | text | 通过物理连接发送消息的网络状态,包含时间(微秒)、调用次数、吞吐量(字节)。集中式不支持该字段。 | L0 | 
| net_recv_info | text | 通过物理连接接收消息的网络状态,包含时间(微秒)、调用次数、吞吐量(字节)。集中式不支持该字段。 | L0 | 
| net_stream_send_info | text | 通过逻辑连接发送消息的网络状态,包含时间(微秒)、调用次数、吞吐量(字节)。集中式不支持该字段。 | L0 | 
| net_stream_recv_info | text | 通过逻辑连接接收消息的网络状态,包含时间(微秒)、调用次数、吞吐量(字节)。集中式不支持该字段。 | L0 | 
| lock_count | bigint | 加锁次数。 | L0 | 
| lock_time | bigint | 加锁耗时。 | L1 | 
| lock_wait_count | bigint | 加锁等待次数。 | L0 | 
| lock_wait_time | bigint | 加锁等待耗时。 | L1 | 
| lock_max_count | bigint | 最大持锁数量。 | L0 | 
| lwlock_count | bigint | 轻量级加锁次数(预留)。 | L0 | 
| lwlock_wait_count | bigint | 轻量级等锁次数。 | L0 | 
| lwlock_time | bigint | 轻量级加锁时间(预留)。 | L1 | 
| lwlock_wait_time | bigint | 轻量级等锁时间。 | L1 | 
| details | bytea | 等待事件和语句锁事件的列表。 记录级别的值>=L0时,开始记录等待事件的列表。显示当前语句event等待相关的统计信息。具体事件信息见表2、15.3.67-表3 轻量级锁等待事件列表、表4和表5。关于每种事务锁对业务的影响程度,请参考LOCK语法小节的详细描述。 记录级别的值是L2时,开始记录语句锁事件的列表,该列表按时间顺序记录事件,记录的数量受参数track_stmt_details_size的影响。 该字段为二进制,需要借助解析函数pg_catalog.statement_detail_decode读取,见(表6)。 事件包括: 
 | L0/L2 | 
| is_slow_sql | boolean | 该SQL是否为slow SQL。 
 | L0 | 
| trace_id | text | 驱动传入的trace id,与应用的一次请求相关联。 | L0 | 
| advise | text | 可能导致该SQL为slow SQL的风险信息。 | L0 | 
| parent_unique_sql_id | bigint | 当前语句的外层SQL的归一化SQL ID,对于存储过程内执行的语句,该值为调用存储过程语句的归一化SQL ID,存储过程外的语句该值为0。 | L0 | 
 
  