0

PostgreSQL 有这些监控表,特别是pg_stat_statements,包含这个列的名称queryId,我想知道它是如何计算的。如果有人知道我在哪里可以找到源代码,那将不胜感激。

所以pg_stat_statements表格会显示如下:

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

我对这123是如何计算的很感兴趣。

4

1 回答 1

0

此查询 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.
于 2020-10-02T06:54:03.570 回答