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-03-23 21:56:39 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.35 2007/03/23 20:56:39 alvherre 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-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;
|
|
|
|
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;
|
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/* Flag to tell if we are in the autovacuum daemon 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
|
|
|
|
2005-07-14 07:13:45 +02:00
|
|
|
/* struct to keep list of candidate databases for vacuum */
|
|
|
|
typedef struct autovac_dbase
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Oid oid;
|
|
|
|
char *name;
|
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 frozenxid;
|
2005-07-14 07:13:45 +02:00
|
|
|
PgStat_StatDBEntry *entry;
|
|
|
|
} autovac_dbase;
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* struct to keep track of tables to vacuum and/or analyze */
|
|
|
|
typedef struct autovac_table
|
|
|
|
{
|
|
|
|
Oid relid;
|
2005-08-15 18:25:19 +02:00
|
|
|
Oid toastrelid;
|
2005-08-11 23:11:50 +02:00
|
|
|
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
|
|
|
int freeze_min_age;
|
2005-08-11 23:11:50 +02:00
|
|
|
int vacuum_cost_delay;
|
|
|
|
int vacuum_cost_limit;
|
|
|
|
} autovac_table;
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Oid process_db; /* OID of database to process */
|
|
|
|
int worker_pid; /* PID of the worker process, if any */
|
|
|
|
} AutoVacuumShmemStruct;
|
|
|
|
|
|
|
|
static AutoVacuumShmemStruct *AutoVacuumShmem;
|
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[]);
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
static void do_autovacuum(PgStat_StatDBEntry *dbentry);
|
2005-07-14 07:13:45 +02:00
|
|
|
static List *autovac_get_database_list(void);
|
|
|
|
static void test_rel_for_autovac(Oid relid, PgStat_StatTabEntry *tabentry,
|
2005-10-15 04:49:52 +02:00
|
|
|
Form_pg_class classForm,
|
|
|
|
Form_pg_autovacuum avForm,
|
|
|
|
List **vacuum_tables,
|
|
|
|
List **toast_table_ids);
|
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,
|
|
|
|
bool doanalyze, int freeze_min_age);
|
|
|
|
static void autovac_report_activity(VacuumStmt *vacstmt, Oid relid);
|
2007-02-16 00:23:23 +01:00
|
|
|
static void avl_sighup_handler(SIGNAL_ARGS);
|
|
|
|
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.
|
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;
|
|
|
|
List *dblist;
|
|
|
|
bool for_xid_wrap;
|
|
|
|
autovac_dbase *db;
|
|
|
|
MemoryContext avlauncher_cxt;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
* XXX It may be a good idea to receive signals when an avworker process
|
|
|
|
* finishes.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
pqsignal(SIGUSR1, SIG_IGN);
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
avlauncher_cxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"Autovacuum Launcher",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
MemoryContextSwitchTo(avlauncher_cxt);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
MemoryContextSwitchTo(avlauncher_cxt);
|
|
|
|
FlushErrorState();
|
|
|
|
|
|
|
|
/* Flush any leaked data in the top-level context */
|
|
|
|
MemoryContextResetAndDeleteChildren(avlauncher_cxt);
|
|
|
|
|
|
|
|
/* 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")));
|
|
|
|
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take a nap before executing the first iteration, unless we were
|
|
|
|
* requested an emergency run.
|
|
|
|
*/
|
|
|
|
if (autovacuum_start_daemon)
|
|
|
|
pg_usleep(autovacuum_naptime * 1000000L);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
TransactionId xidForceLimit;
|
|
|
|
ListCell *cell;
|
|
|
|
int worker_pid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emergency bailout if postmaster has died. This is to avoid the
|
|
|
|
* necessity for manual cleanup of all postmaster children.
|
|
|
|
*/
|
|
|
|
if (!PostmasterIsAlive(true))
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (avlauncher_shutdown_request)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if there's a worker already running, sleep until it
|
|
|
|
* disappears.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_SHARED);
|
|
|
|
worker_pid = AutoVacuumShmem->worker_pid;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
if (worker_pid != 0)
|
|
|
|
{
|
|
|
|
PGPROC *proc = BackendPidGetProc(worker_pid);
|
|
|
|
|
|
|
|
if (proc != NULL && proc->isAutovacuum)
|
|
|
|
goto sleep;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if the worker is not really running (or it's a process
|
|
|
|
* that's not an autovacuum worker), remove the PID from shmem.
|
|
|
|
* This should not happen, because either the worker exits
|
|
|
|
* cleanly, in which case it'll remove the PID, or it dies, in
|
|
|
|
* which case postmaster will cause a system reset cycle.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
worker_pid = 0;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a list of databases */
|
|
|
|
dblist = autovac_get_database_list();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
db = NULL;
|
|
|
|
for_xid_wrap = false;
|
|
|
|
foreach(cell, dblist)
|
|
|
|
{
|
|
|
|
autovac_dbase *tmp = lfirst(cell);
|
|
|
|
|
|
|
|
/* Find pgstat entry if any */
|
|
|
|
tmp->entry = pgstat_fetch_stat_dbentry(tmp->oid);
|
|
|
|
|
|
|
|
/* Check to see if this one is at risk of wraparound */
|
|
|
|
if (TransactionIdPrecedes(tmp->frozenxid, xidForceLimit))
|
|
|
|
{
|
|
|
|
if (db == NULL ||
|
|
|
|
TransactionIdPrecedes(tmp->frozenxid, db->frozenxid))
|
|
|
|
db = tmp;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
if (!tmp->entry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember the db with oldest autovac time. (If we are here,
|
|
|
|
* both tmp->entry and db->entry must be non-null.)
|
|
|
|
*/
|
|
|
|
if (db == NULL ||
|
|
|
|
tmp->entry->last_autovac_time < db->entry->last_autovac_time)
|
|
|
|
db = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Found a database -- process it */
|
|
|
|
if (db != NULL)
|
|
|
|
{
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
AutoVacuumShmem->process_db = db->oid;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep:
|
|
|
|
/*
|
|
|
|
* in emergency mode, exit immediately so that the postmaster can
|
|
|
|
* request another run right away if needed.
|
|
|
|
*
|
|
|
|
* XXX -- maybe it would be better to handle this inside the launcher
|
|
|
|
* itself.
|
|
|
|
*/
|
|
|
|
if (!autovacuum_start_daemon)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* have pgstat read the file again next time */
|
|
|
|
pgstat_clear_snapshot();
|
|
|
|
|
|
|
|
/* now sleep until the next autovac iteration */
|
|
|
|
pg_usleep(autovacuum_naptime * 1000000L);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normal exit from the autovac launcher is here */
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("autovacuum launcher shutting down")));
|
|
|
|
|
|
|
|
proc_exit(0); /* done */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-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-02-16 00:23:23 +01: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-02-16 00:23:23 +01:00
|
|
|
* Get the database Id we're going to work on, and announce our PID
|
|
|
|
* in the shared memory area. We remove the database OID immediately
|
|
|
|
* from the shared memory area.
|
2005-07-29 21:30:09 +02:00
|
|
|
*/
|
2007-02-16 00:23:23 +01:00
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
2005-07-29 21:30:09 +02:00
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
dbid = AutoVacuumShmem->process_db;
|
|
|
|
AutoVacuumShmem->process_db = InvalidOid;
|
|
|
|
AutoVacuumShmem->worker_pid = 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-02-16 00:23:23 +01:00
|
|
|
LWLockRelease(AutovacuumLock);
|
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-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;
|
|
|
|
PgStat_StatDBEntry *dbentry;
|
|
|
|
|
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,
|
|
|
|
"Autovacuum context",
|
|
|
|
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();
|
|
|
|
dbentry = pgstat_fetch_stat_dbentry(dbid);
|
|
|
|
do_autovacuum(dbentry);
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 00:23:23 +01:00
|
|
|
/*
|
|
|
|
* Now remove our PID from shared memory, so that the launcher can start
|
|
|
|
* another worker as soon as appropriate.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
|
|
|
|
AutoVacuumShmem->worker_pid = 0;
|
|
|
|
LWLockRelease(AutovacuumLock);
|
|
|
|
|
|
|
|
/* All done, go away */
|
2005-07-14 07:13:45 +02:00
|
|
|
proc_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autovac_get_database_list
|
|
|
|
*
|
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 *
|
|
|
|
autovac_get_database_list(void)
|
|
|
|
{
|
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
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
autovac_dbase *db;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
db = (autovac_dbase *) palloc(sizeof(autovac_dbase));
|
|
|
|
|
|
|
|
db->oid = db_id;
|
|
|
|
db->name = pstrdup(thisname);
|
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->frozenxid = db_frozenxid;
|
|
|
|
/* this gets set later: */
|
2005-07-14 07:13:45 +02:00
|
|
|
db->entry = NULL;
|
|
|
|
|
|
|
|
dblist = lappend(dblist, db);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*
|
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
|
|
|
* dbentry is either a pointer to the database entry in the stats databases
|
|
|
|
* hash table, or NULL if we couldn't find any entry (the latter case occurs
|
|
|
|
* only if we are forcing a vacuum for anti-wrap purposes).
|
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
|
2005-08-11 23:11:50 +02:00
|
|
|
do_autovacuum(PgStat_StatDBEntry *dbentry)
|
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;
|
2005-10-15 04:49:52 +02:00
|
|
|
List *vacuum_tables = NIL;
|
|
|
|
List *toast_table_ids = NIL;
|
|
|
|
ListCell *cell;
|
2005-08-11 23:11:50 +02:00
|
|
|
PgStat_StatDBEntry *shared;
|
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;
|
2005-10-15 04:49:52 +02:00
|
|
|
SysScanDesc avScan;
|
2005-08-11 23:11:50 +02:00
|
|
|
HeapTuple avTup;
|
2005-10-15 04:49:52 +02:00
|
|
|
ScanKeyData entry[1];
|
2005-08-11 23:11:50 +02:00
|
|
|
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
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* See if we have a pg_autovacuum entry for this relation. */
|
|
|
|
ScanKeyInit(&entry[0],
|
|
|
|
Anum_pg_autovacuum_vacrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relid));
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
avScan = systable_beginscan(avRel, AutovacuumRelidIndexId, true,
|
|
|
|
SnapshotNow, 1, entry);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
avTup = systable_getnext(avScan);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
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
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
if (classForm->relisshared && PointerIsValid(shared))
|
|
|
|
tabentry = hash_search(shared->tables, &relid,
|
|
|
|
HASH_FIND, NULL);
|
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 if (PointerIsValid(dbentry))
|
2005-08-11 23:11:50 +02:00
|
|
|
tabentry = hash_search(dbentry->tables, &relid,
|
|
|
|
HASH_FIND, NULL);
|
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
|
|
|
|
tabentry = NULL;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
test_rel_for_autovac(relid, tabentry, classForm, avForm,
|
2005-08-15 18:25:19 +02:00
|
|
|
&vacuum_tables, &toast_table_ids);
|
2005-07-29 21:30:09 +02:00
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
systable_endscan(avScan);
|
|
|
|
}
|
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
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/*
|
|
|
|
* Perform operations on collected tables.
|
|
|
|
*/
|
|
|
|
foreach(cell, vacuum_tables)
|
|
|
|
{
|
|
|
|
autovac_table *tab = lfirst(cell);
|
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
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/*
|
|
|
|
* Check to see if we need to force vacuuming of this table because
|
|
|
|
* its toast table needs it.
|
|
|
|
*/
|
|
|
|
if (OidIsValid(tab->toastrelid) && !tab->dovacuum &&
|
|
|
|
list_member_oid(toast_table_ids, tab->toastrelid))
|
|
|
|
{
|
|
|
|
tab->dovacuum = true;
|
|
|
|
elog(DEBUG2, "autovac: VACUUM %u because of TOAST table",
|
|
|
|
tab->relid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, ignore table if it needs no work */
|
|
|
|
if (!tab->dovacuum && !tab->doanalyze)
|
|
|
|
continue;
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
/* Set the vacuum cost parameters for this table */
|
|
|
|
VacuumCostDelay = tab->vacuum_cost_delay;
|
|
|
|
VacuumCostLimit = tab->vacuum_cost_limit;
|
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
|
|
|
autovacuum_do_vac_analyze(tab->relid,
|
2005-08-11 23:11:50 +02:00
|
|
|
tab->dovacuum,
|
|
|
|
tab->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
|
|
|
tab->freeze_min_age);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* test_rel_for_autovac
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check whether a table needs to be vacuumed or analyzed. Add it to the
|
2005-08-15 18:25:19 +02:00
|
|
|
* appropriate output list if so.
|
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
|
|
|
|
test_rel_for_autovac(Oid relid, PgStat_StatTabEntry *tabentry,
|
|
|
|
Form_pg_class classForm,
|
|
|
|
Form_pg_autovacuum avForm,
|
2005-08-15 18:25:19 +02:00
|
|
|
List **vacuum_tables,
|
|
|
|
List **toast_table_ids)
|
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;
|
|
|
|
bool dovacuum;
|
|
|
|
bool doanalyze;
|
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_min_age;
|
|
|
|
int freeze_max_age;
|
|
|
|
TransactionId xidForceLimit;
|
2005-08-11 23:11:50 +02:00
|
|
|
/* cost-based vacuum delay parameters */
|
2005-10-15 04:49:52 +02:00
|
|
|
int vac_cost_limit;
|
|
|
|
int vac_cost_delay;
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
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_min_age = (avForm->freeze_min_age >= 0) ?
|
|
|
|
avForm->freeze_min_age : default_freeze_min_age;
|
|
|
|
freeze_max_age = (avForm->freeze_max_age >= 0) ?
|
|
|
|
Min(avForm->freeze_max_age, autovacuum_freeze_max_age) :
|
|
|
|
autovacuum_freeze_max_age;
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
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);
|
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_min_age = default_freeze_min_age;
|
|
|
|
freeze_max_age = autovacuum_freeze_max_age;
|
|
|
|
|
2005-08-11 23:11:50 +02:00
|
|
|
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;
|
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)
|
|
|
|
return;
|
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. */
|
|
|
|
dovacuum = force_vacuum || (vactuples > vacthresh);
|
|
|
|
doanalyze = (anltuples > anlthresh);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
dovacuum = force_vacuum;
|
|
|
|
doanalyze = false;
|
|
|
|
}
|
2005-08-11 23:11:50 +02:00
|
|
|
|
|
|
|
/* ANALYZE refuses to work with pg_statistics */
|
|
|
|
if (relid == StatisticRelationId)
|
|
|
|
doanalyze = false;
|
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
Assert(CurrentMemoryContext == AutovacMemCxt);
|
|
|
|
|
|
|
|
if (classForm->relkind == RELKIND_RELATION)
|
2005-07-14 07:13:45 +02:00
|
|
|
{
|
2005-08-15 18:25:19 +02:00
|
|
|
if (dovacuum || doanalyze)
|
|
|
|
elog(DEBUG2, "autovac: will%s%s %s",
|
|
|
|
(dovacuum ? " VACUUM" : ""),
|
|
|
|
(doanalyze ? " ANALYZE" : ""),
|
2006-07-31 22:09:10 +02:00
|
|
|
NameStr(classForm->relname));
|
2005-08-11 23:11:50 +02:00
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
/*
|
|
|
|
* we must record tables that have a toast table, even if we currently
|
|
|
|
* don't think they need vacuuming.
|
|
|
|
*/
|
|
|
|
if (dovacuum || doanalyze || OidIsValid(classForm->reltoastrelid))
|
|
|
|
{
|
|
|
|
autovac_table *tab;
|
2005-08-11 23:11:50 +02:00
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
tab = (autovac_table *) palloc(sizeof(autovac_table));
|
|
|
|
tab->relid = relid;
|
|
|
|
tab->toastrelid = classForm->reltoastrelid;
|
|
|
|
tab->dovacuum = dovacuum;
|
|
|
|
tab->doanalyze = 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
|
|
|
tab->freeze_min_age = freeze_min_age;
|
2005-08-15 18:25:19 +02:00
|
|
|
tab->vacuum_cost_limit = vac_cost_limit;
|
|
|
|
tab->vacuum_cost_delay = vac_cost_delay;
|
2005-08-11 23:11:50 +02:00
|
|
|
|
2005-08-15 18:25:19 +02:00
|
|
|
*vacuum_tables = lappend(*vacuum_tables, tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(classForm->relkind == RELKIND_TOASTVALUE);
|
|
|
|
if (dovacuum)
|
|
|
|
*toast_table_ids = lappend_oid(*toast_table_ids, relid);
|
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,
|
|
|
|
int freeze_min_age)
|
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;
|
|
|
|
|
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 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-03-23 21:56:39 +01:00
|
|
|
vacuum(&vacstmt, list_make1_oid(relid), 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)
|
|
|
|
{
|
|
|
|
return sizeof(AutoVacuumShmemStruct);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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")));
|
|
|
|
if (found)
|
|
|
|
return; /* already initialized */
|
|
|
|
|
|
|
|
MemSet(AutoVacuumShmem, 0, sizeof(AutoVacuumShmemStruct));
|
2005-07-14 07:13:45 +02:00
|
|
|
}
|