2003-03-21 02:14:46 +08:00
/* pg_autovacuum.h
* Header file for pg_autovacuum . c
* ( c ) 2003 Matthew T . O ' Connor
*/
2003-06-12 09:36:44 +08:00
2003-05-14 11:27:22 +08:00
# include "postgres_fe.h"
2003-03-21 02:14:46 +08:00
# include <unistd.h>
2003-08-08 05:11:58 +08:00
# ifdef HAVE_GETOPT_H
2003-06-12 09:36:44 +08:00
# include <getopt.h>
# endif
2003-08-01 10:25:31 +08:00
# include <time.h>
2003-03-21 02:14:46 +08:00
# include <sys/time.h>
2003-05-14 11:27:22 +08:00
2003-06-12 09:36:44 +08:00
/* These next two lines are correct when pg_autovaccum is compiled
from within the postgresql source tree */
2003-05-14 11:27:22 +08:00
# include "libpq-fe.h"
2003-03-21 02:14:46 +08:00
# include "lib/dllist.h"
2003-06-12 09:36:44 +08:00
/* Had to change the last two lines to compile on
Redhat outside of postgresql source tree */
/*
# include "/usr/include/libpq-fe.h"
# include "/usr/include/pgsql/server/lib/dllist.h"
*/
2003-03-21 02:14:46 +08:00
2003-08-04 08:43:34 +08:00
# define AUTOVACUUM_DEBUG 1
# define VACBASETHRESHOLD 1000
# define VACSCALINGFACTOR 2
# define SLEEPBASEVALUE 300
# define SLEEPSCALINGFACTOR 2
# define UPDATE_INTERVAL 2
2003-06-12 09:36:44 +08:00
/* these two constants are used to tell update_table_stats what operation we just perfomred */
2003-08-04 08:43:34 +08:00
# define VACUUM_ANALYZE 0
# define ANALYZE_ONLY 1
2003-06-12 09:36:44 +08:00
2003-09-14 00:26:18 +08:00
# 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'"
2003-03-21 02:14:46 +08:00
# define FRONTEND
2003-06-12 09:36:44 +08:00
# 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'"
# define FROZENOID_QUERY2 "select oid,datname,age(datfrozenxid) from pg_database where datname!='template0'"
2003-03-21 02:14:46 +08:00
2003-06-12 09:36:44 +08:00
/* define cmd_args stucture */
struct cmdargs
{
2003-08-04 08:43:34 +08:00
int vacuum_base_threshold ,
analyze_base_threshold ,
sleep_base_value ,
debug ,
daemonize ;
float vacuum_scaling_factor ,
analyze_scaling_factor ,
sleep_scaling_factor ;
char * user ,
* password ,
* host ,
* logfile ,
* port ;
2003-06-12 09:36:44 +08:00
} ;
typedef struct cmdargs cmd_args ;
2003-03-21 02:14:46 +08:00
/* define cmd_args as global so we can get to them everywhere */
2003-08-04 08:43:34 +08:00
cmd_args * args ;
2003-03-21 02:14:46 +08:00
/* Might need to add a time value for last time the whold database was vacuumed.
2003-08-04 08:43:34 +08:00
I think we need to guarantee this happens approx every 1 Million TX ' s */
2003-06-12 09:36:44 +08:00
struct dbinfo
{
2003-08-04 08:43:34 +08:00
int oid ,
age ;
int analyze_threshold ,
vacuum_threshold ; /* Use these as defaults for table
* thresholds */
PGconn * conn ;
char * dbname ,
* username ,
* password ;
Dllist * table_list ;
2003-06-12 09:36:44 +08:00
} ;
typedef struct dbinfo db_info ;
struct tableinfo
{
2003-08-04 08:43:34 +08:00
char * schema_name ,
* table_name ;
int relfilenode ,
reltuples ,
2003-09-14 00:26:18 +08:00
relisshared ,
2003-08-04 08:43:34 +08:00
relpages ;
long analyze_threshold ,
vacuum_threshold ;
long CountAtLastAnalyze ; /* equal to: inserts + updates as
* of the last analyze or initial
* values at startup */
long CountAtLastVacuum ; /* equal to: deletes + updates as
* of the last vacuum or initial
* values at startup */
long curr_analyze_count ,
curr_vacuum_count ; /* Latest values from stats system */
db_info * dbi ; /* pointer to the database that this table
* belongs to */
2003-06-12 09:36:44 +08:00
} ;
typedef struct tableinfo tbl_info ;
2003-03-21 02:14:46 +08:00
/* Functions for dealing with command line arguements */
2003-08-04 08:43:34 +08:00
static cmd_args * get_cmd_args ( int argc , char * argv [ ] ) ;
static void print_cmd_args ( void ) ;
static void free_cmd_args ( void ) ;
static void usage ( void ) ;
2003-03-21 02:14:46 +08:00
/* Functions for managing database lists */
2003-08-04 08:43:34 +08:00
static Dllist * init_db_list ( void ) ;
static db_info * init_dbinfo ( char * dbname , int oid , int age ) ;
static void update_db_list ( Dllist * db_list ) ;
static void remove_db_from_list ( Dlelem * db_to_remove ) ;
static void print_db_info ( db_info * dbi , int print_table_list ) ;
static void print_db_list ( Dllist * db_list , int print_table_lists ) ;
static int xid_wraparound_check ( db_info * dbi ) ;
static void free_db_list ( Dllist * db_list ) ;
2003-03-21 02:14:46 +08:00
/* Functions for managing table lists */
2003-08-04 08:43:34 +08:00
static tbl_info * init_table_info ( PGresult * conn , int row , db_info * dbi ) ;
static void update_table_list ( db_info * dbi ) ;
static void remove_table_from_list ( Dlelem * tbl_to_remove ) ;
static void print_table_list ( Dllist * tbl_node ) ;
static void print_table_info ( tbl_info * tbl ) ;
static void update_table_thresholds ( db_info * dbi , tbl_info * tbl , int vacuum_type ) ;
static void free_tbl_list ( Dllist * tbl_list ) ;
2003-03-21 02:14:46 +08:00
/* A few database helper functions */
2003-08-04 08:43:34 +08:00
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 ) ;
2003-03-21 02:14:46 +08:00
2003-06-12 09:36:44 +08:00
/* Other Generally needed Functions */
static void daemonize ( void ) ;
2003-08-04 08:43:34 +08:00
static void log_entry ( const char * logentry ) ;