PostgreSQL Source Code git master
Loading...
Searching...
No Matches
verify_heapam.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/genam.h"
#include "access/heaptoast.h"
#include "access/multixact.h"
#include "access/relation.h"
#include "access/table.h"
#include "access/toast_internals.h"
#include "access/visibilitymap.h"
#include "access/xact.h"
#include "catalog/pg_am.h"
#include "catalog/pg_class.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/procarray.h"
#include "storage/read_stream.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/tuplestore.h"
Include dependency graph for verify_heapam.c:

Go to the source code of this file.

Data Structures

struct  ToastedAttribute
 
struct  HeapCheckContext
 
struct  HeapCheckReadStreamData
 

Macros

#define HEAPCHECK_RELATION_COLS   4
 
#define VARLENA_SIZE_LIMIT   0x3FFFFFFF
 

Typedefs

typedef enum XidBoundsViolation XidBoundsViolation
 
typedef enum XidCommitStatus XidCommitStatus
 
typedef enum SkipPages SkipPages
 
typedef struct ToastedAttribute ToastedAttribute
 
typedef struct HeapCheckContext HeapCheckContext
 
typedef struct HeapCheckReadStreamData HeapCheckReadStreamData
 

Enumerations

enum  XidBoundsViolation {
  XID_INVALID , XID_IN_FUTURE , XID_PRECEDES_CLUSTERMIN , XID_PRECEDES_RELMIN ,
  XID_BOUNDS_OK
}
 
enum  XidCommitStatus { XID_COMMITTED , XID_IS_CURRENT_XID , XID_IN_PROGRESS , XID_ABORTED }
 
enum  SkipPages { SKIP_PAGES_ALL_FROZEN , SKIP_PAGES_ALL_VISIBLE , SKIP_PAGES_NONE }
 

Functions

 PG_FUNCTION_INFO_V1 (verify_heapam)
 
static BlockNumber heapcheck_read_stream_next_unskippable (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 
static void check_tuple (HeapCheckContext *ctx, bool *xmin_commit_status_ok, XidCommitStatus *xmin_commit_status)
 
static void check_toast_tuple (HeapTuple toasttup, HeapCheckContext *ctx, ToastedAttribute *ta, int32 *expected_chunk_seq, uint32 extsize)
 
static bool check_tuple_attribute (HeapCheckContext *ctx)
 
static void check_toasted_attribute (HeapCheckContext *ctx, ToastedAttribute *ta)
 
static bool check_tuple_header (HeapCheckContext *ctx)
 
static bool check_tuple_visibility (HeapCheckContext *ctx, bool *xmin_commit_status_ok, XidCommitStatus *xmin_commit_status)
 
static void report_corruption (HeapCheckContext *ctx, char *msg)
 
static void report_toast_corruption (HeapCheckContext *ctx, ToastedAttribute *ta, char *msg)
 
static FullTransactionId FullTransactionIdFromXidAndCtx (TransactionId xid, const HeapCheckContext *ctx)
 
static void update_cached_xid_range (HeapCheckContext *ctx)
 
static void update_cached_mxid_range (HeapCheckContext *ctx)
 
static XidBoundsViolation check_mxid_in_range (MultiXactId mxid, HeapCheckContext *ctx)
 
static XidBoundsViolation check_mxid_valid_in_rel (MultiXactId mxid, HeapCheckContext *ctx)
 
static XidBoundsViolation get_xid_status (TransactionId xid, HeapCheckContext *ctx, XidCommitStatus *status)
 
Datum verify_heapam (PG_FUNCTION_ARGS)
 
static void report_corruption_internal (Tuplestorestate *tupstore, TupleDesc tupdesc, BlockNumber blkno, OffsetNumber offnum, AttrNumber attnum, char *msg)
 
static bool fxid_in_cached_range (FullTransactionId fxid, const HeapCheckContext *ctx)
 

Macro Definition Documentation

◆ HEAPCHECK_RELATION_COLS

#define HEAPCHECK_RELATION_COLS   4

Definition at line 37 of file verify_heapam.c.

◆ VARLENA_SIZE_LIMIT

#define VARLENA_SIZE_LIMIT   0x3FFFFFFF

Definition at line 40 of file verify_heapam.c.

Typedef Documentation

◆ HeapCheckContext

◆ HeapCheckReadStreamData

◆ SkipPages

◆ ToastedAttribute

◆ XidBoundsViolation

◆ XidCommitStatus

Enumeration Type Documentation

◆ SkipPages

Enumerator
SKIP_PAGES_ALL_FROZEN 
SKIP_PAGES_ALL_VISIBLE 
SKIP_PAGES_NONE 

Definition at line 63 of file verify_heapam.c.

64{
68} SkipPages;
SkipPages
@ SKIP_PAGES_ALL_VISIBLE
@ SKIP_PAGES_ALL_FROZEN
@ SKIP_PAGES_NONE

◆ XidBoundsViolation

Enumerator
XID_INVALID 
XID_IN_FUTURE 
XID_PRECEDES_CLUSTERMIN 
XID_PRECEDES_RELMIN 
XID_BOUNDS_OK 

Definition at line 46 of file verify_heapam.c.

47{
XidBoundsViolation
@ XID_IN_FUTURE
@ XID_PRECEDES_CLUSTERMIN
@ XID_INVALID
@ XID_PRECEDES_RELMIN
@ XID_BOUNDS_OK

◆ XidCommitStatus

Enumerator
XID_COMMITTED 
XID_IS_CURRENT_XID 
XID_IN_PROGRESS 
XID_ABORTED 

Definition at line 55 of file verify_heapam.c.

56{
XidCommitStatus
@ XID_IS_CURRENT_XID
@ XID_IN_PROGRESS
@ XID_COMMITTED
@ XID_ABORTED

Function Documentation

◆ check_mxid_in_range()

static XidBoundsViolation check_mxid_in_range ( MultiXactId  mxid,
HeapCheckContext ctx 
)
static

Definition at line 2058 of file verify_heapam.c.

2059{
2061 return XID_INVALID;
2063 return XID_PRECEDES_RELMIN;
2067 return XID_IN_FUTURE;
2068 return XID_BOUNDS_OK;
2069}
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2857
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2871
static int fb(int x)
TransactionId relminmxid
MultiXactId next_mxact
MultiXactId oldest_mxact
#define TransactionIdIsValid(xid)
Definition transam.h:41

References fb(), MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), HeapCheckContext::next_mxact, HeapCheckContext::oldest_mxact, HeapCheckContext::relminmxid, TransactionIdIsValid, XID_BOUNDS_OK, XID_IN_FUTURE, XID_INVALID, XID_PRECEDES_CLUSTERMIN, and XID_PRECEDES_RELMIN.

Referenced by check_mxid_valid_in_rel().

◆ check_mxid_valid_in_rel()

static XidBoundsViolation check_mxid_valid_in_rel ( MultiXactId  mxid,
HeapCheckContext ctx 
)
static

Definition at line 2080 of file verify_heapam.c.

2081{
2082 XidBoundsViolation result;
2083
2084 result = check_mxid_in_range(mxid, ctx);
2085 if (result == XID_BOUNDS_OK)
2086 return XID_BOUNDS_OK;
2087
2088 /* The range may have advanced. Recheck. */
2090 return check_mxid_in_range(mxid, ctx);
2091}
static XidBoundsViolation check_mxid_in_range(MultiXactId mxid, HeapCheckContext *ctx)
static void update_cached_mxid_range(HeapCheckContext *ctx)

References check_mxid_in_range(), fb(), update_cached_mxid_range(), and XID_BOUNDS_OK.

Referenced by check_tuple_visibility().

◆ check_toast_tuple()

static void check_toast_tuple ( HeapTuple  toasttup,
HeapCheckContext ctx,
ToastedAttribute ta,
int32 expected_chunk_seq,
uint32  extsize 
)
static

Definition at line 1555 of file verify_heapam.c.

1558{
1560 int32 last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
1561 Pointer chunk;
1562 bool isnull;
1565
1566 /* Sanity-check the sequence number. */
1568 ctx->toast_rel->rd_att, &isnull));
1569 if (isnull)
1570 {
1572 psprintf("toast value %u has toast chunk with null sequence number",
1573 ta->toast_pointer.va_valueid));
1574 return;
1575 }
1577 {
1578 /* Either the TOAST index is corrupt, or we don't have all chunks. */
1580 psprintf("toast value %u index scan returned chunk %d when expecting chunk %d",
1581 ta->toast_pointer.va_valueid,
1583 }
1585
1586 /* Sanity-check the chunk data. */
1588 ctx->toast_rel->rd_att, &isnull));
1589 if (isnull)
1590 {
1592 psprintf("toast value %u chunk %d has null data",
1593 ta->toast_pointer.va_valueid,
1594 chunk_seq));
1595 return;
1596 }
1597 if (!VARATT_IS_EXTENDED(chunk))
1598 chunksize = VARSIZE(chunk) - VARHDRSZ;
1599 else if (VARATT_IS_SHORT(chunk))
1600 {
1601 /*
1602 * could happen due to heap_form_tuple doing its thing
1603 */
1605 }
1606 else
1607 {
1608 /* should never happen */
1609 uint32 header = ((varattrib_4b *) chunk)->va_4byte.va_header;
1610
1612 psprintf("toast value %u chunk %d has invalid varlena header %0x",
1613 ta->toast_pointer.va_valueid,
1614 chunk_seq, header));
1615 return;
1616 }
1617
1618 /*
1619 * Some checks on the data we've found
1620 */
1621 if (chunk_seq > last_chunk_seq)
1622 {
1624 psprintf("toast value %u chunk %d follows last expected chunk %d",
1625 ta->toast_pointer.va_valueid,
1626 chunk_seq, last_chunk_seq));
1627 return;
1628 }
1629
1630 expected_size = chunk_seq < last_chunk_seq ? TOAST_MAX_CHUNK_SIZE
1631 : extsize - (last_chunk_seq * TOAST_MAX_CHUNK_SIZE);
1632
1633 if (chunksize != expected_size)
1635 psprintf("toast value %u chunk %d has size %u, but expected size %u",
1636 ta->toast_pointer.va_valueid,
1638}
#define VARHDRSZ
Definition c.h:783
int32_t int32
Definition c.h:614
uint32_t uint32
Definition c.h:618
void * Pointer
Definition c.h:609
#define TOAST_MAX_CHUNK_SIZE
Definition heaptoast.h:84
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
TupleDesc rd_att
Definition rel.h:112
#define VARHDRSZ_SHORT
Definition varatt.h:278
static bool VARATT_IS_SHORT(const void *PTR)
Definition varatt.h:403
static bool VARATT_IS_EXTENDED(const void *PTR)
Definition varatt.h:410
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static Size VARSIZE_SHORT(const void *PTR)
Definition varatt.h:312
static void report_toast_corruption(HeapCheckContext *ctx, ToastedAttribute *ta, char *msg)

References DatumGetInt32(), DatumGetPointer(), fastgetattr(), fb(), psprintf(), RelationData::rd_att, report_toast_corruption(), TOAST_MAX_CHUNK_SIZE, HeapCheckContext::toast_rel, VARATT_IS_EXTENDED(), VARATT_IS_SHORT(), VARHDRSZ, VARHDRSZ_SHORT, VARSIZE(), and VARSIZE_SHORT().

Referenced by check_toasted_attribute().

◆ check_toasted_attribute()

static void check_toasted_attribute ( HeapCheckContext ctx,
ToastedAttribute ta 
)
static

Definition at line 1861 of file verify_heapam.c.

1862{
1865 bool found_toasttup;
1869 int32 last_chunk_seq;
1870
1871 extsize = VARATT_EXTERNAL_GET_EXTSIZE(ta->toast_pointer);
1872 last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
1873
1874 /*
1875 * Setup a scan key to find chunks in toast table with matching va_valueid
1876 */
1878 (AttrNumber) 1,
1880 ObjectIdGetDatum(ta->toast_pointer.va_valueid));
1881
1882 /*
1883 * Check if any chunks for this toasted object exist in the toast table,
1884 * accessible via the index.
1885 */
1887 ctx->valid_toast_index,
1888 get_toast_snapshot(), 1,
1889 &toastkey);
1890 found_toasttup = false;
1891 while ((toasttup =
1894 {
1895 found_toasttup = true;
1897 }
1899
1900 if (!found_toasttup)
1902 psprintf("toast value %u not found in toast table",
1903 ta->toast_pointer.va_valueid));
1904 else if (expected_chunk_seq <= last_chunk_seq)
1906 psprintf("toast value %u was expected to end at chunk %d, but ended while expecting chunk %d",
1907 ta->toast_pointer.va_valueid,
1908 last_chunk_seq, expected_chunk_seq));
1909}
int16 AttrNumber
Definition attnum.h:21
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:650
void systable_endscan_ordered(SysScanDesc sysscan)
Definition genam.c:757
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition genam.c:732
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
@ ForwardScanDirection
Definition sdir.h:28
#define BTEqualStrategyNumber
Definition stratnum.h:31
Relation valid_toast_index
Snapshot get_toast_snapshot(void)
static Size VARATT_EXTERNAL_GET_EXTSIZE(varatt_external toast_pointer)
Definition varatt.h:507
static void check_toast_tuple(HeapTuple toasttup, HeapCheckContext *ctx, ToastedAttribute *ta, int32 *expected_chunk_seq, uint32 extsize)

References BTEqualStrategyNumber, check_toast_tuple(), fb(), ForwardScanDirection, get_toast_snapshot(), ObjectIdGetDatum(), psprintf(), report_toast_corruption(), ScanKeyInit(), systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), TOAST_MAX_CHUNK_SIZE, HeapCheckContext::toast_rel, HeapCheckContext::valid_toast_index, and VARATT_EXTERNAL_GET_EXTSIZE().

Referenced by verify_heapam().

◆ check_tuple()

static void check_tuple ( HeapCheckContext ctx,
bool xmin_commit_status_ok,
XidCommitStatus xmin_commit_status 
)
static

Definition at line 1919 of file verify_heapam.c.

1921{
1922 /*
1923 * Check various forms of tuple header corruption, and if the header is
1924 * too corrupt, do not continue with other checks.
1925 */
1926 if (!check_tuple_header(ctx))
1927 return;
1928
1929 /*
1930 * Check tuple visibility. If the inserting transaction aborted, we
1931 * cannot assume our relation description matches the tuple structure, and
1932 * therefore cannot check it.
1933 */
1936 return;
1937
1938 /*
1939 * The tuple is visible, so it must be compatible with the current version
1940 * of the relation descriptor. It might have fewer columns than are
1941 * present in the relation descriptor, but it cannot have more.
1942 */
1943 if (RelationGetDescr(ctx->rel)->natts < ctx->natts)
1944 {
1946 psprintf("number of attributes %u exceeds maximum %u expected for table",
1947 ctx->natts,
1948 RelationGetDescr(ctx->rel)->natts));
1949 return;
1950 }
1951
1952 /*
1953 * Check each attribute unless we hit corruption that confuses what to do
1954 * next, at which point we abort further attribute checks for this tuple.
1955 * Note that we don't abort for all types of corruption, only for those
1956 * types where we don't know how to continue. We also don't abort the
1957 * checking of toasted attributes collected from the tuple prior to
1958 * aborting. Those will still be checked later along with other toasted
1959 * attributes collected from the page.
1960 */
1961 ctx->offset = 0;
1962 for (ctx->attnum = 0; ctx->attnum < ctx->natts; ctx->attnum++)
1963 if (!check_tuple_attribute(ctx))
1964 break; /* cannot continue */
1965
1966 /* revert attnum to -1 until we again examine individual attributes */
1967 ctx->attnum = -1;
1968}
#define RelationGetDescr(relation)
Definition rel.h:540
static bool check_tuple_visibility(HeapCheckContext *ctx, bool *xmin_commit_status_ok, XidCommitStatus *xmin_commit_status)
static bool check_tuple_header(HeapCheckContext *ctx)
static bool check_tuple_attribute(HeapCheckContext *ctx)
static void report_corruption(HeapCheckContext *ctx, char *msg)

References HeapCheckContext::attnum, check_tuple_attribute(), check_tuple_header(), check_tuple_visibility(), fb(), HeapCheckContext::natts, HeapCheckContext::offset, psprintf(), HeapCheckContext::rel, RelationGetDescr, and report_corruption().

Referenced by verify_heapam().

◆ check_tuple_attribute()

static bool check_tuple_attribute ( HeapCheckContext ctx)
static

Definition at line 1661 of file verify_heapam.c.

1662{
1664 varlena *attr;
1665 char *tp; /* pointer to the tuple data */
1668 varatt_external toast_pointer;
1669
1670 infomask = ctx->tuphdr->t_infomask;
1672
1673 tp = (char *) ctx->tuphdr + ctx->tuphdr->t_hoff;
1674
1675 if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
1676 {
1678 psprintf("attribute with length %u starts at offset %u beyond total tuple length %u",
1679 thisatt->attlen,
1680 ctx->tuphdr->t_hoff + ctx->offset,
1681 ctx->lp_len));
1682 return false;
1683 }
1684
1685 /* Skip null values */
1686 if (infomask & HEAP_HASNULL && att_isnull(ctx->attnum, ctx->tuphdr->t_bits))
1687 return true;
1688
1689 /* Skip non-varlena values, but update offset first */
1690 if (thisatt->attlen != -1)
1691 {
1692 ctx->offset = att_nominal_alignby(ctx->offset, thisatt->attalignby);
1693 ctx->offset = att_addlength_pointer(ctx->offset, thisatt->attlen,
1694 tp + ctx->offset);
1695 if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
1696 {
1698 psprintf("attribute with length %u ends at offset %u beyond total tuple length %u",
1699 thisatt->attlen,
1700 ctx->tuphdr->t_hoff + ctx->offset,
1701 ctx->lp_len));
1702 return false;
1703 }
1704 return true;
1705 }
1706
1707 /* Ok, we're looking at a varlena attribute. */
1708 ctx->offset = att_pointer_alignby(ctx->offset, thisatt->attalignby, -1,
1709 tp + ctx->offset);
1710
1711 /* Get the (possibly corrupt) varlena datum */
1712 attdatum = fetchatt(thisatt, tp + ctx->offset);
1713
1714 /*
1715 * We have the datum, but we cannot decode it carelessly, as it may still
1716 * be corrupt.
1717 */
1718
1719 /*
1720 * Check that VARTAG_SIZE won't hit an Assert on a corrupt va_tag before
1721 * risking a call into att_addlength_pointer
1722 */
1723 if (VARATT_IS_EXTERNAL(tp + ctx->offset))
1724 {
1725 uint8 va_tag = VARTAG_EXTERNAL(tp + ctx->offset);
1726
1727 if (va_tag != VARTAG_ONDISK)
1728 {
1730 psprintf("toasted attribute has unexpected TOAST tag %u",
1731 va_tag));
1732 /* We can't know where the next attribute begins */
1733 return false;
1734 }
1735 }
1736
1737 /* Ok, should be safe now */
1738 ctx->offset = att_addlength_pointer(ctx->offset, thisatt->attlen,
1739 tp + ctx->offset);
1740
1741 if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
1742 {
1744 psprintf("attribute with length %u ends at offset %u beyond total tuple length %u",
1745 thisatt->attlen,
1746 ctx->tuphdr->t_hoff + ctx->offset,
1747 ctx->lp_len));
1748
1749 return false;
1750 }
1751
1752 /*
1753 * heap_deform_tuple would be done with this attribute at this point,
1754 * having stored it in values[], and would continue to the next attribute.
1755 * We go further, because we need to check if the toast datum is corrupt.
1756 */
1757
1758 attr = (varlena *) DatumGetPointer(attdatum);
1759
1760 /*
1761 * Now we follow the logic of detoast_external_attr(), with the same
1762 * caveats about being paranoid about corruption.
1763 */
1764
1765 /* Skip values that are not external */
1766 if (!VARATT_IS_EXTERNAL(attr))
1767 return true;
1768
1769 /* It is external, and we're looking at a page on disk */
1770
1771 /*
1772 * Must copy attr into toast_pointer for alignment considerations
1773 */
1774 VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
1775
1776 /* Toasted attributes too large to be untoasted should never be stored */
1777 if (toast_pointer.va_rawsize > VARLENA_SIZE_LIMIT)
1779 psprintf("toast value %u rawsize %d exceeds limit %d",
1780 toast_pointer.va_valueid,
1781 toast_pointer.va_rawsize,
1783
1784 if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
1785 {
1787 bool valid = false;
1788
1789 /* Compressed attributes should have a valid compression method */
1790 cmid = TOAST_COMPRESS_METHOD(&toast_pointer);
1791 switch (cmid)
1792 {
1793 /* List of all valid compression method IDs */
1796 valid = true;
1797 break;
1798
1799 /* Recognized but invalid compression method ID */
1801 break;
1802
1803 /* Intentionally no default here */
1804 }
1805 if (!valid)
1807 psprintf("toast value %u has invalid compression method id %d",
1808 toast_pointer.va_valueid, cmid));
1809 }
1810
1811 /* The tuple header better claim to contain toasted values */
1812 if (!(infomask & HEAP_HASEXTERNAL))
1813 {
1815 psprintf("toast value %u is external but tuple header flag HEAP_HASEXTERNAL not set",
1816 toast_pointer.va_valueid));
1817 return true;
1818 }
1819
1820 /* The relation better have a toast table */
1821 if (!ctx->rel->rd_rel->reltoastrelid)
1822 {
1824 psprintf("toast value %u is external but relation has no toast relation",
1825 toast_pointer.va_valueid));
1826 return true;
1827 }
1828
1829 /* If we were told to skip toast checking, then we're done. */
1830 if (ctx->toast_rel == NULL)
1831 return true;
1832
1833 /*
1834 * If this tuple is eligible to be pruned, we cannot check the toast.
1835 * Otherwise, we push a copy of the toast tuple so we can check it after
1836 * releasing the main table buffer lock.
1837 */
1838 if (!ctx->tuple_could_be_pruned)
1839 {
1841
1843
1844 VARATT_EXTERNAL_GET_POINTER(ta->toast_pointer, attr);
1845 ta->blkno = ctx->blkno;
1846 ta->offnum = ctx->offnum;
1847 ta->attnum = ctx->attnum;
1849 }
1850
1851 return true;
1852}
uint8_t uint8
Definition c.h:616
uint16_t uint16
Definition c.h:617
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition detoast.h:22
#define palloc0_object(type)
Definition fe_memutils.h:75
#define HEAP_HASNULL
#define HEAP_HASEXTERNAL
List * lappend(List *list, void *datum)
Definition list.c:339
uint64_t Datum
Definition postgres.h:70
BlockNumber blkno
OffsetNumber offnum
HeapTupleHeader tuphdr
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Form_pg_class rd_rel
Definition rel.h:111
int32 va_rawsize
Definition varatt.h:34
Definition c.h:778
ToastCompressionId
@ TOAST_INVALID_COMPRESSION_ID
@ TOAST_LZ4_COMPRESSION_ID
@ TOAST_PGLZ_COMPRESSION_ID
#define TOAST_COMPRESS_METHOD(ptr)
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193
#define att_nominal_alignby(cur_offset, attalignby)
Definition tupmacs.h:411
static bool att_isnull(int ATT, const bits8 *BITS)
Definition tupmacs.h:28
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition tupmacs.h:431
#define att_pointer_alignby(cur_offset, attalignby, attlen, attptr)
Definition tupmacs.h:383
#define fetchatt(A, T)
Definition tupmacs.h:102
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
static vartag_external VARTAG_EXTERNAL(const void *PTR)
Definition varatt.h:326
@ VARTAG_ONDISK
Definition varatt.h:89
static bool VARATT_EXTERNAL_IS_COMPRESSED(varatt_external toast_pointer)
Definition varatt.h:536
#define VARLENA_SIZE_LIMIT

References att_addlength_pointer, att_isnull(), att_nominal_alignby, att_pointer_alignby, HeapCheckContext::attnum, HeapCheckContext::blkno, DatumGetPointer(), fb(), fetchatt, HEAP_HASEXTERNAL, HEAP_HASNULL, lappend(), HeapCheckContext::lp_len, HeapCheckContext::offnum, HeapCheckContext::offset, palloc0_object, psprintf(), RelationData::rd_rel, HeapCheckContext::rel, RelationGetDescr, report_corruption(), HeapTupleHeaderData::t_bits, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, TOAST_COMPRESS_METHOD, TOAST_INVALID_COMPRESSION_ID, TOAST_LZ4_COMPRESSION_ID, TOAST_PGLZ_COMPRESSION_ID, HeapCheckContext::toast_rel, HeapCheckContext::toasted_attributes, HeapCheckContext::tuphdr, HeapCheckContext::tuple_could_be_pruned, TupleDescCompactAttr(), varatt_external::va_rawsize, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED(), VARATT_IS_EXTERNAL(), VARLENA_SIZE_LIMIT, VARTAG_EXTERNAL(), and VARTAG_ONDISK.

Referenced by check_tuple().

◆ check_tuple_header()

static bool check_tuple_header ( HeapCheckContext ctx)
static

Definition at line 1000 of file verify_heapam.c.

1001{
1002 HeapTupleHeader tuphdr = ctx->tuphdr;
1003 uint16 infomask = tuphdr->t_infomask;
1005 bool result = true;
1006 unsigned expected_hoff;
1007
1008 if (ctx->tuphdr->t_hoff > ctx->lp_len)
1009 {
1011 psprintf("data begins at offset %u beyond the tuple length %u",
1012 ctx->tuphdr->t_hoff, ctx->lp_len));
1013 result = false;
1014 }
1015
1016 if ((ctx->tuphdr->t_infomask & HEAP_XMAX_COMMITTED) &&
1018 {
1020 pstrdup("multixact should not be marked committed"));
1021
1022 /*
1023 * This condition is clearly wrong, but it's not enough to justify
1024 * skipping further checks, because we don't rely on this to determine
1025 * whether the tuple is visible or to interpret other relevant header
1026 * fields.
1027 */
1028 }
1029
1032 {
1034 psprintf("tuple has been HOT updated, but xmax is 0"));
1035
1036 /*
1037 * As above, even though this shouldn't happen, it's not sufficient
1038 * justification for skipping further checks, we should still be able
1039 * to perform sensibly.
1040 */
1041 }
1042
1043 if (HeapTupleHeaderIsHeapOnly(tuphdr) &&
1044 ((tuphdr->t_infomask & HEAP_UPDATED) == 0))
1045 {
1047 psprintf("tuple is heap only, but not the result of an update"));
1048
1049 /* Here again, we can still perform further checks. */
1050 }
1051
1052 if (infomask & HEAP_HASNULL)
1054 else
1056 if (ctx->tuphdr->t_hoff != expected_hoff)
1057 {
1058 if ((infomask & HEAP_HASNULL) && ctx->natts == 1)
1060 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, has nulls)",
1061 expected_hoff, ctx->tuphdr->t_hoff));
1062 else if ((infomask & HEAP_HASNULL))
1064 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, has nulls)",
1065 expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
1066 else if (ctx->natts == 1)
1068 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, no nulls)",
1069 expected_hoff, ctx->tuphdr->t_hoff));
1070 else
1072 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, no nulls)",
1073 expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
1074 result = false;
1075 }
1076
1077 return result;
1078}
#define MAXALIGN(LEN)
Definition c.h:898
uint32 TransactionId
Definition c.h:738
#define SizeofHeapTupleHeader
static int BITMAPLEN(int NATTS)
static bool HeapTupleHeaderIsHeapOnly(const HeapTupleHeaderData *tup)
#define HEAP_XMAX_IS_MULTI
#define HEAP_XMAX_COMMITTED
static bool HeapTupleHeaderIsHotUpdated(const HeapTupleHeaderData *tup)
static TransactionId HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
#define HEAP_UPDATED
char * pstrdup(const char *in)
Definition mcxt.c:1781

References BITMAPLEN(), fb(), HEAP_HASNULL, HEAP_UPDATED, HEAP_XMAX_COMMITTED, HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderIsHeapOnly(), HeapTupleHeaderIsHotUpdated(), HeapCheckContext::lp_len, MAXALIGN, HeapCheckContext::natts, psprintf(), pstrdup(), report_corruption(), SizeofHeapTupleHeader, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, TransactionIdIsValid, and HeapCheckContext::tuphdr.

Referenced by check_tuple().

◆ check_tuple_visibility()

static bool check_tuple_visibility ( HeapCheckContext ctx,
bool xmin_commit_status_ok,
XidCommitStatus xmin_commit_status 
)
static

Definition at line 1113 of file verify_heapam.c.

1115{
1116 TransactionId xmin;
1118 TransactionId xmax;
1122 HeapTupleHeader tuphdr = ctx->tuphdr;
1123
1124 ctx->tuple_could_be_pruned = true; /* have not yet proven otherwise */
1125 *xmin_commit_status_ok = false; /* have not yet proven otherwise */
1126
1127 /* If xmin is normal, it should be within valid range */
1128 xmin = HeapTupleHeaderGetXmin(tuphdr);
1129 switch (get_xid_status(xmin, ctx, &xmin_status))
1130 {
1131 case XID_INVALID:
1132 /* Could be the result of a speculative insertion that aborted. */
1133 return false;
1134 case XID_BOUNDS_OK:
1135 *xmin_commit_status_ok = true;
1137 break;
1138 case XID_IN_FUTURE:
1140 psprintf("xmin %u equals or exceeds next valid transaction ID %u:%u",
1141 xmin,
1144 return false;
1147 psprintf("xmin %u precedes oldest valid transaction ID %u:%u",
1148 xmin,
1151 return false;
1154 psprintf("xmin %u precedes relation freeze threshold %u:%u",
1155 xmin,
1158 return false;
1159 }
1160
1161 /*
1162 * Has inserting transaction committed?
1163 */
1164 if (!HeapTupleHeaderXminCommitted(tuphdr))
1165 {
1166 if (HeapTupleHeaderXminInvalid(tuphdr))
1167 return false; /* inserter aborted, don't check */
1168 /* Used by pre-9.0 binary upgrades */
1169 else if (tuphdr->t_infomask & HEAP_MOVED_OFF)
1170 {
1171 xvac = HeapTupleHeaderGetXvac(tuphdr);
1172
1173 switch (get_xid_status(xvac, ctx, &xvac_status))
1174 {
1175 case XID_INVALID:
1177 pstrdup("old-style VACUUM FULL transaction ID for moved off tuple is invalid"));
1178 return false;
1179 case XID_IN_FUTURE:
1181 psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple equals or exceeds next valid transaction ID %u:%u",
1182 xvac,
1185 return false;
1188 psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple precedes relation freeze threshold %u:%u",
1189 xvac,
1192 return false;
1195 psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple precedes oldest valid transaction ID %u:%u",
1196 xvac,
1199 return false;
1200 case XID_BOUNDS_OK:
1201 break;
1202 }
1203
1204 switch (xvac_status)
1205 {
1206 case XID_IS_CURRENT_XID:
1208 psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple matches our current transaction ID",
1209 xvac));
1210 return false;
1211 case XID_IN_PROGRESS:
1213 psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple appears to be in progress",
1214 xvac));
1215 return false;
1216
1217 case XID_COMMITTED:
1218
1219 /*
1220 * The tuple is dead, because the xvac transaction moved
1221 * it off and committed. It's checkable, but also
1222 * prunable.
1223 */
1224 return true;
1225
1226 case XID_ABORTED:
1227
1228 /*
1229 * The original xmin must have committed, because the xvac
1230 * transaction tried to move it later. Since xvac is
1231 * aborted, whether it's still alive now depends on the
1232 * status of xmax.
1233 */
1234 break;
1235 }
1236 }
1237 /* Used by pre-9.0 binary upgrades */
1238 else if (tuphdr->t_infomask & HEAP_MOVED_IN)
1239 {
1240 xvac = HeapTupleHeaderGetXvac(tuphdr);
1241
1242 switch (get_xid_status(xvac, ctx, &xvac_status))
1243 {
1244 case XID_INVALID:
1246 pstrdup("old-style VACUUM FULL transaction ID for moved in tuple is invalid"));
1247 return false;
1248 case XID_IN_FUTURE:
1250 psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple equals or exceeds next valid transaction ID %u:%u",
1251 xvac,
1254 return false;
1257 psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple precedes relation freeze threshold %u:%u",
1258 xvac,
1261 return false;
1264 psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple precedes oldest valid transaction ID %u:%u",
1265 xvac,
1268 return false;
1269 case XID_BOUNDS_OK:
1270 break;
1271 }
1272
1273 switch (xvac_status)
1274 {
1275 case XID_IS_CURRENT_XID:
1277 psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple matches our current transaction ID",
1278 xvac));
1279 return false;
1280 case XID_IN_PROGRESS:
1282 psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple appears to be in progress",
1283 xvac));
1284 return false;
1285
1286 case XID_COMMITTED:
1287
1288 /*
1289 * The original xmin must have committed, because the xvac
1290 * transaction moved it later. Whether it's still alive
1291 * now depends on the status of xmax.
1292 */
1293 break;
1294
1295 case XID_ABORTED:
1296
1297 /*
1298 * The tuple is dead, because the xvac transaction moved
1299 * it off and committed. It's checkable, but also
1300 * prunable.
1301 */
1302 return true;
1303 }
1304 }
1305 else if (xmin_status != XID_COMMITTED)
1306 {
1307 /*
1308 * Inserting transaction is not in progress, and not committed, so
1309 * it might have changed the TupleDesc in ways we don't know
1310 * about. Thus, don't try to check the tuple structure.
1311 *
1312 * If xmin_status happens to be XID_IS_CURRENT_XID, then in theory
1313 * any such DDL changes ought to be visible to us, so perhaps we
1314 * could check anyway in that case. But, for now, let's be
1315 * conservative and treat this like any other uncommitted insert.
1316 */
1317 return false;
1318 }
1319 }
1320
1321 /*
1322 * Okay, the inserter committed, so it was good at some point. Now what
1323 * about the deleting transaction?
1324 */
1325
1326 if (tuphdr->t_infomask & HEAP_XMAX_IS_MULTI)
1327 {
1328 /*
1329 * xmax is a multixact, so sanity-check the MXID. Note that we do this
1330 * prior to checking for HEAP_XMAX_INVALID or
1331 * HEAP_XMAX_IS_LOCKED_ONLY. This might therefore complain about
1332 * things that wouldn't actually be a problem during a normal scan,
1333 * but eventually we're going to have to freeze, and that process will
1334 * ignore hint bits.
1335 *
1336 * Even if the MXID is out of range, we still know that the original
1337 * insert committed, so we can check the tuple itself. However, we
1338 * can't rule out the possibility that this tuple is dead, so don't
1339 * clear ctx->tuple_could_be_pruned. Possibly we should go ahead and
1340 * clear that flag anyway if HEAP_XMAX_INVALID is set or if
1341 * HEAP_XMAX_IS_LOCKED_ONLY is true, but for now we err on the side of
1342 * avoiding possibly-bogus complaints about missing TOAST entries.
1343 */
1344 xmax = HeapTupleHeaderGetRawXmax(tuphdr);
1345 switch (check_mxid_valid_in_rel(xmax, ctx))
1346 {
1347 case XID_INVALID:
1349 pstrdup("multitransaction ID is invalid"));
1350 return true;
1353 psprintf("multitransaction ID %u precedes relation minimum multitransaction ID threshold %u",
1354 xmax, ctx->relminmxid));
1355 return true;
1358 psprintf("multitransaction ID %u precedes oldest valid multitransaction ID threshold %u",
1359 xmax, ctx->oldest_mxact));
1360 return true;
1361 case XID_IN_FUTURE:
1363 psprintf("multitransaction ID %u equals or exceeds next valid multitransaction ID %u",
1364 xmax,
1365 ctx->next_mxact));
1366 return true;
1367 case XID_BOUNDS_OK:
1368 break;
1369 }
1370 }
1371
1372 if (tuphdr->t_infomask & HEAP_XMAX_INVALID)
1373 {
1374 /*
1375 * This tuple is live. A concurrently running transaction could
1376 * delete it before we get around to checking the toast, but any such
1377 * running transaction is surely not less than our safe_xmin, so the
1378 * toast cannot be vacuumed out from under us.
1379 */
1380 ctx->tuple_could_be_pruned = false;
1381 return true;
1382 }
1383
1385 {
1386 /*
1387 * "Deleting" xact really only locked it, so the tuple is live in any
1388 * case. As above, a concurrently running transaction could delete
1389 * it, but it cannot be vacuumed out from under us.
1390 */
1391 ctx->tuple_could_be_pruned = false;
1392 return true;
1393 }
1394
1395 if (tuphdr->t_infomask & HEAP_XMAX_IS_MULTI)
1396 {
1397 /*
1398 * We already checked above that this multixact is within limits for
1399 * this table. Now check the update xid from this multixact.
1400 */
1401 xmax = HeapTupleGetUpdateXid(tuphdr);
1402 switch (get_xid_status(xmax, ctx, &xmax_status))
1403 {
1404 case XID_INVALID:
1405 /* not LOCKED_ONLY, so it has to have an xmax */
1407 pstrdup("update xid is invalid"));
1408 return true;
1409 case XID_IN_FUTURE:
1411 psprintf("update xid %u equals or exceeds next valid transaction ID %u:%u",
1412 xmax,
1415 return true;
1418 psprintf("update xid %u precedes relation freeze threshold %u:%u",
1419 xmax,
1422 return true;
1425 psprintf("update xid %u precedes oldest valid transaction ID %u:%u",
1426 xmax,
1429 return true;
1430 case XID_BOUNDS_OK:
1431 break;
1432 }
1433
1434 switch (xmax_status)
1435 {
1436 case XID_IS_CURRENT_XID:
1437 case XID_IN_PROGRESS:
1438
1439 /*
1440 * The delete is in progress, so it cannot be visible to our
1441 * snapshot.
1442 */
1443 ctx->tuple_could_be_pruned = false;
1444 break;
1445 case XID_COMMITTED:
1446
1447 /*
1448 * The delete committed. Whether the toast can be vacuumed
1449 * away depends on how old the deleting transaction is.
1450 */
1452 ctx->safe_xmin);
1453 break;
1454 case XID_ABORTED:
1455
1456 /*
1457 * The delete aborted or crashed. The tuple is still live.
1458 */
1459 ctx->tuple_could_be_pruned = false;
1460 break;
1461 }
1462
1463 /* Tuple itself is checkable even if it's dead. */
1464 return true;
1465 }
1466
1467 /* xmax is an XID, not a MXID. Sanity check it. */
1468 xmax = HeapTupleHeaderGetRawXmax(tuphdr);
1469 switch (get_xid_status(xmax, ctx, &xmax_status))
1470 {
1471 case XID_INVALID:
1472 ctx->tuple_could_be_pruned = false;
1473 return true;
1474 case XID_IN_FUTURE:
1476 psprintf("xmax %u equals or exceeds next valid transaction ID %u:%u",
1477 xmax,
1480 return false; /* corrupt */
1483 psprintf("xmax %u precedes relation freeze threshold %u:%u",
1484 xmax,
1487 return false; /* corrupt */
1490 psprintf("xmax %u precedes oldest valid transaction ID %u:%u",
1491 xmax,
1494 return false; /* corrupt */
1495 case XID_BOUNDS_OK:
1496 break;
1497 }
1498
1499 /*
1500 * Whether the toast can be vacuumed away depends on how old the deleting
1501 * transaction is.
1502 */
1503 switch (xmax_status)
1504 {
1505 case XID_IS_CURRENT_XID:
1506 case XID_IN_PROGRESS:
1507
1508 /*
1509 * The delete is in progress, so it cannot be visible to our
1510 * snapshot.
1511 */
1512 ctx->tuple_could_be_pruned = false;
1513 break;
1514
1515 case XID_COMMITTED:
1516
1517 /*
1518 * The delete committed. Whether the toast can be vacuumed away
1519 * depends on how old the deleting transaction is.
1520 */
1522 ctx->safe_xmin);
1523 break;
1524
1525 case XID_ABORTED:
1526
1527 /*
1528 * The delete aborted or crashed. The tuple is still live.
1529 */
1530 ctx->tuple_could_be_pruned = false;
1531 break;
1532 }
1533
1534 /* Tuple itself is checkable even if it's dead. */
1535 return true;
1536}
TransactionId HeapTupleGetUpdateXid(const HeapTupleHeaderData *tup)
Definition heapam.c:7679
#define HEAP_MOVED_OFF
static bool HEAP_XMAX_IS_LOCKED_ONLY(uint16 infomask)
static bool HeapTupleHeaderXminInvalid(const HeapTupleHeaderData *tup)
static TransactionId HeapTupleHeaderGetXvac(const HeapTupleHeaderData *tup)
#define HEAP_MOVED_IN
static TransactionId HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
static TransactionId HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
#define HEAP_XMAX_INVALID
static bool HeapTupleHeaderXminCommitted(const HeapTupleHeaderData *tup)
FullTransactionId oldest_fxid
FullTransactionId next_fxid
FullTransactionId relfrozenfxid
TransactionId safe_xmin
#define EpochFromFullTransactionId(x)
Definition transam.h:47
#define XidFromFullTransactionId(x)
Definition transam.h:48
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
static XidBoundsViolation check_mxid_valid_in_rel(MultiXactId mxid, HeapCheckContext *ctx)
static XidBoundsViolation get_xid_status(TransactionId xid, HeapCheckContext *ctx, XidCommitStatus *status)

References check_mxid_valid_in_rel(), EpochFromFullTransactionId, fb(), get_xid_status(), HEAP_MOVED_IN, HEAP_MOVED_OFF, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HeapTupleGetUpdateXid(), HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetXmin(), HeapTupleHeaderGetXvac(), HeapTupleHeaderXminCommitted(), HeapTupleHeaderXminInvalid(), HeapCheckContext::next_fxid, HeapCheckContext::next_mxact, HeapCheckContext::oldest_fxid, HeapCheckContext::oldest_mxact, psprintf(), pstrdup(), HeapCheckContext::relfrozenfxid, HeapCheckContext::relminmxid, report_corruption(), HeapCheckContext::safe_xmin, HeapTupleHeaderData::t_infomask, TransactionIdPrecedes(), HeapCheckContext::tuphdr, HeapCheckContext::tuple_could_be_pruned, XID_ABORTED, XID_BOUNDS_OK, XID_COMMITTED, XID_IN_FUTURE, XID_IN_PROGRESS, XID_INVALID, XID_IS_CURRENT_XID, XID_PRECEDES_CLUSTERMIN, XID_PRECEDES_RELMIN, and XidFromFullTransactionId.

Referenced by check_tuple().

◆ FullTransactionIdFromXidAndCtx()

static FullTransactionId FullTransactionIdFromXidAndCtx ( TransactionId  xid,
const HeapCheckContext ctx 
)
static

Definition at line 1978 of file verify_heapam.c.

1979{
1981 int32 diff;
1982 FullTransactionId fxid;
1983
1987
1988 if (!TransactionIdIsNormal(xid))
1989 return FullTransactionIdFromEpochAndXid(0, xid);
1990
1992
1993 /* compute the 32bit modulo difference */
1994 diff = (int32) (ctx->next_xid - xid);
1995
1996 /*
1997 * In cases of corruption we might see a 32bit xid that is before epoch 0.
1998 * We can't represent that as a 64bit xid, due to 64bit xids being
1999 * unsigned integers, without the modulo arithmetic of 32bit xid. There's
2000 * no really nice way to deal with that, but it works ok enough to use
2001 * FirstNormalFullTransactionId in that case, as a freshly initdb'd
2002 * cluster already has a newer horizon.
2003 */
2005 {
2008 }
2009 else
2011
2013 return fxid;
2014}
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
uint64_t uint64
Definition c.h:619
TransactionId next_xid
#define FullTransactionIdIsNormal(x)
Definition transam.h:58
#define U64FromFullTransactionId(x)
Definition transam.h:49
static FullTransactionId FullTransactionIdFromU64(uint64 value)
Definition transam.h:81
#define FirstNormalTransactionId
Definition transam.h:34
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition transam.h:71
#define TransactionIdIsNormal(xid)
Definition transam.h:42
#define FirstNormalFullTransactionId
Definition transam.h:57

References Assert, EpochFromFullTransactionId, fb(), FirstNormalFullTransactionId, FirstNormalTransactionId, FullTransactionIdFromEpochAndXid(), FullTransactionIdFromU64(), FullTransactionIdIsNormal, HeapCheckContext::next_fxid, HeapCheckContext::next_xid, TransactionIdIsNormal, U64FromFullTransactionId, and XidFromFullTransactionId.

Referenced by get_xid_status(), update_cached_xid_range(), and verify_heapam().

◆ fxid_in_cached_range()

static bool fxid_in_cached_range ( FullTransactionId  fxid,
const HeapCheckContext ctx 
)
inlinestatic

Definition at line 2047 of file verify_heapam.c.

2048{
2049 return (FullTransactionIdPrecedesOrEquals(ctx->oldest_fxid, fxid) &&
2051}
#define FullTransactionIdPrecedesOrEquals(a, b)
Definition transam.h:52
#define FullTransactionIdPrecedes(a, b)
Definition transam.h:51

References FullTransactionIdPrecedes, FullTransactionIdPrecedesOrEquals, HeapCheckContext::next_fxid, and HeapCheckContext::oldest_fxid.

Referenced by get_xid_status().

◆ get_xid_status()

static XidBoundsViolation get_xid_status ( TransactionId  xid,
HeapCheckContext ctx,
XidCommitStatus status 
)
static

Definition at line 2112 of file verify_heapam.c.

2114{
2115 FullTransactionId fxid;
2117
2118 /* Quick check for special xids */
2119 if (!TransactionIdIsValid(xid))
2120 return XID_INVALID;
2121 else if (xid == BootstrapTransactionId || xid == FrozenTransactionId)
2122 {
2123 if (status != NULL)
2124 *status = XID_COMMITTED;
2125 return XID_BOUNDS_OK;
2126 }
2127
2128 /* Check if the xid is within bounds */
2129 fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
2130 if (!fxid_in_cached_range(fxid, ctx))
2131 {
2132 /*
2133 * We may have been checking against stale values. Update the cached
2134 * range to be sure, and since we relied on the cached range when we
2135 * performed the full xid conversion, reconvert.
2136 */
2138 fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
2139 }
2140
2142 return XID_IN_FUTURE;
2143 if (FullTransactionIdPrecedes(fxid, ctx->oldest_fxid))
2146 return XID_PRECEDES_RELMIN;
2147
2148 /* Early return if the caller does not request clog checking */
2149 if (status == NULL)
2150 return XID_BOUNDS_OK;
2151
2152 /* Early return if we just checked this xid in a prior call */
2153 if (xid == ctx->cached_xid)
2154 {
2155 *status = ctx->cached_status;
2156 return XID_BOUNDS_OK;
2157 }
2158
2159 *status = XID_COMMITTED;
2161 clog_horizon =
2163 ctx);
2165 {
2167 *status = XID_IS_CURRENT_XID;
2168 else if (TransactionIdIsInProgress(xid))
2169 *status = XID_IN_PROGRESS;
2170 else if (TransactionIdDidCommit(xid))
2171 *status = XID_COMMITTED;
2172 else
2173 *status = XID_ABORTED;
2174 }
2176 ctx->cached_xid = xid;
2177 ctx->cached_status = *status;
2178 return XID_BOUNDS_OK;
2179}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
@ LW_SHARED
Definition lwlock.h:113
bool TransactionIdIsInProgress(TransactionId xid)
Definition procarray.c:1401
TransactionId cached_xid
XidCommitStatus cached_status
TransactionId oldestClogXid
Definition transam.h:253
bool TransactionIdDidCommit(TransactionId transactionId)
Definition transam.c:126
#define FrozenTransactionId
Definition transam.h:33
#define BootstrapTransactionId
Definition transam.h:32
TransamVariablesData * TransamVariables
Definition varsup.c:34
static bool fxid_in_cached_range(FullTransactionId fxid, const HeapCheckContext *ctx)
static void update_cached_xid_range(HeapCheckContext *ctx)
static FullTransactionId FullTransactionIdFromXidAndCtx(TransactionId xid, const HeapCheckContext *ctx)
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition xact.c:943

References BootstrapTransactionId, HeapCheckContext::cached_status, HeapCheckContext::cached_xid, fb(), FrozenTransactionId, FullTransactionIdFromXidAndCtx(), FullTransactionIdPrecedes, FullTransactionIdPrecedesOrEquals, fxid_in_cached_range(), LW_SHARED, LWLockAcquire(), LWLockRelease(), HeapCheckContext::next_fxid, HeapCheckContext::oldest_fxid, TransamVariablesData::oldestClogXid, HeapCheckContext::relfrozenfxid, TransactionIdDidCommit(), TransactionIdIsCurrentTransactionId(), TransactionIdIsInProgress(), TransactionIdIsValid, TransamVariables, update_cached_xid_range(), XID_ABORTED, XID_BOUNDS_OK, XID_COMMITTED, XID_IN_FUTURE, XID_IN_PROGRESS, XID_INVALID, XID_IS_CURRENT_XID, XID_PRECEDES_CLUSTERMIN, and XID_PRECEDES_RELMIN.

Referenced by check_tuple_visibility().

◆ heapcheck_read_stream_next_unskippable()

static BlockNumber heapcheck_read_stream_next_unskippable ( ReadStream stream,
void callback_private_data,
void per_buffer_data 
)
static

Definition at line 882 of file verify_heapam.c.

885{
886 HeapCheckReadStreamData *p = callback_private_data;
887
888 /* Loops over [current_blocknum, last_exclusive) blocks */
890 {
892
894 {
896 continue;
897 }
898
900 {
902 continue;
903 }
904
905 return i;
906 }
907
908 return InvalidBlockNumber;
909}
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
int i
Definition isn.c:77
BlockRangeReadStreamPrivate range
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
#define VISIBILITYMAP_ALL_FROZEN
#define VISIBILITYMAP_ALL_VISIBLE

References BlockRangeReadStreamPrivate::current_blocknum, fb(), i, InvalidBlockNumber, BlockRangeReadStreamPrivate::last_exclusive, HeapCheckReadStreamData::range, HeapCheckReadStreamData::rel, HeapCheckReadStreamData::skip_option, SKIP_PAGES_ALL_FROZEN, SKIP_PAGES_ALL_VISIBLE, VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, visibilitymap_get_status(), and HeapCheckReadStreamData::vmbuffer.

Referenced by verify_heapam().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( verify_heapam  )

◆ report_corruption()

static void report_corruption ( HeapCheckContext ctx,
char msg 
)
static

Definition at line 952 of file verify_heapam.c.

953{
955 ctx->offnum, ctx->attnum, msg);
956 ctx->is_corrupt = true;
957}
Tuplestorestate * tupstore
static void report_corruption_internal(Tuplestorestate *tupstore, TupleDesc tupdesc, BlockNumber blkno, OffsetNumber offnum, AttrNumber attnum, char *msg)

References HeapCheckContext::attnum, HeapCheckContext::blkno, HeapCheckContext::is_corrupt, HeapCheckContext::offnum, report_corruption_internal(), HeapCheckContext::tupdesc, and HeapCheckContext::tupstore.

Referenced by check_tuple(), check_tuple_attribute(), check_tuple_header(), check_tuple_visibility(), and verify_heapam().

◆ report_corruption_internal()

static void report_corruption_internal ( Tuplestorestate tupstore,
TupleDesc  tupdesc,
BlockNumber  blkno,
OffsetNumber  offnum,
AttrNumber  attnum,
char msg 
)
static

Definition at line 916 of file verify_heapam.c.

919{
921 bool nulls[HEAPCHECK_RELATION_COLS] = {0};
922 HeapTuple tuple;
923
924 values[0] = Int64GetDatum(blkno);
925 values[1] = Int32GetDatum(offnum);
927 nulls[2] = (attnum < 0);
928 values[3] = CStringGetTextDatum(msg);
929
930 /*
931 * In principle, there is nothing to prevent a scan over a large, highly
932 * corrupted table from using work_mem worth of memory building up the
933 * tuplestore. That's ok, but if we also leak the msg argument memory
934 * until the end of the query, we could exceed work_mem by more than a
935 * trivial amount. Therefore, free the msg argument each time we are
936 * called rather than waiting for our current memory context to be freed.
937 */
938 pfree(msg);
939
940 tuple = heap_form_tuple(tupdesc, values, nulls);
941 tuplestore_puttuple(tupstore, tuple);
942}
static Datum values[MAXATTR]
Definition bootstrap.c:188
#define CStringGetTextDatum(s)
Definition builtins.h:98
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1037
void pfree(void *pointer)
Definition mcxt.c:1616
int16 attnum
static Datum Int64GetDatum(int64 X)
Definition postgres.h:413
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition tuplestore.c:765
#define HEAPCHECK_RELATION_COLS

References attnum, CStringGetTextDatum, heap_form_tuple(), HEAPCHECK_RELATION_COLS, Int32GetDatum(), Int64GetDatum(), pfree(), tuplestore_puttuple(), and values.

Referenced by report_corruption(), and report_toast_corruption().

◆ report_toast_corruption()

static void report_toast_corruption ( HeapCheckContext ctx,
ToastedAttribute ta,
char msg 
)
static

Definition at line 968 of file verify_heapam.c.

970{
972 ta->offnum, ta->attnum, msg);
973 ctx->is_corrupt = true;
974}

References fb(), HeapCheckContext::is_corrupt, report_corruption_internal(), HeapCheckContext::tupdesc, and HeapCheckContext::tupstore.

Referenced by check_toast_tuple(), and check_toasted_attribute().

◆ update_cached_mxid_range()

static void update_cached_mxid_range ( HeapCheckContext ctx)
static

Definition at line 2037 of file verify_heapam.c.

2038{
2040}
void ReadMultiXactIdRange(MultiXactId *oldest, MultiXactId *next)
Definition multixact.c:685

References HeapCheckContext::next_mxact, HeapCheckContext::oldest_mxact, and ReadMultiXactIdRange().

Referenced by check_mxid_valid_in_rel(), and verify_heapam().

◆ update_cached_xid_range()

◆ verify_heapam()

Datum verify_heapam ( PG_FUNCTION_ARGS  )

Definition at line 251 of file verify_heapam.c.

252{
253 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
255 Buffer vmbuffer = InvalidBuffer;
256 Oid relid;
257 bool on_error_stop;
258 bool check_toast;
259 SkipPages skip_option = SKIP_PAGES_NONE;
262 BlockNumber nblocks;
263 const char *skip;
264 ReadStream *stream;
265 int stream_flags;
267 void *stream_data;
269
270 /* Check supplied arguments */
271 if (PG_ARGISNULL(0))
274 errmsg("relation cannot be null")));
275 relid = PG_GETARG_OID(0);
276
277 if (PG_ARGISNULL(1))
280 errmsg("on_error_stop cannot be null")));
281 on_error_stop = PG_GETARG_BOOL(1);
282
283 if (PG_ARGISNULL(2))
286 errmsg("check_toast cannot be null")));
288
289 if (PG_ARGISNULL(3))
292 errmsg("skip cannot be null")));
294 if (pg_strcasecmp(skip, "all-visible") == 0)
295 skip_option = SKIP_PAGES_ALL_VISIBLE;
296 else if (pg_strcasecmp(skip, "all-frozen") == 0)
297 skip_option = SKIP_PAGES_ALL_FROZEN;
298 else if (pg_strcasecmp(skip, "none") == 0)
299 skip_option = SKIP_PAGES_NONE;
300 else
303 errmsg("invalid skip option"),
304 errhint("Valid skip options are \"all-visible\", \"all-frozen\", and \"none\".")));
305
306 memset(&ctx, 0, sizeof(HeapCheckContext));
309
310 /*
311 * Any xmin newer than the xmin of our snapshot can't become all-visible
312 * while we're running.
313 */
315
316 /*
317 * If we report corruption when not examining some individual attribute,
318 * we need attnum to be reported as NULL. Set that up before any
319 * corruption reporting might happen.
320 */
321 ctx.attnum = -1;
322
323 /* Construct the tuplestore and tuple descriptor */
324 InitMaterializedSRF(fcinfo, 0);
325 ctx.tupdesc = rsinfo->setDesc;
326 ctx.tupstore = rsinfo->setResult;
327
328 /* Open relation, check relkind and access method */
329 ctx.rel = relation_open(relid, AccessShareLock);
330
331 /*
332 * Check that a relation's relkind and access method are both supported.
333 */
334 if (!RELKIND_HAS_TABLE_AM(ctx.rel->rd_rel->relkind) &&
335 ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE)
338 errmsg("cannot check relation \"%s\"",
341
342 /*
343 * Sequences always use heap AM, but they don't show that in the catalogs.
344 * Other relkinds might be using a different AM, so check.
345 */
346 if (ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
347 ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
350 errmsg("only heap AM is supported")));
351
352 /*
353 * Early exit for unlogged relations during recovery. These will have no
354 * relation fork, so there won't be anything to check. We behave as if
355 * the relation is empty.
356 */
357 if (ctx.rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
359 {
362 errmsg("cannot verify unlogged relation \"%s\" during recovery, skipping",
366 }
367
368 /* Early exit if the relation is empty */
369 nblocks = RelationGetNumberOfBlocks(ctx.rel);
370 if (!nblocks)
371 {
374 }
375
377 ctx.buffer = InvalidBuffer;
378 ctx.page = NULL;
379
380 /* Validate block numbers, or handle nulls. */
381 if (PG_ARGISNULL(4))
382 first_block = 0;
383 else
384 {
386
387 if (fb < 0 || fb >= nblocks)
390 errmsg("starting block number must be between 0 and %u",
391 nblocks - 1)));
393 }
394 if (PG_ARGISNULL(5))
395 last_block = nblocks - 1;
396 else
397 {
398 int64 lb = PG_GETARG_INT64(5);
399
400 if (lb < 0 || lb >= nblocks)
403 errmsg("ending block number must be between 0 and %u",
404 nblocks - 1)));
405 last_block = (BlockNumber) lb;
406 }
407
408 /* Optionally open the toast relation, if any. */
409 if (ctx.rel->rd_rel->reltoastrelid && check_toast)
410 {
411 int offset;
412
413 /* Main relation has associated toast relation */
414 ctx.toast_rel = table_open(ctx.rel->rd_rel->reltoastrelid,
416 offset = toast_open_indexes(ctx.toast_rel,
418 &(ctx.toast_indexes),
419 &(ctx.num_toast_indexes));
420 ctx.valid_toast_index = ctx.toast_indexes[offset];
421 }
422 else
423 {
424 /*
425 * Main relation has no associated toast relation, or we're
426 * intentionally skipping it.
427 */
428 ctx.toast_rel = NULL;
429 ctx.toast_indexes = NULL;
430 ctx.num_toast_indexes = 0;
431 }
432
435 ctx.relfrozenxid = ctx.rel->rd_rel->relfrozenxid;
437 ctx.relminmxid = ctx.rel->rd_rel->relminmxid;
438
440 ctx.oldest_xid = ctx.relfrozenxid;
441
442 /* Now that `ctx` is set up, set up the read stream */
443 stream_skip_data.range.current_blocknum = first_block;
444 stream_skip_data.range.last_exclusive = last_block + 1;
445 stream_skip_data.skip_option = skip_option;
446 stream_skip_data.rel = ctx.rel;
447 stream_skip_data.vmbuffer = &vmbuffer;
448
449 if (skip_option == SKIP_PAGES_NONE)
450 {
451 /*
452 * It is safe to use batchmode as block_range_read_stream_cb takes no
453 * locks.
454 */
460 }
461 else
462 {
463 /*
464 * It would not be safe to naively use batchmode, as
465 * heapcheck_read_stream_next_unskippable takes locks. It shouldn't be
466 * too hard to convert though.
467 */
471 }
472
474 ctx.bstrategy,
475 ctx.rel,
477 stream_cb,
479 0);
480
481 while ((ctx.buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
482 {
483 OffsetNumber maxoff;
489
491
493
494 /* Lock the next page. */
497
499 ctx.page = BufferGetPage(ctx.buffer);
500
501 /* Perform tuple checks */
502 maxoff = PageGetMaxOffsetNumber(ctx.page);
503 for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
504 ctx.offnum = OffsetNumberNext(ctx.offnum))
505 {
506 BlockNumber nextblkno;
508
510 lp_valid[ctx.offnum] = false;
511 xmin_commit_status_ok[ctx.offnum] = false;
512 ctx.itemid = PageGetItemId(ctx.page, ctx.offnum);
513
514 /* Skip over unused/dead line pointers */
515 if (!ItemIdIsUsed(ctx.itemid) || ItemIdIsDead(ctx.itemid))
516 continue;
517
518 /*
519 * If this line pointer has been redirected, check that it
520 * redirects to a valid offset within the line pointer array
521 */
522 if (ItemIdIsRedirected(ctx.itemid))
523 {
526
528 {
530 psprintf("line pointer redirection to item at offset %d precedes minimum offset %d",
531 rdoffnum,
533 continue;
534 }
535 if (rdoffnum > maxoff)
536 {
538 psprintf("line pointer redirection to item at offset %d exceeds maximum offset %d",
539 rdoffnum,
540 maxoff));
541 continue;
542 }
543
544 /*
545 * Since we've checked that this redirect points to a line
546 * pointer between FirstOffsetNumber and maxoff, it should now
547 * be safe to fetch the referenced line pointer. We expect it
548 * to be LP_NORMAL; if not, that's corruption.
549 */
551 if (!ItemIdIsUsed(rditem))
552 {
554 psprintf("redirected line pointer points to an unused item at offset %d",
555 rdoffnum));
556 continue;
557 }
558 else if (ItemIdIsDead(rditem))
559 {
561 psprintf("redirected line pointer points to a dead item at offset %d",
562 rdoffnum));
563 continue;
564 }
565 else if (ItemIdIsRedirected(rditem))
566 {
568 psprintf("redirected line pointer points to another redirected line pointer at offset %d",
569 rdoffnum));
570 continue;
571 }
572
573 /*
574 * Record the fact that this line pointer has passed basic
575 * sanity checking, and also the offset number to which it
576 * points.
577 */
578 lp_valid[ctx.offnum] = true;
580 continue;
581 }
582
583 /* Sanity-check the line pointer's offset and length values */
584 ctx.lp_len = ItemIdGetLength(ctx.itemid);
585 ctx.lp_off = ItemIdGetOffset(ctx.itemid);
586
587 if (ctx.lp_off != MAXALIGN(ctx.lp_off))
588 {
590 psprintf("line pointer to page offset %u is not maximally aligned",
591 ctx.lp_off));
592 continue;
593 }
595 {
597 psprintf("line pointer length %u is less than the minimum tuple header size %u",
598 ctx.lp_len,
599 (unsigned) MAXALIGN(SizeofHeapTupleHeader)));
600 continue;
601 }
602 if (ctx.lp_off + ctx.lp_len > BLCKSZ)
603 {
605 psprintf("line pointer to page offset %u with length %u ends beyond maximum page offset %d",
606 ctx.lp_off,
607 ctx.lp_len,
608 BLCKSZ));
609 continue;
610 }
611
612 /* It should be safe to examine the tuple's header, at least */
613 lp_valid[ctx.offnum] = true;
614 ctx.tuphdr = (HeapTupleHeader) PageGetItem(ctx.page, ctx.itemid);
616
617 /* Ok, ready to check this next tuple */
618 check_tuple(&ctx,
621
622 /*
623 * If the CTID field of this tuple seems to point to another tuple
624 * on the same page, record that tuple as the successor of this
625 * one.
626 */
627 nextblkno = ItemPointerGetBlockNumber(&(ctx.tuphdr)->t_ctid);
629 if (nextblkno == ctx.blkno && nextoffnum != ctx.offnum &&
632 }
633
634 /*
635 * Update chain validation. Check each line pointer that's got a valid
636 * successor against that successor.
637 */
638 ctx.attnum = -1;
639 for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
640 ctx.offnum = OffsetNumberNext(ctx.offnum))
641 {
650
651 /*
652 * The current line pointer may not have a successor, either
653 * because it's not valid or because it didn't point to anything.
654 * In either case, we have to give up.
655 *
656 * If the current line pointer does point to something, it's
657 * possible that the target line pointer isn't valid. We have to
658 * give up in that case, too.
659 */
661 continue;
662
663 /* We have two valid line pointers that we can examine. */
664 curr_lp = PageGetItemId(ctx.page, ctx.offnum);
666
667 /* Handle the cases where the current line pointer is a redirect. */
669 {
670 /*
671 * We should not have set successor[ctx.offnum] to a value
672 * other than InvalidOffsetNumber unless that line pointer is
673 * LP_NORMAL.
674 */
676
677 /* Can only redirect to a HOT tuple. */
680 {
682 psprintf("redirected line pointer points to a non-heap-only tuple at offset %d",
683 nextoffnum));
684 }
685
686 /* HOT chains should not intersect. */
688 {
690 psprintf("redirect line pointer points to offset %d, but offset %d also points there",
692 continue;
693 }
694
695 /*
696 * This redirect and the tuple to which it points seem to be
697 * part of an update chain.
698 */
700 continue;
701 }
702
703 /*
704 * If the next line pointer is a redirect, or if it's a tuple but
705 * the XMAX of this tuple doesn't match the XMIN of the next
706 * tuple, then the two aren't part of the same update chain and
707 * there is nothing more to do.
708 */
710 continue;
717 continue;
718
719 /* HOT chains should not intersect. */
721 {
723 psprintf("tuple points to new version at offset %d, but offset %d also points there",
725 continue;
726 }
727
728 /*
729 * This tuple and the tuple to which it points seem to be part of
730 * an update chain.
731 */
733
734 /*
735 * If the current tuple is marked as HOT-updated, then the next
736 * tuple should be marked as a heap-only tuple. Conversely, if the
737 * current tuple isn't marked as HOT-updated, then the next tuple
738 * shouldn't be marked as a heap-only tuple.
739 *
740 * NB: Can't use HeapTupleHeaderIsHotUpdated() as it checks if
741 * hint bits indicate xmin/xmax aborted.
742 */
743 if (!(curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
745 {
747 psprintf("non-heap-only update produced a heap-only tuple at offset %d",
748 nextoffnum));
749 }
750 if ((curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
752 {
754 psprintf("heap-only update produced a non-heap only tuple at offset %d",
755 nextoffnum));
756 }
757
758 /*
759 * If the current tuple's xmin is still in progress but the
760 * successor tuple's xmin is committed, that's corruption.
761 *
762 * NB: We recheck the commit status of the current tuple's xmin
763 * here, because it might have committed after we checked it and
764 * before we checked the commit status of the successor tuple's
765 * xmin. This should be safe because the xmin itself can't have
766 * changed, only its commit status.
767 */
769 if (xmin_commit_status_ok[ctx.offnum] &&
774 {
776 psprintf("tuple with in-progress xmin %u was updated to produce a tuple at offset %d with committed xmin %u",
777 curr_xmin,
778 ctx.offnum,
779 next_xmin));
780 }
781
782 /*
783 * If the current tuple's xmin is aborted but the successor
784 * tuple's xmin is in-progress or committed, that's corruption.
785 */
786 if (xmin_commit_status_ok[ctx.offnum] &&
789 {
792 psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %d with in-progress xmin %u",
793 curr_xmin,
794 ctx.offnum,
795 next_xmin));
798 psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %d with committed xmin %u",
799 curr_xmin,
800 ctx.offnum,
801 next_xmin));
802 }
803 }
804
805 /*
806 * An update chain can start either with a non-heap-only tuple or with
807 * a redirect line pointer, but not with a heap-only tuple.
808 *
809 * (This check is in a separate loop because we need the predecessor
810 * array to be fully populated before we can perform it.)
811 */
812 for (ctx.offnum = FirstOffsetNumber;
813 ctx.offnum <= maxoff;
814 ctx.offnum = OffsetNumberNext(ctx.offnum))
815 {
816 if (xmin_commit_status_ok[ctx.offnum] &&
820 {
822
823 curr_lp = PageGetItemId(ctx.page, ctx.offnum);
825 {
827
832 psprintf("tuple is root of chain but is marked as heap-only tuple"));
833 }
834 }
835 }
836
837 /* clean up */
839
840 /*
841 * Check any toast pointers from the page whose lock we just released
842 */
843 if (ctx.toasted_attributes != NIL)
844 {
845 ListCell *cell;
846
847 foreach(cell, ctx.toasted_attributes)
848 check_toasted_attribute(&ctx, lfirst(cell));
851 }
852
853 if (on_error_stop && ctx.is_corrupt)
854 break;
855 }
856
857 read_stream_end(stream);
858
859 if (vmbuffer != InvalidBuffer)
860 ReleaseBuffer(vmbuffer);
861
862 /* Close the associated toast table and indexes, if any. */
863 if (ctx.toast_indexes)
866 if (ctx.toast_rel)
868
869 /* Close the main relation */
871
873}
int Buffer
Definition buf.h:23
#define InvalidBuffer
Definition buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4357
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5505
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5522
@ BAS_BULKREAD
Definition bufmgr.h:37
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:470
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:210
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:332
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:421
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:269
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
Definition bufpage.h:379
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:397
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition freelist.c:461
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition funcapi.c:76
HeapTupleHeaderData * HeapTupleHeader
Definition htup.h:23
#define HeapTupleHeaderGetNatts(tup)
#define HEAP_HOT_UPDATED
#define ItemIdGetLength(itemId)
Definition itemid.h:59
#define ItemIdIsNormal(itemId)
Definition itemid.h:99
#define ItemIdGetOffset(itemId)
Definition itemid.h:65
#define ItemIdGetRedirect(itemId)
Definition itemid.h:78
#define ItemIdIsDead(itemId)
Definition itemid.h:113
#define ItemIdIsUsed(itemId)
Definition itemid.h:92
#define ItemIdIsRedirected(itemId)
Definition itemid.h:106
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
void list_free_deep(List *list)
Definition list.c:1560
#define AccessShareLock
Definition lockdefs.h:36
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static char * errmsg
#define InvalidOffsetNumber
Definition off.h:26
#define OffsetNumberNext(offsetNumber)
Definition off.h:52
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
#define MaxOffsetNumber
Definition off.h:28
static const struct exclude_list_item skip[]
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
int pg_strcasecmp(const char *s1, const char *s2)
unsigned int Oid
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
ReadStream * read_stream_begin_relation(int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
void read_stream_end(ReadStream *stream)
BlockNumber block_range_read_stream_cb(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
#define READ_STREAM_USE_BATCHING
Definition read_stream.h:64
BlockNumber(* ReadStreamBlockNumberCB)(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition read_stream.h:77
#define READ_STREAM_FULL
Definition read_stream.h:43
#define READ_STREAM_DEFAULT
Definition read_stream.h:21
#define READ_STREAM_SEQUENTIAL
Definition read_stream.h:36
#define RelationGetRelationName(relation)
Definition rel.h:548
@ MAIN_FORKNUM
Definition relpath.h:58
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
Relation * toast_indexes
BufferAccessStrategy bstrategy
TransactionId relfrozenxid
TransactionId xmin
Definition snapshot.h:153
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void toast_close_indexes(Relation *toastidxs, int num_indexes, LOCKMODE lock)
int toast_open_indexes(Relation toastrel, LOCKMODE lock, Relation **toastidxs, int *num_indexes)
#define InvalidTransactionId
Definition transam.h:31
#define TransactionIdEquals(id1, id2)
Definition transam.h:43
char * text_to_cstring(const text *t)
Definition varlena.c:217
static void check_toasted_attribute(HeapCheckContext *ctx, ToastedAttribute *ta)
static void check_tuple(HeapCheckContext *ctx, bool *xmin_commit_status_ok, XidCommitStatus *xmin_commit_status)
static BlockNumber heapcheck_read_stream_next_unskippable(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
bool RecoveryInProgress(void)
Definition xlog.c:6444

References AccessShareLock, Assert, HeapCheckContext::attnum, BAS_BULKREAD, HeapCheckContext::blkno, block_range_read_stream_cb(), HeapCheckContext::bstrategy, HeapCheckContext::buffer, BUFFER_LOCK_SHARE, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), HeapCheckContext::cached_xid, CHECK_FOR_INTERRUPTS, check_toasted_attribute(), check_tuple(), DEBUG1, ereport, errcode(), errdetail_relkind_not_supported(), errhint(), errmsg, ERROR, fb(), FirstOffsetNumber, FullTransactionIdFromXidAndCtx(), GetAccessStrategy(), GetTransactionSnapshot(), HEAP_HOT_UPDATED, heapcheck_read_stream_next_unskippable(), HeapTupleHeaderGetNatts, HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleHeaderIsHeapOnly(), InitMaterializedSRF(), InvalidBuffer, InvalidOffsetNumber, InvalidTransactionId, HeapCheckContext::is_corrupt, HeapCheckContext::itemid, ItemIdGetLength, ItemIdGetOffset, ItemIdGetRedirect, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), lfirst, list_free_deep(), LockBuffer(), HeapCheckContext::lp_len, HeapCheckContext::lp_off, MAIN_FORKNUM, MAXALIGN, MaxOffsetNumber, HeapCheckContext::natts, NIL, HeapCheckContext::num_toast_indexes, HeapCheckContext::offnum, OffsetNumberNext, HeapCheckContext::oldest_xid, HeapCheckContext::page, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_NULL, pg_strcasecmp(), psprintf(), RelationData::rd_rel, read_stream_begin_relation(), READ_STREAM_DEFAULT, read_stream_end(), READ_STREAM_FULL, read_stream_next_buffer(), READ_STREAM_SEQUENTIAL, READ_STREAM_USE_BATCHING, RecoveryInProgress(), HeapCheckContext::rel, relation_close(), relation_open(), RelationGetNumberOfBlocks, RelationGetRelationName, ReleaseBuffer(), HeapCheckContext::relfrozenfxid, HeapCheckContext::relfrozenxid, HeapCheckContext::relminmxid, report_corruption(), HeapCheckContext::safe_xmin, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SizeofHeapTupleHeader, skip, SKIP_PAGES_ALL_FROZEN, SKIP_PAGES_ALL_VISIBLE, SKIP_PAGES_NONE, table_close(), table_open(), text_to_cstring(), toast_close_indexes(), HeapCheckContext::toast_indexes, toast_open_indexes(), HeapCheckContext::toast_rel, HeapCheckContext::toasted_attributes, TransactionIdEquals, TransactionIdIsInProgress(), TransactionIdIsNormal, TransactionIdIsValid, HeapCheckContext::tupdesc, HeapCheckContext::tuphdr, HeapCheckContext::tupstore, UnlockReleaseBuffer(), update_cached_mxid_range(), update_cached_xid_range(), HeapCheckContext::valid_toast_index, XID_ABORTED, XID_COMMITTED, XID_IN_PROGRESS, and SnapshotData::xmin.