如何获取在 PostgreSQL 中每个 hour/day 执行的查询数

How to get the number of queries which are executed per hour/day in PostgreSQL

有没有办法获取在过去一小时或一天内执行的 Select/Update/Delete/Insert/Commit/Rollback 个查询的数量?

一种方法是启用 t运行saction 日志并使用 Shell/Python 工具搜索像这样的特定查询。

猫 postgresql-2022-02-25_105258.log | grep -i 'statement: select' | wc -l 28888

上面的命令会获取 运行 的 select 个查询的数量,这在某种程度上是有帮助的,但这种方法的问题是我们无法重新启动生产服务器来启用配置设置 (logging_collector = on)

如果您熟悉任何其他方法,请分享。

非常感谢, 苏雷什

在阅读如何获取 CRUD 统计信息时,我开始了解这个过程。

1) Enable the transaction logs in postgres by updating the configuration file
        log_min_duration_statement = 0
        log_line_prefix = '%t [%p]: user=%u,db=%d,app=%a,client=%h '
        log_checkpoints = on
        log_connections = on
        log_disconnections = on
        log_lock_waits = on
        log_temp_files = 0
        log_autovacuum_min_duration = 0
        log_error_verbosity = default
        logging_collector = on
        log_directory = '/var/postgreslog'
        log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
        log_truncate_on_rotation = on
        log_rotation_age = 1d
        log_file_mode = 0600
2) Restart the postgres service
     /usr/lib/postgresql/11/bin/pg_ctl -D /var/lib/postgresql/11/main/ 
3) sudo yum install pgbadger --> if it's not existed on ur Linux box
4) /usr/bin/pgbadger -f /var/postgreslog/*.log --> **give the log file location, 
   in my case logs are there in /var/postgres**
[========================>] Parsed 544074587 bytes of 544074587 (100.00%), queries: 0, events: 0
LOG: Ok, generating html report...
5) The third step will generate an out.html file, open this HTML file from windows. it will give you a wonderful UI with the stats, we can feel the UI by going through the sample page
**https://pgbadger.darold.net/samplev7.html#slowest-individual-queries**