Commit 3bf080da authored by Bruce Momjian's avatar Bruce Momjian

> OK, well as we wait on the fix for the stats system, let me submit my

> patch for pg_autovacuum.  This patch assumes that the stats system will
> be fixed so that all inserts, updates and deletes performed on shared
> tables reguardless of what database those commands were executed from,
> will show up in the stats shown in each database.

I had to make a further change to this to take quotes off the 'last
ANALYZE' in order for it to not overquote the relation name, so
there's a _little_ work left to get it to play well.

I have deployed it onto several boxes that should be doing some
vacuuming over the weekend, and it is now certainly hitting pg_
tables.

I would like to present a CVS-oriented patch; unfortunately, I had to
change the indentation patterns when editing some of it :-(.  The
following _may_ be good; not sure...

Matthew T. O'Connor
Christopher Browne
parent b041d3e3
This diff is collapsed.
......@@ -118,6 +118,12 @@ init_table_info(PGresult *res, int row, db_info * dbi)
new_tbl->reltuples = atoi(PQgetvalue(res, row, PQfnumber(res, "reltuples")));
new_tbl->relpages = atoi(PQgetvalue(res, row, PQfnumber(res, "relpages")));
log_entry(PQgetvalue(res, row, PQfnumber(res, "relisshared")));
if (strcmp("t", PQgetvalue(res, row, PQfnumber(res, "relisshared"))))
new_tbl->relisshared = 0;
else
new_tbl->relisshared = 1;
new_tbl->analyze_threshold =
args->analyze_base_threshold + args->analyze_scaling_factor * new_tbl->reltuples;
new_tbl->vacuum_threshold =
......@@ -213,7 +219,7 @@ update_table_list(db_info * dbi)
* both remove tables from the list that no longer exist and add
* tables to the list that are new
*/
res = send_query(query_table_stats(dbi), dbi);
res = send_query((char *) TABLE_STATS_QUERY, dbi);
t = PQntuples(res);
/*
......@@ -353,7 +359,7 @@ print_table_info(tbl_info * tbl)
{
sprintf(logbuffer, " table name: %s.%s", tbl->dbi->dbname, tbl->table_name);
log_entry(logbuffer);
sprintf(logbuffer, " relfilenode: %i", tbl->relfilenode);
sprintf(logbuffer, " relfilenode: %i; relisshared: %i", tbl->relfilenode, tbl->relisshared);
log_entry(logbuffer);
sprintf(logbuffer, " reltuples: %i; relpages: %i", tbl->reltuples, tbl->relpages);
log_entry(logbuffer);
......@@ -688,19 +694,7 @@ print_db_info(db_info * dbi, int print_tbl_list)
/* End of DB List Management Function */
/* Begninning of misc Functions */
char *
query_table_stats(db_info * dbi)
{
if (!strcmp(dbi->dbname, "template1")) /* Use template1 to
* monitor the system
* tables */
return (char *) TABLE_STATS_ALL;
else
return (char *) TABLE_STATS_USER;
}
/* Beginning of misc Functions */
/* Perhaps add some test to this function to make sure that the stats we need are available */
PGconn *
......@@ -753,6 +747,9 @@ send_query(const char *query, db_info * dbi)
if (NULL == dbi->conn)
return NULL;
if (args->debug >= 4)
log_entry(query);
res = PQexec(dbi->conn, query);
if (!res)
......@@ -964,7 +961,7 @@ main(int argc, char *argv[])
int j = 0,
loops = 0;
/* int numInserts, numDeletes, */
/* int numInserts, numDeletes, */
int sleep_secs;
Dllist *db_list;
Dlelem *db_elem,
......@@ -1055,7 +1052,7 @@ main(int argc, char *argv[])
if (0 == xid_wraparound_check(dbs));
{
res = send_query(query_table_stats(dbs), dbs); /* Get an updated
res = send_query(TABLE_STATS_QUERY, dbs); /* Get an updated
* snapshot of this dbs
* table stats */
for (j = 0; j < PQntuples(res); j++)
......@@ -1087,7 +1084,11 @@ main(int argc, char *argv[])
*/
if ((tbl->curr_vacuum_count - tbl->CountAtLastVacuum) >= tbl->vacuum_threshold)
{
snprintf(buf, sizeof(buf), "VACUUM ANALYZE \"%s\"", tbl->table_name);
/* if relisshared = t and database != template1 then only do an analyze */
if((tbl->relisshared > 0) && (strcmp("template1",dbs->dbname)))
snprintf(buf, sizeof(buf), "ANALYZE %s", tbl->table_name);
else
snprintf(buf, sizeof(buf), "VACUUM ANALYZE %s", tbl->table_name);
if (args->debug >= 1)
{
sprintf(logbuffer, "Performing: %s", buf);
......@@ -1101,7 +1102,7 @@ main(int argc, char *argv[])
}
else if ((tbl->curr_analyze_count - tbl->CountAtLastAnalyze) >= tbl->analyze_threshold)
{
snprintf(buf, sizeof(buf), "ANALYZE \"%s\"", tbl->table_name);
snprintf(buf, sizeof(buf), "ANALYZE %s", tbl->table_name);
if (args->debug >= 1)
{
sprintf(logbuffer, "Performing: %s", buf);
......
......@@ -34,8 +34,8 @@
#define VACUUM_ANALYZE 0
#define ANALYZE_ONLY 1
#define TABLE_STATS_ALL "select a.relfilenode,a.relname,a.relnamespace,a.relpages,a.reltuples,b.schemaname,b.n_tup_ins,b.n_tup_upd,b.n_tup_del from pg_class a, pg_stat_all_tables b where a.relfilenode=b.relid"
#define TABLE_STATS_USER "select a.relfilenode,a.relname,a.relnamespace,a.relpages,a.reltuples,b.schemaname,b.n_tup_ins,b.n_tup_upd,b.n_tup_del from pg_class a, pg_stat_user_tables b where a.relfilenode=b.relid"
#define TABLE_STATS_QUERY "select a.relfilenode,a.relname,a.relnamespace,a.relpages,a.relisshared,a.reltuples,b.schemaname,b.n_tup_ins,b.n_tup_upd,b.n_tup_del from pg_class a, pg_stat_all_tables b where a.relfilenode=b.relid and a.relkind = 'r'"
#define FRONTEND
#define PAGES_QUERY "select relfilenode,reltuples,relpages from pg_class where relfilenode=%i"
#define FROZENOID_QUERY "select oid,age(datfrozenxid) from pg_database where datname = 'template1'"
......@@ -86,6 +86,7 @@ struct tableinfo
*table_name;
int relfilenode,
reltuples,
relisshared,
relpages;
long analyze_threshold,
vacuum_threshold;
......@@ -132,7 +133,6 @@ static int check_stats_enabled(db_info * dbi);
static PGconn *db_connect(db_info * dbi);
static void db_disconnect(db_info * dbi);
static PGresult *send_query(const char *query, db_info * dbi);
static char *query_table_stats(db_info * dbi);
/* Other Generally needed Functions */
static void daemonize(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment