2005-07-14 07:13:45 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* autovacuum.c
|
|
|
|
*
|
|
|
|
* PostgreSQL Integrated Autovacuum Daemon
|
|
|
|
*
|
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2005-07-14 07:13:45 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-05-30 22:12:03 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.47 2007/05/30 20:11:57 tgl Exp $
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
2005-08-15 18:25:19 +02:00
|
|
|
#include <time.h>
|
2005-07-14 07:13:45 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
|
|
|
#include "access/xact.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "catalog/indexing.h"
|
2005-07-29 21:30:09 +02:00
|
|
|
#include "catalog/namespace.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "catalog/pg_autovacuum.h"
|
2005-08-11 23:11:50 +02:00
|
|
|
#include "catalog/pg_database.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "commands/vacuum.h"
|
|
|
|
#include "libpq/hba.h"
|
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "pgstat.h"
|
|
|
|
#include "postmaster/autovacuum.h"
|
|
|
|
#include "postmaster/fork_process.h"
|
|
|
|
#include "postmaster/postmaster.h"
|
|
|
|
#include "storage/fd.h"
|
|
|
|
#include "storage/ipc.h"
|
2007-02-16 00:23:23 +01:00
|
|
|
#include "storage/pmsignal.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "storage/proc.h"
|
2007-02-16 00:23:23 +01:00
|
|
|
#include "storage/procarray.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "storage/sinval.h"
|
|
|
|
#include "tcop/tcopprot.h"
|
|
|
|
#include "utils/flatfiles.h"
|
|
|
|
#include "utils/fmgroids.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/ps_status.h"
|
2006-05-04 00:45:26 +02:00
|
|
|
#include "utils/syscache.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
static volatile sig_atomic_t got_SIGUSR1 = false;
|
2007-02-16 00:23:23 +01:00
|
|
|
static volatile sig_atomic_t got_SIGHUP = false;
|
|
|
|
static volatile sig_atomic_t avlauncher_shutdown_request = false;
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
|
|
|
* GUC parameters
|
|
|
|
*/
|
|
|
|
bool autovacuum_start_daemon = false;
|
2007-04-16 20:30:04 +02:00
|
|
|
int autovacuum_max_workers;
|
2005-07-14 07:13:45 +02:00
|
|
|
int autovacuum_naptime;
|
|
|
|
int autovacuum_vac_thresh;
|
|
|
|
double autovacuum_vac_scale;
|
|
|
|
int autovacuum_anl_thresh;
|
|
|
|
double autovacuum_anl_scale;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
int autovacuum_freeze_max_age;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
int autovacuum_vac_cost_delay;
|
|
|
|
int autovacuum_vac_cost_limit;
|
|
|
|
|
2007-04-18 18:44:18 +02:00
|
|
|
int Log_autovacuum = -1;
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* Flags to tell if we are in an autovacuum process */
|
2007-02-16 00:23:23 +01:00
|
|
|
static bool am_autovacuum_launcher = false;
|
|
|
|
static bool am_autovacuum_worker = false;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/* Comparison point for determining whether freeze_max_age is exceeded */
|
|
|
|
static TransactionId recentXid;
|
|
|
|
|
|
|
|
/* Default freeze_min_age to use for autovacuum (varies by database) */
|
|
|
|
static int default_freeze_min_age;
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* Memory context for long-lived data */
|
2005-10-15 04:49:52 +02:00
|
|
|
static MemoryContext AutovacMemCxt;
|
2005-08-11 23:11:50 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* struct to keep track of databases in launcher */
|
|
|
|
typedef struct avl_dbase
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
Oid adl_datid; /* hash key -- must be first */
|
|
|
|
TimestampTz adl_next_worker;
|
|
|
|
int adl_score;
|
|
|
|
} avl_dbase;
|
|
|
|
|
|
|
|
/* struct to keep track of databases in worker */
|
|
|
|
typedef struct avw_dbase
|
|
|
|
{
|
|
|
|
Oid adw_datid;
|
|
|
|
char *adw_name;
|
|
|
|
TransactionId adw_frozenxid;
|
|
|
|
PgStat_StatDBEntry *adw_entry;
|
|
|
|
} avw_dbase;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-29 00:17:12 +02:00
|
|
|
/* struct to keep track of tables to vacuum and/or analyze, in 1st pass */
|
|
|
|
typedef struct av_relation
|
|
|
|
{
|
|
|
|
Oid ar_relid;
|
|
|
|
Oid ar_toastrelid;
|
|
|
|
} av_relation;
|
|
|
|
|
2007-03-27 22:36:03 +02:00
|
|
|
/* struct to keep track of tables to vacuum and/or analyze, after rechecking */
|
2005-08-11 23:11:50 +02:00
|
|
|
typedef struct autovac_table
|
|
|
|
{
|
2007-03-27 22:36:03 +02:00
|
|
|
Oid at_relid;
|
|
|
|
Oid at_toastrelid;
|
|
|
|
bool at_dovacuum;
|
|
|
|
bool at_doanalyze;
|
|
|
|
int at_freeze_min_age;
|
|
|
|
int at_vacuum_cost_delay;
|
|
|
|
int at_vacuum_cost_limit;
|
2005-08-11 23:11:50 +02:00
|
|
|
} autovac_table;
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*-------------
|
|
|
|
* This struct holds information about a single worker's whereabouts. We keep
|
|
|
|
* an array of these in shared memory, sized according to
|
|
|
|
* autovacuum_max_workers.
|
|
|
|
*
|
|
|
|
* wi_links entry into free list or running list
|
|
|
|
* wi_dboid OID of the database this worker is supposed to work on
|
|
|
|
* wi_tableoid OID of the table currently being vacuumed
|
|
|
|
* wi_workerpid PID of the running worker, 0 if not yet started
|
|
|
|
* wi_launchtime Time at which this worker was launched
|
|
|
|
* wi_cost_* Vacuum cost-based delay parameters current in this worker
|
|
|
|
*
|
|
|
|
* All fields are protected by AutovacuumLock, except for wi_tableoid which is
|
|
|
|
* protected by AutovacuumScheduleLock (which is read-only for everyone except
|
|
|
|
* that worker itself).
|
|
|
|
*-------------
|
|
|
|
*/
|
|
|
|
typedef struct WorkerInfoData
|
|
|
|
{
|
|
|
|
SHM_QUEUE wi_links;
|
|
|
|
Oid wi_dboid;
|
|
|
|
Oid wi_tableoid;
|
|
|
|
int wi_workerpid;
|
|
|
|
TimestampTz wi_launchtime;
|
|
|
|
int wi_cost_delay;
|
|
|
|
int wi_cost_limit;
|
|
|
|
int wi_cost_limit_base;
|
|
|
|
} WorkerInfoData;
|
|
|
|
|
|
|
|
typedef struct WorkerInfoData *WorkerInfo;
|
|
|
|
|
|
|
|
/*-------------
|
|
|
|
* The main autovacuum shmem struct. On shared memory we store this main
|
|
|
|
* struct and the array of WorkerInfo structs. This struct keeps:
|
|
|
|
*
|
|
|
|
* av_launcherpid the PID of the autovacuum launcher
|
|
|
|
* av_freeWorkers the WorkerInfo freelist
|
|
|
|
* av_runningWorkers the WorkerInfo non-free queue
|
|
|
|
* av_startingWorker pointer to WorkerInfo currently being started (cleared by
|
|
|
|
* the worker itself as soon as it's up and running)
|
|
|
|
* av_rebalance true when a worker determines that cost limits must be
|
|
|
|
* rebalanced
|
|
|
|
*
|
|
|
|
* This struct is protected by AutovacuumLock.
|
|
|
|
*-------------
|
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
pid_t av_launcherpid;
|
|
|
|
SHMEM_OFFSET av_freeWorkers;
|
|
|
|
SHM_QUEUE av_runningWorkers;
|
|
|
|
SHMEM_OFFSET av_startingWorker;
|
|
|
|
bool av_rebalance;
|
2007-02-16 00:23:23 +01:00
|
|
|
} AutoVacuumShmemStruct;
|
|
|
|
|
|
|
|
static AutoVacuumShmemStruct *AutoVacuumShmem;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* the database list in the launcher, and the context that contains it */
|
|
|
|
static Dllist *DatabaseList = NULL;
|
|
|
|
static MemoryContext DatabaseListCxt = NULL;
|
|
|
|
|
|
|
|
/* Pointer to my own WorkerInfo, valid on each worker */
|
|
|
|
static WorkerInfo MyWorkerInfo = NULL;
|
|
|
|
|
|
|
|
/* PID of launcher, valid only in worker while shutting down */
|
|
|
|
int AutovacuumLauncherPid = 0;
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
#ifdef EXEC_BACKEND
|
2007-02-16 00:23:23 +01:00
|
|
|
static pid_t avlauncher_forkexec(void);
|
|
|
|
static pid_t avworker_forkexec(void);
|
2005-07-14 07:13:45 +02:00
|
|
|
#endif
|
2007-02-16 00:23:23 +01:00
|
|
|
NON_EXEC_STATIC void AutoVacWorkerMain(int argc, char *argv[]);
|
|
|
|
NON_EXEC_STATIC void AutoVacLauncherMain(int argc, char *argv[]);
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
static Oid do_start_worker(void);
|
|
|
|
static uint64 launcher_determine_sleep(bool canlaunch, bool recursing);
|
|
|
|
static void launch_worker(TimestampTz now);
|
|
|
|
static List *get_database_list(void);
|
|
|
|
static void rebuild_database_list(Oid newdb);
|
|
|
|
static int db_comparator(const void *a, const void *b);
|
|
|
|
static void autovac_balance_cost(void);
|
|
|
|
|
2007-03-29 00:17:12 +02:00
|
|
|
static void do_autovacuum(void);
|
2007-04-16 20:30:04 +02:00
|
|
|
static void FreeWorkerInfo(int code, Datum arg);
|
2007-03-29 00:17:12 +02:00
|
|
|
|
|
|
|
static void relation_check_autovac(Oid relid, Form_pg_class classForm,
|
|
|
|
Form_pg_autovacuum avForm, PgStat_StatTabEntry *tabentry,
|
|
|
|
List **table_oids, List **table_toast_list,
|
|
|
|
List **toast_oids);
|
|
|
|
static autovac_table *table_recheck_autovac(Oid relid);
|
|
|
|
static void relation_needs_vacanalyze(Oid relid, Form_pg_autovacuum avForm,
|
|
|
|
Form_pg_class classForm,
|
|
|
|
PgStat_StatTabEntry *tabentry, bool *dovacuum,
|
|
|
|
bool *doanalyze);
|
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
static void autovacuum_do_vac_analyze(Oid relid, bool dovacuum,
|
2007-05-30 22:12:03 +02:00
|
|
|
bool doanalyze, int freeze_min_age,
|
|
|
|
BufferAccessStrategy bstrategy);
|
2007-03-23 22:23:13 +01:00
|
|
|
static HeapTuple get_pg_autovacuum_tuple_relid(Relation avRel, Oid relid);
|
2007-03-27 22:36:03 +02:00
|
|
|
static PgStat_StatTabEntry *get_pgstat_tabentry_relid(Oid relid, bool isshared,
|
|
|
|
PgStat_StatDBEntry *shared,
|
|
|
|
PgStat_StatDBEntry *dbentry);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
static void autovac_report_activity(VacuumStmt *vacstmt, Oid relid);
|
2007-02-16 00:23:23 +01:00
|
|
|
static void avl_sighup_handler(SIGNAL_ARGS);
|
2007-04-16 20:30:04 +02:00
|
|
|
static void avl_sigusr1_handler(SIGNAL_ARGS);
|
2007-02-16 00:23:23 +01:00
|
|
|
static void avlauncher_shutdown(SIGNAL_ARGS);
|
|
|
|
static void avl_quickdie(SIGNAL_ARGS);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* AUTOVACUUM LAUNCHER CODE
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
2007-02-16 00:23:23 +01:00
|
|
|
* forkexec routine for the autovacuum launcher process.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
2007-02-16 00:23:23 +01:00
|
|
|
* Format up the arglist, then fork and exec.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
static pid_t
|
|
|
|
avlauncher_forkexec(void)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-02-16 00:23:23 +01:00
|
|
|
char *av[10];
|
|
|
|
int ac = 0;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
av[ac++] = "postgres";
|
|
|
|
av[ac++] = "--forkavlauncher";
|
|
|
|
av[ac++] = NULL; /* filled in by postmaster_forkexec */
|
|
|
|
av[ac] = NULL;
|
2006-07-10 18:20:52 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
Assert(ac < lengthof(av));
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
return postmaster_forkexec(ac, av);
|
|
|
|
}
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/*
|
|
|
|
* We need this set from the outside, before InitProcess is called
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AutovacuumLauncherIAm(void)
|
|
|
|
{
|
|
|
|
am_autovacuum_launcher = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main entry point for autovacuum launcher process, to be called from the
|
|
|
|
* postmaster.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
StartAutoVacLauncher(void)
|
|
|
|
{
|
|
|
|
pid_t AutoVacPID;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
2007-02-16 00:23:23 +01:00
|
|
|
switch ((AutoVacPID = avlauncher_forkexec()))
|
2005-07-14 07:13:45 +02:00
|
|
|
#else
|
2005-10-15 04:49:52 +02:00
|
|
|
switch ((AutoVacPID = fork_process()))
|
2005-07-14 07:13:45 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("could not fork autovacuum process: %m")));
|
2005-07-14 07:13:45 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef EXEC_BACKEND
|
|
|
|
case 0:
|
|
|
|
/* in postmaster child ... */
|
|
|
|
/* Close the postmaster's sockets */
|
|
|
|
ClosePostmasterPorts(false);
|
|
|
|
|
2006-01-04 22:06:32 +01:00
|
|
|
/* Lose the postmaster's on-exit routines */
|
|
|
|
on_exit_reset();
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
AutoVacLauncherMain(0, NULL);
|
2005-07-14 07:13:45 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return (int) AutoVacPID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shouldn't get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-16 00:23:23 +01:00
|
|
|
* Main loop for the autovacuum launcher process.
|
2007-04-16 20:30:04 +02:00
|
|
|
*
|
|
|
|
* The signalling between launcher and worker is as follows:
|
|
|
|
*
|
|
|
|
* When the worker has finished starting up, it stores its PID in wi_workerpid
|
|
|
|
* and sends a SIGUSR1 signal to the launcher. The launcher then knows that
|
|
|
|
* the postmaster is ready to start a new worker. We do it this way because
|
|
|
|
* otherwise we risk calling SendPostmasterSignal() when the postmaster hasn't
|
|
|
|
* yet processed the last one, in which case the second signal would be lost.
|
|
|
|
* This is only useful when two workers need to be started close to one
|
|
|
|
* another, which should be rare but it's possible.
|
|
|
|
*
|
|
|
|
* When a worker exits, it resets the WorkerInfo struct and puts it back into
|
|
|
|
* the free list. If there is no free worker slot, it will also signal the
|
|
|
|
* launcher, which then wakes up and can launch a new worker if it needs to.
|
|
|
|
* Note that we only need to do it when there's no free worker slot, because
|
|
|
|
* otherwise there is no need -- the launcher would be awakened normally per
|
|
|
|
* schedule.
|
|
|
|
*
|
|
|
|
* There is a potential problem if, for some reason, a worker starts and is not
|
|
|
|
* able to bootstrap itself correctly. To prevent this situation from starving
|
|
|
|
* the whole system, the launcher checks the launch time of the "starting
|
|
|
|
* worker". If it's too old (older than autovacuum_naptime seconds), it resets
|
|
|
|
* the worker entry and puts it back into the free list.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
NON_EXEC_STATIC void
|
|
|
|
AutoVacLauncherMain(int argc, char *argv[])
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-02-16 00:23:23 +01:00
|
|
|
sigjmp_buf local_sigjmp_buf;
|
|
|
|
|
|
|
|
/* we are a postmaster subprocess now */
|
|
|
|
IsUnderPostmaster = true;
|
|
|
|
am_autovacuum_launcher = true;
|
|
|
|
|
|
|
|
/* reset MyProcPid */
|
|
|
|
MyProcPid = getpid();
|
|
|
|
|
|
|
|
/* Identify myself via ps */
|
|
|
|
init_ps_display("autovacuum launcher process", "", "", "");
|
|
|
|
|
|
|
|
SetProcessingMode(InitProcessing);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
|
|
|
* can signal any child processes too. (autovacuum probably never has
|
|
|
|
* any child processes, but for consistency we make all postmaster
|
|
|
|
* child processes do this.)
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2007-03-07 14:35:03 +01:00
|
|
|
* Set up signal handlers. Since this is an auxiliary process, it has
|
2007-02-16 00:23:23 +01:00
|
|
|
* particular signal requirements -- no deadlock checker or sinval
|
|
|
|
* catchup, for example.
|
|
|
|
*/
|
|
|
|
pqsignal(SIGHUP, avl_sighup_handler);
|
|
|
|
|
|
|
|
pqsignal(SIGINT, SIG_IGN);
|
|
|
|
pqsignal(SIGTERM, avlauncher_shutdown);
|
|
|
|
pqsignal(SIGQUIT, avl_quickdie);
|
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
|
|
|
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
2007-04-16 20:30:04 +02:00
|
|
|
pqsignal(SIGUSR1, avl_sigusr1_handler);
|
2007-02-16 00:23:23 +01:00
|
|
|
/* We don't listen for async notifies */
|
|
|
|
pqsignal(SIGUSR2, SIG_IGN);
|
|
|
|
pqsignal(SIGFPE, FloatExceptionHandler);
|
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
|
|
|
|
/* Early initialization */
|
|
|
|
BaseInit();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a per-backend PGPROC struct in shared memory, except in the
|
|
|
|
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
|
|
|
|
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
|
|
|
|
* had to do some stuff with LWLocks).
|
|
|
|
*/
|
|
|
|
#ifndef EXEC_BACKEND
|
2007-03-07 14:35:03 +01:00
|
|
|
InitAuxiliaryProcess();
|
2007-02-16 00:23:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a memory context that we will do all our work in. We do this so
|
|
|
|
* that we can reset the context during error recovery and thereby avoid
|
|
|
|
* possible memory leaks.
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"Autovacuum Launcher",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
MemoryContextSwitchTo(AutovacMemCxt);
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If an exception is encountered, processing resumes here.
|
|
|
|
*
|
|
|
|
* This code is heavily based on bgwriter.c, q.v.
|
|
|
|
*/
|
|
|
|
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
|
|
|
|
{
|
|
|
|
/* since not using PG_TRY, must reset error stack by hand */
|
|
|
|
error_context_stack = NULL;
|
|
|
|
|
|
|
|
/* Prevents interrupts while cleaning up */
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
|
|
|
/* Report the error to the server log */
|
|
|
|
EmitErrorReport();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These operations are really just a minimal subset of
|
|
|
|
* AbortTransaction(). We don't have very many resources to worry
|
|
|
|
* about, but we do have LWLocks.
|
|
|
|
*/
|
|
|
|
LWLockReleaseAll();
|
|
|
|
AtEOXact_Files();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now return to normal top-level context and clear ErrorContext for
|
|
|
|
* next time.
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
MemoryContextSwitchTo(AutovacMemCxt);
|
2007-02-16 00:23:23 +01:00
|
|
|
FlushErrorState();
|
|
|
|
|
|
|
|
/* Flush any leaked data in the top-level context */
|
2007-04-16 20:30:04 +02:00
|
|
|
MemoryContextResetAndDeleteChildren(AutovacMemCxt);
|
|
|
|
|
|
|
|
/* don't leave dangling pointers to freed memory */
|
|
|
|
DatabaseListCxt = NULL;
|
|
|
|
DatabaseList = NULL;
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
/* Make sure pgstat also considers our stat data as gone */
|
|
|
|
pgstat_clear_snapshot();
|
|
|
|
|
|
|
|
/* Now we can allow interrupts again */
|
|
|
|
RESUME_INTERRUPTS();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sleep at least 1 second after any error. We don't want to be
|
|
|
|
* filling the error logs as fast as we can.
|
|
|
|
*/
|
|
|
|
pg_usleep(1000000L);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can now handle ereport(ERROR) */
|
|
|
|
PG_exception_stack = &local_sigjmp_buf;
|
|
|
|
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("autovacuum launcher started")));
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* must unblock signals before calling rebuild_database_list */
|
2007-02-16 00:23:23 +01:00
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* in emergency mode, just start a worker and go away */
|
|
|
|
if (!autovacuum_start_daemon)
|
|
|
|
{
|
|
|
|
do_start_worker();
|
|
|
|
proc_exit(0); /* done */
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoVacuumShmem->av_launcherpid = MyProcPid;
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/*
|
2007-04-16 20:30:04 +02:00
|
|
|
* Create the initial database list. The invariant we want this list to
|
|
|
|
* keep is that it's ordered by decreasing next_time. As soon as an entry
|
|
|
|
* is updated to a higher time, it will be moved to the front (which is
|
|
|
|
* correct because the only operation is to add autovacuum_naptime to the
|
|
|
|
* entry, and time always increases).
|
2007-02-16 00:23:23 +01:00
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
rebuild_database_list(InvalidOid);
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
uint64 micros;
|
|
|
|
bool can_launch;
|
|
|
|
TimestampTz current_time = 0;
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Emergency bailout if postmaster has died. This is to avoid the
|
|
|
|
* necessity for manual cleanup of all postmaster children.
|
|
|
|
*/
|
|
|
|
if (!PostmasterIsAlive(true))
|
|
|
|
exit(1);
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
micros = launcher_determine_sleep(AutoVacuumShmem->av_freeWorkers !=
|
|
|
|
INVALID_OFFSET, false);
|
|
|
|
|
|
|
|
/* Sleep for a while according to schedule */
|
|
|
|
pg_usleep(micros);
|
|
|
|
|
|
|
|
/* the normal shutdown case */
|
2007-02-16 00:23:23 +01:00
|
|
|
if (avlauncher_shutdown_request)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
2007-04-16 20:30:04 +02:00
|
|
|
|
|
|
|
/* rebalance in case the default cost parameters changed */
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
autovac_balance_cost();
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
/* rebuild the list in case the naptime changed */
|
|
|
|
rebuild_database_list(InvalidOid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a worker started up or finished */
|
|
|
|
if (got_SIGUSR1)
|
|
|
|
{
|
|
|
|
got_SIGUSR1 = false;
|
|
|
|
|
|
|
|
/* rebalance cost limits, if needed */
|
|
|
|
if (AutoVacuumShmem->av_rebalance)
|
|
|
|
{
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
AutoVacuumShmem->av_rebalance = false;
|
|
|
|
autovac_balance_cost();
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
}
|
2007-02-16 00:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-16 20:30:04 +02:00
|
|
|
* There are some conditions that we need to check before trying to
|
|
|
|
* start a launcher. First, we need to make sure that there is a
|
|
|
|
* launcher slot available. Second, we need to make sure that no other
|
|
|
|
* worker is still starting up.
|
2007-02-16 00:23:23 +01:00
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
LWLockAcquire(AutovacuumLock, LW_SHARED);
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
can_launch = (AutoVacuumShmem->av_freeWorkers != INVALID_OFFSET);
|
2007-02-16 00:23:23 +01:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
if (can_launch && AutoVacuumShmem->av_startingWorker != INVALID_OFFSET)
|
|
|
|
{
|
|
|
|
WorkerInfo worker = (WorkerInfo) MAKE_PTR(AutoVacuumShmem->av_startingWorker);
|
|
|
|
|
|
|
|
if (current_time == 0)
|
|
|
|
current_time = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't launch another worker when another one is still
|
|
|
|
* starting up, so just sleep for a bit more; that worker will wake
|
|
|
|
* us up again as soon as it's ready. We will only wait
|
|
|
|
* autovacuum_naptime seconds for this to happen however. Note
|
|
|
|
* that failure to connect to a particular database is not a
|
|
|
|
* problem here, because the worker removes itself from the
|
|
|
|
* startingWorker pointer before trying to connect; only low-level
|
|
|
|
* problems, like fork() failure, can get us here.
|
|
|
|
*/
|
2007-05-02 20:27:57 +02:00
|
|
|
if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time,
|
|
|
|
autovacuum_naptime * 1000))
|
2007-04-16 20:30:04 +02:00
|
|
|
{
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
/*
|
|
|
|
* No other process can put a worker in starting mode, so if
|
|
|
|
* startingWorker is still INVALID after exchanging our lock,
|
|
|
|
* we assume it's the same one we saw above (so we don't
|
|
|
|
* recheck the launch time).
|
|
|
|
*/
|
|
|
|
if (AutoVacuumShmem->av_startingWorker != INVALID_OFFSET)
|
|
|
|
{
|
|
|
|
worker = (WorkerInfo) MAKE_PTR(AutoVacuumShmem->av_startingWorker);
|
|
|
|
worker->wi_dboid = InvalidOid;
|
|
|
|
worker->wi_tableoid = InvalidOid;
|
|
|
|
worker->wi_workerpid = 0;
|
|
|
|
worker->wi_launchtime = 0;
|
|
|
|
worker->wi_links.next = AutoVacuumShmem->av_freeWorkers;
|
|
|
|
AutoVacuumShmem->av_freeWorkers = MAKE_OFFSET(worker);
|
|
|
|
AutoVacuumShmem->av_startingWorker = INVALID_OFFSET;
|
|
|
|
}
|
|
|
|
}
|
2007-02-16 00:23:23 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2007-04-16 20:30:04 +02:00
|
|
|
* maybe the postmaster neglected this start signal --
|
|
|
|
* resend it. Note: the constraints in
|
|
|
|
* launcher_determine_sleep keep us from delivering signals too
|
|
|
|
* quickly (at most once every 100ms).
|
2007-02-16 00:23:23 +01:00
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
|
|
|
|
can_launch = false;
|
2007-02-16 00:23:23 +01:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 20:30:04 +02:00
|
|
|
LWLockRelease(AutovacuumLock); /* either shared or exclusive */
|
2007-02-16 00:23:23 +01:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
if (can_launch)
|
|
|
|
{
|
|
|
|
Dlelem *elem;
|
2007-02-16 00:23:23 +01:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
elem = DLGetTail(DatabaseList);
|
2007-02-16 00:23:23 +01:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
if (current_time == 0)
|
|
|
|
current_time = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
if (elem != NULL)
|
|
|
|
{
|
|
|
|
avl_dbase *avdb = DLE_VAL(elem);
|
|
|
|
|
2007-05-02 20:27:57 +02:00
|
|
|
/*
|
|
|
|
* launch a worker if next_worker is right now or it is in the
|
|
|
|
* past
|
|
|
|
*/
|
|
|
|
if (TimestampDifferenceExceeds(avdb->adl_next_worker,
|
|
|
|
current_time, 0))
|
2007-04-16 20:30:04 +02:00
|
|
|
launch_worker(current_time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Special case when the list is empty: start a worker right
|
|
|
|
* away. This covers the initial case, when no database is in
|
|
|
|
* pgstats (thus the list is empty). Note that the constraints
|
|
|
|
* in launcher_determine_sleep keep us from starting workers
|
|
|
|
* too quickly (at most once every autovacuum_naptime when the
|
|
|
|
* list is empty).
|
|
|
|
*/
|
|
|
|
launch_worker(current_time);
|
|
|
|
}
|
|
|
|
}
|
2007-02-16 00:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Normal exit from the autovac launcher is here */
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("autovacuum launcher shutting down")));
|
2007-04-16 20:30:04 +02:00
|
|
|
AutoVacuumShmem->av_launcherpid = 0;
|
2007-02-16 00:23:23 +01:00
|
|
|
|
|
|
|
proc_exit(0); /* done */
|
|
|
|
}
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
|
|
|
* Determine the time to sleep, in microseconds, based on the database list.
|
|
|
|
*
|
|
|
|
* The "canlaunch" parameter indicates whether we can start a worker right now,
|
|
|
|
* for example due to the workers being all busy.
|
|
|
|
*/
|
|
|
|
static uint64
|
|
|
|
launcher_determine_sleep(bool canlaunch, bool recursing)
|
|
|
|
{
|
|
|
|
long secs;
|
|
|
|
int usecs;
|
|
|
|
Dlelem *elem;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We sleep until the next scheduled vacuum. We trust that when the
|
|
|
|
* database list was built, care was taken so that no entries have times in
|
|
|
|
* the past; if the first entry has too close a next_worker value, or a
|
|
|
|
* time in the past, we will sleep a small nominal time.
|
|
|
|
*/
|
|
|
|
if (!canlaunch)
|
|
|
|
{
|
|
|
|
secs = autovacuum_naptime;
|
|
|
|
usecs = 0;
|
|
|
|
}
|
|
|
|
else if ((elem = DLGetTail(DatabaseList)) != NULL)
|
|
|
|
{
|
|
|
|
avl_dbase *avdb = DLE_VAL(elem);
|
|
|
|
TimestampTz current_time = GetCurrentTimestamp();
|
|
|
|
TimestampTz next_wakeup;
|
|
|
|
|
|
|
|
next_wakeup = avdb->adl_next_worker;
|
|
|
|
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* list is empty, sleep for whole autovacuum_naptime seconds */
|
|
|
|
secs = autovacuum_naptime;
|
|
|
|
usecs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the result is exactly zero, it means a database had an entry with
|
|
|
|
* time in the past. Rebuild the list so that the databases are evenly
|
|
|
|
* distributed again, and recalculate the time to sleep. This can happen
|
|
|
|
* if there are more tables needing vacuum than workers, and they all take
|
|
|
|
* longer to vacuum than autovacuum_naptime.
|
|
|
|
*
|
|
|
|
* We only recurse once. rebuild_database_list should always return times
|
|
|
|
* in the future, but it seems best not to trust too much on that.
|
|
|
|
*/
|
|
|
|
if (secs == 0L && usecs == 0 && !recursing)
|
|
|
|
{
|
|
|
|
rebuild_database_list(InvalidOid);
|
|
|
|
return launcher_determine_sleep(canlaunch, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 100ms is the smallest time we'll allow the launcher to sleep */
|
|
|
|
if (secs <= 0L && usecs <= 100000)
|
|
|
|
{
|
|
|
|
secs = 0L;
|
|
|
|
usecs = 100000; /* 100 ms */
|
|
|
|
}
|
|
|
|
|
|
|
|
return secs * 1000000 + usecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build an updated DatabaseList. It must only contain databases that appear
|
|
|
|
* in pgstats, and must be sorted by next_worker from highest to lowest,
|
|
|
|
* distributed regularly across the next autovacuum_naptime interval.
|
|
|
|
*
|
|
|
|
* Receives the Oid of the database that made this list be generated (we call
|
|
|
|
* this the "new" database, because when the database was already present on
|
|
|
|
* the list, we expect that this function is not called at all). The
|
|
|
|
* preexisting list, if any, will be used to preserve the order of the
|
|
|
|
* databases in the autovacuum_naptime period. The new database is put at the
|
|
|
|
* end of the interval. The actual values are not saved, which should not be
|
|
|
|
* much of a problem.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rebuild_database_list(Oid newdb)
|
|
|
|
{
|
|
|
|
List *dblist;
|
|
|
|
ListCell *cell;
|
|
|
|
MemoryContext newcxt;
|
|
|
|
MemoryContext oldcxt;
|
|
|
|
MemoryContext tmpcxt;
|
|
|
|
HASHCTL hctl;
|
|
|
|
int score;
|
|
|
|
int nelems;
|
|
|
|
HTAB *dbhash;
|
|
|
|
|
|
|
|
/* use fresh stats */
|
|
|
|
pgstat_clear_snapshot();
|
|
|
|
|
|
|
|
newcxt = AllocSetContextCreate(AutovacMemCxt,
|
|
|
|
"AV dblist",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
tmpcxt = AllocSetContextCreate(newcxt,
|
|
|
|
"tmp AV dblist",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
oldcxt = MemoryContextSwitchTo(tmpcxt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementing this is not as simple as it sounds, because we need to put
|
|
|
|
* the new database at the end of the list; next the databases that were
|
|
|
|
* already on the list, and finally (at the tail of the list) all the other
|
|
|
|
* databases that are not on the existing list.
|
|
|
|
*
|
|
|
|
* To do this, we build an empty hash table of scored databases. We will
|
|
|
|
* start with the lowest score (zero) for the new database, then increasing
|
|
|
|
* scores for the databases in the existing list, in order, and lastly
|
|
|
|
* increasing scores for all databases gotten via get_database_list() that
|
|
|
|
* are not already on the hash.
|
|
|
|
*
|
|
|
|
* Then we will put all the hash elements into an array, sort the array by
|
|
|
|
* score, and finally put the array elements into the new doubly linked
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
hctl.keysize = sizeof(Oid);
|
|
|
|
hctl.entrysize = sizeof(avl_dbase);
|
|
|
|
hctl.hash = oid_hash;
|
|
|
|
hctl.hcxt = tmpcxt;
|
|
|
|
dbhash = hash_create("db hash", 20, &hctl, /* magic number here FIXME */
|
|
|
|
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
|
|
|
|
|
|
|
|
/* start by inserting the new database */
|
|
|
|
score = 0;
|
|
|
|
if (OidIsValid(newdb))
|
|
|
|
{
|
|
|
|
avl_dbase *db;
|
|
|
|
PgStat_StatDBEntry *entry;
|
|
|
|
|
|
|
|
/* only consider this database if it has a pgstat entry */
|
|
|
|
entry = pgstat_fetch_stat_dbentry(newdb);
|
|
|
|
if (entry != NULL)
|
|
|
|
{
|
|
|
|
/* we assume it isn't found because the hash was just created */
|
|
|
|
db = hash_search(dbhash, &newdb, HASH_ENTER, NULL);
|
|
|
|
|
|
|
|
/* hash_search already filled in the key */
|
|
|
|
db->adl_score = score++;
|
|
|
|
/* next_worker is filled in later */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now insert the databases from the existing list */
|
|
|
|
if (DatabaseList != NULL)
|
|
|
|
{
|
|
|
|
Dlelem *elem;
|
|
|
|
|
|
|
|
elem = DLGetHead(DatabaseList);
|
|
|
|
while (elem != NULL)
|
|
|
|
{
|
|
|
|
avl_dbase *avdb = DLE_VAL(elem);
|
|
|
|
avl_dbase *db;
|
|
|
|
bool found;
|
|
|
|
PgStat_StatDBEntry *entry;
|
|
|
|
|
|
|
|
elem = DLGetSucc(elem);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip databases with no stat entries -- in particular, this
|
|
|
|
* gets rid of dropped databases
|
|
|
|
*/
|
|
|
|
entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
|
|
|
|
if (entry == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* hash_search already filled in the key */
|
|
|
|
db->adl_score = score++;
|
|
|
|
/* next_worker is filled in later */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally, insert all qualifying databases not previously inserted */
|
|
|
|
dblist = get_database_list();
|
|
|
|
foreach(cell, dblist)
|
|
|
|
{
|
|
|
|
avw_dbase *avdb = lfirst(cell);
|
|
|
|
avl_dbase *db;
|
|
|
|
bool found;
|
|
|
|
PgStat_StatDBEntry *entry;
|
|
|
|
|
|
|
|
/* only consider databases with a pgstat entry */
|
|
|
|
entry = pgstat_fetch_stat_dbentry(avdb->adw_datid);
|
|
|
|
if (entry == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
db = hash_search(dbhash, &(avdb->adw_datid), HASH_ENTER, &found);
|
|
|
|
/* only update the score if the database was not already on the hash */
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* hash_search already filled in the key */
|
|
|
|
db->adl_score = score++;
|
|
|
|
/* next_worker is filled in later */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nelems = score;
|
|
|
|
|
|
|
|
/* from here on, the allocated memory belongs to the new list */
|
|
|
|
MemoryContextSwitchTo(newcxt);
|
|
|
|
DatabaseList = DLNewList();
|
|
|
|
|
|
|
|
if (nelems > 0)
|
|
|
|
{
|
|
|
|
TimestampTz current_time;
|
|
|
|
int millis_increment;
|
|
|
|
avl_dbase *dbary;
|
|
|
|
avl_dbase *db;
|
|
|
|
HASH_SEQ_STATUS seq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* put all the hash elements into an array */
|
|
|
|
dbary = palloc(nelems * sizeof(avl_dbase));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
hash_seq_init(&seq, dbhash);
|
|
|
|
while ((db = hash_seq_search(&seq)) != NULL)
|
|
|
|
memcpy(&(dbary[i++]), db, sizeof(avl_dbase));
|
|
|
|
|
|
|
|
/* sort the array */
|
|
|
|
qsort(dbary, nelems, sizeof(avl_dbase), db_comparator);
|
|
|
|
|
|
|
|
/* this is the time interval between databases in the schedule */
|
|
|
|
millis_increment = 1000.0 * autovacuum_naptime / nelems;
|
|
|
|
current_time = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* move the elements from the array into the dllist, setting the
|
|
|
|
* next_worker while walking the array
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nelems; i++)
|
|
|
|
{
|
|
|
|
avl_dbase *db = &(dbary[i]);
|
|
|
|
Dlelem *elem;
|
|
|
|
|
|
|
|
current_time = TimestampTzPlusMilliseconds(current_time,
|
|
|
|
millis_increment);
|
|
|
|
db->adl_next_worker = current_time;
|
|
|
|
|
|
|
|
elem = DLNewElem(db);
|
|
|
|
/* later elements should go closer to the head of the list */
|
|
|
|
DLAddHead(DatabaseList, elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all done, clean up memory */
|
|
|
|
if (DatabaseListCxt != NULL)
|
|
|
|
MemoryContextDelete(DatabaseListCxt);
|
|
|
|
MemoryContextDelete(tmpcxt);
|
|
|
|
DatabaseListCxt = newcxt;
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* qsort comparator for avl_dbase, using adl_score */
|
|
|
|
static int
|
|
|
|
db_comparator(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((avl_dbase *) a)->adl_score == ((avl_dbase *) b)->adl_score)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (((avl_dbase *) a)->adl_score < ((avl_dbase *) b)->adl_score) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2007-03-23 22:45:17 +01:00
|
|
|
/*
|
|
|
|
* do_start_worker
|
|
|
|
*
|
|
|
|
* Bare-bones procedure for starting an autovacuum worker from the launcher.
|
|
|
|
* It determines what database to work on, sets up shared memory stuff and
|
2007-04-16 20:30:04 +02:00
|
|
|
* signals postmaster to start the worker. It fails gracefully if invoked when
|
|
|
|
* autovacuum_workers are already active.
|
|
|
|
*
|
|
|
|
* Return value is the OID of the database that the worker is going to process,
|
|
|
|
* or InvalidOid if no worker was actually started.
|
2007-03-23 22:45:17 +01:00
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
static Oid
|
2007-03-23 22:45:17 +01:00
|
|
|
do_start_worker(void)
|
|
|
|
{
|
|
|
|
List *dblist;
|
2007-04-16 20:30:04 +02:00
|
|
|
ListCell *cell;
|
2007-03-23 22:45:17 +01:00
|
|
|
TransactionId xidForceLimit;
|
2007-04-16 20:30:04 +02:00
|
|
|
bool for_xid_wrap;
|
|
|
|
avw_dbase *avdb;
|
|
|
|
TimestampTz current_time;
|
|
|
|
bool skipit = false;
|
|
|
|
|
|
|
|
/* return quickly when there are no free workers */
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_SHARED);
|
|
|
|
if (AutoVacuumShmem->av_freeWorkers == INVALID_OFFSET)
|
|
|
|
{
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
return InvalidOid;
|
|
|
|
}
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
/* use fresh stats */
|
|
|
|
pgstat_clear_snapshot();
|
2007-03-23 22:45:17 +01:00
|
|
|
|
|
|
|
/* Get a list of databases */
|
2007-04-16 20:30:04 +02:00
|
|
|
dblist = get_database_list();
|
2007-03-23 22:45:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the oldest datfrozenxid/relfrozenxid that we will allow
|
|
|
|
* to pass without forcing a vacuum. (This limit can be tightened for
|
|
|
|
* particular tables, but not loosened.)
|
|
|
|
*/
|
|
|
|
recentXid = ReadNewTransactionId();
|
|
|
|
xidForceLimit = recentXid - autovacuum_freeze_max_age;
|
|
|
|
/* ensure it's a "normal" XID, else TransactionIdPrecedes misbehaves */
|
|
|
|
if (xidForceLimit < FirstNormalTransactionId)
|
|
|
|
xidForceLimit -= FirstNormalTransactionId;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Choose a database to connect to. We pick the database that was least
|
|
|
|
* recently auto-vacuumed, or one that needs vacuuming to prevent Xid
|
|
|
|
* wraparound-related data loss. If any db at risk of wraparound is
|
|
|
|
* found, we pick the one with oldest datfrozenxid, independently of
|
|
|
|
* autovacuum times.
|
|
|
|
*
|
|
|
|
* Note that a database with no stats entry is not considered, except for
|
|
|
|
* Xid wraparound purposes. The theory is that if no one has ever
|
|
|
|
* connected to it since the stats were last initialized, it doesn't need
|
|
|
|
* vacuuming.
|
|
|
|
*
|
|
|
|
* XXX This could be improved if we had more info about whether it needs
|
|
|
|
* vacuuming before connecting to it. Perhaps look through the pgstats
|
|
|
|
* data for the database's tables? One idea is to keep track of the
|
|
|
|
* number of new and dead tuples per database in pgstats. However it
|
|
|
|
* isn't clear how to construct a metric that measures that and not cause
|
|
|
|
* starvation for less busy databases.
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
avdb = NULL;
|
2007-03-23 22:45:17 +01:00
|
|
|
for_xid_wrap = false;
|
2007-04-16 20:30:04 +02:00
|
|
|
current_time = GetCurrentTimestamp();
|
2007-03-23 22:45:17 +01:00
|
|
|
foreach(cell, dblist)
|
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
avw_dbase *tmp = lfirst(cell);
|
|
|
|
Dlelem *elem;
|
2007-03-23 22:45:17 +01:00
|
|
|
|
|
|
|
/* Find pgstat entry if any */
|
2007-04-16 20:30:04 +02:00
|
|
|
tmp->adw_entry = pgstat_fetch_stat_dbentry(tmp->adw_datid);
|
2007-03-23 22:45:17 +01:00
|
|
|
|
|
|
|
/* Check to see if this one is at risk of wraparound */
|
2007-04-16 20:30:04 +02:00
|
|
|
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
|
2007-03-23 22:45:17 +01:00
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
if (avdb == NULL ||
|
|
|
|
TransactionIdPrecedes(tmp->adw_frozenxid, avdb->adw_frozenxid))
|
|
|
|
avdb = tmp;
|
2007-03-23 22:45:17 +01:00
|
|
|
for_xid_wrap = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (for_xid_wrap)
|
|
|
|
continue; /* ignore not-at-risk DBs */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, skip a database with no pgstat entry; it means it
|
|
|
|
* hasn't seen any activity.
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
if (!tmp->adw_entry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also, skip a database that appears on the database list as having
|
|
|
|
* been processed recently (less than autovacuum_naptime seconds ago).
|
|
|
|
* We do this so that we don't select a database which we just
|
|
|
|
* selected, but that pgstat hasn't gotten around to updating the last
|
|
|
|
* autovacuum time yet.
|
|
|
|
*/
|
|
|
|
skipit = false;
|
|
|
|
elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
|
|
|
|
|
|
|
|
while (elem != NULL)
|
|
|
|
{
|
|
|
|
avl_dbase *dbp = DLE_VAL(elem);
|
|
|
|
|
|
|
|
if (dbp->adl_datid == tmp->adw_datid)
|
|
|
|
{
|
|
|
|
/*
|
2007-05-02 20:27:57 +02:00
|
|
|
* Skip this database if its next_worker value falls between
|
2007-04-16 20:30:04 +02:00
|
|
|
* the current time and the current time plus naptime.
|
|
|
|
*/
|
2007-05-07 22:41:24 +02:00
|
|
|
if (!TimestampDifferenceExceeds(dbp->adl_next_worker,
|
|
|
|
current_time, 0) &&
|
2007-05-02 20:27:57 +02:00
|
|
|
!TimestampDifferenceExceeds(current_time,
|
|
|
|
dbp->adl_next_worker,
|
|
|
|
autovacuum_naptime * 1000))
|
2007-04-16 20:30:04 +02:00
|
|
|
skipit = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
elem = DLGetPred(elem);
|
|
|
|
}
|
|
|
|
if (skipit)
|
2007-03-23 22:45:17 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember the db with oldest autovac time. (If we are here,
|
|
|
|
* both tmp->entry and db->entry must be non-null.)
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
if (avdb == NULL ||
|
|
|
|
tmp->adw_entry->last_autovac_time < avdb->adw_entry->last_autovac_time)
|
|
|
|
avdb = tmp;
|
2007-03-23 22:45:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Found a database -- process it */
|
2007-04-16 20:30:04 +02:00
|
|
|
if (avdb != NULL)
|
2007-03-23 22:45:17 +01:00
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
WorkerInfo worker;
|
|
|
|
SHMEM_OFFSET sworker;
|
|
|
|
|
2007-03-23 22:45:17 +01:00
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
2007-04-16 20:30:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a worker entry from the freelist. We checked above, so there
|
|
|
|
* really should be a free slot -- complain very loudly if there isn't.
|
|
|
|
*/
|
|
|
|
sworker = AutoVacuumShmem->av_freeWorkers;
|
|
|
|
if (sworker == INVALID_OFFSET)
|
|
|
|
elog(FATAL, "no free worker found");
|
|
|
|
|
|
|
|
worker = (WorkerInfo) MAKE_PTR(sworker);
|
|
|
|
AutoVacuumShmem->av_freeWorkers = worker->wi_links.next;
|
|
|
|
|
|
|
|
worker->wi_dboid = avdb->adw_datid;
|
|
|
|
worker->wi_workerpid = 0;
|
|
|
|
worker->wi_launchtime = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
AutoVacuumShmem->av_startingWorker = sworker;
|
|
|
|
|
2007-03-23 22:45:17 +01:00
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
|
2007-04-16 20:30:04 +02:00
|
|
|
|
|
|
|
return avdb->adw_datid;
|
|
|
|
}
|
|
|
|
else if (skipit)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we skipped all databases on the list, rebuild it, because it
|
|
|
|
* probably contains a dropped database.
|
|
|
|
*/
|
|
|
|
rebuild_database_list(InvalidOid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return InvalidOid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* launch_worker
|
|
|
|
*
|
|
|
|
* Wrapper for starting a worker from the launcher. Besides actually starting
|
|
|
|
* it, update the database list to reflect the next time that another one will
|
|
|
|
* need to be started on the selected database. The actual database choice is
|
|
|
|
* left to do_start_worker.
|
|
|
|
*
|
|
|
|
* This routine is also expected to insert an entry into the database list if
|
|
|
|
* the selected database was previously absent from the list. It returns the
|
|
|
|
* new database list.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
launch_worker(TimestampTz now)
|
|
|
|
{
|
|
|
|
Oid dbid;
|
|
|
|
Dlelem *elem;
|
|
|
|
|
|
|
|
dbid = do_start_worker();
|
|
|
|
if (OidIsValid(dbid))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Walk the database list and update the corresponding entry. If the
|
|
|
|
* database is not on the list, we'll recreate the list.
|
|
|
|
*/
|
|
|
|
elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
|
|
|
|
while (elem != NULL)
|
|
|
|
{
|
|
|
|
avl_dbase *avdb = DLE_VAL(elem);
|
|
|
|
|
|
|
|
if (avdb->adl_datid == dbid)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* add autovacuum_naptime seconds to the current time, and use
|
|
|
|
* that as the new "next_worker" field for this database.
|
|
|
|
*/
|
|
|
|
avdb->adl_next_worker =
|
|
|
|
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
|
|
|
|
|
|
|
|
DLMoveToFront(elem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
elem = DLGetSucc(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the database was not present in the database list, we rebuild the
|
|
|
|
* list. It's possible that the database does not get into the list
|
|
|
|
* anyway, for example if it's a database that doesn't have a pgstat
|
|
|
|
* entry, but this is not a problem because we don't want to schedule
|
|
|
|
* workers regularly into those in any case.
|
|
|
|
*/
|
|
|
|
if (elem == NULL)
|
|
|
|
rebuild_database_list(dbid);
|
2007-03-23 22:45:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/* SIGHUP: set flag to re-read config file at next convenient time */
|
|
|
|
static void
|
|
|
|
avl_sighup_handler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
got_SIGHUP = true;
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* SIGUSR1: a worker is up and running, or just finished */
|
|
|
|
static void
|
|
|
|
avl_sigusr1_handler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
got_SIGUSR1 = true;
|
|
|
|
}
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
static void
|
|
|
|
avlauncher_shutdown(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
avlauncher_shutdown_request = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* avl_quickdie occurs when signalled SIGQUIT from postmaster.
|
|
|
|
*
|
|
|
|
* Some backend has bought the farm, so we need to stop what we're doing
|
|
|
|
* and exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
avl_quickdie(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
PG_SETMASK(&BlockSig);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DO NOT proc_exit() -- we're here because shared memory may be
|
|
|
|
* corrupted, so we don't want to try to clean up our transaction. Just
|
|
|
|
* nail the windows shut and get out of town.
|
|
|
|
*
|
|
|
|
* Note we do exit(2) not exit(0). This is to force the postmaster into a
|
|
|
|
* system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
|
|
|
|
* backend. This is necessary precisely because we don't clean up our
|
|
|
|
* shared memory state.
|
|
|
|
*/
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* AUTOVACUUM WORKER CODE
|
|
|
|
********************************************************************/
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
/*
|
2007-02-16 00:23:23 +01:00
|
|
|
* forkexec routines for the autovacuum worker.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
2007-02-16 00:23:23 +01:00
|
|
|
* Format up the arglist, then fork and exec.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
static pid_t
|
2007-02-16 00:23:23 +01:00
|
|
|
avworker_forkexec(void)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
|
|
|
char *av[10];
|
|
|
|
int ac = 0;
|
|
|
|
|
|
|
|
av[ac++] = "postgres";
|
2007-02-16 00:23:23 +01:00
|
|
|
av[ac++] = "--forkavworker";
|
2005-10-15 04:49:52 +02:00
|
|
|
av[ac++] = NULL; /* filled in by postmaster_forkexec */
|
2005-07-14 07:13:45 +02:00
|
|
|
av[ac] = NULL;
|
|
|
|
|
|
|
|
Assert(ac < lengthof(av));
|
|
|
|
|
|
|
|
return postmaster_forkexec(ac, av);
|
|
|
|
}
|
2007-01-16 14:28:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need this set from the outside, before InitProcess is called
|
|
|
|
*/
|
|
|
|
void
|
2007-02-16 00:23:23 +01:00
|
|
|
AutovacuumWorkerIAm(void)
|
|
|
|
{
|
|
|
|
am_autovacuum_worker = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main entry point for autovacuum worker process.
|
|
|
|
*
|
|
|
|
* This code is heavily based on pgarch.c, q.v.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
StartAutoVacWorker(void)
|
2007-01-16 14:28:57 +01:00
|
|
|
{
|
2007-02-16 00:23:23 +01:00
|
|
|
pid_t worker_pid;
|
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
switch ((worker_pid = avworker_forkexec()))
|
|
|
|
#else
|
|
|
|
switch ((worker_pid = fork_process()))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("could not fork autovacuum process: %m")));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef EXEC_BACKEND
|
|
|
|
case 0:
|
|
|
|
/* in postmaster child ... */
|
|
|
|
/* Close the postmaster's sockets */
|
|
|
|
ClosePostmasterPorts(false);
|
|
|
|
|
|
|
|
/* Lose the postmaster's on-exit routines */
|
|
|
|
on_exit_reset();
|
|
|
|
|
|
|
|
AutoVacWorkerMain(0, NULL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return (int) worker_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shouldn't get here */
|
|
|
|
return 0;
|
2007-01-16 14:28:57 +01:00
|
|
|
}
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/*
|
2007-02-16 00:23:23 +01:00
|
|
|
* AutoVacWorkerMain
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
NON_EXEC_STATIC void
|
2007-02-16 00:23:23 +01:00
|
|
|
AutoVacWorkerMain(int argc, char *argv[])
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
sigjmp_buf local_sigjmp_buf;
|
2007-05-04 04:06:13 +02:00
|
|
|
Oid dbid;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/* we are a postmaster subprocess now */
|
|
|
|
IsUnderPostmaster = true;
|
2007-02-16 00:23:23 +01:00
|
|
|
am_autovacuum_worker = true;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/* reset MyProcPid */
|
|
|
|
MyProcPid = getpid();
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* Identify myself via ps */
|
2007-02-16 00:23:23 +01:00
|
|
|
init_ps_display("autovacuum worker process", "", "", "");
|
2005-08-11 23:11:50 +02:00
|
|
|
|
|
|
|
SetProcessingMode(InitProcessing);
|
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
|
|
|
* can signal any child processes too. (autovacuum probably never has
|
|
|
|
* any child processes, but for consistency we make all postmaster
|
|
|
|
* child processes do this.)
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Set up signal handlers. We operate on databases much like a regular
|
|
|
|
* backend, so we use the same signal handling. See equivalent code in
|
|
|
|
* tcop/postgres.c.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Currently, we don't pay attention to postgresql.conf changes that
|
|
|
|
* happen during a single daemon iteration, so we can ignore SIGHUP.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
pqsignal(SIGHUP, SIG_IGN);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Presently, SIGINT will lead to autovacuum shutdown, because that's how
|
|
|
|
* we handle ereport(ERROR). It could be improved however.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
pqsignal(SIGINT, StatementCancelHandler);
|
|
|
|
pqsignal(SIGTERM, die);
|
|
|
|
pqsignal(SIGQUIT, quickdie);
|
|
|
|
pqsignal(SIGALRM, handle_sig_alarm);
|
|
|
|
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
|
|
|
pqsignal(SIGUSR1, CatchupInterruptHandler);
|
|
|
|
/* We don't listen for async notifies */
|
|
|
|
pqsignal(SIGUSR2, SIG_IGN);
|
2005-08-11 23:11:50 +02:00
|
|
|
pqsignal(SIGFPE, FloatExceptionHandler);
|
2005-07-14 07:13:45 +02:00
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
|
|
|
|
/* Early initialization */
|
|
|
|
BaseInit();
|
|
|
|
|
2006-01-04 22:06:32 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Create a per-backend PGPROC struct in shared memory, except in the
|
|
|
|
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
|
|
|
|
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
|
|
|
|
* had to do some stuff with LWLocks).
|
2006-01-04 22:06:32 +01:00
|
|
|
*/
|
|
|
|
#ifndef EXEC_BACKEND
|
|
|
|
InitProcess();
|
|
|
|
#endif
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
|
|
|
* If an exception is encountered, processing resumes here.
|
|
|
|
*
|
|
|
|
* See notes in postgres.c about the design of this coding.
|
|
|
|
*/
|
|
|
|
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
|
|
|
|
{
|
|
|
|
/* Prevents interrupts while cleaning up */
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
|
|
|
/* Report the error to the server log */
|
|
|
|
EmitErrorReport();
|
|
|
|
|
|
|
|
/*
|
2007-01-16 14:28:57 +01:00
|
|
|
* We can now go away. Note that because we called InitProcess, a
|
|
|
|
* callback was registered to do ProcKill, which will clean up
|
2005-10-15 04:49:52 +02:00
|
|
|
* necessary state.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
proc_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can now handle ereport(ERROR) */
|
|
|
|
PG_exception_stack = &local_sigjmp_buf;
|
|
|
|
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
2006-03-07 18:32:22 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Force zero_damaged_pages OFF in the autovac process, even if it is set
|
|
|
|
* in postgresql.conf. We don't really want such a dangerous option being
|
|
|
|
* applied non-interactively.
|
2006-03-07 18:32:22 +01:00
|
|
|
*/
|
|
|
|
SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
|
|
|
|
|
2005-07-29 21:30:09 +02:00
|
|
|
/*
|
2007-04-16 20:30:04 +02:00
|
|
|
* Force statement_timeout to zero to avoid a timeout setting from
|
|
|
|
* preventing regular maintenance from being executed.
|
2005-07-29 21:30:09 +02:00
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
|
2005-07-29 21:30:09 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
|
|
|
* Get the info about the database we're going to work on.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
2007-05-02 17:47:14 +02:00
|
|
|
* beware of startingWorker being INVALID; this could happen if the
|
|
|
|
* launcher thinks we've taking too long to start.
|
2007-04-16 20:30:04 +02:00
|
|
|
*/
|
2007-05-02 17:47:14 +02:00
|
|
|
if (AutoVacuumShmem->av_startingWorker != INVALID_OFFSET)
|
|
|
|
{
|
|
|
|
MyWorkerInfo = (WorkerInfo) MAKE_PTR(AutoVacuumShmem->av_startingWorker);
|
|
|
|
dbid = MyWorkerInfo->wi_dboid;
|
|
|
|
MyWorkerInfo->wi_workerpid = MyProcPid;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
2007-05-02 17:47:14 +02:00
|
|
|
/* insert into the running list */
|
|
|
|
SHMQueueInsertBefore(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&MyWorkerInfo->wi_links);
|
|
|
|
/*
|
2007-05-04 04:06:13 +02:00
|
|
|
* remove from the "starting" pointer, so that the launcher can start
|
|
|
|
* a new worker if required
|
2007-05-02 17:47:14 +02:00
|
|
|
*/
|
|
|
|
AutoVacuumShmem->av_startingWorker = INVALID_OFFSET;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
2007-04-16 20:30:04 +02:00
|
|
|
|
2007-05-02 17:47:14 +02:00
|
|
|
on_shmem_exit(FreeWorkerInfo, 0);
|
|
|
|
|
|
|
|
/* wake up the launcher */
|
|
|
|
if (AutoVacuumShmem->av_launcherpid != 0)
|
|
|
|
kill(AutoVacuumShmem->av_launcherpid, SIGUSR1);
|
|
|
|
}
|
|
|
|
else
|
2007-05-04 04:06:13 +02:00
|
|
|
{
|
2007-05-02 17:47:14 +02:00
|
|
|
/* no worker entry for me, go away */
|
2007-05-04 04:06:13 +02:00
|
|
|
dbid = InvalidOid;
|
2007-05-02 17:47:14 +02:00
|
|
|
LWLockRelease(AutovacuumLock);
|
2007-05-04 04:06:13 +02:00
|
|
|
}
|
2007-04-16 20:30:04 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
if (OidIsValid(dbid))
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-02-16 00:23:23 +01:00
|
|
|
char *dbname;
|
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Report autovac startup to the stats collector. We deliberately do
|
|
|
|
* this before InitPostgres, so that the last_autovac_time will get
|
|
|
|
* updated even if the connection attempt fails. This is to prevent
|
|
|
|
* autovac from getting "stuck" repeatedly selecting an unopenable
|
|
|
|
* database, rather than making any progress on stuff it can connect
|
|
|
|
* to.
|
2005-08-15 18:25:19 +02:00
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
pgstat_report_autovac(dbid);
|
2005-08-15 18:25:19 +02:00
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
|
|
|
* Connect to the selected database
|
2006-04-06 22:38:00 +02:00
|
|
|
*
|
|
|
|
* Note: if we have selected a just-deleted database (due to using
|
|
|
|
* stale stats info), we'll fail and exit here.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
InitPostgres(NULL, dbid, NULL, &dbname);
|
2005-07-14 07:13:45 +02:00
|
|
|
SetProcessingMode(NormalProcessing);
|
2007-02-16 00:23:23 +01:00
|
|
|
set_ps_display(dbname, false);
|
2006-04-27 17:57:10 +02:00
|
|
|
ereport(DEBUG1,
|
2007-02-16 00:23:23 +01:00
|
|
|
(errmsg("autovacuum: processing database \"%s\"", dbname)));
|
2005-08-11 23:11:50 +02:00
|
|
|
|
|
|
|
/* Create the memory context where cross-transaction state is stored */
|
|
|
|
AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
|
2007-04-16 20:30:04 +02:00
|
|
|
"AV worker",
|
2005-08-11 23:11:50 +02:00
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/* And do an appropriate amount of work */
|
|
|
|
recentXid = ReadNewTransactionId();
|
2007-03-29 00:17:12 +02:00
|
|
|
do_autovacuum();
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/*
|
2007-05-02 17:47:14 +02:00
|
|
|
* The launcher will be notified of my death in ProcKill, *if* we managed
|
|
|
|
* to get a worker slot at all
|
2007-02-16 00:23:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* All done, go away */
|
2005-07-14 07:13:45 +02:00
|
|
|
proc_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-04 04:06:13 +02:00
|
|
|
* Return a WorkerInfo to the free list
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
static void
|
|
|
|
FreeWorkerInfo(int code, Datum arg)
|
|
|
|
{
|
|
|
|
if (MyWorkerInfo != NULL)
|
|
|
|
{
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this worker shuts down when there is no free worker slot, wake
|
|
|
|
* the launcher up so that he can launch a new worker immediately if
|
|
|
|
* required. We only save the launcher's PID in local memory here --
|
|
|
|
* the actual signal will be sent when the PGPROC is recycled, because
|
|
|
|
* that is when the new worker can actually be launched.
|
|
|
|
*
|
|
|
|
* We somewhat ignore the risk that the launcher changes its PID
|
|
|
|
* between we reading it and the actual kill; we expect ProcKill to be
|
|
|
|
* called shortly after us, and we assume that PIDs are not reused too
|
|
|
|
* quickly after a process exits.
|
|
|
|
*/
|
|
|
|
if (AutoVacuumShmem->av_freeWorkers == INVALID_OFFSET)
|
|
|
|
AutovacuumLauncherPid = AutoVacuumShmem->av_launcherpid;
|
|
|
|
|
|
|
|
SHMQueueDelete(&MyWorkerInfo->wi_links);
|
|
|
|
MyWorkerInfo->wi_links.next = AutoVacuumShmem->av_freeWorkers;
|
|
|
|
MyWorkerInfo->wi_dboid = InvalidOid;
|
|
|
|
MyWorkerInfo->wi_tableoid = InvalidOid;
|
|
|
|
MyWorkerInfo->wi_workerpid = 0;
|
|
|
|
MyWorkerInfo->wi_launchtime = 0;
|
|
|
|
MyWorkerInfo->wi_cost_delay = 0;
|
|
|
|
MyWorkerInfo->wi_cost_limit = 0;
|
|
|
|
MyWorkerInfo->wi_cost_limit_base = 0;
|
|
|
|
AutoVacuumShmem->av_freeWorkers = MAKE_OFFSET(MyWorkerInfo);
|
|
|
|
/* not mine anymore */
|
|
|
|
MyWorkerInfo = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now that we're inactive, cause a rebalancing of the surviving
|
|
|
|
* workers
|
|
|
|
*/
|
|
|
|
AutoVacuumShmem->av_rebalance = true;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the cost-based delay parameters, so that multiple workers consume
|
|
|
|
* each a fraction of the total available I/O.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AutoVacuumUpdateDelay(void)
|
|
|
|
{
|
|
|
|
if (MyWorkerInfo)
|
|
|
|
{
|
|
|
|
VacuumCostDelay = MyWorkerInfo->wi_cost_delay;
|
|
|
|
VacuumCostLimit = MyWorkerInfo->wi_cost_limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autovac_balance_cost
|
|
|
|
* Recalculate the cost limit setting for each active workers.
|
|
|
|
*
|
|
|
|
* Caller must hold the AutovacuumLock in exclusive mode.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
autovac_balance_cost(void)
|
|
|
|
{
|
|
|
|
WorkerInfo worker;
|
|
|
|
int vac_cost_limit = (autovacuum_vac_cost_limit >= 0 ?
|
|
|
|
autovacuum_vac_cost_limit : VacuumCostLimit);
|
|
|
|
int vac_cost_delay = (autovacuum_vac_cost_delay >= 0 ?
|
|
|
|
autovacuum_vac_cost_delay : VacuumCostDelay);
|
|
|
|
double cost_total;
|
|
|
|
double cost_avail;
|
|
|
|
|
|
|
|
/* not set? nothing to do */
|
|
|
|
if (vac_cost_limit <= 0 || vac_cost_delay <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* caculate the total base cost limit of active workers */
|
|
|
|
cost_total = 0.0;
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
while (worker)
|
|
|
|
{
|
|
|
|
if (worker->wi_workerpid != 0 &&
|
|
|
|
worker->wi_cost_limit_base > 0 && worker->wi_cost_delay > 0)
|
|
|
|
cost_total +=
|
|
|
|
(double) worker->wi_cost_limit_base / worker->wi_cost_delay;
|
|
|
|
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&worker->wi_links,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
}
|
|
|
|
/* there are no cost limits -- nothing to do */
|
|
|
|
if (cost_total <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust each cost limit of active workers to balance the total of
|
|
|
|
* cost limit to autovacuum_vacuum_cost_limit.
|
|
|
|
*/
|
|
|
|
cost_avail = (double) vac_cost_limit / vac_cost_delay;
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
while (worker)
|
|
|
|
{
|
|
|
|
if (worker->wi_workerpid != 0 &&
|
|
|
|
worker->wi_cost_limit_base > 0 && worker->wi_cost_delay > 0)
|
|
|
|
{
|
|
|
|
int limit = (int)
|
|
|
|
(cost_avail * worker->wi_cost_limit_base / cost_total);
|
|
|
|
|
|
|
|
worker->wi_cost_limit = Min(limit, worker->wi_cost_limit_base);
|
|
|
|
|
|
|
|
elog(DEBUG2, "autovac_balance_cost(pid=%u db=%u, rel=%u, cost_limit=%d, cost_delay=%d)",
|
|
|
|
worker->wi_workerpid, worker->wi_dboid,
|
|
|
|
worker->wi_tableoid, worker->wi_cost_limit, worker->wi_cost_delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&worker->wi_links,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_database_list
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Return a list of all databases. Note we cannot use pg_database,
|
2007-02-16 00:23:23 +01:00
|
|
|
* because we aren't connected; we use the flat database file.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
static List *
|
2007-04-16 20:30:04 +02:00
|
|
|
get_database_list(void)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
char *filename;
|
|
|
|
List *dblist = NIL;
|
|
|
|
char thisname[NAMEDATALEN];
|
|
|
|
FILE *db_file;
|
|
|
|
Oid db_id;
|
|
|
|
Oid db_tablespace;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
TransactionId db_frozenxid;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
filename = database_getflatfilename();
|
|
|
|
db_file = AllocateFile(filename, "r");
|
|
|
|
if (db_file == NULL)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\": %m", filename)));
|
|
|
|
|
2005-07-29 21:30:09 +02:00
|
|
|
while (read_pg_database_line(db_file, thisname, &db_id,
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
&db_tablespace, &db_frozenxid))
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
avw_dbase *avdb;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
avdb = (avw_dbase *) palloc(sizeof(avw_dbase));
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
avdb->adw_datid = db_id;
|
|
|
|
avdb->adw_name = pstrdup(thisname);
|
|
|
|
avdb->adw_frozenxid = db_frozenxid;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/* this gets set later: */
|
2007-04-16 20:30:04 +02:00
|
|
|
avdb->adw_entry = NULL;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-27 22:36:03 +02:00
|
|
|
dblist = lappend(dblist, avdb);
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeFile(db_file);
|
|
|
|
pfree(filename);
|
|
|
|
|
|
|
|
return dblist;
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/*
|
|
|
|
* Process a database table-by-table
|
2005-07-29 21:30:09 +02:00
|
|
|
*
|
2005-07-14 07:13:45 +02:00
|
|
|
* Note that CHECK_FOR_INTERRUPTS is supposed to be used in certain spots in
|
|
|
|
* order not to ignore shutdown commands for too long.
|
|
|
|
*/
|
|
|
|
static void
|
2007-03-29 00:17:12 +02:00
|
|
|
do_autovacuum(void)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Relation classRel,
|
|
|
|
avRel;
|
|
|
|
HeapTuple tuple;
|
|
|
|
HeapScanDesc relScan;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
Form_pg_database dbForm;
|
2007-03-29 00:17:12 +02:00
|
|
|
List *table_oids = NIL;
|
|
|
|
List *toast_oids = NIL;
|
|
|
|
List *table_toast_list = NIL;
|
2005-10-15 04:49:52 +02:00
|
|
|
ListCell *cell;
|
2005-08-11 23:11:50 +02:00
|
|
|
PgStat_StatDBEntry *shared;
|
2007-03-27 22:36:03 +02:00
|
|
|
PgStat_StatDBEntry *dbentry;
|
2007-05-30 22:12:03 +02:00
|
|
|
BufferAccessStrategy bstrategy;
|
2007-03-27 22:36:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* may be NULL if we couldn't find an entry (only happens if we
|
|
|
|
* are forcing a vacuum for anti-wrap purposes).
|
|
|
|
*/
|
2007-03-29 00:17:12 +02:00
|
|
|
dbentry = pgstat_fetch_stat_dbentry(MyDatabaseId);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/* Start a transaction so our commands have one to play into. */
|
|
|
|
StartTransactionCommand();
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
/* functions in indexes may want a snapshot set */
|
2005-11-28 18:23:11 +01:00
|
|
|
ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
|
|
|
|
|
2006-01-18 21:35:06 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Clean up any dead statistics collector entries for this DB. We always
|
|
|
|
* want to do this exactly once per DB-processing cycle, even if we find
|
|
|
|
* nothing worth vacuuming in the database.
|
2006-01-18 21:35:06 +01:00
|
|
|
*/
|
|
|
|
pgstat_vacuum_tabstat();
|
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/*
|
|
|
|
* Find the pg_database entry and select the default freeze_min_age.
|
|
|
|
* We use zero in template and nonconnectable databases,
|
|
|
|
* else the system-wide default.
|
|
|
|
*/
|
|
|
|
tuple = SearchSysCache(DATABASEOID,
|
|
|
|
ObjectIdGetDatum(MyDatabaseId),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
|
|
|
|
dbForm = (Form_pg_database) GETSTRUCT(tuple);
|
|
|
|
|
|
|
|
if (dbForm->datistemplate || !dbForm->datallowconn)
|
|
|
|
default_freeze_min_age = 0;
|
|
|
|
else
|
|
|
|
default_freeze_min_age = vacuum_freeze_min_age;
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* StartTransactionCommand and CommitTransactionCommand will automatically
|
|
|
|
* switch to other contexts. We need this one to keep the list of
|
|
|
|
* relations to vacuum/analyze across transactions.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
MemoryContextSwitchTo(AutovacMemCxt);
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* The database hash where pgstat keeps shared relations */
|
|
|
|
shared = pgstat_fetch_stat_dbentry(InvalidOid);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
classRel = heap_open(RelationRelationId, AccessShareLock);
|
|
|
|
avRel = heap_open(AutovacuumRelationId, AccessShareLock);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/*
|
|
|
|
* Scan pg_class and determine which tables to vacuum.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* The stats subsystem collects stats for toast tables independently of
|
|
|
|
* the stats for their parent tables. We need to check those stats since
|
|
|
|
* in cases with short, wide tables there might be proportionally much
|
|
|
|
* more activity in the toast table than in its parent.
|
2005-08-15 18:25:19 +02:00
|
|
|
*
|
|
|
|
* Since we can only issue VACUUM against the parent table, we need to
|
|
|
|
* transpose a decision to vacuum a toast table into a decision to vacuum
|
2005-10-15 04:49:52 +02:00
|
|
|
* its parent. There's no point in considering ANALYZE on a toast table,
|
|
|
|
* either. To support this, we keep a list of OIDs of toast tables that
|
2005-08-15 18:25:19 +02:00
|
|
|
* need vacuuming alongside the list of regular tables. Regular tables
|
|
|
|
* will be entered into the table list even if they appear not to need
|
2005-10-15 04:49:52 +02:00
|
|
|
* vacuuming; we go back and re-mark them after finding all the vacuumable
|
|
|
|
* toast tables.
|
2005-08-15 18:25:19 +02:00
|
|
|
*/
|
2005-08-11 23:11:50 +02:00
|
|
|
relScan = heap_beginscan(classRel, SnapshotNow, 0, NULL);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
|
|
|
|
{
|
|
|
|
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
|
|
|
|
Form_pg_autovacuum avForm = NULL;
|
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
HeapTuple avTup;
|
|
|
|
Oid relid;
|
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/* Consider only regular and toast tables. */
|
|
|
|
if (classForm->relkind != RELKIND_RELATION &&
|
|
|
|
classForm->relkind != RELKIND_TOASTVALUE)
|
2005-08-11 23:11:50 +02:00
|
|
|
continue;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Skip temp tables (i.e. those in temp namespaces). We cannot safely
|
|
|
|
* process other backends' temp tables.
|
2005-08-11 23:11:50 +02:00
|
|
|
*/
|
2006-01-20 16:16:56 +01:00
|
|
|
if (isAnyTempNamespace(classForm->relnamespace))
|
2005-08-11 23:11:50 +02:00
|
|
|
continue;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
relid = HeapTupleGetOid(tuple);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-23 22:23:13 +01:00
|
|
|
/* Fetch the pg_autovacuum tuple for the relation, if any */
|
|
|
|
avTup = get_pg_autovacuum_tuple_relid(avRel, relid);
|
2005-08-11 23:11:50 +02:00
|
|
|
if (HeapTupleIsValid(avTup))
|
|
|
|
avForm = (Form_pg_autovacuum) GETSTRUCT(avTup);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-27 22:36:03 +02:00
|
|
|
/* Fetch the pgstat entry for this table */
|
|
|
|
tabentry = get_pgstat_tabentry_relid(relid, classForm->relisshared,
|
|
|
|
shared, dbentry);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-29 00:17:12 +02:00
|
|
|
relation_check_autovac(relid, classForm, avForm, tabentry,
|
|
|
|
&table_oids, &table_toast_list, &toast_oids);
|
2005-07-29 21:30:09 +02:00
|
|
|
|
2007-03-23 22:23:13 +01:00
|
|
|
if (HeapTupleIsValid(avTup))
|
|
|
|
heap_freetuple(avTup);
|
2005-08-11 23:11:50 +02:00
|
|
|
}
|
2005-07-29 21:30:09 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
heap_endscan(relScan);
|
|
|
|
heap_close(avRel, AccessShareLock);
|
|
|
|
heap_close(classRel, AccessShareLock);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-03-29 00:17:12 +02:00
|
|
|
/*
|
|
|
|
* Add to the list of tables to vacuum, the OIDs of the tables that
|
|
|
|
* correspond to the saved OIDs of toast tables needing vacuum.
|
|
|
|
*/
|
2007-04-16 20:30:04 +02:00
|
|
|
foreach(cell, toast_oids)
|
2007-03-29 00:17:12 +02:00
|
|
|
{
|
|
|
|
Oid toastoid = lfirst_oid(cell);
|
|
|
|
ListCell *cell2;
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
foreach(cell2, table_toast_list)
|
2007-03-29 00:17:12 +02:00
|
|
|
{
|
|
|
|
av_relation *ar = lfirst(cell2);
|
|
|
|
|
|
|
|
if (ar->ar_toastrelid == toastoid)
|
|
|
|
{
|
|
|
|
table_oids = lappend_oid(table_oids, ar->ar_relid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_free_deep(table_toast_list);
|
|
|
|
table_toast_list = NIL;
|
|
|
|
list_free(toast_oids);
|
|
|
|
toast_oids = NIL;
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
/*
|
|
|
|
* Create a buffer access strategy object for VACUUM to use. We want
|
|
|
|
* to use the same one across all the vacuum operations we perform,
|
|
|
|
* since the point is for VACUUM not to blow out the shared cache.
|
|
|
|
*/
|
|
|
|
bstrategy = GetAccessStrategy(BAS_VACUUM);
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/*
|
|
|
|
* Perform operations on collected tables.
|
|
|
|
*/
|
2007-03-29 00:17:12 +02:00
|
|
|
foreach(cell, table_oids)
|
2005-08-11 23:11:50 +02:00
|
|
|
{
|
2007-03-29 00:17:12 +02:00
|
|
|
Oid relid = lfirst_oid(cell);
|
|
|
|
autovac_table *tab;
|
2007-04-16 20:30:04 +02:00
|
|
|
WorkerInfo worker;
|
|
|
|
bool skipit;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-07-29 21:30:09 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
|
|
|
* hold schedule lock from here until we're sure that this table
|
|
|
|
* still needs vacuuming. We also need the AutovacuumLock to walk
|
|
|
|
* the worker array, but we'll let go of that one quickly.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_SHARED);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the table is being vacuumed concurrently by another
|
|
|
|
* worker.
|
|
|
|
*/
|
|
|
|
skipit = false;
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
while (worker)
|
|
|
|
{
|
|
|
|
/* ignore myself */
|
|
|
|
if (worker == MyWorkerInfo)
|
|
|
|
goto next_worker;
|
|
|
|
|
|
|
|
/* ignore workers in other databases */
|
|
|
|
if (worker->wi_dboid != MyDatabaseId)
|
|
|
|
goto next_worker;
|
|
|
|
|
|
|
|
if (worker->wi_tableoid == relid)
|
|
|
|
{
|
|
|
|
skipit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_worker:
|
|
|
|
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
|
|
|
|
&worker->wi_links,
|
|
|
|
offsetof(WorkerInfoData, wi_links));
|
|
|
|
}
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
if (skipit)
|
|
|
|
{
|
|
|
|
LWLockRelease(AutovacuumScheduleLock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/*
|
2007-03-29 00:17:12 +02:00
|
|
|
* Check whether pgstat data still says we need to vacuum this table.
|
|
|
|
* It could have changed if something else processed the table while we
|
|
|
|
* weren't looking.
|
|
|
|
*
|
|
|
|
* FIXME we ignore the possibility that the table was finished being
|
|
|
|
* vacuumed in the last 500ms (PGSTAT_STAT_INTERVAL). This is a bug.
|
2005-08-15 18:25:19 +02:00
|
|
|
*/
|
2007-03-29 00:17:12 +02:00
|
|
|
tab = table_recheck_autovac(relid);
|
|
|
|
if (tab == NULL)
|
2005-08-15 18:25:19 +02:00
|
|
|
{
|
2007-03-29 00:17:12 +02:00
|
|
|
/* someone else vacuumed the table */
|
2007-04-16 20:30:04 +02:00
|
|
|
LWLockRelease(AutovacuumScheduleLock);
|
2005-08-15 18:25:19 +02:00
|
|
|
continue;
|
2007-03-29 00:17:12 +02:00
|
|
|
}
|
2005-08-15 18:25:19 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
|
|
|
* Ok, good to go. Store the table in shared memory before releasing
|
|
|
|
* the lock so that other workers don't vacuum it concurrently.
|
|
|
|
*/
|
|
|
|
MyWorkerInfo->wi_tableoid = relid;
|
|
|
|
LWLockRelease(AutovacuumScheduleLock);
|
|
|
|
|
|
|
|
/* Set the initial vacuum cost parameters for this table */
|
2007-03-27 22:36:03 +02:00
|
|
|
VacuumCostDelay = tab->at_vacuum_cost_delay;
|
|
|
|
VacuumCostLimit = tab->at_vacuum_cost_limit;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/*
|
|
|
|
* Advertise my cost delay parameters for the balancing algorithm, and
|
|
|
|
* do a balance
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
MyWorkerInfo->wi_cost_delay = tab->at_vacuum_cost_delay;
|
|
|
|
MyWorkerInfo->wi_cost_limit = tab->at_vacuum_cost_limit;
|
|
|
|
MyWorkerInfo->wi_cost_limit_base = tab->at_vacuum_cost_limit;
|
|
|
|
autovac_balance_cost();
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
/* have at it */
|
2007-03-27 22:36:03 +02:00
|
|
|
autovacuum_do_vac_analyze(tab->at_relid,
|
|
|
|
tab->at_dovacuum,
|
|
|
|
tab->at_doanalyze,
|
2007-05-30 22:12:03 +02:00
|
|
|
tab->at_freeze_min_age,
|
|
|
|
bstrategy);
|
2007-03-29 00:17:12 +02:00
|
|
|
/* be tidy */
|
|
|
|
pfree(tab);
|
2005-07-29 21:30:09 +02:00
|
|
|
}
|
2005-07-14 07:13:45 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/*
|
|
|
|
* Update pg_database.datfrozenxid, and truncate pg_clog if possible.
|
|
|
|
* We only need to do this once, not after each table.
|
|
|
|
*/
|
|
|
|
vac_update_datfrozenxid();
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/* Finally close out the last transaction. */
|
|
|
|
CommitTransactionCommand();
|
|
|
|
}
|
|
|
|
|
2007-03-23 22:23:13 +01:00
|
|
|
/*
|
|
|
|
* Returns a copy of the pg_autovacuum tuple for the given relid, or NULL if
|
|
|
|
* there isn't any. avRel is pg_autovacuum, already open and suitably locked.
|
|
|
|
*/
|
|
|
|
static HeapTuple
|
|
|
|
get_pg_autovacuum_tuple_relid(Relation avRel, Oid relid)
|
|
|
|
{
|
|
|
|
ScanKeyData entry[1];
|
|
|
|
SysScanDesc avScan;
|
|
|
|
HeapTuple avTup;
|
|
|
|
|
|
|
|
ScanKeyInit(&entry[0],
|
|
|
|
Anum_pg_autovacuum_vacrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relid));
|
|
|
|
|
|
|
|
avScan = systable_beginscan(avRel, AutovacuumRelidIndexId, true,
|
|
|
|
SnapshotNow, 1, entry);
|
|
|
|
|
|
|
|
avTup = systable_getnext(avScan);
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(avTup))
|
|
|
|
avTup = heap_copytuple(avTup);
|
|
|
|
|
|
|
|
systable_endscan(avScan);
|
|
|
|
|
|
|
|
return avTup;
|
|
|
|
}
|
|
|
|
|
2007-03-27 22:36:03 +02:00
|
|
|
/*
|
|
|
|
* get_pgstat_tabentry_relid
|
|
|
|
*
|
|
|
|
* Fetch the pgstat entry of a table, either local to a database or shared.
|
|
|
|
*/
|
|
|
|
static PgStat_StatTabEntry *
|
|
|
|
get_pgstat_tabentry_relid(Oid relid, bool isshared, PgStat_StatDBEntry *shared,
|
|
|
|
PgStat_StatDBEntry *dbentry)
|
|
|
|
{
|
|
|
|
PgStat_StatTabEntry *tabentry = NULL;
|
|
|
|
|
|
|
|
if (isshared)
|
|
|
|
{
|
|
|
|
if (PointerIsValid(shared))
|
|
|
|
tabentry = hash_search(shared->tables, &relid,
|
|
|
|
HASH_FIND, NULL);
|
|
|
|
}
|
|
|
|
else if (PointerIsValid(dbentry))
|
|
|
|
tabentry = hash_search(dbentry->tables, &relid,
|
|
|
|
HASH_FIND, NULL);
|
|
|
|
|
|
|
|
return tabentry;
|
|
|
|
}
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
2007-03-29 00:17:12 +02:00
|
|
|
* relation_check_autovac
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
2007-03-29 00:17:12 +02:00
|
|
|
* For a given relation (either a plain table or TOAST table), check whether it
|
|
|
|
* needs vacuum or analyze.
|
|
|
|
*
|
|
|
|
* Plain tables that need either are added to the table_list. TOAST tables
|
|
|
|
* that need vacuum are added to toast_list. Plain tables that don't need
|
|
|
|
* either but which have a TOAST table are added, as a struct, to
|
|
|
|
* table_toast_list. The latter is to allow appending the OIDs of the plain
|
|
|
|
* tables whose TOAST table needs vacuuming into the plain tables list, which
|
|
|
|
* allows us to substantially reduce the number of "rechecks" that we need to
|
|
|
|
* do later on.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
relation_check_autovac(Oid relid, Form_pg_class classForm,
|
|
|
|
Form_pg_autovacuum avForm, PgStat_StatTabEntry *tabentry,
|
|
|
|
List **table_oids, List **table_toast_list,
|
|
|
|
List **toast_oids)
|
|
|
|
{
|
|
|
|
bool dovacuum;
|
|
|
|
bool doanalyze;
|
|
|
|
|
|
|
|
relation_needs_vacanalyze(relid, avForm, classForm, tabentry,
|
|
|
|
&dovacuum, &doanalyze);
|
|
|
|
|
|
|
|
if (classForm->relkind == RELKIND_TOASTVALUE)
|
|
|
|
{
|
|
|
|
if (dovacuum)
|
|
|
|
*toast_oids = lappend_oid(*toast_oids, relid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(classForm->relkind == RELKIND_RELATION);
|
|
|
|
|
|
|
|
if (dovacuum || doanalyze)
|
|
|
|
*table_oids = lappend_oid(*table_oids, relid);
|
|
|
|
else if (OidIsValid(classForm->reltoastrelid))
|
|
|
|
{
|
|
|
|
av_relation *rel = palloc(sizeof(av_relation));
|
|
|
|
|
|
|
|
rel->ar_relid = relid;
|
|
|
|
rel->ar_toastrelid = classForm->reltoastrelid;
|
|
|
|
|
|
|
|
*table_toast_list = lappend(*table_toast_list, rel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* table_recheck_autovac
|
|
|
|
*
|
|
|
|
* Recheck whether a plain table still needs vacuum or analyze; be it because
|
|
|
|
* it does directly, or because its TOAST table does. Return value is a valid
|
|
|
|
* autovac_table pointer if it does, NULL otherwise.
|
|
|
|
*/
|
|
|
|
static autovac_table *
|
|
|
|
table_recheck_autovac(Oid relid)
|
|
|
|
{
|
|
|
|
Form_pg_autovacuum avForm = NULL;
|
|
|
|
Form_pg_class classForm;
|
|
|
|
HeapTuple classTup;
|
|
|
|
HeapTuple avTup;
|
|
|
|
Relation avRel;
|
|
|
|
bool dovacuum;
|
|
|
|
bool doanalyze;
|
|
|
|
autovac_table *tab = NULL;
|
|
|
|
PgStat_StatTabEntry *tabentry;
|
|
|
|
bool doit = false;
|
|
|
|
PgStat_StatDBEntry *shared;
|
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
/* use fresh stats */
|
2007-03-29 00:17:12 +02:00
|
|
|
pgstat_clear_snapshot();
|
|
|
|
|
|
|
|
shared = pgstat_fetch_stat_dbentry(InvalidOid);
|
|
|
|
dbentry = pgstat_fetch_stat_dbentry(MyDatabaseId);
|
|
|
|
|
|
|
|
/* fetch the relation's relcache entry */
|
|
|
|
classTup = SearchSysCacheCopy(RELOID,
|
2007-04-16 20:30:04 +02:00
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
0, 0, 0);
|
2007-03-29 00:17:12 +02:00
|
|
|
if (!HeapTupleIsValid(classTup))
|
|
|
|
return NULL;
|
|
|
|
classForm = (Form_pg_class) GETSTRUCT(classTup);
|
|
|
|
|
|
|
|
/* fetch the pg_autovacuum entry, if any */
|
|
|
|
avRel = heap_open(AutovacuumRelationId, AccessShareLock);
|
|
|
|
avTup = get_pg_autovacuum_tuple_relid(avRel, relid);
|
|
|
|
if (HeapTupleIsValid(avTup))
|
|
|
|
avForm = (Form_pg_autovacuum) GETSTRUCT(avTup);
|
|
|
|
|
|
|
|
/* fetch the pgstat table entry */
|
|
|
|
tabentry = get_pgstat_tabentry_relid(relid, classForm->relisshared,
|
|
|
|
shared, dbentry);
|
|
|
|
|
|
|
|
relation_needs_vacanalyze(relid, avForm, classForm, tabentry,
|
|
|
|
&dovacuum, &doanalyze);
|
|
|
|
|
|
|
|
/* OK, it needs vacuum by itself */
|
|
|
|
if (dovacuum)
|
|
|
|
doit = true;
|
|
|
|
/* it doesn't need vacuum, but what about it's TOAST table? */
|
|
|
|
else if (OidIsValid(classForm->reltoastrelid))
|
|
|
|
{
|
|
|
|
Oid toastrelid = classForm->reltoastrelid;
|
|
|
|
HeapTuple toastClassTup;
|
|
|
|
|
|
|
|
toastClassTup = SearchSysCacheCopy(RELOID,
|
|
|
|
ObjectIdGetDatum(toastrelid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (HeapTupleIsValid(toastClassTup))
|
|
|
|
{
|
|
|
|
bool toast_dovacuum;
|
|
|
|
bool toast_doanalyze;
|
|
|
|
Form_pg_class toastClassForm;
|
|
|
|
PgStat_StatTabEntry *toasttabentry;
|
|
|
|
|
|
|
|
toastClassForm = (Form_pg_class) GETSTRUCT(toastClassTup);
|
|
|
|
toasttabentry = get_pgstat_tabentry_relid(toastrelid,
|
|
|
|
toastClassForm->relisshared,
|
|
|
|
shared, dbentry);
|
|
|
|
|
|
|
|
/* note we use the pg_autovacuum entry for the main table */
|
|
|
|
relation_needs_vacanalyze(toastrelid, avForm, toastClassForm,
|
|
|
|
toasttabentry, &toast_dovacuum,
|
|
|
|
&toast_doanalyze);
|
|
|
|
/* we only consider VACUUM for toast tables */
|
|
|
|
if (toast_dovacuum)
|
|
|
|
{
|
|
|
|
dovacuum = true;
|
|
|
|
doit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_freetuple(toastClassTup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doanalyze)
|
|
|
|
doit = true;
|
|
|
|
|
|
|
|
if (doit)
|
|
|
|
{
|
|
|
|
int freeze_min_age;
|
|
|
|
int vac_cost_limit;
|
|
|
|
int vac_cost_delay;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the vacuum cost parameters and the minimum freeze age. If
|
|
|
|
* there is a tuple in pg_autovacuum, use it; else, use the GUC
|
|
|
|
* defaults. Note that the fields may contain "-1" (or indeed any
|
|
|
|
* negative value), which means use the GUC defaults for each setting.
|
|
|
|
*/
|
|
|
|
if (avForm != NULL)
|
|
|
|
{
|
|
|
|
vac_cost_limit = (avForm->vac_cost_limit >= 0) ?
|
|
|
|
avForm->vac_cost_limit :
|
|
|
|
((autovacuum_vac_cost_limit >= 0) ?
|
|
|
|
autovacuum_vac_cost_limit : VacuumCostLimit);
|
|
|
|
|
|
|
|
vac_cost_delay = (avForm->vac_cost_delay >= 0) ?
|
|
|
|
avForm->vac_cost_delay :
|
|
|
|
((autovacuum_vac_cost_delay >= 0) ?
|
|
|
|
autovacuum_vac_cost_delay : VacuumCostDelay);
|
|
|
|
|
|
|
|
freeze_min_age = (avForm->freeze_min_age >= 0) ?
|
|
|
|
avForm->freeze_min_age : default_freeze_min_age;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vac_cost_limit = (autovacuum_vac_cost_limit >= 0) ?
|
|
|
|
autovacuum_vac_cost_limit : VacuumCostLimit;
|
|
|
|
|
|
|
|
vac_cost_delay = (autovacuum_vac_cost_delay >= 0) ?
|
|
|
|
autovacuum_vac_cost_delay : VacuumCostDelay;
|
|
|
|
|
|
|
|
freeze_min_age = default_freeze_min_age;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab = palloc(sizeof(autovac_table));
|
|
|
|
tab->at_relid = relid;
|
|
|
|
tab->at_dovacuum = dovacuum;
|
|
|
|
tab->at_doanalyze = doanalyze;
|
|
|
|
tab->at_freeze_min_age = freeze_min_age;
|
|
|
|
tab->at_vacuum_cost_limit = vac_cost_limit;
|
|
|
|
tab->at_vacuum_cost_delay = vac_cost_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(avRel, AccessShareLock);
|
|
|
|
if (HeapTupleIsValid(avTup))
|
|
|
|
heap_freetuple(avTup);
|
|
|
|
heap_freetuple(classTup);
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* relation_needs_vacanalyze
|
|
|
|
*
|
|
|
|
* Check whether a relation needs to be vacuumed or analyzed; return each into
|
|
|
|
* "dovacuum" and "doanalyze", respectively. avForm and tabentry can be NULL,
|
|
|
|
* classForm shouldn't.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
|
|
|
* A table needs to be vacuumed if the number of dead tuples exceeds a
|
|
|
|
* threshold. This threshold is calculated as
|
|
|
|
*
|
|
|
|
* threshold = vac_base_thresh + vac_scale_factor * reltuples
|
|
|
|
*
|
|
|
|
* For analyze, the analysis done is that the number of tuples inserted,
|
|
|
|
* deleted and updated since the last analyze exceeds a threshold calculated
|
|
|
|
* in the same fashion as above. Note that the collector actually stores
|
|
|
|
* the number of tuples (both live and dead) that there were as of the last
|
|
|
|
* analyze. This is asymmetric to the VACUUM case.
|
|
|
|
*
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
* We also force vacuum if the table's relfrozenxid is more than freeze_max_age
|
|
|
|
* transactions back.
|
|
|
|
*
|
2005-07-14 07:13:45 +02:00
|
|
|
* A table whose pg_autovacuum.enabled value is false, is automatically
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
* skipped (unless we have to vacuum it due to freeze_max_age). Thus
|
|
|
|
* autovacuum can be disabled for specific tables. Also, when the stats
|
|
|
|
* collector does not have data about a table, it will be skipped.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
|
|
|
* A table whose vac_base_thresh value is <0 takes the base value from the
|
|
|
|
* autovacuum_vacuum_threshold GUC variable. Similarly, a vac_scale_factor
|
|
|
|
* value <0 is substituted with the value of
|
|
|
|
* autovacuum_vacuum_scale_factor GUC variable. Ditto for analyze.
|
|
|
|
*/
|
|
|
|
static void
|
2007-03-29 00:17:12 +02:00
|
|
|
relation_needs_vacanalyze(Oid relid,
|
|
|
|
Form_pg_autovacuum avForm,
|
|
|
|
Form_pg_class classForm,
|
|
|
|
PgStat_StatTabEntry *tabentry,
|
|
|
|
/* output params below */
|
|
|
|
bool *dovacuum,
|
|
|
|
bool *doanalyze)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
bool force_vacuum;
|
2005-10-15 04:49:52 +02:00
|
|
|
float4 reltuples; /* pg_class.reltuples */
|
2005-07-14 07:13:45 +02:00
|
|
|
/* constants from pg_autovacuum or GUC variables */
|
2005-10-15 04:49:52 +02:00
|
|
|
int vac_base_thresh,
|
|
|
|
anl_base_thresh;
|
|
|
|
float4 vac_scale_factor,
|
|
|
|
anl_scale_factor;
|
2005-07-14 07:13:45 +02:00
|
|
|
/* thresholds calculated from above constants */
|
2005-10-15 04:49:52 +02:00
|
|
|
float4 vacthresh,
|
|
|
|
anlthresh;
|
2005-07-14 07:13:45 +02:00
|
|
|
/* number of vacuum (resp. analyze) tuples at this time */
|
2005-10-15 04:49:52 +02:00
|
|
|
float4 vactuples,
|
|
|
|
anltuples;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/* freeze parameters */
|
|
|
|
int freeze_max_age;
|
|
|
|
TransactionId xidForceLimit;
|
2007-03-29 00:17:12 +02:00
|
|
|
|
|
|
|
AssertArg(classForm != NULL);
|
|
|
|
AssertArg(OidIsValid(relid));
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/*
|
2007-03-29 00:17:12 +02:00
|
|
|
* Determine vacuum/analyze equation parameters. If there is a tuple in
|
|
|
|
* pg_autovacuum, use it; else, use the GUC defaults. Note that the fields
|
|
|
|
* may contain "-1" (or indeed any negative value), which means use the GUC
|
|
|
|
* defaults for each setting.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
if (avForm != NULL)
|
|
|
|
{
|
2005-08-11 23:11:50 +02:00
|
|
|
vac_scale_factor = (avForm->vac_scale_factor >= 0) ?
|
|
|
|
avForm->vac_scale_factor : autovacuum_vac_scale;
|
|
|
|
vac_base_thresh = (avForm->vac_base_thresh >= 0) ?
|
|
|
|
avForm->vac_base_thresh : autovacuum_vac_thresh;
|
|
|
|
|
|
|
|
anl_scale_factor = (avForm->anl_scale_factor >= 0) ?
|
|
|
|
avForm->anl_scale_factor : autovacuum_anl_scale;
|
|
|
|
anl_base_thresh = (avForm->anl_base_thresh >= 0) ?
|
|
|
|
avForm->anl_base_thresh : autovacuum_anl_thresh;
|
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
freeze_max_age = (avForm->freeze_max_age >= 0) ?
|
|
|
|
Min(avForm->freeze_max_age, autovacuum_freeze_max_age) :
|
|
|
|
autovacuum_freeze_max_age;
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vac_scale_factor = autovacuum_vac_scale;
|
|
|
|
vac_base_thresh = autovacuum_vac_thresh;
|
|
|
|
|
|
|
|
anl_scale_factor = autovacuum_anl_scale;
|
|
|
|
anl_base_thresh = autovacuum_anl_thresh;
|
2005-08-11 23:11:50 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
freeze_max_age = autovacuum_freeze_max_age;
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/* Force vacuum if table is at risk of wraparound */
|
|
|
|
xidForceLimit = recentXid - freeze_max_age;
|
|
|
|
if (xidForceLimit < FirstNormalTransactionId)
|
|
|
|
xidForceLimit -= FirstNormalTransactionId;
|
|
|
|
force_vacuum = (TransactionIdIsNormal(classForm->relfrozenxid) &&
|
|
|
|
TransactionIdPrecedes(classForm->relfrozenxid,
|
|
|
|
xidForceLimit));
|
2005-07-14 07:13:45 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/* User disabled it in pg_autovacuum? (But ignore if at risk) */
|
|
|
|
if (avForm && !avForm->enabled && !force_vacuum)
|
2007-03-29 00:17:12 +02:00
|
|
|
{
|
|
|
|
*doanalyze = false;
|
|
|
|
*dovacuum = false;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
return;
|
2007-03-29 00:17:12 +02:00
|
|
|
}
|
2005-07-14 07:13:45 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
if (PointerIsValid(tabentry))
|
|
|
|
{
|
|
|
|
reltuples = classForm->reltuples;
|
|
|
|
vactuples = tabentry->n_dead_tuples;
|
|
|
|
anltuples = tabentry->n_live_tuples + tabentry->n_dead_tuples -
|
|
|
|
tabentry->last_anl_tuples;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
|
|
|
|
anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that we don't need to take special consideration for stat
|
|
|
|
* reset, because if that happens, the last vacuum and analyze counts
|
|
|
|
* will be reset too.
|
|
|
|
*/
|
|
|
|
elog(DEBUG3, "%s: vac: %.0f (threshold %.0f), anl: %.0f (threshold %.0f)",
|
|
|
|
NameStr(classForm->relname),
|
|
|
|
vactuples, vacthresh, anltuples, anlthresh);
|
|
|
|
|
|
|
|
/* Determine if this table needs vacuum or analyze. */
|
2007-03-29 00:17:12 +02:00
|
|
|
*dovacuum = force_vacuum || (vactuples > vacthresh);
|
|
|
|
*doanalyze = (anltuples > anlthresh);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Skip a table not found in stat hash, unless we have to force
|
|
|
|
* vacuum for anti-wrap purposes. If it's not acted upon, there's
|
|
|
|
* no need to vacuum it.
|
|
|
|
*/
|
2007-03-29 00:17:12 +02:00
|
|
|
*dovacuum = force_vacuum;
|
|
|
|
*doanalyze = false;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
}
|
2005-08-11 23:11:50 +02:00
|
|
|
|
|
|
|
/* ANALYZE refuses to work with pg_statistics */
|
|
|
|
if (relid == StatisticRelationId)
|
2007-03-29 00:17:12 +02:00
|
|
|
*doanalyze = false;
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autovacuum_do_vac_analyze
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
* Vacuum and/or analyze the specified table
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
static void
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
autovacuum_do_vac_analyze(Oid relid, bool dovacuum, bool doanalyze,
|
2007-05-30 22:12:03 +02:00
|
|
|
int freeze_min_age,
|
|
|
|
BufferAccessStrategy bstrategy)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-03-23 21:56:39 +01:00
|
|
|
VacuumStmt vacstmt;
|
2005-10-15 04:49:52 +02:00
|
|
|
MemoryContext old_cxt;
|
|
|
|
|
2007-03-23 22:57:10 +01:00
|
|
|
MemSet(&vacstmt, 0, sizeof(vacstmt));
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/*
|
2007-03-23 21:56:39 +01:00
|
|
|
* The list must survive transaction boundaries, so make sure we create it
|
2005-08-11 23:11:50 +02:00
|
|
|
* in a long-lived context
|
|
|
|
*/
|
|
|
|
old_cxt = MemoryContextSwitchTo(AutovacMemCxt);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/* Set up command parameters */
|
2007-03-23 22:57:10 +01:00
|
|
|
vacstmt.type = T_VacuumStmt;
|
2007-03-23 21:56:39 +01:00
|
|
|
vacstmt.vacuum = dovacuum;
|
|
|
|
vacstmt.full = false;
|
|
|
|
vacstmt.analyze = doanalyze;
|
|
|
|
vacstmt.freeze_min_age = freeze_min_age;
|
|
|
|
vacstmt.verbose = false;
|
|
|
|
vacstmt.relation = NULL; /* not used since we pass a relids list */
|
|
|
|
vacstmt.va_cols = NIL;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2006-05-19 17:15:37 +02:00
|
|
|
/* Let pgstat know what we're doing */
|
2007-03-23 21:56:39 +01:00
|
|
|
autovac_report_activity(&vacstmt, relid);
|
2005-08-11 23:11:50 +02:00
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
vacuum(&vacstmt, list_make1_oid(relid), bstrategy, true);
|
2005-08-11 23:11:50 +02:00
|
|
|
MemoryContextSwitchTo(old_cxt);
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2006-05-19 17:15:37 +02:00
|
|
|
/*
|
|
|
|
* autovac_report_activity
|
2006-10-04 02:30:14 +02:00
|
|
|
* Report to pgstat what autovacuum is doing
|
2006-05-19 17:15:37 +02:00
|
|
|
*
|
|
|
|
* We send a SQL string corresponding to what the user would see if the
|
|
|
|
* equivalent command was to be issued manually.
|
|
|
|
*
|
|
|
|
* Note we assume that we are going to report the next command as soon as we're
|
|
|
|
* done with the current one, and exiting right after the last one, so we don't
|
|
|
|
* bother to report "<IDLE>" or some such.
|
|
|
|
*/
|
|
|
|
static void
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
autovac_report_activity(VacuumStmt *vacstmt, Oid relid)
|
2006-05-19 17:15:37 +02:00
|
|
|
{
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
char *relname = get_rel_name(relid);
|
|
|
|
char *nspname = get_namespace_name(get_rel_namespace(relid));
|
2006-07-31 22:09:10 +02:00
|
|
|
#define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 32)
|
2006-05-19 17:15:37 +02:00
|
|
|
char activity[MAX_AUTOVAC_ACTIV_LEN];
|
|
|
|
|
|
|
|
/* Report the command and possible options */
|
|
|
|
if (vacstmt->vacuum)
|
|
|
|
snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
"VACUUM%s",
|
2006-07-31 22:09:10 +02:00
|
|
|
vacstmt->analyze ? " ANALYZE" : "");
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
else
|
2006-05-19 17:15:37 +02:00
|
|
|
snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
|
2006-07-31 22:09:10 +02:00
|
|
|
"ANALYZE");
|
2006-05-19 17:15:37 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/*
|
|
|
|
* Report the qualified name of the relation.
|
|
|
|
*
|
|
|
|
* Paranoia is appropriate here in case relation was recently dropped
|
|
|
|
* --- the lsyscache routines we just invoked will return NULL rather
|
|
|
|
* than failing.
|
|
|
|
*/
|
|
|
|
if (relname && nspname)
|
2006-05-19 17:15:37 +02:00
|
|
|
{
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
int len = strlen(activity);
|
2006-05-19 17:15:37 +02:00
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
|
|
|
|
" %s.%s", nspname, relname);
|
2006-05-19 17:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pgstat_report_activity(activity);
|
|
|
|
}
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
|
|
|
* AutoVacuumingActive
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check GUC vars and report whether the autovacuum process should be
|
|
|
|
* running.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
AutoVacuumingActive(void)
|
|
|
|
{
|
|
|
|
if (!autovacuum_start_daemon || !pgstat_collect_startcollector ||
|
|
|
|
!pgstat_collect_tuplelevel)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autovac_init
|
2005-10-15 04:49:52 +02:00
|
|
|
* This is called at postmaster initialization.
|
2005-07-14 07:13:45 +02:00
|
|
|
*
|
|
|
|
* Annoy the user if he got it wrong.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
autovac_init(void)
|
|
|
|
{
|
|
|
|
if (!autovacuum_start_daemon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pgstat_collect_startcollector || !pgstat_collect_tuplelevel)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("autovacuum not started because of misconfiguration"),
|
|
|
|
errhint("Enable options \"stats_start_collector\" and \"stats_row_level\".")));
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/*
|
|
|
|
* Set the GUC var so we don't fork autovacuum uselessly, and also to
|
|
|
|
* help debugging.
|
|
|
|
*/
|
|
|
|
autovacuum_start_daemon = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-16 00:23:23 +01:00
|
|
|
* IsAutoVacuum functions
|
|
|
|
* Return whether this is either a launcher autovacuum process or a worker
|
|
|
|
* process.
|
2005-07-14 07:13:45 +02:00
|
|
|
*/
|
|
|
|
bool
|
2007-02-16 00:23:23 +01:00
|
|
|
IsAutoVacuumLauncherProcess(void)
|
|
|
|
{
|
|
|
|
return am_autovacuum_launcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsAutoVacuumWorkerProcess(void)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2007-02-16 00:23:23 +01:00
|
|
|
return am_autovacuum_worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AutoVacuumShmemSize
|
|
|
|
* Compute space needed for autovacuum-related shared memory
|
|
|
|
*/
|
|
|
|
Size
|
|
|
|
AutoVacuumShmemSize(void)
|
|
|
|
{
|
2007-04-16 20:30:04 +02:00
|
|
|
Size size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need the fixed struct and the array of WorkerInfoData.
|
|
|
|
*/
|
|
|
|
size = sizeof(AutoVacuumShmemStruct);
|
|
|
|
size = MAXALIGN(size);
|
|
|
|
size = add_size(size, mul_size(autovacuum_max_workers,
|
|
|
|
sizeof(WorkerInfoData)));
|
|
|
|
return size;
|
2007-02-16 00:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AutoVacuumShmemInit
|
|
|
|
* Allocate and initialize autovacuum-related shared memory
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AutoVacuumShmemInit(void)
|
|
|
|
{
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
AutoVacuumShmem = (AutoVacuumShmemStruct *)
|
|
|
|
ShmemInitStruct("AutoVacuum Data",
|
|
|
|
AutoVacuumShmemSize(),
|
|
|
|
&found);
|
|
|
|
if (AutoVacuumShmem == NULL)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("not enough shared memory for autovacuum")));
|
|
|
|
|
2007-04-16 20:30:04 +02:00
|
|
|
if (!IsUnderPostmaster)
|
|
|
|
{
|
|
|
|
WorkerInfo worker;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Assert(!found);
|
|
|
|
|
|
|
|
AutoVacuumShmem->av_launcherpid = 0;
|
|
|
|
AutoVacuumShmem->av_freeWorkers = INVALID_OFFSET;
|
|
|
|
SHMQueueInit(&AutoVacuumShmem->av_runningWorkers);
|
|
|
|
AutoVacuumShmem->av_startingWorker = INVALID_OFFSET;
|
|
|
|
|
|
|
|
worker = (WorkerInfo) ((char *) AutoVacuumShmem +
|
|
|
|
MAXALIGN(sizeof(AutoVacuumShmemStruct)));
|
|
|
|
|
|
|
|
/* initialize the WorkerInfo free list */
|
|
|
|
for (i = 0; i < autovacuum_max_workers; i++)
|
|
|
|
{
|
|
|
|
worker[i].wi_links.next = AutoVacuumShmem->av_freeWorkers;
|
|
|
|
AutoVacuumShmem->av_freeWorkers = MAKE_OFFSET(&worker[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Assert(found);
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|