35

我已经启动并运行了 postgresql-9.4,并且最近在官方文档的帮助下启用了 pg_stat_statements 模块。

但是我在使用时遇到以下错误:

postgres=# SELECT * FROM pg_stat_statements;
ERROR:  relation "pg_stat_statements" does not exist
LINE 1: SELECT * FROM pg_stat_statements;


postgres=# SELECT pg_stat_statements_reset();
ERROR:  function pg_stat_statements_reset() does not exist
LINE 1: SELECT pg_stat_statements_reset();

我使用 postgres 用户登录到 psql。我还检查了可用的扩展名列表:

postgres=# SELECT * FROM pg_available_extensions WHERE name = 'pg_stat_statements'
;
        name        | default_version | installed_version |                          comment                          
--------------------+-----------------+-------------------+-----------------------------------------------------------
 pg_stat_statements | 1.2             |                   | track execution statistics of all SQL statements executed
(1 row)

这是扩展版本查询的结果:

postgres=# SELECT * FROM pg_available_extension_versions WHERE name = 'pg_stat_statements';
        name        | version | installed | superuser | relocatable | schema | requires |                          comment                          
--------------------+---------+-----------+-----------+-------------+--------+----------+-----------------------------------------------------------
 pg_stat_statements | 1.2     | f         | t         | t           |        |          | track execution statistics of all SQL statements executed
(1 row)

任何帮助将不胜感激。

4

3 回答 3

73

未安装扩展程序:

SELECT * 
FROM pg_available_extensions 
WHERE 
    name = 'pg_stat_statements' and 
    installed_version is not null;

如果表为空,则创建扩展:

CREATE EXTENSION pg_stat_statements;
于 2015-06-24T08:17:15.283 回答
2

我在配置 Percona 监控和管理 (PMM) 时遇到了这个问题,因为由于某种奇怪的原因 PMM 连接到带有 name 的数据库postgres,所以pg_stat_statements必须在这个数据库中创建扩展:

yourdb# \c postgres
postgres# CREATE EXTENSION pg_stat_statements SCHEMA public;
于 2021-06-26T16:32:47.883 回答
1

我第一次使用 liquibase 部署环境时遇到了同样的问题。我知道我的回复可能与您的问题无关,但这是第一个谷歌结果,所以我认为像我这样的其他人可以带着我同样的 Liquibase 问题到达这里。

这些是在您生成第一个 xml 文件时由 liquibase 检索的 PosGreSQL 元数据表。

在我的情况下,它只是无用的自动生成代码,所以我解决了它删除这些行:

 <changeSet author="martinlarizzate (generated)" id="1588181532394-7">
        <createView fullDefinition="false" viewName="pg_stat_statements"> SELECT pg_stat_statements.userid,
    pg_stat_statements.dbid,
    pg_stat_statements.queryid,
    pg_stat_statements.query,
    pg_stat_statements.calls,
    pg_stat_statements.total_time,
    pg_stat_statements.min_time,
    pg_stat_statements.max_time,
    pg_stat_statements.mean_time,
    pg_stat_statements.stddev_time,
    pg_stat_statements.rows,
    pg_stat_statements.shared_blks_hit,
    pg_stat_statements.shared_blks_read,
    pg_stat_statements.shared_blks_dirtied,
    pg_stat_statements.shared_blks_written,
    pg_stat_statements.local_blks_hit,
    pg_stat_statements.local_blks_read,
    pg_stat_statements.local_blks_dirtied,
    pg_stat_statements.local_blks_written,
    pg_stat_statements.temp_blks_read,
    pg_stat_statements.temp_blks_written,
    pg_stat_statements.blk_read_time,
    pg_stat_statements.blk_write_time
   FROM pg_stat_statements(true) pg_stat_statements(userid, dbid, queryid, query, calls, total_time, min_time, max_time, mean_time, stddev_time, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time, blk_write_time);</createView>
    </changeSet>
于 2020-05-02T19:22:54.923 回答