2001-07-14 00:55:59 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* vacuumlazy.c
|
|
|
|
* Concurrent ("lazy") vacuuming.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The major space usage for LAZY VACUUM is storage for the array of dead
|
|
|
|
* tuple TIDs, with the next biggest need being storage for per-disk-page
|
|
|
|
* free space info. We want to ensure we can vacuum even the very largest
|
|
|
|
* relations with finite memory space usage. To do that, we set upper bounds
|
|
|
|
* on the number of tuples and pages we will keep track of at once.
|
|
|
|
*
|
2004-02-03 18:34:04 +01:00
|
|
|
* We are willing to use at most maintenance_work_mem memory space to keep
|
2007-09-24 05:52:55 +02:00
|
|
|
* track of dead tuples. We initially allocate an array of TIDs of that size,
|
|
|
|
* with an upper limit that depends on table size (this limit ensures we don't
|
2007-11-15 22:14:46 +01:00
|
|
|
* allocate a huge area uselessly for vacuuming small tables). If the array
|
2007-09-24 05:52:55 +02:00
|
|
|
* threatens to overflow, we suspend the heap scan phase and perform a pass of
|
|
|
|
* index cleanup and page compaction, then resume the heap scan with an empty
|
|
|
|
* TID array.
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
|
|
|
* We can limit the storage for page free space to MaxFSMPages entries,
|
|
|
|
* since that's the most the free space map will be willing to remember
|
2001-10-25 07:50:21 +02:00
|
|
|
* anyway. If the relation has fewer than that many pages with free space,
|
|
|
|
* life is easy: just build an array of per-page info. If it has more,
|
2001-07-14 00:55:59 +02:00
|
|
|
* we store the free space info as a heap ordered by amount of free space,
|
|
|
|
* so that we can discard the pages with least free space to ensure we never
|
|
|
|
* have more than MaxFSMPages entries in all. The surviving page entries
|
|
|
|
* are passed to the free space map at conclusion of the scan.
|
|
|
|
*
|
2006-09-13 19:47:08 +02:00
|
|
|
* If we're processing a table with no indexes, we can just vacuum each page
|
|
|
|
* as we go; there's no need to save up multiple tuples to minimize the number
|
|
|
|
* of index scans performed. So we don't use maintenance_work_mem memory for
|
|
|
|
* the TID array, just enough to hold as many heap tuples as fit on one page.
|
|
|
|
*
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2001-07-14 00:55:59 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2008-03-26 22:10:39 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.106 2008/03/26 21:10:38 alvherre Exp $
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-12-01 20:00:56 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
2007-04-18 18:44:18 +02:00
|
|
|
#include "commands/dbcommands.h"
|
2001-07-14 00:55:59 +02:00
|
|
|
#include "commands/vacuum.h"
|
|
|
|
#include "miscadmin.h"
|
2005-07-14 07:13:45 +02:00
|
|
|
#include "pgstat.h"
|
2007-04-18 18:44:18 +02:00
|
|
|
#include "postmaster/autovacuum.h"
|
2001-07-14 00:55:59 +02:00
|
|
|
#include "storage/freespace.h"
|
2002-04-02 03:03:07 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2006-03-04 20:09:09 +01:00
|
|
|
#include "utils/memutils.h"
|
2005-10-04 00:52:26 +02:00
|
|
|
#include "utils/pg_rusage.h"
|
2008-03-26 22:10:39 +01:00
|
|
|
#include "utils/tqual.h"
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Space/time tradeoff parameters: do these need to be user-tunable?
|
|
|
|
*
|
|
|
|
* To consider truncating the relation, we want there to be at least
|
2003-03-04 22:51:22 +01:00
|
|
|
* REL_TRUNCATE_MINIMUM or (relsize / REL_TRUNCATE_FRACTION) (whichever
|
|
|
|
* is less) potentially-freeable pages.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2003-03-04 22:51:22 +01:00
|
|
|
#define REL_TRUNCATE_MINIMUM 1000
|
2001-07-14 00:55:59 +02:00
|
|
|
#define REL_TRUNCATE_FRACTION 16
|
|
|
|
|
2007-09-24 05:52:55 +02:00
|
|
|
/*
|
|
|
|
* Guesstimation of number of dead tuples per page. This is used to
|
|
|
|
* provide an upper limit to memory allocated when vacuuming small
|
|
|
|
* tables.
|
|
|
|
*/
|
2007-09-26 22:16:28 +02:00
|
|
|
#define LAZY_ALLOC_TUPLES MaxHeapTuplesPerPage
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
typedef struct LVRelStats
|
|
|
|
{
|
2006-09-13 19:47:08 +02:00
|
|
|
/* hasindex = true means two-pass strategy; false means one-pass */
|
|
|
|
bool hasindex;
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Overall statistics about rel */
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber rel_pages;
|
2001-07-14 00:55:59 +02:00
|
|
|
double rel_tuples;
|
2005-10-15 04:49:52 +02:00
|
|
|
BlockNumber pages_removed;
|
2004-12-01 20:00:56 +01:00
|
|
|
double tuples_deleted;
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */
|
2003-03-04 22:51:22 +01:00
|
|
|
Size threshold; /* minimum interesting free space */
|
2001-07-14 00:55:59 +02:00
|
|
|
/* List of TIDs of tuples we intend to delete */
|
|
|
|
/* NB: this list is ordered by TID address */
|
2001-10-28 07:26:15 +01:00
|
|
|
int num_dead_tuples; /* current # of entries */
|
|
|
|
int max_dead_tuples; /* # slots allocated in array */
|
2001-10-25 07:50:21 +02:00
|
|
|
ItemPointer dead_tuples; /* array of ItemPointerData */
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Array or heap of per-page info about free space */
|
|
|
|
/* We use a simple array until it fills up, then convert to heap */
|
2001-10-25 07:50:21 +02:00
|
|
|
bool fs_is_heap; /* are we using heap organization? */
|
|
|
|
int num_free_pages; /* current # of entries */
|
2002-09-20 21:56:01 +02:00
|
|
|
int max_free_pages; /* # slots allocated in array */
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageData *free_pages; /* array or heap of blkno/avail */
|
2006-10-04 02:30:14 +02:00
|
|
|
BlockNumber tot_free_pages; /* total pages with >= threshold space */
|
2007-04-18 18:44:18 +02:00
|
|
|
int num_index_scans;
|
2001-07-14 00:55:59 +02:00
|
|
|
} LVRelStats;
|
|
|
|
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
/* A few variables that don't seem worth passing around as parameters */
|
2002-09-04 22:31:48 +02:00
|
|
|
static int elevel = -1;
|
2001-07-14 00:55:59 +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
|
|
|
static TransactionId OldestXmin;
|
|
|
|
static TransactionId FreezeLimit;
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
static BufferAccessStrategy vac_strategy;
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* non-export function prototypes */
|
|
|
|
static void lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
|
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
|
|
|
Relation *Irel, int nindexes);
|
2001-07-14 00:55:59 +02:00
|
|
|
static void lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats);
|
2004-12-01 20:00:56 +01:00
|
|
|
static void lazy_vacuum_index(Relation indrel,
|
2006-10-04 02:30:14 +02:00
|
|
|
IndexBulkDeleteResult **stats,
|
|
|
|
LVRelStats *vacrelstats);
|
2006-05-03 00:25:10 +02:00
|
|
|
static void lazy_cleanup_index(Relation indrel,
|
2006-10-04 02:30:14 +02:00
|
|
|
IndexBulkDeleteResult *stats,
|
|
|
|
LVRelStats *vacrelstats);
|
2001-10-25 07:50:21 +02:00
|
|
|
static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
|
|
|
|
int tupindex, LVRelStats *vacrelstats);
|
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 lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
static BlockNumber count_nondeletable_pages(Relation onerel,
|
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
|
|
|
LVRelStats *vacrelstats);
|
2006-09-13 19:47:08 +02:00
|
|
|
static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks);
|
2001-07-14 00:55:59 +02:00
|
|
|
static void lazy_record_dead_tuple(LVRelStats *vacrelstats,
|
2001-10-25 07:50:21 +02:00
|
|
|
ItemPointer itemptr);
|
2001-07-14 00:55:59 +02:00
|
|
|
static void lazy_record_free_space(LVRelStats *vacrelstats,
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber page, Size avail);
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
static bool lazy_tid_reaped(ItemPointer itemptr, void *state);
|
2001-07-14 00:55:59 +02:00
|
|
|
static void lazy_update_fsm(Relation onerel, LVRelStats *vacrelstats);
|
|
|
|
static int vac_cmp_itemptr(const void *left, const void *right);
|
2002-09-20 21:56:01 +02:00
|
|
|
static int vac_cmp_page_spaces(const void *left, const void *right);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_vacuum_rel() -- perform LAZY VACUUM for one heap relation
|
|
|
|
*
|
|
|
|
* This routine vacuums a single heap, cleans out its indexes, and
|
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
|
|
|
* updates its relpages and reltuples statistics.
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
|
|
|
* At entry, we have already established a transaction and opened
|
|
|
|
* and locked the relation.
|
|
|
|
*/
|
|
|
|
void
|
2007-05-30 22:12:03 +02:00
|
|
|
lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
|
|
|
|
BufferAccessStrategy bstrategy)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
LVRelStats *vacrelstats;
|
|
|
|
Relation *Irel;
|
|
|
|
int nindexes;
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber possibly_freeable;
|
2007-04-18 18:44:18 +02:00
|
|
|
PGRUsage ru0;
|
2007-11-15 22:14:46 +01:00
|
|
|
TimestampTz starttime = 0;
|
2007-04-18 18:44:18 +02:00
|
|
|
|
|
|
|
pg_rusage_init(&ru0);
|
|
|
|
|
|
|
|
/* measure elapsed time iff autovacuum logging requires it */
|
2007-09-24 05:12:23 +02:00
|
|
|
if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration > 0)
|
2007-04-18 18:44:18 +02:00
|
|
|
starttime = GetCurrentTimestamp();
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
if (vacstmt->verbose)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elevel = INFO;
|
2001-07-14 00:55:59 +02:00
|
|
|
else
|
2003-05-27 19:49:47 +02:00
|
|
|
elevel = DEBUG2;
|
2002-03-06 07:10:59 +01:00
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
vac_strategy = bstrategy;
|
|
|
|
|
2007-05-17 17:28:29 +02:00
|
|
|
vacuum_set_xid_limits(vacstmt->freeze_min_age, onerel->rd_rel->relisshared,
|
2001-08-26 18:56:03 +02:00
|
|
|
&OldestXmin, &FreezeLimit);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2002-11-13 01:39:48 +01:00
|
|
|
vacrelstats = (LVRelStats *) palloc0(sizeof(LVRelStats));
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2003-03-04 22:51:22 +01:00
|
|
|
/* Set threshold for interesting free space = average request size */
|
|
|
|
/* XXX should we scale it up or down? Adjust vacuum.c too, if so */
|
|
|
|
vacrelstats->threshold = GetAvgFSMRequestSize(&onerel->rd_node);
|
|
|
|
|
2007-04-18 18:44:18 +02:00
|
|
|
vacrelstats->num_index_scans = 0;
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Open all indexes of the relation */
|
2006-07-31 22:09:10 +02:00
|
|
|
vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel);
|
2006-09-13 19:47:08 +02:00
|
|
|
vacrelstats->hasindex = (nindexes > 0);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* Do the vacuuming */
|
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
|
|
|
lazy_scan_heap(onerel, vacrelstats, Irel, nindexes);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* Done with indexes */
|
2004-10-01 01:21:26 +02:00
|
|
|
vac_close_indexes(nindexes, Irel, NoLock);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Optionally truncate the relation.
|
|
|
|
*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Don't even think about it unless we have a shot at releasing a goodly
|
|
|
|
* number of pages. Otherwise, the time taken isn't worth it.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
possibly_freeable = vacrelstats->rel_pages - vacrelstats->nonempty_pages;
|
2003-03-04 22:51:22 +01:00
|
|
|
if (possibly_freeable >= REL_TRUNCATE_MINIMUM ||
|
2005-10-15 04:49:52 +02:00
|
|
|
possibly_freeable >= vacrelstats->rel_pages / REL_TRUNCATE_FRACTION)
|
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
|
|
|
lazy_truncate_heap(onerel, vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* Update shared free space map with final free space info */
|
|
|
|
lazy_update_fsm(onerel, vacrelstats);
|
|
|
|
|
2007-02-21 23:15:21 +01:00
|
|
|
if (vacrelstats->tot_free_pages > MaxFSMPages)
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("relation \"%s.%s\" contains more than \"max_fsm_pages\" pages with useful free space",
|
|
|
|
get_namespace_name(RelationGetNamespace(onerel)),
|
|
|
|
RelationGetRelationName(onerel)),
|
2008-03-24 20:12:49 +01:00
|
|
|
/* Only suggest VACUUM FULL if > 20% free */
|
|
|
|
(vacrelstats->tot_free_pages > vacrelstats->rel_pages * 0.20) ?
|
|
|
|
errhint("Consider using VACUUM FULL on this relation or increasing the configuration parameter \"max_fsm_pages\".") :
|
|
|
|
errhint("Consider increasing the configuration parameter \"max_fsm_pages\".")));
|
2007-02-21 23:15:21 +01:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Update statistics in pg_class */
|
2005-03-25 23:51:31 +01:00
|
|
|
vac_update_relstats(RelationGetRelid(onerel),
|
|
|
|
vacrelstats->rel_pages,
|
|
|
|
vacrelstats->rel_tuples,
|
2006-09-13 19:47:08 +02:00
|
|
|
vacrelstats->hasindex,
|
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
|
|
|
FreezeLimit);
|
2005-07-14 07:13:45 +02:00
|
|
|
|
|
|
|
/* report results to the stats collector, too */
|
2005-07-29 21:30:09 +02:00
|
|
|
pgstat_report_vacuum(RelationGetRelid(onerel), onerel->rd_rel->relisshared,
|
2005-10-15 04:49:52 +02:00
|
|
|
vacstmt->analyze, vacrelstats->rel_tuples);
|
2007-04-18 18:44:18 +02:00
|
|
|
|
|
|
|
/* and log the action if appropriate */
|
2007-09-24 05:12:23 +02:00
|
|
|
if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
|
2007-04-18 18:44:18 +02:00
|
|
|
{
|
2007-09-24 05:12:23 +02:00
|
|
|
if (Log_autovacuum_min_duration == 0 ||
|
2007-04-30 05:23:49 +02:00
|
|
|
TimestampDifferenceExceeds(starttime, GetCurrentTimestamp(),
|
2007-09-24 05:12:23 +02:00
|
|
|
Log_autovacuum_min_duration))
|
2007-04-18 18:44:18 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"
|
|
|
|
"pages: %d removed, %d remain\n"
|
|
|
|
"tuples: %.0f removed, %.0f remain\n"
|
|
|
|
"system usage: %s",
|
|
|
|
get_database_name(MyDatabaseId),
|
|
|
|
get_namespace_name(RelationGetNamespace(onerel)),
|
|
|
|
RelationGetRelationName(onerel),
|
|
|
|
vacrelstats->num_index_scans,
|
2007-11-15 22:14:46 +01:00
|
|
|
vacrelstats->pages_removed, vacrelstats->rel_pages,
|
|
|
|
vacrelstats->tuples_deleted, vacrelstats->rel_tuples,
|
2007-04-18 18:44:18 +02:00
|
|
|
pg_rusage_show(&ru0))));
|
|
|
|
}
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_scan_heap() -- scan an open heap relation
|
|
|
|
*
|
|
|
|
* This routine sets commit status bits, builds lists of dead tuples
|
|
|
|
* and pages with free space, and calculates statistics on the number
|
|
|
|
* of live tuples in the heap. When done, or when we run low on space
|
2006-09-13 19:47:08 +02:00
|
|
|
* for dead-tuple TIDs, invoke vacuuming of indexes and heap.
|
2006-07-10 18:20:52 +02:00
|
|
|
*
|
2006-09-13 19:47:08 +02:00
|
|
|
* If there are no indexes then we just vacuum each dirty page as we
|
|
|
|
* process it, since there's no point in gathering many tuples.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
|
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
|
|
|
Relation *Irel, int nindexes)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
BlockNumber nblocks,
|
|
|
|
blkno;
|
|
|
|
HeapTupleData tuple;
|
|
|
|
char *relname;
|
2006-09-13 19:47:08 +02:00
|
|
|
BlockNumber empty_pages,
|
|
|
|
vacuumed_pages;
|
2001-07-14 00:55:59 +02:00
|
|
|
double num_tuples,
|
|
|
|
tups_vacuumed,
|
|
|
|
nkeep,
|
|
|
|
nunused;
|
2006-05-03 00:25:10 +02:00
|
|
|
IndexBulkDeleteResult **indstats;
|
2001-07-14 00:55:59 +02:00
|
|
|
int i;
|
2005-10-04 00:52:26 +02:00
|
|
|
PGRUsage ru0;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_init(&ru0);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
relname = RelationGetRelationName(onerel);
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
|
|
|
(errmsg("vacuuming \"%s.%s\"",
|
|
|
|
get_namespace_name(RelationGetNamespace(onerel)),
|
|
|
|
relname)));
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2006-09-13 19:47:08 +02:00
|
|
|
empty_pages = vacuumed_pages = 0;
|
2001-07-14 00:55:59 +02:00
|
|
|
num_tuples = tups_vacuumed = nkeep = nunused = 0;
|
|
|
|
|
2006-05-03 00:25:10 +02:00
|
|
|
indstats = (IndexBulkDeleteResult **)
|
|
|
|
palloc0(nindexes * sizeof(IndexBulkDeleteResult *));
|
2004-12-01 20:00:56 +01:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
nblocks = RelationGetNumberOfBlocks(onerel);
|
|
|
|
vacrelstats->rel_pages = nblocks;
|
|
|
|
vacrelstats->nonempty_pages = 0;
|
|
|
|
|
2006-09-13 19:47:08 +02:00
|
|
|
lazy_space_alloc(vacrelstats, nblocks);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
for (blkno = 0; blkno < nblocks; blkno++)
|
|
|
|
{
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum,
|
|
|
|
maxoff;
|
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 tupgone,
|
2001-07-14 00:55:59 +02:00
|
|
|
hastup;
|
|
|
|
int prev_dead_count;
|
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
|
|
|
OffsetNumber frozen[MaxOffsetNumber];
|
|
|
|
int nfrozen;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2004-02-10 04:42:45 +01:00
|
|
|
vacuum_delay_point();
|
2004-02-06 20:36:18 +01:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we are close to overrunning the available space for dead-tuple
|
|
|
|
* TIDs, pause and do a cycle of vacuuming before we tackle this page.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2005-09-02 21:02:20 +02:00
|
|
|
if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MaxHeapTuplesPerPage &&
|
2001-07-14 00:55:59 +02:00
|
|
|
vacrelstats->num_dead_tuples > 0)
|
|
|
|
{
|
|
|
|
/* Remove index entries */
|
|
|
|
for (i = 0; i < nindexes; i++)
|
2004-12-01 20:00:56 +01:00
|
|
|
lazy_vacuum_index(Irel[i],
|
2006-05-03 00:25:10 +02:00
|
|
|
&indstats[i],
|
2004-12-01 20:00:56 +01:00
|
|
|
vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Remove tuples from heap */
|
|
|
|
lazy_vacuum_heap(onerel, vacrelstats);
|
|
|
|
/* Forget the now-vacuumed tuples, and press on */
|
|
|
|
vacrelstats->num_dead_tuples = 0;
|
2007-04-18 18:44:18 +02:00
|
|
|
vacrelstats->num_index_scans++;
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
buf = ReadBufferWithStrategy(onerel, blkno, vac_strategy);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
/* We need buffer cleanup lock so that we can prune HOT chains. */
|
|
|
|
LockBufferForCleanup(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
|
|
|
if (PageIsNew(page))
|
|
|
|
{
|
2005-05-07 23:32:24 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* An all-zeroes page could be left over if a backend extends the
|
|
|
|
* relation but crashes before initializing the page. Reclaim such
|
|
|
|
* pages for use.
|
2005-05-07 23:32:24 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We have to be careful here because we could be looking at a
|
|
|
|
* page that someone has just added to the relation and not yet
|
|
|
|
* been able to initialize (see RelationGetBufferForTuple). To
|
2007-09-20 19:56:33 +02:00
|
|
|
* protect against that, release the buffer lock, grab the
|
2007-11-15 22:14:46 +01:00
|
|
|
* relation extension lock momentarily, and re-lock the buffer. If
|
|
|
|
* the page is still uninitialized by then, it must be left over
|
|
|
|
* from a crashed backend, and we can initialize it.
|
2005-05-07 23:32:24 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We don't really need the relation lock when this is a new or
|
|
|
|
* temp relation, but it's probably not worth the code space to
|
|
|
|
* check that, since this surely isn't a critical path.
|
2005-05-07 23:32:24 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note: the comparable code in vacuum.c need not worry because
|
|
|
|
* it's got exclusive lock on the whole relation.
|
2005-05-07 23:32:24 +02:00
|
|
|
*/
|
2001-07-14 00:55:59 +02:00
|
|
|
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
|
2005-05-07 23:32:24 +02:00
|
|
|
LockRelationForExtension(onerel, ExclusiveLock);
|
|
|
|
UnlockRelationForExtension(onerel, ExclusiveLock);
|
2007-09-20 19:56:33 +02:00
|
|
|
LockBufferForCleanup(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
if (PageIsNew(page))
|
|
|
|
{
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(WARNING,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("relation \"%s\" page %u is uninitialized --- fixing",
|
|
|
|
relname, blkno)));
|
2001-07-14 00:55:59 +02:00
|
|
|
PageInit(page, BufferGetPageSize(buf), 0);
|
2003-07-20 23:56:35 +02:00
|
|
|
empty_pages++;
|
2001-07-14 00:55:59 +02:00
|
|
|
lazy_record_free_space(vacrelstats, blkno,
|
2007-09-20 19:56:33 +02:00
|
|
|
PageGetHeapFreeSpace(page));
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buf);
|
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PageIsEmpty(page))
|
|
|
|
{
|
|
|
|
empty_pages++;
|
|
|
|
lazy_record_free_space(vacrelstats, blkno,
|
2007-09-20 19:56:33 +02:00
|
|
|
PageGetHeapFreeSpace(page));
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/*
|
2007-09-20 19:56:33 +02:00
|
|
|
* Prune all HOT-update chains in this page.
|
|
|
|
*
|
|
|
|
* We count tuples removed by the pruning step as removed by VACUUM.
|
|
|
|
*/
|
|
|
|
tups_vacuumed += heap_page_prune(onerel, buf, OldestXmin,
|
|
|
|
false, false);
|
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Now scan the page to collect vacuumable items and check for tuples
|
|
|
|
* requiring freezing.
|
2007-09-20 19:56:33 +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
|
|
|
nfrozen = 0;
|
2001-07-14 00:55:59 +02:00
|
|
|
hastup = false;
|
|
|
|
prev_dead_count = vacrelstats->num_dead_tuples;
|
|
|
|
maxoff = PageGetMaxOffsetNumber(page);
|
|
|
|
for (offnum = FirstOffsetNumber;
|
|
|
|
offnum <= maxoff;
|
|
|
|
offnum = OffsetNumberNext(offnum))
|
|
|
|
{
|
|
|
|
ItemId itemid;
|
|
|
|
|
|
|
|
itemid = PageGetItemId(page, offnum);
|
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
/* Unused items require no processing, but we count 'em */
|
2001-07-14 00:55:59 +02:00
|
|
|
if (!ItemIdIsUsed(itemid))
|
|
|
|
{
|
|
|
|
nunused += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
/* Redirect items mustn't be touched */
|
2007-11-15 22:14:46 +01:00
|
|
|
if (ItemIdIsRedirected(itemid))
|
|
|
|
{
|
2007-09-20 19:56:33 +02:00
|
|
|
hastup = true; /* this page won't be truncatable */
|
2007-11-15 22:14:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-20 19:56:33 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
ItemPointerSet(&(tuple.t_self), blkno, offnum);
|
2007-09-20 19:56:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DEAD item pointers are to be vacuumed normally; but we don't
|
2007-11-15 22:14:46 +01:00
|
|
|
* count them in tups_vacuumed, else we'd be double-counting (at
|
|
|
|
* least in the common case where heap_page_prune() just freed up
|
|
|
|
* a non-HOT tuple).
|
2007-09-20 19:56:33 +02:00
|
|
|
*/
|
|
|
|
if (ItemIdIsDead(itemid))
|
|
|
|
{
|
|
|
|
lazy_record_dead_tuple(vacrelstats, &(tuple.t_self));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(ItemIdIsNormal(itemid));
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
|
|
|
|
tuple.t_len = ItemIdGetLength(itemid);
|
|
|
|
|
|
|
|
tupgone = false;
|
|
|
|
|
2004-10-16 00:40:29 +02:00
|
|
|
switch (HeapTupleSatisfiesVacuum(tuple.t_data, OldestXmin, buf))
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
case HEAPTUPLE_DEAD:
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
/*
|
|
|
|
* Ordinarily, DEAD tuples would have been removed by
|
|
|
|
* heap_page_prune(), but it's possible that the tuple
|
|
|
|
* state changed since heap_page_prune() looked. In
|
|
|
|
* particular an INSERT_IN_PROGRESS tuple could have
|
|
|
|
* changed to DEAD if the inserter aborted. So this
|
|
|
|
* cannot be considered an error condition.
|
|
|
|
*
|
|
|
|
* If the tuple is HOT-updated then it must only be
|
2007-11-15 22:14:46 +01:00
|
|
|
* removed by a prune operation; so we keep it just as if
|
|
|
|
* it were RECENTLY_DEAD. Also, if it's a heap-only
|
|
|
|
* tuple, we choose to keep it, because it'll be a lot
|
|
|
|
* cheaper to get rid of it in the next pruning pass than
|
|
|
|
* to treat it like an indexed tuple.
|
2007-09-20 19:56:33 +02:00
|
|
|
*/
|
|
|
|
if (HeapTupleIsHotUpdated(&tuple) ||
|
|
|
|
HeapTupleIsHeapOnly(&tuple))
|
|
|
|
nkeep += 1;
|
|
|
|
else
|
2007-11-15 22:14:46 +01:00
|
|
|
tupgone = true; /* we can delete the tuple */
|
2001-07-14 00:55:59 +02:00
|
|
|
break;
|
|
|
|
case HEAPTUPLE_LIVE:
|
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
|
|
|
/* Tuple is good --- but let's do some validity checks */
|
2004-02-12 06:39:55 +01:00
|
|
|
if (onerel->rd_rel->relhasoids &&
|
|
|
|
!OidIsValid(HeapTupleGetOid(&tuple)))
|
|
|
|
elog(WARNING, "relation \"%s\" TID %u/%u: OID is invalid",
|
|
|
|
relname, blkno, offnum);
|
2001-07-14 00:55:59 +02:00
|
|
|
break;
|
|
|
|
case HEAPTUPLE_RECENTLY_DEAD:
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If tuple is recently deleted then we must not remove it
|
|
|
|
* from relation.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
nkeep += 1;
|
|
|
|
break;
|
|
|
|
case HEAPTUPLE_INSERT_IN_PROGRESS:
|
|
|
|
/* This is an expected case during concurrent vacuum */
|
|
|
|
break;
|
|
|
|
case HEAPTUPLE_DELETE_IN_PROGRESS:
|
|
|
|
/* This is an expected case during concurrent vacuum */
|
|
|
|
break;
|
|
|
|
default:
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
|
2001-07-14 00:55:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tupgone)
|
|
|
|
{
|
|
|
|
lazy_record_dead_tuple(vacrelstats, &(tuple.t_self));
|
|
|
|
tups_vacuumed += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
num_tuples += 1;
|
|
|
|
hastup = true;
|
2006-07-10 18:20:52 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Each non-removable tuple must be checked to see if it needs
|
|
|
|
* freezing. Note we already have exclusive buffer lock.
|
2006-07-10 18:20:52 +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 (heap_freeze_tuple(tuple.t_data, FreezeLimit,
|
2007-09-20 19:56:33 +02:00
|
|
|
InvalidBuffer))
|
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
|
|
|
frozen[nfrozen++] = offnum;
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
} /* scan along page */
|
2001-07-14 00:55:59 +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 we froze any tuples, mark the buffer dirty, and write a WAL
|
|
|
|
* record recording the changes. We must log the changes to be
|
|
|
|
* crash-safe against future truncation of CLOG.
|
|
|
|
*/
|
|
|
|
if (nfrozen > 0)
|
|
|
|
{
|
|
|
|
MarkBufferDirty(buf);
|
|
|
|
/* no XLOG for temp tables, though */
|
|
|
|
if (!onerel->rd_istemp)
|
|
|
|
{
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
|
|
|
|
recptr = log_heap_freeze(onerel, buf, FreezeLimit,
|
|
|
|
frozen, nfrozen);
|
|
|
|
PageSetLSN(page, recptr);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 19:47:08 +02:00
|
|
|
/*
|
|
|
|
* If there are no indexes then we can vacuum the page right now
|
|
|
|
* instead of doing a second scan.
|
|
|
|
*/
|
|
|
|
if (nindexes == 0 &&
|
|
|
|
vacrelstats->num_dead_tuples > 0)
|
|
|
|
{
|
|
|
|
/* Remove tuples from heap */
|
|
|
|
lazy_vacuum_page(onerel, blkno, buf, 0, vacrelstats);
|
|
|
|
/* Forget the now-vacuumed tuples, and press on */
|
|
|
|
vacrelstats->num_dead_tuples = 0;
|
|
|
|
vacuumed_pages++;
|
|
|
|
}
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* If we remembered any tuples for deletion, then the page will be
|
2005-10-15 04:49:52 +02:00
|
|
|
* visited again by lazy_vacuum_heap, which will compute and record
|
|
|
|
* its post-compaction free space. If not, then we're done with this
|
2006-10-04 02:30:14 +02:00
|
|
|
* page, so remember its free space as-is. (This path will always be
|
|
|
|
* taken if there are no indexes.)
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
if (vacrelstats->num_dead_tuples == prev_dead_count)
|
|
|
|
{
|
|
|
|
lazy_record_free_space(vacrelstats, blkno,
|
2007-09-20 19:56:33 +02:00
|
|
|
PageGetHeapFreeSpace(page));
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember the location of the last page with nonremovable tuples */
|
|
|
|
if (hastup)
|
|
|
|
vacrelstats->nonempty_pages = blkno + 1;
|
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
2001-07-18 02:46:25 +02:00
|
|
|
/* save stats for use later */
|
|
|
|
vacrelstats->rel_tuples = num_tuples;
|
2004-12-01 20:00:56 +01:00
|
|
|
vacrelstats->tuples_deleted = tups_vacuumed;
|
2001-07-18 02:46:25 +02:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/* If any tuples need to be deleted, perform final vacuum cycle */
|
2003-02-22 01:45:05 +01:00
|
|
|
/* XXX put a threshold on min number of tuples here? */
|
2001-07-14 00:55:59 +02:00
|
|
|
if (vacrelstats->num_dead_tuples > 0)
|
|
|
|
{
|
|
|
|
/* Remove index entries */
|
|
|
|
for (i = 0; i < nindexes; i++)
|
2004-12-01 20:00:56 +01:00
|
|
|
lazy_vacuum_index(Irel[i],
|
2006-05-03 00:25:10 +02:00
|
|
|
&indstats[i],
|
2004-12-01 20:00:56 +01:00
|
|
|
vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Remove tuples from heap */
|
|
|
|
lazy_vacuum_heap(onerel, vacrelstats);
|
2007-04-18 18:44:18 +02:00
|
|
|
vacrelstats->num_index_scans++;
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
2006-05-03 00:25:10 +02:00
|
|
|
|
|
|
|
/* Do post-vacuum cleanup and statistics update for each index */
|
|
|
|
for (i = 0; i < nindexes; i++)
|
|
|
|
lazy_cleanup_index(Irel[i], indstats[i], vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2006-09-13 19:47:08 +02:00
|
|
|
/* If no indexes, make log report that lazy_vacuum_heap would've made */
|
|
|
|
if (vacuumed_pages)
|
|
|
|
ereport(elevel,
|
|
|
|
(errmsg("\"%s\": removed %.0f row versions in %u pages",
|
|
|
|
RelationGetRelationName(onerel),
|
|
|
|
tups_vacuumed, vacuumed_pages)));
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u pages",
|
2003-07-20 23:56:35 +02:00
|
|
|
RelationGetRelationName(onerel),
|
|
|
|
tups_vacuumed, num_tuples, nblocks),
|
2003-09-25 08:58:07 +02:00
|
|
|
errdetail("%.0f dead row versions cannot be removed yet.\n"
|
2003-07-20 23:56:35 +02:00
|
|
|
"There were %.0f unused item pointers.\n"
|
2006-09-21 22:31:22 +02:00
|
|
|
"%u pages contain useful free space.\n"
|
2003-07-20 23:56:35 +02:00
|
|
|
"%u pages are entirely empty.\n"
|
2005-10-04 00:52:26 +02:00
|
|
|
"%s.",
|
2003-07-20 23:56:35 +02:00
|
|
|
nkeep,
|
|
|
|
nunused,
|
2006-09-21 22:31:22 +02:00
|
|
|
vacrelstats->tot_free_pages,
|
2003-07-20 23:56:35 +02:00
|
|
|
empty_pages,
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_show(&ru0))));
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_vacuum_heap() -- second pass over the heap
|
|
|
|
*
|
|
|
|
* This routine marks dead tuples as unused and compacts out free
|
|
|
|
* space on their pages. Pages not having dead tuples recorded from
|
|
|
|
* lazy_scan_heap are not visited at all.
|
|
|
|
*
|
|
|
|
* Note: the reason for doing this as a second pass is we cannot remove
|
|
|
|
* the tuples until we've removed their index entries, and we want to
|
|
|
|
* process index entry removal in batches as large as possible.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
|
|
|
|
{
|
|
|
|
int tupindex;
|
|
|
|
int npages;
|
2005-10-04 00:52:26 +02:00
|
|
|
PGRUsage ru0;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_init(&ru0);
|
2001-07-14 00:55:59 +02:00
|
|
|
npages = 0;
|
|
|
|
|
|
|
|
tupindex = 0;
|
|
|
|
while (tupindex < vacrelstats->num_dead_tuples)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber tblk;
|
2001-07-14 00:55:59 +02:00
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
|
2004-02-10 04:42:45 +01:00
|
|
|
vacuum_delay_point();
|
2004-02-06 20:36:18 +01:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
tblk = ItemPointerGetBlockNumber(&vacrelstats->dead_tuples[tupindex]);
|
2007-05-30 22:12:03 +02:00
|
|
|
buf = ReadBufferWithStrategy(onerel, tblk, vac_strategy);
|
2001-07-14 00:55:59 +02:00
|
|
|
LockBufferForCleanup(buf);
|
|
|
|
tupindex = lazy_vacuum_page(onerel, tblk, buf, tupindex, vacrelstats);
|
|
|
|
/* Now that we've compacted the page, record its available space */
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
lazy_record_free_space(vacrelstats, tblk,
|
2007-09-20 19:56:33 +02:00
|
|
|
PageGetHeapFreeSpace(page));
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
npages++;
|
|
|
|
}
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("\"%s\": removed %d row versions in %d pages",
|
2003-07-20 23:56:35 +02:00
|
|
|
RelationGetRelationName(onerel),
|
|
|
|
tupindex, npages),
|
2005-10-04 00:52:26 +02:00
|
|
|
errdetail("%s.",
|
|
|
|
pg_rusage_show(&ru0))));
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_vacuum_page() -- free dead tuples on a page
|
|
|
|
* and repair its fragmentation.
|
|
|
|
*
|
2007-09-20 19:56:33 +02:00
|
|
|
* Caller must hold pin and buffer cleanup lock on the buffer.
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
|
|
|
* tupindex is the index in vacrelstats->dead_tuples of the first dead
|
|
|
|
* tuple for this page. We assume the rest follow sequentially.
|
|
|
|
* The return value is the first tupindex after the tuples of this page.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
|
|
|
|
int tupindex, LVRelStats *vacrelstats)
|
|
|
|
{
|
|
|
|
Page page = BufferGetPage(buffer);
|
2007-09-20 19:56:33 +02:00
|
|
|
OffsetNumber unused[MaxOffsetNumber];
|
|
|
|
int uncnt = 0;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
START_CRIT_SECTION();
|
2006-04-01 01:32:07 +02:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
for (; tupindex < vacrelstats->num_dead_tuples; tupindex++)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber tblk;
|
|
|
|
OffsetNumber toff;
|
2007-09-20 19:56:33 +02:00
|
|
|
ItemId itemid;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
tblk = ItemPointerGetBlockNumber(&vacrelstats->dead_tuples[tupindex]);
|
|
|
|
if (tblk != blkno)
|
|
|
|
break; /* past end of tuples for this block */
|
|
|
|
toff = ItemPointerGetOffsetNumber(&vacrelstats->dead_tuples[tupindex]);
|
|
|
|
itemid = PageGetItemId(page, toff);
|
2007-09-13 00:10:26 +02:00
|
|
|
ItemIdSetUnused(itemid);
|
2007-09-20 19:56:33 +02:00
|
|
|
unused[uncnt++] = toff;
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
PageRepairFragmentation(page);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buffer);
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* XLOG stuff */
|
|
|
|
if (!onerel->rd_istemp)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
|
2007-09-20 19:56:33 +02:00
|
|
|
recptr = log_heap_clean(onerel, buffer,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
unused, uncnt,
|
|
|
|
false);
|
2001-07-14 00:55:59 +02:00
|
|
|
PageSetLSN(page, recptr);
|
2004-07-22 00:31:26 +02:00
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
2002-08-06 04:36:35 +02:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
|
|
|
return tupindex;
|
|
|
|
}
|
|
|
|
|
2001-07-18 02:46:25 +02:00
|
|
|
/*
|
2006-05-03 00:25:10 +02:00
|
|
|
* lazy_vacuum_index() -- vacuum one index relation.
|
2001-07-18 02:46:25 +02:00
|
|
|
*
|
2006-05-03 00:25:10 +02:00
|
|
|
* Delete all the index entries pointing to tuples listed in
|
|
|
|
* vacrelstats->dead_tuples, and update running statistics.
|
2001-07-18 02:46:25 +02:00
|
|
|
*/
|
|
|
|
static void
|
2006-05-03 00:25:10 +02:00
|
|
|
lazy_vacuum_index(Relation indrel,
|
|
|
|
IndexBulkDeleteResult **stats,
|
|
|
|
LVRelStats *vacrelstats)
|
2001-07-18 02:46:25 +02:00
|
|
|
{
|
2006-05-03 00:25:10 +02:00
|
|
|
IndexVacuumInfo ivinfo;
|
2005-10-04 00:52:26 +02:00
|
|
|
PGRUsage ru0;
|
2001-07-18 02:46:25 +02:00
|
|
|
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_init(&ru0);
|
2001-07-18 02:46:25 +02:00
|
|
|
|
2006-05-03 00:25:10 +02:00
|
|
|
ivinfo.index = indrel;
|
|
|
|
ivinfo.vacuum_full = false;
|
|
|
|
ivinfo.message_level = elevel;
|
|
|
|
/* We don't yet know rel_tuples, so pass -1 */
|
|
|
|
ivinfo.num_heap_tuples = -1;
|
2007-05-30 22:12:03 +02:00
|
|
|
ivinfo.strategy = vac_strategy;
|
2003-02-22 01:45:05 +01:00
|
|
|
|
2006-05-03 00:25:10 +02:00
|
|
|
/* Do bulk deletion */
|
|
|
|
*stats = index_bulk_delete(&ivinfo, *stats,
|
|
|
|
lazy_tid_reaped, (void *) vacrelstats);
|
2003-02-22 01:45:05 +01:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
2006-05-03 00:25:10 +02:00
|
|
|
(errmsg("scanned index \"%s\" to remove %d row versions",
|
2005-10-15 04:49:52 +02:00
|
|
|
RelationGetRelationName(indrel),
|
2006-05-03 00:25:10 +02:00
|
|
|
vacrelstats->num_dead_tuples),
|
|
|
|
errdetail("%s.", pg_rusage_show(&ru0))));
|
2001-07-18 02:46:25 +02:00
|
|
|
}
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
2006-05-03 00:25:10 +02:00
|
|
|
* lazy_cleanup_index() -- do post-vacuum cleanup for one index relation.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
static void
|
2006-05-03 00:25:10 +02:00
|
|
|
lazy_cleanup_index(Relation indrel,
|
|
|
|
IndexBulkDeleteResult *stats,
|
|
|
|
LVRelStats *vacrelstats)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
2006-05-03 00:25:10 +02:00
|
|
|
IndexVacuumInfo ivinfo;
|
2005-10-04 00:52:26 +02:00
|
|
|
PGRUsage ru0;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_init(&ru0);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2006-05-03 00:25:10 +02:00
|
|
|
ivinfo.index = indrel;
|
|
|
|
ivinfo.vacuum_full = false;
|
|
|
|
ivinfo.message_level = elevel;
|
|
|
|
ivinfo.num_heap_tuples = vacrelstats->rel_tuples;
|
2007-05-30 22:12:03 +02:00
|
|
|
ivinfo.strategy = vac_strategy;
|
2003-02-22 01:45:05 +01:00
|
|
|
|
2006-05-03 00:25:10 +02:00
|
|
|
stats = index_vacuum_cleanup(&ivinfo, stats);
|
2003-02-22 01:45:05 +01:00
|
|
|
|
|
|
|
if (!stats)
|
|
|
|
return;
|
|
|
|
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
/* now update statistics in pg_class */
|
2005-03-25 23:51:31 +01:00
|
|
|
vac_update_relstats(RelationGetRelid(indrel),
|
|
|
|
stats->num_pages,
|
|
|
|
stats->num_index_tuples,
|
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
|
|
|
false, InvalidTransactionId);
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("index \"%s\" now contains %.0f row versions in %u pages",
|
|
|
|
RelationGetRelationName(indrel),
|
|
|
|
stats->num_index_tuples,
|
|
|
|
stats->num_pages),
|
|
|
|
errdetail("%.0f index row versions were removed.\n"
|
|
|
|
"%u index pages have been deleted, %u are currently reusable.\n"
|
|
|
|
"%s.",
|
|
|
|
stats->tuples_removed,
|
|
|
|
stats->pages_deleted, stats->pages_free,
|
|
|
|
pg_rusage_show(&ru0))));
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
|
2003-02-22 01:45:05 +01:00
|
|
|
pfree(stats);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_truncate_heap - try to truncate off any empty pages at the end
|
|
|
|
*/
|
|
|
|
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
|
|
|
lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
BlockNumber old_rel_pages = vacrelstats->rel_pages;
|
|
|
|
BlockNumber new_rel_pages;
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageData *pageSpaces;
|
2001-07-14 00:55:59 +02:00
|
|
|
int n;
|
|
|
|
int i,
|
|
|
|
j;
|
2005-10-04 00:52:26 +02:00
|
|
|
PGRUsage ru0;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2005-10-04 00:52:26 +02:00
|
|
|
pg_rusage_init(&ru0);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We need full exclusive lock on the relation in order to do truncation.
|
|
|
|
* If we can't get it, give up rather than waiting --- we don't want to
|
|
|
|
* block other backends, and we don't want to deadlock (which is quite
|
|
|
|
* possible considering we already hold a lower-grade lock).
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
if (!ConditionalLockRelation(onerel, AccessExclusiveLock))
|
2001-07-14 00:55:59 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we have exclusive lock, look to see if the rel has grown
|
2005-10-15 04:49:52 +02:00
|
|
|
* whilst we were vacuuming with non-exclusive lock. If so, give up; the
|
|
|
|
* newly added pages presumably contain non-deletable tuples.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
new_rel_pages = RelationGetNumberOfBlocks(onerel);
|
|
|
|
if (new_rel_pages != old_rel_pages)
|
|
|
|
{
|
|
|
|
/* might as well use the latest news when we update pg_class stats */
|
|
|
|
vacrelstats->rel_pages = new_rel_pages;
|
|
|
|
UnlockRelation(onerel, AccessExclusiveLock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan backwards from the end to verify that the end pages actually
|
2007-09-16 04:37:46 +02:00
|
|
|
* contain no tuples. This is *necessary*, not optional, because other
|
|
|
|
* backends could have added tuples to these pages whilst we were
|
|
|
|
* vacuuming.
|
2001-07-14 00:55:59 +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
|
|
|
new_rel_pages = count_nondeletable_pages(onerel, vacrelstats);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
if (new_rel_pages >= old_rel_pages)
|
|
|
|
{
|
|
|
|
/* can't do anything after all */
|
|
|
|
UnlockRelation(onerel, AccessExclusiveLock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay to truncate.
|
|
|
|
*/
|
2004-05-08 21:09:25 +02:00
|
|
|
RelationTruncate(onerel, new_rel_pages);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2007-09-20 23:43:27 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Note: once we have truncated, we *must* keep the exclusive lock until
|
|
|
|
* commit. The sinval message that will be sent at commit (as a result of
|
|
|
|
* vac_update_relstats()) must be received by other backends, to cause
|
|
|
|
* them to reset their rd_targblock values, before they can safely access
|
|
|
|
* the table again.
|
2007-09-20 23:43:27 +02:00
|
|
|
*/
|
2007-09-10 23:40:03 +02:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
|
|
|
* Drop free-space info for removed blocks; these must not get entered
|
|
|
|
* into the FSM!
|
|
|
|
*/
|
2002-09-20 21:56:01 +02:00
|
|
|
pageSpaces = vacrelstats->free_pages;
|
2001-07-14 00:55:59 +02:00
|
|
|
n = vacrelstats->num_free_pages;
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
if (FSMPageGetPageNum(&pageSpaces[i]) < new_rel_pages)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
2002-09-20 21:56:01 +02:00
|
|
|
pageSpaces[j] = pageSpaces[i];
|
2001-07-14 00:55:59 +02:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vacrelstats->num_free_pages = j;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-09-21 22:31:22 +02:00
|
|
|
/*
|
|
|
|
* If tot_free_pages was more than num_free_pages, we can't tell for sure
|
|
|
|
* what its correct value is now, because we don't know which of the
|
2006-10-04 02:30:14 +02:00
|
|
|
* forgotten pages are getting truncated. Conservatively set it equal to
|
|
|
|
* num_free_pages.
|
2006-09-21 22:31:22 +02:00
|
|
|
*/
|
|
|
|
vacrelstats->tot_free_pages = j;
|
|
|
|
|
2002-09-20 21:56:01 +02:00
|
|
|
/* We destroyed the heap ordering, so mark array unordered */
|
|
|
|
vacrelstats->fs_is_heap = false;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2004-12-01 20:00:56 +01:00
|
|
|
/* update statistics */
|
|
|
|
vacrelstats->rel_pages = new_rel_pages;
|
|
|
|
vacrelstats->pages_removed = old_rel_pages - new_rel_pages;
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(elevel,
|
|
|
|
(errmsg("\"%s\": truncated %u to %u pages",
|
|
|
|
RelationGetRelationName(onerel),
|
|
|
|
old_rel_pages, new_rel_pages),
|
2005-10-04 00:52:26 +02:00
|
|
|
errdetail("%s.",
|
|
|
|
pg_rusage_show(&ru0))));
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-16 04:37:46 +02:00
|
|
|
* Rescan end pages to verify that they are (still) empty of tuples.
|
2001-07-14 00:55:59 +02:00
|
|
|
*
|
|
|
|
* Returns number of nondeletable pages (last nonempty page + 1).
|
|
|
|
*/
|
|
|
|
static BlockNumber
|
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
|
|
|
count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
BlockNumber blkno;
|
|
|
|
|
|
|
|
/* Strange coding of loop control is needed because blkno is unsigned */
|
|
|
|
blkno = vacrelstats->rel_pages;
|
|
|
|
while (blkno > vacrelstats->nonempty_pages)
|
|
|
|
{
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offnum,
|
|
|
|
maxoff;
|
2007-09-16 04:37:46 +02:00
|
|
|
bool hastup;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2007-09-10 19:58:45 +02:00
|
|
|
/*
|
|
|
|
* We don't insert a vacuum delay point here, because we have an
|
2007-11-15 22:14:46 +01:00
|
|
|
* exclusive lock on the table which we want to hold for as short a
|
|
|
|
* time as possible. We still need to check for interrupts however.
|
2007-09-10 19:58:45 +02:00
|
|
|
*/
|
2007-09-12 04:05:48 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
2004-02-06 20:36:18 +01:00
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
blkno--;
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
buf = ReadBufferWithStrategy(onerel, blkno, vac_strategy);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* In this phase we only need shared access to the buffer */
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
|
|
|
if (PageIsNew(page) || PageIsEmpty(page))
|
|
|
|
{
|
2004-04-26 01:50:58 +02:00
|
|
|
/* PageIsNew probably shouldn't happen... */
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hastup = false;
|
|
|
|
maxoff = PageGetMaxOffsetNumber(page);
|
|
|
|
for (offnum = FirstOffsetNumber;
|
|
|
|
offnum <= maxoff;
|
|
|
|
offnum = OffsetNumberNext(offnum))
|
|
|
|
{
|
|
|
|
ItemId itemid;
|
|
|
|
|
|
|
|
itemid = PageGetItemId(page, offnum);
|
|
|
|
|
2007-09-16 04:37:46 +02:00
|
|
|
/*
|
|
|
|
* Note: any non-unused item should be taken as a reason to keep
|
|
|
|
* this page. We formerly thought that DEAD tuples could be
|
|
|
|
* thrown away, but that's not so, because we'd not have cleaned
|
|
|
|
* out their index entries.
|
|
|
|
*/
|
|
|
|
if (ItemIdIsUsed(itemid))
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
hastup = true;
|
|
|
|
break; /* can stop scanning */
|
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
} /* scan along page */
|
2001-07-14 00:55:59 +02:00
|
|
|
|
2006-04-01 01:32:07 +02:00
|
|
|
UnlockReleaseBuffer(buf);
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
/* Done scanning if we found a tuple here */
|
|
|
|
if (hastup)
|
|
|
|
return blkno + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we fall out of the loop, all the previously-thought-to-be-empty
|
2007-09-16 04:37:46 +02:00
|
|
|
* pages still are; we need not bother to look at the last known-nonempty
|
2005-10-15 04:49:52 +02:00
|
|
|
* page.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
return vacrelstats->nonempty_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_space_alloc - space allocation decisions for lazy vacuum
|
|
|
|
*
|
|
|
|
* See the comments at the head of this file for rationale.
|
|
|
|
*/
|
|
|
|
static void
|
2006-09-13 19:47:08 +02:00
|
|
|
lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
long maxtuples;
|
2001-07-14 00:55:59 +02:00
|
|
|
int maxpages;
|
|
|
|
|
2006-09-13 19:47:08 +02:00
|
|
|
if (vacrelstats->hasindex)
|
|
|
|
{
|
2006-09-04 23:40:23 +02:00
|
|
|
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
|
|
|
|
maxtuples = Min(maxtuples, INT_MAX);
|
|
|
|
maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData));
|
2007-09-24 05:52:55 +02:00
|
|
|
|
|
|
|
/* curious coding here to ensure the multiplication can't overflow */
|
|
|
|
if ((BlockNumber) (maxtuples / LAZY_ALLOC_TUPLES) > relblocks)
|
|
|
|
maxtuples = relblocks * LAZY_ALLOC_TUPLES;
|
|
|
|
|
2006-09-04 23:40:23 +02:00
|
|
|
/* stay sane if small maintenance_work_mem */
|
|
|
|
maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
|
2006-09-13 19:47:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-04 23:40:23 +02:00
|
|
|
maxtuples = MaxHeapTuplesPerPage;
|
|
|
|
}
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
vacrelstats->num_dead_tuples = 0;
|
2005-08-21 01:26:37 +02:00
|
|
|
vacrelstats->max_dead_tuples = (int) maxtuples;
|
2001-07-14 00:55:59 +02:00
|
|
|
vacrelstats->dead_tuples = (ItemPointer)
|
|
|
|
palloc(maxtuples * sizeof(ItemPointerData));
|
|
|
|
|
|
|
|
maxpages = MaxFSMPages;
|
2008-03-10 03:04:10 +01:00
|
|
|
maxpages = Min(maxpages, MaxAllocSize / sizeof(FSMPageData));
|
2001-07-14 00:55:59 +02:00
|
|
|
/* No need to allocate more pages than the relation has blocks */
|
|
|
|
if (relblocks < (BlockNumber) maxpages)
|
|
|
|
maxpages = (int) relblocks;
|
|
|
|
|
|
|
|
vacrelstats->fs_is_heap = false;
|
|
|
|
vacrelstats->num_free_pages = 0;
|
|
|
|
vacrelstats->max_free_pages = maxpages;
|
2008-03-10 03:04:10 +01:00
|
|
|
vacrelstats->free_pages = (FSMPageData *)
|
|
|
|
palloc(maxpages * sizeof(FSMPageData));
|
2006-09-21 22:31:22 +02:00
|
|
|
vacrelstats->tot_free_pages = 0;
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_record_dead_tuple - remember one deletable tuple
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lazy_record_dead_tuple(LVRelStats *vacrelstats,
|
|
|
|
ItemPointer itemptr)
|
|
|
|
{
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* The array shouldn't overflow under normal behavior, but perhaps it
|
2004-02-03 18:34:04 +01:00
|
|
|
* could if we are given a really small maintenance_work_mem. In that
|
2007-09-20 19:56:33 +02:00
|
|
|
* case, just forget the last few tuples (we'll get 'em next time).
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
|
|
|
if (vacrelstats->num_dead_tuples < vacrelstats->max_dead_tuples)
|
|
|
|
{
|
|
|
|
vacrelstats->dead_tuples[vacrelstats->num_dead_tuples] = *itemptr;
|
|
|
|
vacrelstats->num_dead_tuples++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_record_free_space - remember free space on one page
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lazy_record_free_space(LVRelStats *vacrelstats,
|
|
|
|
BlockNumber page,
|
|
|
|
Size avail)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageData *pageSpaces;
|
2001-07-14 00:55:59 +02:00
|
|
|
int n;
|
|
|
|
|
2003-03-04 22:51:22 +01:00
|
|
|
/*
|
|
|
|
* A page with less than stats->threshold free space will be forgotten
|
|
|
|
* immediately, and never passed to the free space map. Removing the
|
2005-10-15 04:49:52 +02:00
|
|
|
* uselessly small entries early saves cycles, and in particular reduces
|
|
|
|
* the amount of time we spend holding the FSM lock when we finally call
|
|
|
|
* RecordRelationFreeSpace. Since the FSM will probably drop pages with
|
|
|
|
* little free space anyway, there's no point in making this really small.
|
2003-03-04 22:51:22 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* XXX Is it worth trying to measure average tuple size, and using that to
|
|
|
|
* adjust the threshold? Would be worthwhile if FSM has no stats yet for
|
|
|
|
* this relation. But changing the threshold as we scan the rel might
|
|
|
|
* lead to bizarre behavior, too. Also, it's probably better if vacuum.c
|
|
|
|
* has the same thresholding behavior as we do here.
|
2003-03-04 22:51:22 +01:00
|
|
|
*/
|
|
|
|
if (avail < vacrelstats->threshold)
|
2001-07-14 00:55:59 +02:00
|
|
|
return;
|
|
|
|
|
2006-09-21 22:31:22 +02:00
|
|
|
/* Count all pages over threshold, even if not enough space in array */
|
|
|
|
vacrelstats->tot_free_pages++;
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/* Copy pointers to local variables for notational simplicity */
|
2002-09-20 21:56:01 +02:00
|
|
|
pageSpaces = vacrelstats->free_pages;
|
2001-07-14 00:55:59 +02:00
|
|
|
n = vacrelstats->max_free_pages;
|
|
|
|
|
|
|
|
/* If we haven't filled the array yet, just keep adding entries */
|
|
|
|
if (vacrelstats->num_free_pages < n)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageSetPageNum(&pageSpaces[vacrelstats->num_free_pages], page);
|
|
|
|
FSMPageSetSpace(&pageSpaces[vacrelstats->num_free_pages], avail);
|
2001-07-14 00:55:59 +02:00
|
|
|
vacrelstats->num_free_pages++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------
|
|
|
|
* The rest of this routine works with "heap" organization of the
|
|
|
|
* free space arrays, wherein we maintain the heap property
|
2003-08-04 02:43:34 +02:00
|
|
|
* avail[(j-1) div 2] <= avail[j] for 0 < j < n.
|
2001-07-14 00:55:59 +02:00
|
|
|
* In particular, the zero'th element always has the smallest available
|
|
|
|
* space and can be discarded to make room for a new page with more space.
|
|
|
|
* See Knuth's discussion of heap-based priority queues, sec 5.2.3;
|
|
|
|
* but note he uses 1-origin array subscripts, not 0-origin.
|
|
|
|
*----------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* If we haven't yet converted the array to heap organization, do it */
|
2001-10-25 07:50:21 +02:00
|
|
|
if (!vacrelstats->fs_is_heap)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Scan backwards through the array, "sift-up" each value into its
|
2005-10-15 04:49:52 +02:00
|
|
|
* correct position. We can start the scan at n/2-1 since each entry
|
|
|
|
* above that position has no children to worry about.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
int l = n / 2;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
while (--l >= 0)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
BlockNumber R = FSMPageGetPageNum(&pageSpaces[l]);
|
|
|
|
Size K = FSMPageGetSpace(&pageSpaces[l]);
|
2001-07-14 00:55:59 +02:00
|
|
|
int i; /* i is where the "hole" is */
|
|
|
|
|
|
|
|
i = l;
|
|
|
|
for (;;)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int j = 2 * i + 1;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
if (j >= n)
|
|
|
|
break;
|
2008-03-10 03:04:10 +01:00
|
|
|
if (j + 1 < n && FSMPageGetSpace(&pageSpaces[j]) > FSMPageGetSpace(&pageSpaces[j + 1]))
|
2001-07-14 00:55:59 +02:00
|
|
|
j++;
|
2008-03-10 03:04:10 +01:00
|
|
|
if (K <= FSMPageGetSpace(&pageSpaces[j]))
|
2001-07-14 00:55:59 +02:00
|
|
|
break;
|
2002-09-20 21:56:01 +02:00
|
|
|
pageSpaces[i] = pageSpaces[j];
|
2001-07-14 00:55:59 +02:00
|
|
|
i = j;
|
|
|
|
}
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageSetPageNum(&pageSpaces[i], R);
|
|
|
|
FSMPageSetSpace(&pageSpaces[i], K);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vacrelstats->fs_is_heap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If new page has more than zero'th entry, insert it into heap */
|
2008-03-10 03:04:10 +01:00
|
|
|
if (avail > FSMPageGetSpace(&pageSpaces[0]))
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Notionally, we replace the zero'th entry with the new data, and
|
2005-10-15 04:49:52 +02:00
|
|
|
* then sift-up to maintain the heap property. Physically, the new
|
|
|
|
* data doesn't get stored into the arrays until we find the right
|
|
|
|
* location for it.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
int i = 0; /* i is where the "hole" is */
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int j = 2 * i + 1;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
if (j >= n)
|
|
|
|
break;
|
2008-03-10 03:04:10 +01:00
|
|
|
if (j + 1 < n && FSMPageGetSpace(&pageSpaces[j]) > FSMPageGetSpace(&pageSpaces[j + 1]))
|
2001-07-14 00:55:59 +02:00
|
|
|
j++;
|
2008-03-10 03:04:10 +01:00
|
|
|
if (avail <= FSMPageGetSpace(&pageSpaces[j]))
|
2001-07-14 00:55:59 +02:00
|
|
|
break;
|
2002-09-20 21:56:01 +02:00
|
|
|
pageSpaces[i] = pageSpaces[j];
|
2001-07-14 00:55:59 +02:00
|
|
|
i = j;
|
|
|
|
}
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageSetPageNum(&pageSpaces[i], page);
|
|
|
|
FSMPageSetSpace(&pageSpaces[i], avail);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy_tid_reaped() -- is a particular tid deletable?
|
|
|
|
*
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
* This has the right signature to be an IndexBulkDeleteCallback.
|
|
|
|
*
|
2001-07-14 00:55:59 +02:00
|
|
|
* Assumes dead_tuples array is in sorted order.
|
|
|
|
*/
|
|
|
|
static bool
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
lazy_tid_reaped(ItemPointer itemptr, void *state)
|
2001-07-14 00:55:59 +02:00
|
|
|
{
|
Restructure index AM interface for index building and index tuple deletion,
per previous discussion on pghackers. Most of the duplicate code in
different AMs' ambuild routines has been moved out to a common routine
in index.c; this means that all index types now do the right things about
inserting recently-dead tuples, etc. (I also removed support for EXTEND
INDEX in the ambuild routines, since that's about to go away anyway, and
it cluttered the code a lot.) The retail indextuple deletion routines have
been replaced by a "bulk delete" routine in which the indexscan is inside
the access method. I haven't pushed this change as far as it should go yet,
but it should allow considerable simplification of the internal bookkeeping
for deletions. Also, add flag columns to pg_am to eliminate various
hardcoded tests on AM OIDs, and remove unused pg_am columns.
Fix rtree and gist index types to not attempt to store NULLs; before this,
gist usually crashed, while rtree managed not to crash but computed wacko
bounding boxes for NULL entries (which might have had something to do with
the performance problems we've heard about occasionally).
Add AtEOXact routines to hash, rtree, and gist, all of which have static
state that needs to be reset after an error. We discovered this need long
ago for btree, but missed the other guys.
Oh, one more thing: concurrent VACUUM is now the default.
2001-07-16 00:48:19 +02:00
|
|
|
LVRelStats *vacrelstats = (LVRelStats *) state;
|
2001-10-25 07:50:21 +02:00
|
|
|
ItemPointer res;
|
2001-07-14 00:55:59 +02:00
|
|
|
|
|
|
|
res = (ItemPointer) bsearch((void *) itemptr,
|
|
|
|
(void *) vacrelstats->dead_tuples,
|
|
|
|
vacrelstats->num_dead_tuples,
|
|
|
|
sizeof(ItemPointerData),
|
|
|
|
vac_cmp_itemptr);
|
|
|
|
|
|
|
|
return (res != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the shared Free Space Map with the info we now have about
|
|
|
|
* free space in the relation, discarding any old info the map may have.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lazy_update_fsm(Relation onerel, LVRelStats *vacrelstats)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageData *pageSpaces = vacrelstats->free_pages;
|
2002-09-20 21:56:01 +02:00
|
|
|
int nPages = vacrelstats->num_free_pages;
|
|
|
|
|
2001-07-14 00:55:59 +02:00
|
|
|
/*
|
2003-03-04 22:51:22 +01:00
|
|
|
* Sort data into order, as required by RecordRelationFreeSpace.
|
2001-07-14 00:55:59 +02:00
|
|
|
*/
|
2002-09-20 21:56:01 +02:00
|
|
|
if (nPages > 1)
|
2008-03-10 03:04:10 +01:00
|
|
|
qsort(pageSpaces, nPages, sizeof(FSMPageData),
|
2002-09-20 21:56:01 +02:00
|
|
|
vac_cmp_page_spaces);
|
|
|
|
|
2006-09-21 22:31:22 +02:00
|
|
|
RecordRelationFreeSpace(&onerel->rd_node, vacrelstats->tot_free_pages,
|
|
|
|
nPages, pageSpaces);
|
2001-07-14 00:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Comparator routines for use with qsort() and bsearch().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
vac_cmp_itemptr(const void *left, const void *right)
|
|
|
|
{
|
|
|
|
BlockNumber lblk,
|
|
|
|
rblk;
|
|
|
|
OffsetNumber loff,
|
|
|
|
roff;
|
|
|
|
|
|
|
|
lblk = ItemPointerGetBlockNumber((ItemPointer) left);
|
|
|
|
rblk = ItemPointerGetBlockNumber((ItemPointer) right);
|
|
|
|
|
|
|
|
if (lblk < rblk)
|
|
|
|
return -1;
|
|
|
|
if (lblk > rblk)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
loff = ItemPointerGetOffsetNumber((ItemPointer) left);
|
|
|
|
roff = ItemPointerGetOffsetNumber((ItemPointer) right);
|
|
|
|
|
|
|
|
if (loff < roff)
|
|
|
|
return -1;
|
|
|
|
if (loff > roff)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2002-09-20 21:56:01 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
vac_cmp_page_spaces(const void *left, const void *right)
|
|
|
|
{
|
2008-03-10 03:04:10 +01:00
|
|
|
FSMPageData *linfo = (FSMPageData *) left;
|
|
|
|
FSMPageData *rinfo = (FSMPageData *) right;
|
|
|
|
BlockNumber lblkno = FSMPageGetPageNum(linfo);
|
|
|
|
BlockNumber rblkno = FSMPageGetPageNum(rinfo);
|
2002-09-20 21:56:01 +02:00
|
|
|
|
2008-03-10 03:04:10 +01:00
|
|
|
if (lblkno < rblkno)
|
2002-09-20 21:56:01 +02:00
|
|
|
return -1;
|
2008-03-10 03:04:10 +01:00
|
|
|
else if (lblkno > rblkno)
|
2002-09-20 21:56:01 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|