PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
vacuumlazy.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/genam.h"
#include "access/heapam.h"
#include "access/heapam_xlog.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/transam.h"
#include "access/visibilitymap.h"
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "catalog/storage.h"
#include "commands/dbcommands.h"
#include "commands/progress.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "portability/instr_time.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "storage/lmgr.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/timestamp.h"
#include "utils/tqual.h"
Include dependency graph for vacuumlazy.c:

Go to the source code of this file.

Data Structures

struct  LVRelStats
 

Macros

#define REL_TRUNCATE_MINIMUM   1000
 
#define REL_TRUNCATE_FRACTION   16
 
#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL   20 /* ms */
 
#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL   50 /* ms */
 
#define VACUUM_TRUNCATE_LOCK_TIMEOUT   5000 /* ms */
 
#define LAZY_ALLOC_TUPLES   MaxHeapTuplesPerPage
 
#define SKIP_PAGES_THRESHOLD   ((BlockNumber) 32)
 
#define PREFETCH_SIZE   ((BlockNumber) 32)
 
#define FORCE_CHECK_PAGE()   (blkno == nblocks - 1 && should_attempt_truncation(vacrelstats))
 

Typedefs

typedef struct LVRelStats LVRelStats
 

Functions

static void lazy_scan_heap (Relation onerel, int options, LVRelStats *vacrelstats, Relation *Irel, int nindexes, bool aggressive)
 
static void lazy_vacuum_heap (Relation onerel, LVRelStats *vacrelstats)
 
static bool lazy_check_needs_freeze (Buffer buf, bool *hastup)
 
static void lazy_vacuum_index (Relation indrel, IndexBulkDeleteResult **stats, LVRelStats *vacrelstats)
 
static void lazy_cleanup_index (Relation indrel, IndexBulkDeleteResult *stats, LVRelStats *vacrelstats)
 
static int lazy_vacuum_page (Relation onerel, BlockNumber blkno, Buffer buffer, int tupindex, LVRelStats *vacrelstats, Buffer *vmbuffer)
 
static bool should_attempt_truncation (LVRelStats *vacrelstats)
 
static void lazy_truncate_heap (Relation onerel, LVRelStats *vacrelstats)
 
static BlockNumber count_nondeletable_pages (Relation onerel, LVRelStats *vacrelstats)
 
static void lazy_space_alloc (LVRelStats *vacrelstats, BlockNumber relblocks)
 
static void lazy_record_dead_tuple (LVRelStats *vacrelstats, ItemPointer itemptr)
 
static bool lazy_tid_reaped (ItemPointer itemptr, void *state)
 
static int vac_cmp_itemptr (const void *left, const void *right)
 
static bool heap_page_is_all_visible (Relation rel, Buffer buf, TransactionId *visibility_cutoff_xid, bool *all_frozen)
 
void lazy_vacuum_rel (Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy)
 
static void vacuum_log_cleanup_info (Relation rel, LVRelStats *vacrelstats)
 

Variables

static int elevel = -1
 
static TransactionId OldestXmin
 
static TransactionId FreezeLimit
 
static MultiXactId MultiXactCutoff
 
static BufferAccessStrategy vac_strategy
 

Macro Definition Documentation

#define FORCE_CHECK_PAGE ( )    (blkno == nblocks - 1 && should_attempt_truncation(vacrelstats))

Referenced by lazy_scan_heap().

#define LAZY_ALLOC_TUPLES   MaxHeapTuplesPerPage

Definition at line 93 of file vacuumlazy.c.

Referenced by lazy_space_alloc().

#define PREFETCH_SIZE   ((BlockNumber) 32)

Definition at line 105 of file vacuumlazy.c.

Referenced by count_nondeletable_pages().

#define REL_TRUNCATE_FRACTION   16

Definition at line 75 of file vacuumlazy.c.

Referenced by should_attempt_truncation().

#define REL_TRUNCATE_MINIMUM   1000

Definition at line 74 of file vacuumlazy.c.

Referenced by should_attempt_truncation().

#define SKIP_PAGES_THRESHOLD   ((BlockNumber) 32)

Definition at line 99 of file vacuumlazy.c.

Referenced by lazy_scan_heap().

#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL   20 /* ms */

Definition at line 84 of file vacuumlazy.c.

Referenced by count_nondeletable_pages().

#define VACUUM_TRUNCATE_LOCK_TIMEOUT   5000 /* ms */

Definition at line 86 of file vacuumlazy.c.

Referenced by lazy_truncate_heap().

#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL   50 /* ms */

Definition at line 85 of file vacuumlazy.c.

Referenced by lazy_truncate_heap().

Typedef Documentation

Function Documentation

static BlockNumber count_nondeletable_pages ( Relation  onerel,
LVRelStats vacrelstats 
)
static

Definition at line 1832 of file vacuumlazy.c.

References AccessExclusiveLock, buf, BUFFER_LOCK_SHARE, BufferGetPage, CHECK_FOR_INTERRUPTS, elevel, ereport, errmsg(), FirstOffsetNumber, INSTR_TIME_GET_MICROSEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, InvalidBlockNumber, ItemIdIsUsed, LVRelStats::lock_waiter_detected, LockBuffer(), LockHasWaitersRelation(), MAIN_FORKNUM, LVRelStats::nonempty_pages, OffsetNumberNext, PageGetItemId, PageGetMaxOffsetNumber, PageIsEmpty, PageIsNew, PREFETCH_SIZE, PrefetchBuffer(), RBM_NORMAL, ReadBufferExtended(), LVRelStats::rel_pages, RelationGetRelationName, StaticAssertStmt, UnlockReleaseBuffer(), and VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL.

Referenced by lazy_truncate_heap().

1833 {
1834  BlockNumber blkno;
1835  BlockNumber prefetchedUntil;
1836  instr_time starttime;
1837 
1838  /* Initialize the starttime if we check for conflicting lock requests */
1839  INSTR_TIME_SET_CURRENT(starttime);
1840 
1841  /*
1842  * Start checking blocks at what we believe relation end to be and move
1843  * backwards. (Strange coding of loop control is needed because blkno is
1844  * unsigned.) To make the scan faster, we prefetch a few blocks at a time
1845  * in forward direction, so that OS-level readahead can kick in.
1846  */
1847  blkno = vacrelstats->rel_pages;
1849  "prefetch size must be power of 2");
1850  prefetchedUntil = InvalidBlockNumber;
1851  while (blkno > vacrelstats->nonempty_pages)
1852  {
1853  Buffer buf;
1854  Page page;
1855  OffsetNumber offnum,
1856  maxoff;
1857  bool hastup;
1858 
1859  /*
1860  * Check if another process requests a lock on our relation. We are
1861  * holding an AccessExclusiveLock here, so they will be waiting. We
1862  * only do this once per VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL, and we
1863  * only check if that interval has elapsed once every 32 blocks to
1864  * keep the number of system calls and actual shared lock table
1865  * lookups to a minimum.
1866  */
1867  if ((blkno % 32) == 0)
1868  {
1869  instr_time currenttime;
1870  instr_time elapsed;
1871 
1872  INSTR_TIME_SET_CURRENT(currenttime);
1873  elapsed = currenttime;
1874  INSTR_TIME_SUBTRACT(elapsed, starttime);
1875  if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000)
1877  {
1879  {
1880  ereport(elevel,
1881  (errmsg("\"%s\": suspending truncate due to conflicting lock request",
1882  RelationGetRelationName(onerel))));
1883 
1884  vacrelstats->lock_waiter_detected = true;
1885  return blkno;
1886  }
1887  starttime = currenttime;
1888  }
1889  }
1890 
1891  /*
1892  * We don't insert a vacuum delay point here, because we have an
1893  * exclusive lock on the table which we want to hold for as short a
1894  * time as possible. We still need to check for interrupts however.
1895  */
1897 
1898  blkno--;
1899 
1900  /* If we haven't prefetched this lot yet, do so now. */
1901  if (prefetchedUntil > blkno)
1902  {
1903  BlockNumber prefetchStart;
1904  BlockNumber pblkno;
1905 
1906  prefetchStart = blkno & ~(PREFETCH_SIZE - 1);
1907  for (pblkno = prefetchStart; pblkno <= blkno; pblkno++)
1908  {
1909  PrefetchBuffer(onerel, MAIN_FORKNUM, pblkno);
1911  }
1912  prefetchedUntil = prefetchStart;
1913  }
1914 
1915  buf = ReadBufferExtended(onerel, MAIN_FORKNUM, blkno,
1917 
1918  /* In this phase we only need shared access to the buffer */
1920 
1921  page = BufferGetPage(buf);
1922 
1923  if (PageIsNew(page) || PageIsEmpty(page))
1924  {
1925  /* PageIsNew probably shouldn't happen... */
1926  UnlockReleaseBuffer(buf);
1927  continue;
1928  }
1929 
1930  hastup = false;
1931  maxoff = PageGetMaxOffsetNumber(page);
1932  for (offnum = FirstOffsetNumber;
1933  offnum <= maxoff;
1934  offnum = OffsetNumberNext(offnum))
1935  {
1936  ItemId itemid;
1937 
1938  itemid = PageGetItemId(page, offnum);
1939 
1940  /*
1941  * Note: any non-unused item should be taken as a reason to keep
1942  * this page. We formerly thought that DEAD tuples could be
1943  * thrown away, but that's not so, because we'd not have cleaned
1944  * out their index entries.
1945  */
1946  if (ItemIdIsUsed(itemid))
1947  {
1948  hastup = true;
1949  break; /* can stop scanning */
1950  }
1951  } /* scan along page */
1952 
1953  UnlockReleaseBuffer(buf);
1954 
1955  /* Done scanning if we found a tuple here */
1956  if (hastup)
1957  return blkno + 1;
1958  }
1959 
1960  /*
1961  * If we fall out of the loop, all the previously-thought-to-be-empty
1962  * pages still are; we need not bother to look at the last known-nonempty
1963  * page.
1964  */
1965  return vacrelstats->nonempty_pages;
1966 }
#define PageIsEmpty(page)
Definition: bufpage.h:219
BlockNumber rel_pages
Definition: vacuumlazy.c:113
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
struct timeval instr_time
Definition: instr_time.h:147
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:753
#define PREFETCH_SIZE
Definition: vacuumlazy.c:105
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
static char * buf
Definition: pg_test_fsync.c:65
#define FirstOffsetNumber
Definition: off.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
static int elevel
Definition: vacuumlazy.c:136
#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL
Definition: vacuumlazy.c:84
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
void PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:529
bool LockHasWaitersRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:275
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:202
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define InvalidBlockNumber
Definition: block.h:33
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
#define AccessExclusiveLock
Definition: lockdefs.h:46
BlockNumber nonempty_pages
Definition: vacuumlazy.c:123
#define PageIsNew(page)
Definition: bufpage.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
bool lock_waiter_detected
Definition: vacuumlazy.c:131
static bool heap_page_is_all_visible ( Relation  rel,
Buffer  buf,
TransactionId visibility_cutoff_xid,
bool all_frozen 
)
static

Definition at line 2085 of file vacuumlazy.c.

References Assert, BufferGetBlockNumber(), BufferGetPage, elog, ERROR, FirstOffsetNumber, heap_tuple_needs_eventual_freeze(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetXmin, HeapTupleHeaderXminCommitted, HeapTupleSatisfiesVacuum(), InvalidTransactionId, ItemIdGetLength, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerSet, OffsetNumberNext, OldestXmin, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, RelationGetRelid, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdFollows(), and TransactionIdPrecedes().

Referenced by lazy_vacuum_page().

2088 {
2089  Page page = BufferGetPage(buf);
2091  OffsetNumber offnum,
2092  maxoff;
2093  bool all_visible = true;
2094 
2095  *visibility_cutoff_xid = InvalidTransactionId;
2096  *all_frozen = true;
2097 
2098  /*
2099  * This is a stripped down version of the line pointer scan in
2100  * lazy_scan_heap(). So if you change anything here, also check that code.
2101  */
2102  maxoff = PageGetMaxOffsetNumber(page);
2103  for (offnum = FirstOffsetNumber;
2104  offnum <= maxoff && all_visible;
2105  offnum = OffsetNumberNext(offnum))
2106  {
2107  ItemId itemid;
2108  HeapTupleData tuple;
2109 
2110  itemid = PageGetItemId(page, offnum);
2111 
2112  /* Unused or redirect line pointers are of no interest */
2113  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid))
2114  continue;
2115 
2116  ItemPointerSet(&(tuple.t_self), blockno, offnum);
2117 
2118  /*
2119  * Dead line pointers can have index pointers pointing to them. So
2120  * they can't be treated as visible
2121  */
2122  if (ItemIdIsDead(itemid))
2123  {
2124  all_visible = false;
2125  *all_frozen = false;
2126  break;
2127  }
2128 
2129  Assert(ItemIdIsNormal(itemid));
2130 
2131  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
2132  tuple.t_len = ItemIdGetLength(itemid);
2133  tuple.t_tableOid = RelationGetRelid(rel);
2134 
2135  switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
2136  {
2137  case HEAPTUPLE_LIVE:
2138  {
2139  TransactionId xmin;
2140 
2141  /* Check comments in lazy_scan_heap. */
2143  {
2144  all_visible = false;
2145  *all_frozen = false;
2146  break;
2147  }
2148 
2149  /*
2150  * The inserter definitely committed. But is it old enough
2151  * that everyone sees it as committed?
2152  */
2153  xmin = HeapTupleHeaderGetXmin(tuple.t_data);
2154  if (!TransactionIdPrecedes(xmin, OldestXmin))
2155  {
2156  all_visible = false;
2157  *all_frozen = false;
2158  break;
2159  }
2160 
2161  /* Track newest xmin on page. */
2162  if (TransactionIdFollows(xmin, *visibility_cutoff_xid))
2163  *visibility_cutoff_xid = xmin;
2164 
2165  /* Check whether this tuple is already frozen or not */
2166  if (all_visible && *all_frozen &&
2168  *all_frozen = false;
2169  }
2170  break;
2171 
2172  case HEAPTUPLE_DEAD:
2176  {
2177  all_visible = false;
2178  *all_frozen = false;
2179  break;
2180  }
2181  default:
2182  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
2183  break;
2184  }
2185  } /* scan along page */
2186 
2187  return all_visible;
2188 }
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:105
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
uint32 TransactionId
Definition: c.h:394
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
Definition: tqual.c:1164
uint32 BlockNumber
Definition: block.h:31
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:67
bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple)
Definition: heapam.c:7186
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define HeapTupleHeaderXminCommitted(tup)
Definition: htup_details.h:318
uint32 t_len
Definition: htup.h:64
static char * buf
Definition: pg_test_fsync.c:65
#define FirstOffsetNumber
Definition: off.h:27
#define InvalidTransactionId
Definition: transam.h:31
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define Assert(condition)
Definition: c.h:671
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:307
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
#define elog
Definition: elog.h:219
#define RelationGetRelid(relation)
Definition: rel.h:413
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:86
static bool lazy_check_needs_freeze ( Buffer  buf,
bool hastup 
)
static

Definition at line 1533 of file vacuumlazy.c.

References BufferGetPage, FirstOffsetNumber, FreezeLimit, heap_tuple_needs_freeze(), ItemIdIsNormal, ItemIdIsUsed, MultiXactCutoff, OffsetNumberNext, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageIsEmpty, and PageIsNew.

Referenced by lazy_scan_heap().

1534 {
1535  Page page = BufferGetPage(buf);
1536  OffsetNumber offnum,
1537  maxoff;
1538  HeapTupleHeader tupleheader;
1539 
1540  *hastup = false;
1541 
1542  /* If we hit an uninitialized page, we want to force vacuuming it. */
1543  if (PageIsNew(page))
1544  return true;
1545 
1546  /* Quick out for ordinary empty page. */
1547  if (PageIsEmpty(page))
1548  return false;
1549 
1550  maxoff = PageGetMaxOffsetNumber(page);
1551  for (offnum = FirstOffsetNumber;
1552  offnum <= maxoff;
1553  offnum = OffsetNumberNext(offnum))
1554  {
1555  ItemId itemid;
1556 
1557  itemid = PageGetItemId(page, offnum);
1558 
1559  /* this should match hastup test in count_nondeletable_pages() */
1560  if (ItemIdIsUsed(itemid))
1561  *hastup = true;
1562 
1563  /* dead and redirect items never need freezing */
1564  if (!ItemIdIsNormal(itemid))
1565  continue;
1566 
1567  tupleheader = (HeapTupleHeader) PageGetItem(page, itemid);
1568 
1569  if (heap_tuple_needs_freeze(tupleheader, FreezeLimit,
1570  MultiXactCutoff, buf))
1571  return true;
1572  } /* scan along page */
1573 
1574  return false;
1575 }
#define PageIsEmpty(page)
Definition: bufpage.h:219
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
static TransactionId FreezeLimit
Definition: vacuumlazy.c:139
static char * buf
Definition: pg_test_fsync.c:65
#define FirstOffsetNumber
Definition: off.h:27
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:140
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
bool heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid, MultiXactId cutoff_multi, Buffer buf)
Definition: heapam.c:7239
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define PageIsNew(page)
Definition: bufpage.h:226
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
static void lazy_cleanup_index ( Relation  indrel,
IndexBulkDeleteResult stats,
LVRelStats vacrelstats 
)
static

Definition at line 1616 of file vacuumlazy.c.

References IndexVacuumInfo::analyze_only, elevel, ereport, errdetail(), errmsg(), IndexVacuumInfo::estimated_count, IndexBulkDeleteResult::estimated_count, IndexVacuumInfo::index, index_vacuum_cleanup(), InvalidMultiXactId, InvalidTransactionId, IndexVacuumInfo::message_level, LVRelStats::new_rel_tuples, IndexVacuumInfo::num_heap_tuples, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, IndexBulkDeleteResult::pages_deleted, IndexBulkDeleteResult::pages_free, pfree(), pg_rusage_init(), pg_rusage_show(), LVRelStats::rel_pages, RelationGetRelationName, LVRelStats::scanned_pages, IndexVacuumInfo::strategy, IndexBulkDeleteResult::tuples_removed, vac_strategy, and vac_update_relstats().

Referenced by lazy_scan_heap().

1619 {
1620  IndexVacuumInfo ivinfo;
1621  PGRUsage ru0;
1622 
1623  pg_rusage_init(&ru0);
1624 
1625  ivinfo.index = indrel;
1626  ivinfo.analyze_only = false;
1627  ivinfo.estimated_count = (vacrelstats->scanned_pages < vacrelstats->rel_pages);
1628  ivinfo.message_level = elevel;
1629  ivinfo.num_heap_tuples = vacrelstats->new_rel_tuples;
1630  ivinfo.strategy = vac_strategy;
1631 
1632  stats = index_vacuum_cleanup(&ivinfo, stats);
1633 
1634  if (!stats)
1635  return;
1636 
1637  /*
1638  * Now update statistics in pg_class, but only if the index says the count
1639  * is accurate.
1640  */
1641  if (!stats->estimated_count)
1642  vac_update_relstats(indrel,
1643  stats->num_pages,
1644  stats->num_index_tuples,
1645  0,
1646  false,
1649  false);
1650 
1651  ereport(elevel,
1652  (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
1653  RelationGetRelationName(indrel),
1654  stats->num_index_tuples,
1655  stats->num_pages),
1656  errdetail("%.0f index row versions were removed.\n"
1657  "%u index pages have been deleted, %u are currently reusable.\n"
1658  "%s.",
1659  stats->tuples_removed,
1660  stats->pages_deleted, stats->pages_free,
1661  pg_rusage_show(&ru0))));
1662 
1663  pfree(stats);
1664 }
double new_rel_tuples
Definition: vacuumlazy.c:119
double tuples_removed
Definition: genam.h:77
BlockNumber rel_pages
Definition: vacuumlazy.c:113
bool analyze_only
Definition: genam.h:47
BufferAccessStrategy strategy
Definition: genam.h:51
Relation index
Definition: genam.h:46
BlockNumber scanned_pages
Definition: vacuumlazy.c:114
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
void pfree(void *pointer)
Definition: mcxt.c:992
BlockNumber num_pages
Definition: genam.h:73
BlockNumber pages_free
Definition: genam.h:79
int errdetail(const char *fmt,...)
Definition: elog.c:873
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define InvalidTransactionId
Definition: transam.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:433
BlockNumber pages_deleted
Definition: genam.h:78
#define ereport(elevel, rest)
Definition: elog.h:122
static int elevel
Definition: vacuumlazy.c:136
#define InvalidMultiXactId
Definition: multixact.h:23
int message_level
Definition: genam.h:49
double num_heap_tuples
Definition: genam.h:50
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: indexam.c:764
int errmsg(const char *fmt,...)
Definition: elog.c:797
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool in_outer_xact)
Definition: vacuum.c:755
double num_index_tuples
Definition: genam.h:76
bool estimated_count
Definition: genam.h:75
bool estimated_count
Definition: genam.h:48
static void lazy_record_dead_tuple ( LVRelStats vacrelstats,
ItemPointer  itemptr 
)
static

Definition at line 2009 of file vacuumlazy.c.

References LVRelStats::dead_tuples, LVRelStats::max_dead_tuples, LVRelStats::num_dead_tuples, pgstat_progress_update_param(), and PROGRESS_VACUUM_NUM_DEAD_TUPLES.

Referenced by lazy_scan_heap().

2011 {
2012  /*
2013  * The array shouldn't overflow under normal behavior, but perhaps it
2014  * could if we are given a really small maintenance_work_mem. In that
2015  * case, just forget the last few tuples (we'll get 'em next time).
2016  */
2017  if (vacrelstats->num_dead_tuples < vacrelstats->max_dead_tuples)
2018  {
2019  vacrelstats->dead_tuples[vacrelstats->num_dead_tuples] = *itemptr;
2020  vacrelstats->num_dead_tuples++;
2022  vacrelstats->num_dead_tuples);
2023  }
2024 }
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:2900
int max_dead_tuples
Definition: vacuumlazy.c:127
ItemPointer dead_tuples
Definition: vacuumlazy.c:128
int num_dead_tuples
Definition: vacuumlazy.c:126
#define PROGRESS_VACUUM_NUM_DEAD_TUPLES
Definition: progress.h:27
static void lazy_scan_heap ( Relation  onerel,
int  options,
LVRelStats vacrelstats,
Relation Irel,
int  nindexes,
bool  aggressive 
)
static

Definition at line 453 of file vacuumlazy.c.

References _, appendStringInfo(), Assert, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferGetPageSize, BufferIsValid, ConditionalLockBufferForCleanup(), StringInfoData::data, elevel, elog, END_CRIT_SECTION, ereport, errdetail_internal(), errmsg(), ERROR, ExclusiveLock, FirstOffsetNumber, FORCE_CHECK_PAGE, FreezeLimit, LVRelStats::frozenskipped_pages, get_namespace_name(), heap_execute_freeze_tuple(), heap_page_prune(), heap_prepare_freeze_tuple(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleGetOid, HeapTupleHeaderAdvanceLatestRemovedXid(), HeapTupleHeaderGetXmin, HeapTupleHeaderXminCommitted, HeapTupleIsHeapOnly, HeapTupleIsHotUpdated, HeapTupleSatisfiesVacuum(), i, initStringInfo(), InvalidBuffer, InvalidTransactionId, InvalidXLogRecPtr, ItemIdGetLength, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerSet, LVRelStats::latestRemovedXid, lazy_check_needs_freeze(), lazy_cleanup_index(), lazy_record_dead_tuple(), lazy_space_alloc(), lazy_vacuum_heap(), lazy_vacuum_index(), lazy_vacuum_page(), LockBuffer(), LockBufferForCleanup(), LockRelationForExtension(), log_heap_freeze(), log_newpage_buffer(), MAIN_FORKNUM, MarkBufferDirty(), LVRelStats::max_dead_tuples, MaxHeapTuplesPerPage, MultiXactCutoff, LVRelStats::new_dead_tuples, LVRelStats::new_rel_tuples, ngettext, LVRelStats::nonempty_pages, LVRelStats::num_dead_tuples, LVRelStats::num_index_scans, xl_heap_freeze_tuple::offset, OffsetNumberNext, OidIsValid, OldestXmin, PageClearAllVisible, PageGetHeapFreeSpace(), PageGetItem, PageGetItemId, PageGetLSN, PageGetMaxOffsetNumber, PageInit(), PageIsAllVisible, PageIsEmpty, PageIsNew, PageSetAllVisible, PageSetLSN, palloc(), palloc0(), pfree(), pg_rusage_init(), pg_rusage_show(), pgstat_progress_update_multi_param(), pgstat_progress_update_param(), LVRelStats::pinskipped_pages, PROGRESS_VACUUM_HEAP_BLKS_SCANNED, PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, PROGRESS_VACUUM_MAX_DEAD_TUPLES, PROGRESS_VACUUM_NUM_INDEX_VACUUMS, PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_PHASE_INDEX_CLEANUP, PROGRESS_VACUUM_PHASE_SCAN_HEAP, PROGRESS_VACUUM_PHASE_VACUUM_HEAP, PROGRESS_VACUUM_PHASE_VACUUM_INDEX, PROGRESS_VACUUM_TOTAL_HEAP_BLKS, RBM_NORMAL, RelationData::rd_rel, ReadBufferExtended(), RecordPageWithFreeSpace(), LVRelStats::rel_pages, RelationGetNamespace, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetRelid, RelationNeedsWAL, ReleaseBuffer(), LVRelStats::scanned_pages, LVRelStats::scanned_tuples, SKIP_PAGES_THRESHOLD, START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdFollows(), TransactionIdPrecedes(), LVRelStats::tuples_deleted, UnlockRelationForExtension(), UnlockReleaseBuffer(), vac_estimate_reltuples(), VACOPT_DISABLE_PAGE_SKIPPING, vacuum_delay_point(), vacuum_log_cleanup_info(), VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, visibilitymap_clear(), visibilitymap_get_status(), visibilitymap_pin(), visibilitymap_set(), VISIBILITYMAP_VALID_BITS, VM_ALL_FROZEN, VM_ALL_VISIBLE, and WARNING.

Referenced by lazy_vacuum_rel().

455 {
456  BlockNumber nblocks,
457  blkno;
458  HeapTupleData tuple;
459  char *relname;
460  BlockNumber empty_pages,
461  vacuumed_pages;
462  double num_tuples,
463  tups_vacuumed,
464  nkeep,
465  nunused;
466  IndexBulkDeleteResult **indstats;
467  int i;
468  PGRUsage ru0;
469  Buffer vmbuffer = InvalidBuffer;
470  BlockNumber next_unskippable_block;
471  bool skipping_blocks;
472  xl_heap_freeze_tuple *frozen;
474  const int initprog_index[] = {
478  };
479  int64 initprog_val[3];
480 
481  pg_rusage_init(&ru0);
482 
483  relname = RelationGetRelationName(onerel);
484  ereport(elevel,
485  (errmsg("vacuuming \"%s.%s\"",
487  relname)));
488 
489  empty_pages = vacuumed_pages = 0;
490  num_tuples = tups_vacuumed = nkeep = nunused = 0;
491 
492  indstats = (IndexBulkDeleteResult **)
493  palloc0(nindexes * sizeof(IndexBulkDeleteResult *));
494 
495  nblocks = RelationGetNumberOfBlocks(onerel);
496  vacrelstats->rel_pages = nblocks;
497  vacrelstats->scanned_pages = 0;
498  vacrelstats->nonempty_pages = 0;
499  vacrelstats->latestRemovedXid = InvalidTransactionId;
500 
501  lazy_space_alloc(vacrelstats, nblocks);
503 
504  /* Report that we're scanning the heap, advertising total # of blocks */
505  initprog_val[0] = PROGRESS_VACUUM_PHASE_SCAN_HEAP;
506  initprog_val[1] = nblocks;
507  initprog_val[2] = vacrelstats->max_dead_tuples;
508  pgstat_progress_update_multi_param(3, initprog_index, initprog_val);
509 
510  /*
511  * Except when aggressive is set, we want to skip pages that are
512  * all-visible according to the visibility map, but only when we can skip
513  * at least SKIP_PAGES_THRESHOLD consecutive pages. Since we're reading
514  * sequentially, the OS should be doing readahead for us, so there's no
515  * gain in skipping a page now and then; that's likely to disable
516  * readahead and so be counterproductive. Also, skipping even a single
517  * page means that we can't update relfrozenxid, so we only want to do it
518  * if we can skip a goodly number of pages.
519  *
520  * When aggressive is set, we can't skip pages just because they are
521  * all-visible, but we can still skip pages that are all-frozen, since
522  * such pages do not need freezing and do not affect the value that we can
523  * safely set for relfrozenxid or relminmxid.
524  *
525  * Before entering the main loop, establish the invariant that
526  * next_unskippable_block is the next block number >= blkno that's not we
527  * can't skip based on the visibility map, either all-visible for a
528  * regular scan or all-frozen for an aggressive scan. We set it to
529  * nblocks if there's no such block. We also set up the skipping_blocks
530  * flag correctly at this stage.
531  *
532  * Note: The value returned by visibilitymap_get_status could be slightly
533  * out-of-date, since we make this test before reading the corresponding
534  * heap page or locking the buffer. This is OK. If we mistakenly think
535  * that the page is all-visible or all-frozen when in fact the flag's just
536  * been cleared, we might fail to vacuum the page. It's easy to see that
537  * skipping a page when aggressive is not set is not a very big deal; we
538  * might leave some dead tuples lying around, but the next vacuum will
539  * find them. But even when aggressive *is* set, it's still OK if we miss
540  * a page whose all-frozen marking has just been cleared. Any new XIDs
541  * just added to that page are necessarily newer than the GlobalXmin we
542  * computed, so they'll have no effect on the value to which we can safely
543  * set relfrozenxid. A similar argument applies for MXIDs and relminmxid.
544  *
545  * We will scan the table's last page, at least to the extent of
546  * determining whether it has tuples or not, even if it should be skipped
547  * according to the above rules; except when we've already determined that
548  * it's not worth trying to truncate the table. This avoids having
549  * lazy_truncate_heap() take access-exclusive lock on the table to attempt
550  * a truncation that just fails immediately because there are tuples in
551  * the last page. This is worth avoiding mainly because such a lock must
552  * be replayed on any hot standby, where it can be disruptive.
553  */
554  next_unskippable_block = 0;
556  {
557  while (next_unskippable_block < nblocks)
558  {
559  uint8 vmstatus;
560 
561  vmstatus = visibilitymap_get_status(onerel, next_unskippable_block,
562  &vmbuffer);
563  if (aggressive)
564  {
565  if ((vmstatus & VISIBILITYMAP_ALL_FROZEN) == 0)
566  break;
567  }
568  else
569  {
570  if ((vmstatus & VISIBILITYMAP_ALL_VISIBLE) == 0)
571  break;
572  }
574  next_unskippable_block++;
575  }
576  }
577 
578  if (next_unskippable_block >= SKIP_PAGES_THRESHOLD)
579  skipping_blocks = true;
580  else
581  skipping_blocks = false;
582 
583  for (blkno = 0; blkno < nblocks; blkno++)
584  {
585  Buffer buf;
586  Page page;
587  OffsetNumber offnum,
588  maxoff;
589  bool tupgone,
590  hastup;
591  int prev_dead_count;
592  int nfrozen;
593  Size freespace;
594  bool all_visible_according_to_vm = false;
595  bool all_visible;
596  bool all_frozen = true; /* provided all_visible is also true */
597  bool has_dead_tuples;
598  TransactionId visibility_cutoff_xid = InvalidTransactionId;
599 
600  /* see note above about forcing scanning of last page */
601 #define FORCE_CHECK_PAGE() \
602  (blkno == nblocks - 1 && should_attempt_truncation(vacrelstats))
603 
605 
606  if (blkno == next_unskippable_block)
607  {
608  /* Time to advance next_unskippable_block */
609  next_unskippable_block++;
610  if ((options & VACOPT_DISABLE_PAGE_SKIPPING) == 0)
611  {
612  while (next_unskippable_block < nblocks)
613  {
614  uint8 vmskipflags;
615 
616  vmskipflags = visibilitymap_get_status(onerel,
617  next_unskippable_block,
618  &vmbuffer);
619  if (aggressive)
620  {
621  if ((vmskipflags & VISIBILITYMAP_ALL_FROZEN) == 0)
622  break;
623  }
624  else
625  {
626  if ((vmskipflags & VISIBILITYMAP_ALL_VISIBLE) == 0)
627  break;
628  }
630  next_unskippable_block++;
631  }
632  }
633 
634  /*
635  * We know we can't skip the current block. But set up
636  * skipping_all_visible_blocks to do the right thing at the
637  * following blocks.
638  */
639  if (next_unskippable_block - blkno > SKIP_PAGES_THRESHOLD)
640  skipping_blocks = true;
641  else
642  skipping_blocks = false;
643 
644  /*
645  * Normally, the fact that we can't skip this block must mean that
646  * it's not all-visible. But in an aggressive vacuum we know only
647  * that it's not all-frozen, so it might still be all-visible.
648  */
649  if (aggressive && VM_ALL_VISIBLE(onerel, blkno, &vmbuffer))
650  all_visible_according_to_vm = true;
651  }
652  else
653  {
654  /*
655  * The current block is potentially skippable; if we've seen a
656  * long enough run of skippable blocks to justify skipping it, and
657  * we're not forced to check it, then go ahead and skip.
658  * Otherwise, the page must be at least all-visible if not
659  * all-frozen, so we can set all_visible_according_to_vm = true.
660  */
661  if (skipping_blocks && !FORCE_CHECK_PAGE())
662  {
663  /*
664  * Tricky, tricky. If this is in aggressive vacuum, the page
665  * must have been all-frozen at the time we checked whether it
666  * was skippable, but it might not be any more. We must be
667  * careful to count it as a skipped all-frozen page in that
668  * case, or else we'll think we can't update relfrozenxid and
669  * relminmxid. If it's not an aggressive vacuum, we don't
670  * know whether it was all-frozen, so we have to recheck; but
671  * in this case an approximate answer is OK.
672  */
673  if (aggressive || VM_ALL_FROZEN(onerel, blkno, &vmbuffer))
674  vacrelstats->frozenskipped_pages++;
675  continue;
676  }
677  all_visible_according_to_vm = true;
678  }
679 
681 
682  /*
683  * If we are close to overrunning the available space for dead-tuple
684  * TIDs, pause and do a cycle of vacuuming before we tackle this page.
685  */
686  if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MaxHeapTuplesPerPage &&
687  vacrelstats->num_dead_tuples > 0)
688  {
689  const int hvp_index[] = {
692  };
693  int64 hvp_val[2];
694 
695  /*
696  * Before beginning index vacuuming, we release any pin we may
697  * hold on the visibility map page. This isn't necessary for
698  * correctness, but we do it anyway to avoid holding the pin
699  * across a lengthy, unrelated operation.
700  */
701  if (BufferIsValid(vmbuffer))
702  {
703  ReleaseBuffer(vmbuffer);
704  vmbuffer = InvalidBuffer;
705  }
706 
707  /* Log cleanup info before we touch indexes */
708  vacuum_log_cleanup_info(onerel, vacrelstats);
709 
710  /* Report that we are now vacuuming indexes */
713 
714  /* Remove index entries */
715  for (i = 0; i < nindexes; i++)
716  lazy_vacuum_index(Irel[i],
717  &indstats[i],
718  vacrelstats);
719 
720  /*
721  * Report that we are now vacuuming the heap. We also increase
722  * the number of index scans here; note that by using
723  * pgstat_progress_update_multi_param we can update both
724  * parameters atomically.
725  */
727  hvp_val[1] = vacrelstats->num_index_scans + 1;
728  pgstat_progress_update_multi_param(2, hvp_index, hvp_val);
729 
730  /* Remove tuples from heap */
731  lazy_vacuum_heap(onerel, vacrelstats);
732 
733  /*
734  * Forget the now-vacuumed tuples, and press on, but be careful
735  * not to reset latestRemovedXid since we want that value to be
736  * valid.
737  */
738  vacrelstats->num_dead_tuples = 0;
739  vacrelstats->num_index_scans++;
740 
741  /* Report that we are once again scanning the heap */
744  }
745 
746  /*
747  * Pin the visibility map page in case we need to mark the page
748  * all-visible. In most cases this will be very cheap, because we'll
749  * already have the correct page pinned anyway. However, it's
750  * possible that (a) next_unskippable_block is covered by a different
751  * VM page than the current block or (b) we released our pin and did a
752  * cycle of index vacuuming.
753  *
754  */
755  visibilitymap_pin(onerel, blkno, &vmbuffer);
756 
757  buf = ReadBufferExtended(onerel, MAIN_FORKNUM, blkno,
759 
760  /* We need buffer cleanup lock so that we can prune HOT chains. */
762  {
763  /*
764  * If we're not performing an aggressive scan to guard against XID
765  * wraparound, and we don't want to forcibly check the page, then
766  * it's OK to skip vacuuming pages we get a lock conflict on. They
767  * will be dealt with in some future vacuum.
768  */
769  if (!aggressive && !FORCE_CHECK_PAGE())
770  {
771  ReleaseBuffer(buf);
772  vacrelstats->pinskipped_pages++;
773  continue;
774  }
775 
776  /*
777  * Read the page with share lock to see if any xids on it need to
778  * be frozen. If not we just skip the page, after updating our
779  * scan statistics. If there are some, we wait for cleanup lock.
780  *
781  * We could defer the lock request further by remembering the page
782  * and coming back to it later, or we could even register
783  * ourselves for multiple buffers and then service whichever one
784  * is received first. For now, this seems good enough.
785  *
786  * If we get here with aggressive false, then we're just forcibly
787  * checking the page, and so we don't want to insist on getting
788  * the lock; we only need to know if the page contains tuples, so
789  * that we can update nonempty_pages correctly. It's convenient
790  * to use lazy_check_needs_freeze() for both situations, though.
791  */
793  if (!lazy_check_needs_freeze(buf, &hastup))
794  {
795  UnlockReleaseBuffer(buf);
796  vacrelstats->scanned_pages++;
797  vacrelstats->pinskipped_pages++;
798  if (hastup)
799  vacrelstats->nonempty_pages = blkno + 1;
800  continue;
801  }
802  if (!aggressive)
803  {
804  /*
805  * Here, we must not advance scanned_pages; that would amount
806  * to claiming that the page contains no freezable tuples.
807  */
808  UnlockReleaseBuffer(buf);
809  vacrelstats->pinskipped_pages++;
810  if (hastup)
811  vacrelstats->nonempty_pages = blkno + 1;
812  continue;
813  }
816  /* drop through to normal processing */
817  }
818 
819  vacrelstats->scanned_pages++;
820 
821  page = BufferGetPage(buf);
822 
823  if (PageIsNew(page))
824  {
825  /*
826  * An all-zeroes page could be left over if a backend extends the
827  * relation but crashes before initializing the page. Reclaim such
828  * pages for use.
829  *
830  * We have to be careful here because we could be looking at a
831  * page that someone has just added to the relation and not yet
832  * been able to initialize (see RelationGetBufferForTuple). To
833  * protect against that, release the buffer lock, grab the
834  * relation extension lock momentarily, and re-lock the buffer. If
835  * the page is still uninitialized by then, it must be left over
836  * from a crashed backend, and we can initialize it.
837  *
838  * We don't really need the relation lock when this is a new or
839  * temp relation, but it's probably not worth the code space to
840  * check that, since this surely isn't a critical path.
841  *
842  * Note: the comparable code in vacuum.c need not worry because
843  * it's got exclusive lock on the whole relation.
844  */
849  if (PageIsNew(page))
850  {
852  (errmsg("relation \"%s\" page %u is uninitialized --- fixing",
853  relname, blkno)));
854  PageInit(page, BufferGetPageSize(buf), 0);
855  empty_pages++;
856  }
857  freespace = PageGetHeapFreeSpace(page);
858  MarkBufferDirty(buf);
859  UnlockReleaseBuffer(buf);
860 
861  RecordPageWithFreeSpace(onerel, blkno, freespace);
862  continue;
863  }
864 
865  if (PageIsEmpty(page))
866  {
867  empty_pages++;
868  freespace = PageGetHeapFreeSpace(page);
869 
870  /* empty pages are always all-visible and all-frozen */
871  if (!PageIsAllVisible(page))
872  {
874 
875  /* mark buffer dirty before writing a WAL record */
876  MarkBufferDirty(buf);
877 
878  /*
879  * It's possible that another backend has extended the heap,
880  * initialized the page, and then failed to WAL-log the page
881  * due to an ERROR. Since heap extension is not WAL-logged,
882  * recovery might try to replay our record setting the page
883  * all-visible and find that the page isn't initialized, which
884  * will cause a PANIC. To prevent that, check whether the
885  * page has been previously WAL-logged, and if not, do that
886  * now.
887  */
888  if (RelationNeedsWAL(onerel) &&
889  PageGetLSN(page) == InvalidXLogRecPtr)
890  log_newpage_buffer(buf, true);
891 
892  PageSetAllVisible(page);
893  visibilitymap_set(onerel, blkno, buf, InvalidXLogRecPtr,
894  vmbuffer, InvalidTransactionId,
897  }
898 
899  UnlockReleaseBuffer(buf);
900  RecordPageWithFreeSpace(onerel, blkno, freespace);
901  continue;
902  }
903 
904  /*
905  * Prune all HOT-update chains in this page.
906  *
907  * We count tuples removed by the pruning step as removed by VACUUM.
908  */
909  tups_vacuumed += heap_page_prune(onerel, buf, OldestXmin, false,
910  &vacrelstats->latestRemovedXid);
911 
912  /*
913  * Now scan the page to collect vacuumable items and check for tuples
914  * requiring freezing.
915  */
916  all_visible = true;
917  has_dead_tuples = false;
918  nfrozen = 0;
919  hastup = false;
920  prev_dead_count = vacrelstats->num_dead_tuples;
921  maxoff = PageGetMaxOffsetNumber(page);
922 
923  /*
924  * Note: If you change anything in the loop below, also look at
925  * heap_page_is_all_visible to see if that needs to be changed.
926  */
927  for (offnum = FirstOffsetNumber;
928  offnum <= maxoff;
929  offnum = OffsetNumberNext(offnum))
930  {
931  ItemId itemid;
932 
933  itemid = PageGetItemId(page, offnum);
934 
935  /* Unused items require no processing, but we count 'em */
936  if (!ItemIdIsUsed(itemid))
937  {
938  nunused += 1;
939  continue;
940  }
941 
942  /* Redirect items mustn't be touched */
943  if (ItemIdIsRedirected(itemid))
944  {
945  hastup = true; /* this page won't be truncatable */
946  continue;
947  }
948 
949  ItemPointerSet(&(tuple.t_self), blkno, offnum);
950 
951  /*
952  * DEAD item pointers are to be vacuumed normally; but we don't
953  * count them in tups_vacuumed, else we'd be double-counting (at
954  * least in the common case where heap_page_prune() just freed up
955  * a non-HOT tuple).
956  */
957  if (ItemIdIsDead(itemid))
958  {
959  lazy_record_dead_tuple(vacrelstats, &(tuple.t_self));
960  all_visible = false;
961  continue;
962  }
963 
964  Assert(ItemIdIsNormal(itemid));
965 
966  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
967  tuple.t_len = ItemIdGetLength(itemid);
968  tuple.t_tableOid = RelationGetRelid(onerel);
969 
970  tupgone = false;
971 
972  switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
973  {
974  case HEAPTUPLE_DEAD:
975 
976  /*
977  * Ordinarily, DEAD tuples would have been removed by
978  * heap_page_prune(), but it's possible that the tuple
979  * state changed since heap_page_prune() looked. In
980  * particular an INSERT_IN_PROGRESS tuple could have
981  * changed to DEAD if the inserter aborted. So this
982  * cannot be considered an error condition.
983  *
984  * If the tuple is HOT-updated then it must only be
985  * removed by a prune operation; so we keep it just as if
986  * it were RECENTLY_DEAD. Also, if it's a heap-only
987  * tuple, we choose to keep it, because it'll be a lot
988  * cheaper to get rid of it in the next pruning pass than
989  * to treat it like an indexed tuple.
990  */
991  if (HeapTupleIsHotUpdated(&tuple) ||
992  HeapTupleIsHeapOnly(&tuple))
993  nkeep += 1;
994  else
995  tupgone = true; /* we can delete the tuple */
996  all_visible = false;
997  break;
998  case HEAPTUPLE_LIVE:
999  /* Tuple is good --- but let's do some validity checks */
1000  if (onerel->rd_rel->relhasoids &&
1001  !OidIsValid(HeapTupleGetOid(&tuple)))
1002  elog(WARNING, "relation \"%s\" TID %u/%u: OID is invalid",
1003  relname, blkno, offnum);
1004 
1005  /*
1006  * Is the tuple definitely visible to all transactions?
1007  *
1008  * NB: Like with per-tuple hint bits, we can't set the
1009  * PD_ALL_VISIBLE flag if the inserter committed
1010  * asynchronously. See SetHintBits for more info. Check
1011  * that the tuple is hinted xmin-committed because of
1012  * that.
1013  */
1014  if (all_visible)
1015  {
1016  TransactionId xmin;
1017 
1019  {
1020  all_visible = false;
1021  break;
1022  }
1023 
1024  /*
1025  * The inserter definitely committed. But is it old
1026  * enough that everyone sees it as committed?
1027  */
1028  xmin = HeapTupleHeaderGetXmin(tuple.t_data);
1029  if (!TransactionIdPrecedes(xmin, OldestXmin))
1030  {
1031  all_visible = false;
1032  break;
1033  }
1034 
1035  /* Track newest xmin on page. */
1036  if (TransactionIdFollows(xmin, visibility_cutoff_xid))
1037  visibility_cutoff_xid = xmin;
1038  }
1039  break;
1041 
1042  /*
1043  * If tuple is recently deleted then we must not remove it
1044  * from relation.
1045  */
1046  nkeep += 1;
1047  all_visible = false;
1048  break;
1050  /* This is an expected case during concurrent vacuum */
1051  all_visible = false;
1052  break;
1054  /* This is an expected case during concurrent vacuum */
1055  all_visible = false;
1056  break;
1057  default:
1058  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1059  break;
1060  }
1061 
1062  if (tupgone)
1063  {
1064  lazy_record_dead_tuple(vacrelstats, &(tuple.t_self));
1066  &vacrelstats->latestRemovedXid);
1067  tups_vacuumed += 1;
1068  has_dead_tuples = true;
1069  }
1070  else
1071  {
1072  bool tuple_totally_frozen;
1073 
1074  num_tuples += 1;
1075  hastup = true;
1076 
1077  /*
1078  * Each non-removable tuple must be checked to see if it needs
1079  * freezing. Note we already have exclusive buffer lock.
1080  */
1082  MultiXactCutoff, &frozen[nfrozen],
1083  &tuple_totally_frozen))
1084  frozen[nfrozen++].offset = offnum;
1085 
1086  if (!tuple_totally_frozen)
1087  all_frozen = false;
1088  }
1089  } /* scan along page */
1090 
1091  /*
1092  * If we froze any tuples, mark the buffer dirty, and write a WAL
1093  * record recording the changes. We must log the changes to be
1094  * crash-safe against future truncation of CLOG.
1095  */
1096  if (nfrozen > 0)
1097  {
1099 
1100  MarkBufferDirty(buf);
1101 
1102  /* execute collected freezes */
1103  for (i = 0; i < nfrozen; i++)
1104  {
1105  ItemId itemid;
1106  HeapTupleHeader htup;
1107 
1108  itemid = PageGetItemId(page, frozen[i].offset);
1109  htup = (HeapTupleHeader) PageGetItem(page, itemid);
1110 
1111  heap_execute_freeze_tuple(htup, &frozen[i]);
1112  }
1113 
1114  /* Now WAL-log freezing if necessary */
1115  if (RelationNeedsWAL(onerel))
1116  {
1117  XLogRecPtr recptr;
1118 
1119  recptr = log_heap_freeze(onerel, buf, FreezeLimit,
1120  frozen, nfrozen);
1121  PageSetLSN(page, recptr);
1122  }
1123 
1124  END_CRIT_SECTION();
1125  }
1126 
1127  /*
1128  * If there are no indexes then we can vacuum the page right now
1129  * instead of doing a second scan.
1130  */
1131  if (nindexes == 0 &&
1132  vacrelstats->num_dead_tuples > 0)
1133  {
1134  /* Remove tuples from heap */
1135  lazy_vacuum_page(onerel, blkno, buf, 0, vacrelstats, &vmbuffer);
1136  has_dead_tuples = false;
1137 
1138  /*
1139  * Forget the now-vacuumed tuples, and press on, but be careful
1140  * not to reset latestRemovedXid since we want that value to be
1141  * valid.
1142  */
1143  vacrelstats->num_dead_tuples = 0;
1144  vacuumed_pages++;
1145  }
1146 
1147  freespace = PageGetHeapFreeSpace(page);
1148 
1149  /* mark page all-visible, if appropriate */
1150  if (all_visible && !all_visible_according_to_vm)
1151  {
1153 
1154  if (all_frozen)
1155  flags |= VISIBILITYMAP_ALL_FROZEN;
1156 
1157  /*
1158  * It should never be the case that the visibility map page is set
1159  * while the page-level bit is clear, but the reverse is allowed
1160  * (if checksums are not enabled). Regardless, set the both bits
1161  * so that we get back in sync.
1162  *
1163  * NB: If the heap page is all-visible but the VM bit is not set,
1164  * we don't need to dirty the heap page. However, if checksums
1165  * are enabled, we do need to make sure that the heap page is
1166  * dirtied before passing it to visibilitymap_set(), because it
1167  * may be logged. Given that this situation should only happen in
1168  * rare cases after a crash, it is not worth optimizing.
1169  */
1170  PageSetAllVisible(page);
1171  MarkBufferDirty(buf);
1172  visibilitymap_set(onerel, blkno, buf, InvalidXLogRecPtr,
1173  vmbuffer, visibility_cutoff_xid, flags);
1174  }
1175 
1176  /*
1177  * As of PostgreSQL 9.2, the visibility map bit should never be set if
1178  * the page-level bit is clear. However, it's possible that the bit
1179  * got cleared after we checked it and before we took the buffer
1180  * content lock, so we must recheck before jumping to the conclusion
1181  * that something bad has happened.
1182  */
1183  else if (all_visible_according_to_vm && !PageIsAllVisible(page)
1184  && VM_ALL_VISIBLE(onerel, blkno, &vmbuffer))
1185  {
1186  elog(WARNING, "page is not marked all-visible but visibility map bit is set in relation \"%s\" page %u",
1187  relname, blkno);
1188  visibilitymap_clear(onerel, blkno, vmbuffer,
1190  }
1191 
1192  /*
1193  * It's possible for the value returned by GetOldestXmin() to move
1194  * backwards, so it's not wrong for us to see tuples that appear to
1195  * not be visible to everyone yet, while PD_ALL_VISIBLE is already
1196  * set. The real safe xmin value never moves backwards, but
1197  * GetOldestXmin() is conservative and sometimes returns a value
1198  * that's unnecessarily small, so if we see that contradiction it just
1199  * means that the tuples that we think are not visible to everyone yet
1200  * actually are, and the PD_ALL_VISIBLE flag is correct.
1201  *
1202  * There should never be dead tuples on a page with PD_ALL_VISIBLE
1203  * set, however.
1204  */
1205  else if (PageIsAllVisible(page) && has_dead_tuples)
1206  {
1207  elog(WARNING, "page containing dead tuples is marked as all-visible in relation \"%s\" page %u",
1208  relname, blkno);
1209  PageClearAllVisible(page);
1210  MarkBufferDirty(buf);
1211  visibilitymap_clear(onerel, blkno, vmbuffer,
1213  }
1214 
1215  /*
1216  * If the all-visible page is turned out to be all-frozen but not
1217  * marked, we should so mark it. Note that all_frozen is only valid
1218  * if all_visible is true, so we must check both.
1219  */
1220  else if (all_visible_according_to_vm && all_visible && all_frozen &&
1221  !VM_ALL_FROZEN(onerel, blkno, &vmbuffer))
1222  {
1223  /*
1224  * We can pass InvalidTransactionId as the cutoff XID here,
1225  * because setting the all-frozen bit doesn't cause recovery
1226  * conflicts.
1227  */
1228  visibilitymap_set(onerel, blkno, buf, InvalidXLogRecPtr,
1229  vmbuffer, InvalidTransactionId,
1231  }
1232 
1233  UnlockReleaseBuffer(buf);
1234 
1235  /* Remember the location of the last page with nonremovable tuples */
1236  if (hastup)
1237  vacrelstats->nonempty_pages = blkno + 1;
1238 
1239  /*
1240  * If we remembered any tuples for deletion, then the page will be
1241  * visited again by lazy_vacuum_heap, which will compute and record
1242  * its post-compaction free space. If not, then we're done with this
1243  * page, so remember its free space as-is. (This path will always be
1244  * taken if there are no indexes.)
1245  */
1246  if (vacrelstats->num_dead_tuples == prev_dead_count)
1247  RecordPageWithFreeSpace(onerel, blkno, freespace);
1248  }
1249 
1250  /* report that everything is scanned and vacuumed */
1252 
1253  pfree(frozen);
1254 
1255  /* save stats for use later */
1256  vacrelstats->scanned_tuples = num_tuples;
1257  vacrelstats->tuples_deleted = tups_vacuumed;
1258  vacrelstats->new_dead_tuples = nkeep;
1259 
1260  /* now we can compute the new value for pg_class.reltuples */
1261  vacrelstats->new_rel_tuples = vac_estimate_reltuples(onerel, false,
1262  nblocks,
1263  vacrelstats->scanned_pages,
1264  num_tuples);
1265 
1266  /*
1267  * Release any remaining pin on visibility map page.
1268  */
1269  if (BufferIsValid(vmbuffer))
1270  {
1271  ReleaseBuffer(vmbuffer);
1272  vmbuffer = InvalidBuffer;
1273  }
1274 
1275  /* If any tuples need to be deleted, perform final vacuum cycle */
1276  /* XXX put a threshold on min number of tuples here? */
1277  if (vacrelstats->num_dead_tuples > 0)
1278  {
1279  const int hvp_index[] = {
1282  };
1283  int64 hvp_val[2];
1284 
1285  /* Log cleanup info before we touch indexes */
1286  vacuum_log_cleanup_info(onerel, vacrelstats);
1287 
1288  /* Report that we are now vacuuming indexes */
1291 
1292  /* Remove index entries */
1293  for (i = 0; i < nindexes; i++)
1294  lazy_vacuum_index(Irel[i],
1295  &indstats[i],
1296  vacrelstats);
1297 
1298  /* Report that we are now vacuuming the heap */
1299  hvp_val[0] = PROGRESS_VACUUM_PHASE_VACUUM_HEAP;
1300  hvp_val[1] = vacrelstats->num_index_scans + 1;
1301  pgstat_progress_update_multi_param(2, hvp_index, hvp_val);
1302 
1303  /* Remove tuples from heap */
1306  lazy_vacuum_heap(onerel, vacrelstats);
1307  vacrelstats->num_index_scans++;
1308  }
1309 
1310  /* report all blocks vacuumed; and that we're cleaning up */
1314 
1315  /* Do post-vacuum cleanup and statistics update for each index */
1316  for (i = 0; i < nindexes; i++)
1317  lazy_cleanup_index(Irel[i], indstats[i], vacrelstats);
1318 
1319  /* If no indexes, make log report that lazy_vacuum_heap would've made */
1320  if (vacuumed_pages)
1321  ereport(elevel,
1322  (errmsg("\"%s\": removed %.0f row versions in %u pages",
1323  RelationGetRelationName(onerel),
1324  tups_vacuumed, vacuumed_pages)));
1325 
1326  /*
1327  * This is pretty messy, but we split it up so that we can skip emitting
1328  * individual parts of the message when not applicable.
1329  */
1330  initStringInfo(&buf);
1331  appendStringInfo(&buf,
1332  _("%.0f dead row versions cannot be removed yet.\n"),
1333  nkeep);
1334  appendStringInfo(&buf, _("There were %.0f unused item pointers.\n"),
1335  nunused);
1336  appendStringInfo(&buf, ngettext("Skipped %u page due to buffer pins.\n",
1337  "Skipped %u pages due to buffer pins.\n",
1338  vacrelstats->pinskipped_pages),
1339  vacrelstats->pinskipped_pages);
1340  appendStringInfo(&buf, ngettext("%u page is entirely empty.\n",
1341  "%u pages are entirely empty.\n",
1342  empty_pages),
1343  empty_pages);
1344  appendStringInfo(&buf, _("%s."),
1345  pg_rusage_show(&ru0));
1346 
1347  ereport(elevel,
1348  (errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u out of %u pages",
1349  RelationGetRelationName(onerel),
1350  tups_vacuumed, num_tuples,
1351  vacrelstats->scanned_pages, nblocks),
1352  errdetail_internal("%s", buf.data)));
1353  pfree(buf.data);
1354 }
double new_rel_tuples
Definition: vacuumlazy.c:119
void HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple, TransactionId *latestRemovedXid)
Definition: heapam.c:7318
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, int tupindex, LVRelStats *vacrelstats, Buffer *vmbuffer)
Definition: vacuumlazy.c:1435
int heap_page_prune(Relation relation, Buffer buffer, TransactionId OldestXmin, bool report_stats, TransactionId *latestRemovedXid)
Definition: pruneheap.c:182
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3586
XLogRecPtr log_heap_freeze(Relation reln, Buffer buffer, TransactionId cutoff_xid, xl_heap_freeze_tuple *tuples, int ntuples)
Definition: heapam.c:7437
#define PROGRESS_VACUUM_HEAP_BLKS_VACUUMED
Definition: progress.h:24
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define PageIsEmpty(page)
Definition: bufpage.h:219
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1010
BlockNumber rel_pages
Definition: vacuumlazy.c:113
OffsetNumber offset
Definition: heapam_xlog.h:299
static void lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1369
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:105
double vac_estimate_reltuples(Relation relation, bool is_analyze, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:655
static void lazy_record_dead_tuple(LVRelStats *vacrelstats, ItemPointer itemptr)
Definition: vacuumlazy.c:2009
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
#define PageIsAllVisible(page)
Definition: bufpage.h:382
uint32 TransactionId
Definition: c.h:394
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
#define PROGRESS_VACUUM_MAX_DEAD_TUPLES
Definition: progress.h:26
#define PROGRESS_VACUUM_PHASE_VACUUM_INDEX
Definition: progress.h:31
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *buf)
double tuples_deleted
Definition: vacuumlazy.c:122
void visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
#define ExclusiveLock
Definition: lockdefs.h:44
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:2900
#define VISIBILITYMAP_ALL_FROZEN
Definition: visibilitymap.h:27
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
#define VM_ALL_FROZEN(r, b, v)
Definition: visibilitymap.h:34
unsigned char uint8
Definition: c.h:263
#define PROGRESS_VACUUM_HEAP_BLKS_SCANNED
Definition: progress.h:23
#define InvalidBuffer
Definition: buf.h:25
static void lazy_cleanup_index(Relation indrel, IndexBulkDeleteResult *stats, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1616
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
Definition: tqual.c:1164
#define PROGRESS_VACUUM_TOTAL_HEAP_BLKS
Definition: progress.h:22
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
BlockNumber scanned_pages
Definition: vacuumlazy.c:114
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
BlockNumber pinskipped_pages
Definition: vacuumlazy.c:115
Form_pg_class rd_rel
Definition: rel.h:113
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define OidIsValid(objectId)
Definition: c.h:534
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
uint16 OffsetNumber
Definition: off.h:24
#define VISIBILITYMAP_VALID_BITS
Definition: visibilitymap.h:28
HeapTupleHeader t_data
Definition: htup.h:67
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
#define FORCE_CHECK_PAGE()
#define HeapTupleIsHotUpdated(tuple)
Definition: htup_details.h:677
void pfree(void *pointer)
Definition: mcxt.c:992
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer buf, uint8 flags)
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3701
#define ERROR
Definition: elog.h:43
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:639
int max_dead_tuples
Definition: vacuumlazy.c:127
ItemPointerData t_self
Definition: htup.h:65
#define HeapTupleHeaderXminCommitted(tup)
Definition: htup_details.h:318
static TransactionId FreezeLimit
Definition: vacuumlazy.c:139
uint32 t_len
Definition: htup.h:64
void heap_execute_freeze_tuple(HeapTupleHeader tuple, xl_heap_freeze_tuple *frz)
Definition: heapam.c:6786
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
static char * buf
Definition: pg_test_fsync.c:65
#define PageSetAllVisible(page)
Definition: bufpage.h:384
#define FirstOffsetNumber
Definition: off.h:27
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:140
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define InvalidTransactionId
Definition: transam.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:433
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
int num_dead_tuples
Definition: vacuumlazy.c:126
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define SKIP_PAGES_THRESHOLD
Definition: vacuumlazy.c:99
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define WARNING
Definition: elog.h:40
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:332
static int elevel
Definition: vacuumlazy.c:136
#define ngettext(s, p, n)
Definition: c.h:127
void * palloc0(Size size)
Definition: mcxt.c:920
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:382
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:147
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
bool heap_prepare_freeze_tuple(HeapTupleHeader tuple, TransactionId cutoff_xid, TransactionId cutoff_multi, xl_heap_freeze_tuple *frz, bool *totally_frozen_p)
Definition: heapam.c:6608
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
int num_index_scans
Definition: vacuumlazy.c:129
static void vacuum_log_cleanup_info(Relation rel, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:420
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
#define PageClearAllVisible(page)
Definition: bufpage.h:386
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:686
#define Assert(condition)
Definition: c.h:671
double new_dead_tuples
Definition: vacuumlazy.c:120
TransactionId latestRemovedXid
Definition: vacuumlazy.c:130
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
static void lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1585
#define PROGRESS_VACUUM_PHASE_INDEX_CLEANUP
Definition: progress.h:33
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:307
#define VM_ALL_VISIBLE(r, b, v)
Definition: visibilitymap.h:32
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
Definition: pgstat.c:2922
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
size_t Size
Definition: c.h:353
#define PROGRESS_VACUUM_NUM_INDEX_VACUUMS
Definition: progress.h:25
#define PROGRESS_VACUUM_PHASE_SCAN_HEAP
Definition: progress.h:30
#define PROGRESS_VACUUM_PHASE
Definition: progress.h:21
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define RelationNeedsWAL(relation)
Definition: rel.h:502
#define VISIBILITYMAP_ALL_VISIBLE
Definition: visibilitymap.h:26
#define PageGetLSN(page)
Definition: bufpage.h:363
BlockNumber nonempty_pages
Definition: vacuumlazy.c:123
#define PageIsNew(page)
Definition: bufpage.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
BlockNumber frozenskipped_pages
Definition: vacuumlazy.c:116
double scanned_tuples
Definition: vacuumlazy.c:117
int i
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
static bool lazy_check_needs_freeze(Buffer buf, bool *hastup)
Definition: vacuumlazy.c:1533
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
void vacuum_delay_point(void)
Definition: vacuum.c:1515
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
#define _(x)
Definition: elog.c:84
#define RelationGetRelid(relation)
Definition: rel.h:413
#define PROGRESS_VACUUM_PHASE_VACUUM_HEAP
Definition: progress.h:32
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:86
static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
Definition: vacuumlazy.c:1974
#define RelationGetNamespace(relation)
Definition: rel.h:440
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
static void lazy_space_alloc ( LVRelStats vacrelstats,
BlockNumber  relblocks 
)
static

Definition at line 1974 of file vacuumlazy.c.

References autovacuum_work_mem, LVRelStats::dead_tuples, LVRelStats::hasindex, IsAutoVacuumWorkerProcess(), LAZY_ALLOC_TUPLES, maintenance_work_mem, Max, LVRelStats::max_dead_tuples, MaxAllocSize, MaxHeapTuplesPerPage, Min, LVRelStats::num_dead_tuples, and palloc().

Referenced by lazy_scan_heap().

1975 {
1976  long maxtuples;
1977  int vac_work_mem = IsAutoVacuumWorkerProcess() &&
1978  autovacuum_work_mem != -1 ?
1980 
1981  if (vacrelstats->hasindex)
1982  {
1983  maxtuples = (vac_work_mem * 1024L) / sizeof(ItemPointerData);
1984  maxtuples = Min(maxtuples, INT_MAX);
1985  maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData));
1986 
1987  /* curious coding here to ensure the multiplication can't overflow */
1988  if ((BlockNumber) (maxtuples / LAZY_ALLOC_TUPLES) > relblocks)
1989  maxtuples = relblocks * LAZY_ALLOC_TUPLES;
1990 
1991  /* stay sane if small maintenance_work_mem */
1992  maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
1993  }
1994  else
1995  {
1996  maxtuples = MaxHeapTuplesPerPage;
1997  }
1998 
1999  vacrelstats->num_dead_tuples = 0;
2000  vacrelstats->max_dead_tuples = (int) maxtuples;
2001  vacrelstats->dead_tuples = (ItemPointer)
2002  palloc(maxtuples * sizeof(ItemPointerData));
2003 }
int autovacuum_work_mem
Definition: autovacuum.c:113
#define Min(x, y)
Definition: c.h:802
#define MaxHeapTuplesPerPage
Definition: htup_details.h:575
uint32 BlockNumber
Definition: block.h:31
ItemPointerData * ItemPointer
Definition: itemptr.h:48
int max_dead_tuples
Definition: vacuumlazy.c:127
ItemPointer dead_tuples
Definition: vacuumlazy.c:128
int num_dead_tuples
Definition: vacuumlazy.c:126
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2989
#define MaxAllocSize
Definition: memutils.h:40
bool hasindex
Definition: vacuumlazy.c:110
int maintenance_work_mem
Definition: globals.c:113
#define Max(x, y)
Definition: c.h:796
void * palloc(Size size)
Definition: mcxt.c:891
#define LAZY_ALLOC_TUPLES
Definition: vacuumlazy.c:93
static bool lazy_tid_reaped ( ItemPointer  itemptr,
void *  state 
)
static

Definition at line 2034 of file vacuumlazy.c.

References LVRelStats::dead_tuples, NULL, LVRelStats::num_dead_tuples, and vac_cmp_itemptr().

Referenced by lazy_vacuum_index().

2035 {
2036  LVRelStats *vacrelstats = (LVRelStats *) state;
2037  ItemPointer res;
2038 
2039  res = (ItemPointer) bsearch((void *) itemptr,
2040  (void *) vacrelstats->dead_tuples,
2041  vacrelstats->num_dead_tuples,
2042  sizeof(ItemPointerData),
2043  vac_cmp_itemptr);
2044 
2045  return (res != NULL);
2046 }
ItemPointerData * ItemPointer
Definition: itemptr.h:48
ItemPointer dead_tuples
Definition: vacuumlazy.c:128
int num_dead_tuples
Definition: vacuumlazy.c:126
#define NULL
Definition: c.h:226
Definition: regguts.h:298
static int vac_cmp_itemptr(const void *left, const void *right)
Definition: vacuumlazy.c:2052
static void lazy_truncate_heap ( Relation  onerel,
LVRelStats vacrelstats 
)
static

Definition at line 1704 of file vacuumlazy.c.

References AccessExclusiveLock, CHECK_FOR_INTERRUPTS, ConditionalLockRelation(), count_nondeletable_pages(), elevel, ereport, errdetail(), errmsg(), LVRelStats::lock_waiter_detected, LVRelStats::nonempty_pages, LVRelStats::pages_removed, pg_rusage_init(), pg_rusage_show(), pg_usleep(), pgstat_progress_update_param(), PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_PHASE_TRUNCATE, LVRelStats::rel_pages, RelationGetNumberOfBlocks, RelationGetRelationName, RelationTruncate(), UnlockRelation(), VACUUM_TRUNCATE_LOCK_TIMEOUT, and VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL.

Referenced by lazy_vacuum_rel().

1705 {
1706  BlockNumber old_rel_pages = vacrelstats->rel_pages;
1707  BlockNumber new_rel_pages;
1708  PGRUsage ru0;
1709  int lock_retry;
1710 
1711  pg_rusage_init(&ru0);
1712 
1713  /* Report that we are now truncating */
1716 
1717  /*
1718  * Loop until no more truncating can be done.
1719  */
1720  do
1721  {
1722  /*
1723  * We need full exclusive lock on the relation in order to do
1724  * truncation. If we can't get it, give up rather than waiting --- we
1725  * don't want to block other backends, and we don't want to deadlock
1726  * (which is quite possible considering we already hold a lower-grade
1727  * lock).
1728  */
1729  vacrelstats->lock_waiter_detected = false;
1730  lock_retry = 0;
1731  while (true)
1732  {
1734  break;
1735 
1736  /*
1737  * Check for interrupts while trying to (re-)acquire the exclusive
1738  * lock.
1739  */
1741 
1742  if (++lock_retry > (VACUUM_TRUNCATE_LOCK_TIMEOUT /
1744  {
1745  /*
1746  * We failed to establish the lock in the specified number of
1747  * retries. This means we give up truncating.
1748  */
1749  vacrelstats->lock_waiter_detected = true;
1750  ereport(elevel,
1751  (errmsg("\"%s\": stopping truncate due to conflicting lock request",
1752  RelationGetRelationName(onerel))));
1753  return;
1754  }
1755 
1757  }
1758 
1759  /*
1760  * Now that we have exclusive lock, look to see if the rel has grown
1761  * whilst we were vacuuming with non-exclusive lock. If so, give up;
1762  * the newly added pages presumably contain non-deletable tuples.
1763  */
1764  new_rel_pages = RelationGetNumberOfBlocks(onerel);
1765  if (new_rel_pages != old_rel_pages)
1766  {
1767  /*
1768  * Note: we intentionally don't update vacrelstats->rel_pages with
1769  * the new rel size here. If we did, it would amount to assuming
1770  * that the new pages are empty, which is unlikely. Leaving the
1771  * numbers alone amounts to assuming that the new pages have the
1772  * same tuple density as existing ones, which is less unlikely.
1773  */
1775  return;
1776  }
1777 
1778  /*
1779  * Scan backwards from the end to verify that the end pages actually
1780  * contain no tuples. This is *necessary*, not optional, because
1781  * other backends could have added tuples to these pages whilst we
1782  * were vacuuming.
1783  */
1784  new_rel_pages = count_nondeletable_pages(onerel, vacrelstats);
1785 
1786  if (new_rel_pages >= old_rel_pages)
1787  {
1788  /* can't do anything after all */
1790  return;
1791  }
1792 
1793  /*
1794  * Okay to truncate.
1795  */
1796  RelationTruncate(onerel, new_rel_pages);
1797 
1798  /*
1799  * We can release the exclusive lock as soon as we have truncated.
1800  * Other backends can't safely access the relation until they have
1801  * processed the smgr invalidation that smgrtruncate sent out ... but
1802  * that should happen as part of standard invalidation processing once
1803  * they acquire lock on the relation.
1804  */
1806 
1807  /*
1808  * Update statistics. Here, it *is* correct to adjust rel_pages
1809  * without also touching reltuples, since the tuple count wasn't
1810  * changed by the truncation.
1811  */
1812  vacrelstats->pages_removed += old_rel_pages - new_rel_pages;
1813  vacrelstats->rel_pages = new_rel_pages;
1814 
1815  ereport(elevel,
1816  (errmsg("\"%s\": truncated %u to %u pages",
1817  RelationGetRelationName(onerel),
1818  old_rel_pages, new_rel_pages),
1819  errdetail("%s.",
1820  pg_rusage_show(&ru0))));
1821  old_rel_pages = new_rel_pages;
1822  } while (new_rel_pages > vacrelstats->nonempty_pages &&
1823  vacrelstats->lock_waiter_detected);
1824 }
static BlockNumber count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1832
BlockNumber rel_pages
Definition: vacuumlazy.c:113
void UnlockRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:257
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:2900
#define VACUUM_TRUNCATE_LOCK_TIMEOUT
Definition: vacuumlazy.c:86
uint32 BlockNumber
Definition: block.h:31
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
#define PROGRESS_VACUUM_PHASE_TRUNCATE
Definition: progress.h:34
void pg_usleep(long microsec)
Definition: signal.c:53
int errdetail(const char *fmt,...)
Definition: elog.c:873
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
bool ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:226
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define ereport(elevel, rest)
Definition: elog.h:122
#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL
Definition: vacuumlazy.c:85
static int elevel
Definition: vacuumlazy.c:136
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define PROGRESS_VACUUM_PHASE
Definition: progress.h:21
#define AccessExclusiveLock
Definition: lockdefs.h:46
BlockNumber pages_removed
Definition: vacuumlazy.c:121
BlockNumber nonempty_pages
Definition: vacuumlazy.c:123
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void RelationTruncate(Relation rel, BlockNumber nblocks)
Definition: storage.c:227
bool lock_waiter_detected
Definition: vacuumlazy.c:131
static void lazy_vacuum_heap ( Relation  onerel,
LVRelStats vacrelstats 
)
static

Definition at line 1369 of file vacuumlazy.c.

References buf, BufferGetPage, BufferIsValid, ConditionalLockBufferForCleanup(), LVRelStats::dead_tuples, elevel, ereport, errdetail(), errmsg(), InvalidBuffer, ItemPointerGetBlockNumber, lazy_vacuum_page(), MAIN_FORKNUM, PageGetHeapFreeSpace(), pg_rusage_init(), pg_rusage_show(), RBM_NORMAL, ReadBufferExtended(), RecordPageWithFreeSpace(), RelationGetRelationName, ReleaseBuffer(), UnlockReleaseBuffer(), and vacuum_delay_point().

Referenced by lazy_scan_heap().

1370 {
1371  int tupindex;
1372  int npages;
1373  PGRUsage ru0;
1374  Buffer vmbuffer = InvalidBuffer;
1375 
1376  pg_rusage_init(&ru0);
1377  npages = 0;
1378 
1379  tupindex = 0;
1380  while (tupindex < vacrelstats->num_dead_tuples)
1381  {
1382  BlockNumber tblk;
1383  Buffer buf;
1384  Page page;
1385  Size freespace;
1386 
1388 
1389  tblk = ItemPointerGetBlockNumber(&vacrelstats->dead_tuples[tupindex]);
1390  buf = ReadBufferExtended(onerel, MAIN_FORKNUM, tblk, RBM_NORMAL,
1391  vac_strategy);
1393  {
1394  ReleaseBuffer(buf);
1395  ++tupindex;
1396  continue;
1397  }
1398  tupindex = lazy_vacuum_page(onerel, tblk, buf, tupindex, vacrelstats,
1399  &vmbuffer);
1400 
1401  /* Now that we've compacted the page, record its available space */
1402  page = BufferGetPage(buf);
1403  freespace = PageGetHeapFreeSpace(page);
1404 
1405  UnlockReleaseBuffer(buf);
1406  RecordPageWithFreeSpace(onerel, tblk, freespace);
1407  npages++;
1408  }
1409 
1410  if (BufferIsValid(vmbuffer))
1411  {
1412  ReleaseBuffer(vmbuffer);
1413  vmbuffer = InvalidBuffer;
1414  }
1415 
1416  ereport(elevel,
1417  (errmsg("\"%s\": removed %d row versions in %d pages",
1418  RelationGetRelationName(onerel),
1419  tupindex, npages),
1420  errdetail("%s.",
1421  pg_rusage_show(&ru0))));
1422 }
static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, int tupindex, LVRelStats *vacrelstats, Buffer *vmbuffer)
Definition: vacuumlazy.c:1435
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3701
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:639
ItemPointer dead_tuples
Definition: vacuumlazy.c:128
static char * buf
Definition: pg_test_fsync.c:65
int errdetail(const char *fmt,...)
Definition: elog.c:873
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
static int elevel
Definition: vacuumlazy.c:136
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
size_t Size
Definition: c.h:353
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:66
void vacuum_delay_point(void)
Definition: vacuum.c:1515
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
static void lazy_vacuum_index ( Relation  indrel,
IndexBulkDeleteResult **  stats,
LVRelStats vacrelstats 
)
static

Definition at line 1585 of file vacuumlazy.c.

References IndexVacuumInfo::analyze_only, elevel, ereport, errdetail(), errmsg(), IndexVacuumInfo::estimated_count, IndexVacuumInfo::index, index_bulk_delete(), lazy_tid_reaped(), IndexVacuumInfo::message_level, LVRelStats::num_dead_tuples, IndexVacuumInfo::num_heap_tuples, LVRelStats::old_rel_tuples, pg_rusage_init(), pg_rusage_show(), RelationGetRelationName, IndexVacuumInfo::strategy, and vac_strategy.

Referenced by lazy_scan_heap().

1588 {
1589  IndexVacuumInfo ivinfo;
1590  PGRUsage ru0;
1591 
1592  pg_rusage_init(&ru0);
1593 
1594  ivinfo.index = indrel;
1595  ivinfo.analyze_only = false;
1596  ivinfo.estimated_count = true;
1597  ivinfo.message_level = elevel;
1598  ivinfo.num_heap_tuples = vacrelstats->old_rel_tuples;
1599  ivinfo.strategy = vac_strategy;
1600 
1601  /* Do bulk deletion */
1602  *stats = index_bulk_delete(&ivinfo, *stats,
1603  lazy_tid_reaped, (void *) vacrelstats);
1604 
1605  ereport(elevel,
1606  (errmsg("scanned index \"%s\" to remove %d row versions",
1607  RelationGetRelationName(indrel),
1608  vacrelstats->num_dead_tuples),
1609  errdetail("%s.", pg_rusage_show(&ru0))));
1610 }
static bool lazy_tid_reaped(ItemPointer itemptr, void *state)
Definition: vacuumlazy.c:2034
double old_rel_tuples
Definition: vacuumlazy.c:118
bool analyze_only
Definition: genam.h:47
BufferAccessStrategy strategy
Definition: genam.h:51
Relation index
Definition: genam.h:46
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
int errdetail(const char *fmt,...)
Definition: elog.c:873
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define RelationGetRelationName(relation)
Definition: rel.h:433
int num_dead_tuples
Definition: vacuumlazy.c:126
#define ereport(elevel, rest)
Definition: elog.h:122
static int elevel
Definition: vacuumlazy.c:136
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: indexam.c:743
int message_level
Definition: genam.h:49
double num_heap_tuples
Definition: genam.h:50
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool estimated_count
Definition: genam.h:48
static int lazy_vacuum_page ( Relation  onerel,
BlockNumber  blkno,
Buffer  buffer,
int  tupindex,
LVRelStats vacrelstats,
Buffer vmbuffer 
)
static

Definition at line 1435 of file vacuumlazy.c.

References Assert, BufferGetPage, BufferIsValid, LVRelStats::dead_tuples, END_CRIT_SECTION, heap_page_is_all_visible(), InvalidXLogRecPtr, ItemIdSetUnused, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LVRelStats::latestRemovedXid, log_heap_clean(), MarkBufferDirty(), MaxOffsetNumber, NULL, LVRelStats::num_dead_tuples, PageGetItemId, PageIsAllVisible, PageRepairFragmentation(), PageSetAllVisible, PageSetLSN, pgstat_progress_update_param(), PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, RelationNeedsWAL, START_CRIT_SECTION, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, visibilitymap_get_status(), and visibilitymap_set().

Referenced by lazy_scan_heap(), and lazy_vacuum_heap().

1437 {
1438  Page page = BufferGetPage(buffer);
1439  OffsetNumber unused[MaxOffsetNumber];
1440  int uncnt = 0;
1441  TransactionId visibility_cutoff_xid;
1442  bool all_frozen;
1443 
1445 
1447 
1448  for (; tupindex < vacrelstats->num_dead_tuples; tupindex++)
1449  {
1450  BlockNumber tblk;
1451  OffsetNumber toff;
1452  ItemId itemid;
1453 
1454  tblk = ItemPointerGetBlockNumber(&vacrelstats->dead_tuples[tupindex]);
1455  if (tblk != blkno)
1456  break; /* past end of tuples for this block */
1457  toff = ItemPointerGetOffsetNumber(&vacrelstats->dead_tuples[tupindex]);
1458  itemid = PageGetItemId(page, toff);
1459  ItemIdSetUnused(itemid);
1460  unused[uncnt++] = toff;
1461  }
1462 
1464 
1465  /*
1466  * Mark buffer dirty before we write WAL.
1467  */
1468  MarkBufferDirty(buffer);
1469 
1470  /* XLOG stuff */
1471  if (RelationNeedsWAL(onerel))
1472  {
1473  XLogRecPtr recptr;
1474 
1475  recptr = log_heap_clean(onerel, buffer,
1476  NULL, 0, NULL, 0,
1477  unused, uncnt,
1478  vacrelstats->latestRemovedXid);
1479  PageSetLSN(page, recptr);
1480  }
1481 
1482  /*
1483  * End critical section, so we safely can do visibility tests (which
1484  * possibly need to perform IO and allocate memory!). If we crash now the
1485  * page (including the corresponding vm bit) might not be marked all
1486  * visible, but that's fine. A later vacuum will fix that.
1487  */
1488  END_CRIT_SECTION();
1489 
1490  /*
1491  * Now that we have removed the dead tuples from the page, once again
1492  * check if the page has become all-visible. The page is already marked
1493  * dirty, exclusively locked, and, if needed, a full page image has been
1494  * emitted in the log_heap_clean() above.
1495  */
1496  if (heap_page_is_all_visible(onerel, buffer, &visibility_cutoff_xid,
1497  &all_frozen))
1498  PageSetAllVisible(page);
1499 
1500  /*
1501  * All the changes to the heap page have been done. If the all-visible
1502  * flag is now set, also set the VM all-visible bit (and, if possible, the
1503  * all-frozen bit) unless this has already been done previously.
1504  */
1505  if (PageIsAllVisible(page))
1506  {
1507  uint8 vm_status = visibilitymap_get_status(onerel, blkno, vmbuffer);
1508  uint8 flags = 0;
1509 
1510  /* Set the VM all-frozen bit to flag, if needed */
1511  if ((vm_status & VISIBILITYMAP_ALL_VISIBLE) == 0)
1512  flags |= VISIBILITYMAP_ALL_VISIBLE;
1513  if ((vm_status & VISIBILITYMAP_ALL_FROZEN) == 0 && all_frozen)
1514  flags |= VISIBILITYMAP_ALL_FROZEN;
1515 
1516  Assert(BufferIsValid(*vmbuffer));
1517  if (flags != 0)
1518  visibilitymap_set(onerel, blkno, buffer, InvalidXLogRecPtr,
1519  *vmbuffer, visibility_cutoff_xid, flags);
1520  }
1521 
1522  return tupindex;
1523 }
#define PROGRESS_VACUUM_HEAP_BLKS_VACUUMED
Definition: progress.h:24
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define PageIsAllVisible(page)
Definition: bufpage.h:382
uint32 TransactionId
Definition: c.h:394
void visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
#define MaxOffsetNumber
Definition: off.h:28
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:2900
#define VISIBILITYMAP_ALL_FROZEN
Definition: visibilitymap.h:27
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
unsigned char uint8
Definition: c.h:263
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
uint32 BlockNumber
Definition: block.h:31
uint16 OffsetNumber
Definition: off.h:24
ItemPointer dead_tuples
Definition: vacuumlazy.c:128
#define PageSetAllVisible(page)
Definition: bufpage.h:384
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
int num_dead_tuples
Definition: vacuumlazy.c:126
static bool heap_page_is_all_visible(Relation rel, Buffer buf, TransactionId *visibility_cutoff_xid, bool *all_frozen)
Definition: vacuumlazy.c:2085
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
TransactionId latestRemovedXid
Definition: vacuumlazy.c:130
XLogRecPtr log_heap_clean(Relation reln, Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused, TransactionId latestRemovedXid)
Definition: heapam.c:7386
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:76
#define RelationNeedsWAL(relation)
Definition: rel.h:502
#define VISIBILITYMAP_ALL_VISIBLE
Definition: visibilitymap.h:26
void PageRepairFragmentation(Page page)
Definition: bufpage.c:479
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *buf)
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:66
#define ItemIdSetUnused(itemId)
Definition: itemid.h:127
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
Pointer Page
Definition: bufpage.h:74
void lazy_vacuum_rel ( Relation  onerel,
int  options,
VacuumParams params,
BufferAccessStrategy  bstrategy 
)

Definition at line 182 of file vacuumlazy.c.

References _, appendStringInfo(), Assert, buf, StringInfoData::data, DEBUG2, elevel, ereport, errmsg_internal(), FreeSpaceMapVacuum(), VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, FreezeLimit, LVRelStats::frozenskipped_pages, get_database_name(), get_namespace_name(), GetCurrentTimestamp(), LVRelStats::hasindex, INFO, initStringInfo(), InvalidMultiXactId, InvalidTransactionId, IsAutoVacuumWorkerProcess(), lazy_scan_heap(), lazy_truncate_heap(), LVRelStats::lock_waiter_detected, LOG, VacuumParams::log_min_duration, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, MultiXactCutoff, MultiXactIdPrecedesOrEquals(), MyDatabaseId, LVRelStats::new_dead_tuples, LVRelStats::new_rel_tuples, NoLock, NULL, LVRelStats::num_index_scans, LVRelStats::old_rel_pages, LVRelStats::old_rel_tuples, OldestXmin, LVRelStats::pages_removed, palloc0(), pfree(), pg_rusage_init(), pg_rusage_show(), pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_param(), pgstat_report_vacuum(), LVRelStats::pinskipped_pages, PROGRESS_COMMAND_VACUUM, PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_PHASE_FINAL_CLEANUP, RelationData::rd_rel, LVRelStats::rel_pages, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, LVRelStats::scanned_pages, should_attempt_truncation(), TimestampDifference(), TimestampDifferenceExceeds(), TransactionIdPrecedesOrEquals(), LVRelStats::tuples_deleted, vac_close_indexes(), vac_open_indexes(), vac_update_relstats(), VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_VERBOSE, vacuum_set_xid_limits(), VacuumPageDirty, VacuumPageHit, VacuumPageMiss, and visibilitymap_count().

Referenced by vacuum_rel().

184 {
185  LVRelStats *vacrelstats;
186  Relation *Irel;
187  int nindexes;
188  PGRUsage ru0;
189  TimestampTz starttime = 0;
190  long secs;
191  int usecs;
192  double read_rate,
193  write_rate;
194  bool aggressive; /* should we scan all unfrozen pages? */
195  bool scanned_all_unfrozen; /* actually scanned all such pages? */
196  TransactionId xidFullScanLimit;
197  MultiXactId mxactFullScanLimit;
198  BlockNumber new_rel_pages;
199  double new_rel_tuples;
200  BlockNumber new_rel_allvisible;
201  double new_live_tuples;
202  TransactionId new_frozen_xid;
203  MultiXactId new_min_multi;
204 
205  Assert(params != NULL);
206 
207  /* measure elapsed time iff autovacuum logging requires it */
208  if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
209  {
210  pg_rusage_init(&ru0);
211  starttime = GetCurrentTimestamp();
212  }
213 
214  if (options & VACOPT_VERBOSE)
215  elevel = INFO;
216  else
217  elevel = DEBUG2;
218 
220  RelationGetRelid(onerel));
221 
222  vac_strategy = bstrategy;
223 
224  vacuum_set_xid_limits(onerel,
225  params->freeze_min_age,
226  params->freeze_table_age,
227  params->multixact_freeze_min_age,
229  &OldestXmin, &FreezeLimit, &xidFullScanLimit,
230  &MultiXactCutoff, &mxactFullScanLimit);
231 
232  /*
233  * We request an aggressive scan if the table's frozen Xid is now older
234  * than or equal to the requested Xid full-table scan limit; or if the
235  * table's minimum MultiXactId is older than or equal to the requested
236  * mxid full-table scan limit; or if DISABLE_PAGE_SKIPPING was specified.
237  */
238  aggressive = TransactionIdPrecedesOrEquals(onerel->rd_rel->relfrozenxid,
239  xidFullScanLimit);
240  aggressive |= MultiXactIdPrecedesOrEquals(onerel->rd_rel->relminmxid,
241  mxactFullScanLimit);
243  aggressive = true;
244 
245  vacrelstats = (LVRelStats *) palloc0(sizeof(LVRelStats));
246 
247  vacrelstats->old_rel_pages = onerel->rd_rel->relpages;
248  vacrelstats->old_rel_tuples = onerel->rd_rel->reltuples;
249  vacrelstats->num_index_scans = 0;
250  vacrelstats->pages_removed = 0;
251  vacrelstats->lock_waiter_detected = false;
252 
253  /* Open all indexes of the relation */
254  vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel);
255  vacrelstats->hasindex = (nindexes > 0);
256 
257  /* Do the vacuuming */
258  lazy_scan_heap(onerel, options, vacrelstats, Irel, nindexes, aggressive);
259 
260  /* Done with indexes */
261  vac_close_indexes(nindexes, Irel, NoLock);
262 
263  /*
264  * Compute whether we actually scanned the all unfrozen pages. If we did,
265  * we can adjust relfrozenxid and relminmxid.
266  *
267  * NB: We need to check this before truncating the relation, because that
268  * will change ->rel_pages.
269  */
270  if ((vacrelstats->scanned_pages + vacrelstats->frozenskipped_pages)
271  < vacrelstats->rel_pages)
272  {
273  Assert(!aggressive);
274  scanned_all_unfrozen = false;
275  }
276  else
277  scanned_all_unfrozen = true;
278 
279  /*
280  * Optionally truncate the relation.
281  */
282  if (should_attempt_truncation(vacrelstats))
283  lazy_truncate_heap(onerel, vacrelstats);
284 
285  /* Report that we are now doing final cleanup */
288 
289  /* Vacuum the Free Space Map */
290  FreeSpaceMapVacuum(onerel);
291 
292  /*
293  * Update statistics in pg_class.
294  *
295  * A corner case here is that if we scanned no pages at all because every
296  * page is all-visible, we should not update relpages/reltuples, because
297  * we have no new information to contribute. In particular this keeps us
298  * from replacing relpages=reltuples=0 (which means "unknown tuple
299  * density") with nonzero relpages and reltuples=0 (which means "zero
300  * tuple density") unless there's some actual evidence for the latter.
301  *
302  * We do update relallvisible even in the corner case, since if the table
303  * is all-visible we'd definitely like to know that. But clamp the value
304  * to be not more than what we're setting relpages to.
305  *
306  * Also, don't change relfrozenxid/relminmxid if we skipped any pages,
307  * since then we don't know for certain that all tuples have a newer xmin.
308  */
309  new_rel_pages = vacrelstats->rel_pages;
310  new_rel_tuples = vacrelstats->new_rel_tuples;
311  if (vacrelstats->scanned_pages == 0 && new_rel_pages > 0)
312  {
313  new_rel_pages = vacrelstats->old_rel_pages;
314  new_rel_tuples = vacrelstats->old_rel_tuples;
315  }
316 
317  visibilitymap_count(onerel, &new_rel_allvisible, NULL);
318  if (new_rel_allvisible > new_rel_pages)
319  new_rel_allvisible = new_rel_pages;
320 
321  new_frozen_xid = scanned_all_unfrozen ? FreezeLimit : InvalidTransactionId;
322  new_min_multi = scanned_all_unfrozen ? MultiXactCutoff : InvalidMultiXactId;
323 
324  vac_update_relstats(onerel,
325  new_rel_pages,
326  new_rel_tuples,
327  new_rel_allvisible,
328  vacrelstats->hasindex,
329  new_frozen_xid,
330  new_min_multi,
331  false);
332 
333  /* report results to the stats collector, too */
334  new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples;
335  if (new_live_tuples < 0)
336  new_live_tuples = 0; /* just in case */
337 
339  onerel->rd_rel->relisshared,
340  new_live_tuples,
341  vacrelstats->new_dead_tuples);
343 
344  /* and log the action if appropriate */
345  if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
346  {
347  TimestampTz endtime = GetCurrentTimestamp();
348 
349  if (params->log_min_duration == 0 ||
350  TimestampDifferenceExceeds(starttime, endtime,
351  params->log_min_duration))
352  {
354 
355  TimestampDifference(starttime, endtime, &secs, &usecs);
356 
357  read_rate = 0;
358  write_rate = 0;
359  if ((secs > 0) || (usecs > 0))
360  {
361  read_rate = (double) BLCKSZ *VacuumPageMiss / (1024 * 1024) /
362  (secs + usecs / 1000000.0);
363  write_rate = (double) BLCKSZ *VacuumPageDirty / (1024 * 1024) /
364  (secs + usecs / 1000000.0);
365  }
366 
367  /*
368  * This is pretty messy, but we split it up so that we can skip
369  * emitting individual parts of the message when not applicable.
370  */
371  initStringInfo(&buf);
372  appendStringInfo(&buf, _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"),
375  RelationGetRelationName(onerel),
376  vacrelstats->num_index_scans);
377  appendStringInfo(&buf, _("pages: %u removed, %u remain, %u skipped due to pins, %u skipped frozen\n"),
378  vacrelstats->pages_removed,
379  vacrelstats->rel_pages,
380  vacrelstats->pinskipped_pages,
381  vacrelstats->frozenskipped_pages);
382  appendStringInfo(&buf,
383  _("tuples: %.0f removed, %.0f remain, %.0f are dead but not yet removable\n"),
384  vacrelstats->tuples_deleted,
385  vacrelstats->new_rel_tuples,
386  vacrelstats->new_dead_tuples);
387  appendStringInfo(&buf,
388  _("buffer usage: %d hits, %d misses, %d dirtied\n"),
392  appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
393  read_rate, write_rate);
394  appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
395 
396  ereport(LOG,
397  (errmsg_internal("%s", buf.data)));
398  pfree(buf.data);
399  }
400  }
401 }
double new_rel_tuples
Definition: vacuumlazy.c:119
int multixact_freeze_table_age
Definition: vacuum.h:142
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:1494
BlockNumber rel_pages
Definition: vacuumlazy.c:113
uint32 TransactionId
Definition: c.h:394
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
Definition: pgstat.c:2879
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1569
double tuples_deleted
Definition: vacuumlazy.c:122
int64 TimestampTz
Definition: timestamp.h:39
int VacuumPageHit
Definition: globals.c:134
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:2900
double old_rel_tuples
Definition: vacuumlazy.c:118
static void lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats, Relation *Irel, int nindexes, bool aggressive)
Definition: vacuumlazy.c:453
BlockNumber scanned_pages
Definition: vacuumlazy.c:114
#define INFO
Definition: elog.h:33
void vacuum_set_xid_limits(Relation rel, int freeze_min_age, int freeze_table_age, int multixact_freeze_min_age, int multixact_freeze_table_age, TransactionId *oldestXmin, TransactionId *freezeLimit, TransactionId *xidFullScanLimit, MultiXactId *multiXactCutoff, MultiXactId *mxactFullScanLimit)
Definition: vacuum.c:471
uint32 BlockNumber
Definition: block.h:31
BlockNumber pinskipped_pages
Definition: vacuumlazy.c:115
void visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:113
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1648
int freeze_table_age
Definition: vacuum.h:139
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
BlockNumber old_rel_pages
Definition: vacuumlazy.c:112
void pfree(void *pointer)
Definition: mcxt.c:992
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
int freeze_min_age
Definition: vacuum.h:138
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2049
#define DEBUG2
Definition: elog.h:24
static TransactionId FreezeLimit
Definition: vacuumlazy.c:139
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:140
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define InvalidTransactionId
Definition: transam.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:433
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2989
#define ereport(elevel, rest)
Definition: elog.h:122
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:1451
static int elevel
Definition: vacuumlazy.c:136
bool hasindex
Definition: vacuumlazy.c:110
int VacuumPageDirty
Definition: globals.c:136
void * palloc0(Size size)
Definition: mcxt.c:920
void pgstat_progress_end_command(void)
Definition: pgstat.c:2951
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidMultiXactId
Definition: multixact.h:23
static bool should_attempt_truncation(LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1686
#define PROGRESS_VACUUM_PHASE_FINAL_CLEANUP
Definition: progress.h:35
int num_index_scans
Definition: vacuumlazy.c:129
TransactionId MultiXactId
Definition: c.h:404
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
double new_dead_tuples
Definition: vacuumlazy.c:120
#define PROGRESS_VACUUM_PHASE
Definition: progress.h:21
int log_min_duration
Definition: vacuum.h:145
void pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
Definition: pgstat.c:1329
BlockNumber pages_removed
Definition: vacuumlazy.c:121
BlockNumber frozenskipped_pages
Definition: vacuumlazy.c:116
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3149
int VacuumPageMiss
Definition: globals.c:135
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1623
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool in_outer_xact)
Definition: vacuum.c:755
#define _(x)
Definition: elog.c:84
#define RelationGetRelid(relation)
Definition: rel.h:413
int multixact_freeze_min_age
Definition: vacuum.h:140
static void lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1704
#define RelationGetNamespace(relation)
Definition: rel.h:440
bool lock_waiter_detected
Definition: vacuumlazy.c:131
static bool should_attempt_truncation ( LVRelStats vacrelstats)
static

Definition at line 1686 of file vacuumlazy.c.

References LVRelStats::nonempty_pages, old_snapshot_threshold, LVRelStats::rel_pages, REL_TRUNCATE_FRACTION, and REL_TRUNCATE_MINIMUM.

Referenced by lazy_vacuum_rel().

1687 {
1688  BlockNumber possibly_freeable;
1689 
1690  possibly_freeable = vacrelstats->rel_pages - vacrelstats->nonempty_pages;
1691  if (possibly_freeable > 0 &&
1692  (possibly_freeable >= REL_TRUNCATE_MINIMUM ||
1693  possibly_freeable >= vacrelstats->rel_pages / REL_TRUNCATE_FRACTION) &&
1695  return true;
1696  else
1697  return false;
1698 }
BlockNumber rel_pages
Definition: vacuumlazy.c:113
uint32 BlockNumber
Definition: block.h:31
#define REL_TRUNCATE_MINIMUM
Definition: vacuumlazy.c:74
BlockNumber nonempty_pages
Definition: vacuumlazy.c:123
int old_snapshot_threshold
Definition: snapmgr.c:74
#define REL_TRUNCATE_FRACTION
Definition: vacuumlazy.c:75
static int vac_cmp_itemptr ( const void *  left,
const void *  right 
)
static

Definition at line 2052 of file vacuumlazy.c.

References ItemPointerGetBlockNumber, and ItemPointerGetOffsetNumber.

Referenced by lazy_tid_reaped().

2053 {
2054  BlockNumber lblk,
2055  rblk;
2056  OffsetNumber loff,
2057  roff;
2058 
2059  lblk = ItemPointerGetBlockNumber((ItemPointer) left);
2060  rblk = ItemPointerGetBlockNumber((ItemPointer) right);
2061 
2062  if (lblk < rblk)
2063  return -1;
2064  if (lblk > rblk)
2065  return 1;
2066 
2067  loff = ItemPointerGetOffsetNumber((ItemPointer) left);
2068  roff = ItemPointerGetOffsetNumber((ItemPointer) right);
2069 
2070  if (loff < roff)
2071  return -1;
2072  if (loff > roff)
2073  return 1;
2074 
2075  return 0;
2076 }
uint32 BlockNumber
Definition: block.h:31
uint16 OffsetNumber
Definition: off.h:24
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:76
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:66
static void vacuum_log_cleanup_info ( Relation  rel,
LVRelStats vacrelstats 
)
static

Definition at line 420 of file vacuumlazy.c.

References LVRelStats::latestRemovedXid, log_heap_cleanup_info(), RelationData::rd_node, RelationNeedsWAL, TransactionIdIsValid, and XLogIsNeeded.

Referenced by lazy_scan_heap().

421 {
422  /*
423  * Skip this for relations for which no WAL is to be written, or if we're
424  * not trying to support archive recovery.
425  */
426  if (!RelationNeedsWAL(rel) || !XLogIsNeeded())
427  return;
428 
429  /*
430  * No need to write the record at all unless it contains a valid value
431  */
432  if (TransactionIdIsValid(vacrelstats->latestRemovedXid))
433  (void) log_heap_cleanup_info(rel->rd_node, vacrelstats->latestRemovedXid);
434 }
XLogRecPtr log_heap_cleanup_info(RelFileNode rnode, TransactionId latestRemovedXid)
Definition: heapam.c:7357
#define XLogIsNeeded()
Definition: xlog.h:145
RelFileNode rd_node
Definition: rel.h:85
TransactionId latestRemovedXid
Definition: vacuumlazy.c:130
#define RelationNeedsWAL(relation)
Definition: rel.h:502
#define TransactionIdIsValid(xid)
Definition: transam.h:41

Variable Documentation

TransactionId FreezeLimit
static

Definition at line 139 of file vacuumlazy.c.

Referenced by lazy_check_needs_freeze(), lazy_scan_heap(), and lazy_vacuum_rel().

MultiXactId MultiXactCutoff
static
BufferAccessStrategy vac_strategy
static

Definition at line 142 of file vacuumlazy.c.

Referenced by lazy_cleanup_index(), and lazy_vacuum_index().