10

I would like to know for "pg_stat_statements" view in postgres. What is the timeframe for the data? Does it shows query executed in last 24 hours or overall queries executed? As the table doesn't contain any timestamp.

2 Answers 2

11

The timeframe of the view provided by pg_stat_statements is from either the last reset (pg_stat_statements_reset) or the time the extension was created, which may be a very long time.

There is logic to expire infrequent statements if the max threshold is reached (5000 on recent Postgres versions), which means that you may not see the full activity if you query the view infrequently.

To work better with this data, you essentially have two options:

1) Call pg_stat_statements_reset() every 24 hours, which means that the query data will only reflect recent activity (ideally you'd keep track of when the reset happened, so you can figure out number of calls per minute, etc)

2) Use a separate monitoring tool that takes snapshots and can keep historic pg_stat_statements statistics

Which one you go with depends on your requirements, though I would usually go with (2) for production systems.

Disclaimer: I'm the author of pganalyze (https://pganalyze.com), a hosted Postgres monitoring tool that also provides historic pg_stat_statement statistics.

6
  • Thank you so much @LukasFittl I will take a look
    – Madhu
    Jun 14, 2018 at 20:52
  • Does pg_stat_statements_reset() impact performance? As query stats are used by query planner and in this case it has to build stats again. or PostgreSQL uses stats from somewhere else?
    – YogeshR
    Oct 29, 2020 at 10:58
  • 1
    @Yogi Not significantly. pg_stat_statements data is not used by the planner (there are other tables for that). The one downside of frequent resets is that it would increase I/O to the query text file that is used by pg_stat_statements, since it has to write the query texts more often. Its probably a bad idea to reset once a second for that reason, but reasonable to do something like once an hour. Oct 30, 2020 at 15:46
  • Thanks @LukasFittl , Its really helpful.
    – YogeshR
    Nov 2, 2020 at 8:25
  • 1
    @JonatasDelatorre Whilst pg_stat_statements has been improved over the years (with additional data being tracked), the core mechanism still works the same in Postgres 15. Did you forget to restart the server after adding "pg_stat_statements" to your "shared_preload_libraries" setting? That can cause it to not work. Additionally check that the "pg_stat_statements.track" setting is set to either "top" or "all" (and not to "none", which turns it off). Sep 16 at 18:45
5

There is no timeframe only a maximum number of statements tracked.

F.28.3. Configuration Parameters

pg_stat_statements.max (integer)

pg_stat_statements.max is the maximum number of statements tracked by the module (i.e., the maximum number of rows in the pg_stat_statements view). If more distinct statements than that are observed, information about the least-executed statements is discarded. The default value is 1000. This parameter can only be set at server start.

1
  • Thanks but then does it shows queries executed over the last X hours ? whats value of from and to timeframe?
    – Madhu
    Jun 12, 2018 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.