如何从PostgreSQL上的pg_stat_statements计算queryId?

问题描述

Postgresql具有这些监视表,尤其是pg_stat_statements,这些监视表构成了名为queryId的列,我想知道它是如何计算的。如果有人知道在哪里可以找到源代码,将不胜感激。

因此pg_stat_statements表将显示如下内容

userid | dbid | queryid |      query       | other statistics related columns 
1      |  2   | 123     | SELECT * FROM a; | ...

我对如何计算此123感兴趣。

解决方法

此查询ID由pg_stat_statements扩展名计算,请参阅contrib/pg_stat_statements/pg_stat_statements.c

此评论说明其工作方式:

As of Postgres 9.2,this module normalizes query entries.  Normalization
is a process whereby similar queries,typically differing only in their
constants (though the exact rules are somewhat more subtle than that) are
recognized as equivalent,and are tracked as a single entry.  This is
particularly useful for non-prepared queries.

Normalization is implemented by fingerprinting queries,selectively
serializing those fields of each query tree's nodes that are judged to be
essential to the query.  This is referred to as a query jumble.  This is
distinct from a regular serialization in that various extraneous
information is ignored as irrelevant or not essential to the query,such
as the collations of Vars and,most notably,the values of constants.

This jumble is acquired at the end of parse analysis of each query,and
a 32-bit hash of it is stored into the query's Query.queryId field.
The server then copies this value around,making it available in plan
tree(s) generated from the query.  The executor can then use this value
to blame query costs on the proper queryId.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...