*** pg_autovacuum.c	2003-09-12 02:28:38.012400601 -0400
--- /home/matthew/downloads/pg_autovacuum.c	2003-09-12 02:42:35.891499368 -0400
***************
*** 118,130 ****
  	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 =
--- 118,123 ----
***************
*** 220,226 ****
  		 * both remove tables from the list that no longer exist and add
  		 * tables to the list that are new
  		 */
! 		res = send_query((char *) TABLE_STATS_QUERY, dbi);
  		t = PQntuples(res);
  
  		/*
--- 213,219 ----
  		 * 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);
  		t = PQntuples(res);
  
  		/*
***************
*** 360,366 ****
  {
  	sprintf(logbuffer, "  table name:     %s.%s", tbl->dbi->dbname, tbl->table_name);
  	log_entry(logbuffer);
! 	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);
--- 353,359 ----
  {
  	sprintf(logbuffer, "  table name:     %s.%s", tbl->dbi->dbname, tbl->table_name);
  	log_entry(logbuffer);
! 	sprintf(logbuffer, "     relfilenode: %i", tbl->relfilenode);
  	log_entry(logbuffer);
  	sprintf(logbuffer, "     reltuples: %i;  relpages: %i", tbl->reltuples, tbl->relpages);
  	log_entry(logbuffer);
***************
*** 595,601 ****
  
  		res = send_query("VACUUM", dbi);
  		/* FIXME: Perhaps should add a check for PQ_COMMAND_OK */
- 		/* FIXME: We should also reset all table stats since we just vacuumed every table */
  		PQclear(res);
  		return 1;
  	}
--- 588,593 ----
***************
*** 698,703 ****
--- 690,707 ----
  
  /* 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;
+ }
+ 
  /* Perhaps add some test to this function to make sure that the stats we need are available */
  PGconn *
  db_connect(db_info * dbi)
***************
*** 749,757 ****
  	if (NULL == dbi->conn)
  		return NULL;
  
-     if (args->debug >= 4)
- 		log_entry(query);
- 
  	res = PQexec(dbi->conn, query);
  
  	if (!res)
--- 753,758 ----
***************
*** 1054,1060 ****
  
  				if (0 == xid_wraparound_check(dbs));
  				{
! 					res = send_query((char *) TABLE_STATS_QUERY, dbs);		/* Get an updated
  																		 * snapshot of this dbs
  																		 * table stats */
  					for (j = 0; j < PQntuples(res); j++)
--- 1055,1061 ----
  
  				if (0 == xid_wraparound_check(dbs));
  				{
! 					res = send_query(query_table_stats(dbs), dbs);		/* Get an updated
  																		 * snapshot of this dbs
  																		 * table stats */
  					for (j = 0; j < PQntuples(res); j++)
***************
*** 1086,1096 ****
  								 */
  								if ((tbl->curr_vacuum_count - tbl->CountAtLastVacuum) >= tbl->vacuum_threshold)
  								{
! 									/* 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);
--- 1087,1093 ----
  								 */
  								if ((tbl->curr_vacuum_count - tbl->CountAtLastVacuum) >= tbl->vacuum_threshold)
  								{
! 									snprintf(buf, sizeof(buf), "VACUUM ANALYZE \"%s\"", tbl->table_name);
  									if (args->debug >= 1)
  									{
  										sprintf(logbuffer, "Performing: %s", buf);
***************
*** 1104,1110 ****
  								}
  								else if ((tbl->curr_analyze_count - tbl->CountAtLastAnalyze) >= tbl->analyze_threshold)
  								{
! 									snprintf(buf, sizeof(buf), "ANALYZE %s", tbl->table_name);
  									if (args->debug >= 1)
  									{
  										sprintf(logbuffer, "Performing: %s", buf);
--- 1101,1107 ----
  								}
  								else if ((tbl->curr_analyze_count - tbl->CountAtLastAnalyze) >= tbl->analyze_threshold)
  								{
! 									snprintf(buf, sizeof(buf), "ANALYZE \"%s\"", tbl->table_name);
  									if (args->debug >= 1)
  									{
  										sprintf(logbuffer, "Performing: %s", buf);
