title: How do I know if the statistics of a Postgres table are up to date?
body: <p>In pgAdmin, whenever a table's statistics are out-of-date, it prompts:</p>
  
  <blockquote>
    <p>Running VACUUM recommended</p>
    
    <p>The estimated rowcount on the table schema.table deviates
    significantly from the actual rowcount. You should run VACUUM ANALYZE
    on this table.</p>
  </blockquote>
  
  <p>I've tested it using pgAdmin 3 and Postgres 8.4.4, with autovacuum=off. The prompt shows up immediately whenever I click a table that has been changed.</p>
  
  <p>Let's say I'm making a web-based system in Java, how do I detect if a table is out-of-date, so that I can show a prompt like the one in pgAdmin? </p>
  
  <p>Because of the nature of my application, here are a few rules I have to follow:</p>
  
  <ol>
  <li><p>I want to know if the statistics of a certain table in pg_stats and pg_statistic are up to date.</p></li>
  <li><p>I can't set the autovacuum flag in postgresql.conf. (In other words, the autovacuum flag can be on or off. I have no control over it. I need to tell if the stats are up-to-date whether the autovacuum flag is on or off.)</p></li>
  <li><p>I can't run vacuum/analyze every time to make it up-to-date.</p></li>
  <li><p>When a user selects a table, I need to show the prompt that the table is outdated when there are any updates to this table (such as drop, insert, and update) that are not reflected in pg_stats and pg_statistic.</p></li>
  </ol>
  
  <p>It seems that it's not feasible by analyzing timestamps in pg_catalog.pg_stat_all_tables. Of course, if a table hasn't been analyzed before, I can check if it has a timestamp in last_analyze to find out whether the table is up-to-date. Using this method, however, I can't detect if the table is up-to-date when there's already a timestamp. In other words, no matter how many rows I add to the table, its last_analyze timestamp in pg_stat_all_tables is always for the first analyze (assuming the autovacuum flag is off). Therefore, I can only show the "Running VACUUM recommended" prompt for the first time. </p>
  
  <p>It's also not feasible by comparing the last_analyze timestamp to the current timestamp. There might not be any updates to the table for days. And there might be tons of updates in one hour.</p>
  
  <p>Given this scenario, how can I always tell if the statistics of a table are up-to-date?</p>