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/lwlock.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 38 of file verify_heapam.c.

◆ VARLENA_SIZE_LIMIT

#define VARLENA_SIZE_LIMIT   0x3FFFFFFF

Definition at line 41 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 64 of file verify_heapam.c.

65{
69} 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 47 of file verify_heapam.c.

48{
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 56 of file verify_heapam.c.

57{
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 2059 of file verify_heapam.c.

2060{
2061 if (!TransactionIdIsValid(mxid))
2062 return XID_INVALID;
2063 if (MultiXactIdPrecedes(mxid, ctx->relminmxid))
2064 return XID_PRECEDES_RELMIN;
2065 if (MultiXactIdPrecedes(mxid, ctx->oldest_mxact))
2068 return XID_IN_FUTURE;
2069 return XID_BOUNDS_OK;
2070}
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2865
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2879
TransactionId relminmxid
MultiXactId next_mxact
MultiXactId oldest_mxact
#define TransactionIdIsValid(xid)
Definition transam.h:41

References 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 2081 of file verify_heapam.c.

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

References check_mxid_in_range(), result, 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 1556 of file verify_heapam.c.

1559{
1561 int32 last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
1562 Pointer chunk;
1563 bool isnull;
1566
1567 /* Sanity-check the sequence number. */
1569 ctx->toast_rel->rd_att, &isnull));
1570 if (isnull)
1571 {
1573 psprintf("toast value %u has toast chunk with null sequence number",
1574 ta->toast_pointer.va_valueid));
1575 return;
1576 }
1578 {
1579 /* Either the TOAST index is corrupt, or we don't have all chunks. */
1581 psprintf("toast value %u index scan returned chunk %d when expecting chunk %d",
1582 ta->toast_pointer.va_valueid,
1584 }
1586
1587 /* Sanity-check the chunk data. */
1589 ctx->toast_rel->rd_att, &isnull));
1590 if (isnull)
1591 {
1593 psprintf("toast value %u chunk %d has null data",
1594 ta->toast_pointer.va_valueid,
1595 chunk_seq));
1596 return;
1597 }
1598 if (!VARATT_IS_EXTENDED(chunk))
1599 chunksize = VARSIZE(chunk) - VARHDRSZ;
1600 else if (VARATT_IS_SHORT(chunk))
1601 {
1602 /*
1603 * could happen due to heap_form_tuple doing its thing
1604 */
1606 }
1607 else
1608 {
1609 /* should never happen */
1610 uint32 header = ((varattrib_4b *) chunk)->va_4byte.va_header;
1611
1613 psprintf("toast value %u chunk %d has invalid varlena header %0x",
1614 ta->toast_pointer.va_valueid,
1615 chunk_seq, header));
1616 return;
1617 }
1618
1619 /*
1620 * Some checks on the data we've found
1621 */
1622 if (chunk_seq > last_chunk_seq)
1623 {
1625 psprintf("toast value %u chunk %d follows last expected chunk %d",
1626 ta->toast_pointer.va_valueid,
1627 chunk_seq, last_chunk_seq));
1628 return;
1629 }
1630
1631 expected_size = chunk_seq < last_chunk_seq ? TOAST_MAX_CHUNK_SIZE
1632 : extsize - (last_chunk_seq * TOAST_MAX_CHUNK_SIZE);
1633
1634 if (chunksize != expected_size)
1636 psprintf("toast value %u chunk %d has size %u, but expected size %u",
1637 ta->toast_pointer.va_valueid,
1639}
#define VARHDRSZ
Definition c.h:781
int32_t int32
Definition c.h:620
uint32_t uint32
Definition c.h:624
void * Pointer
Definition c.h:615
#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
static int fb(int x)
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 1862 of file verify_heapam.c.

1863{
1866 bool found_toasttup;
1870 int32 last_chunk_seq;
1871
1872 extsize = VARATT_EXTERNAL_GET_EXTSIZE(ta->toast_pointer);
1873 last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
1874
1875 /*
1876 * Setup a scan key to find chunks in toast table with matching va_valueid
1877 */
1879 (AttrNumber) 1,
1881 ObjectIdGetDatum(ta->toast_pointer.va_valueid));
1882
1883 /*
1884 * Check if any chunks for this toasted object exist in the toast table,
1885 * accessible via the index.
1886 */
1888 ctx->valid_toast_index,
1889 get_toast_snapshot(), 1,
1890 &toastkey);
1891 found_toasttup = false;
1892 while ((toasttup =
1895 {
1896 found_toasttup = true;
1898 }
1900
1901 if (!found_toasttup)
1903 psprintf("toast value %u not found in toast table",
1904 ta->toast_pointer.va_valueid));
1905 else if (expected_chunk_seq <= last_chunk_seq)
1907 psprintf("toast value %u was expected to end at chunk %d, but ended while expecting chunk %d",
1908 ta->toast_pointer.va_valueid,
1909 last_chunk_seq, expected_chunk_seq));
1910}
int16 AttrNumber
Definition attnum.h:21
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:659
void systable_endscan_ordered(SysScanDesc sysscan)
Definition genam.c:767
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition genam.c:742
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 1920 of file verify_heapam.c.

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

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

1002{
1003 HeapTupleHeader tuphdr = ctx->tuphdr;
1004 uint16 infomask = tuphdr->t_infomask;
1006 bool result = true;
1007 unsigned expected_hoff;
1008
1009 if (ctx->tuphdr->t_hoff > ctx->lp_len)
1010 {
1012 psprintf("data begins at offset %u beyond the tuple length %u",
1013 ctx->tuphdr->t_hoff, ctx->lp_len));
1014 result = false;
1015 }
1016
1017 if ((ctx->tuphdr->t_infomask & HEAP_XMAX_COMMITTED) &&
1019 {
1021 pstrdup("multixact should not be marked committed"));
1022
1023 /*
1024 * This condition is clearly wrong, but it's not enough to justify
1025 * skipping further checks, because we don't rely on this to determine
1026 * whether the tuple is visible or to interpret other relevant header
1027 * fields.
1028 */
1029 }
1030
1033 {
1035 psprintf("tuple has been HOT updated, but xmax is 0"));
1036
1037 /*
1038 * As above, even though this shouldn't happen, it's not sufficient
1039 * justification for skipping further checks, we should still be able
1040 * to perform sensibly.
1041 */
1042 }
1043
1044 if (HeapTupleHeaderIsHeapOnly(tuphdr) &&
1045 ((tuphdr->t_infomask & HEAP_UPDATED) == 0))
1046 {
1048 psprintf("tuple is heap only, but not the result of an update"));
1049
1050 /* Here again, we can still perform further checks. */
1051 }
1052
1053 if (infomask & HEAP_HASNULL)
1055 else
1057 if (ctx->tuphdr->t_hoff != expected_hoff)
1058 {
1059 if ((infomask & HEAP_HASNULL) && ctx->natts == 1)
1061 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, has nulls)",
1062 expected_hoff, ctx->tuphdr->t_hoff));
1063 else if ((infomask & HEAP_HASNULL))
1065 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, has nulls)",
1066 expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
1067 else if (ctx->natts == 1)
1069 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, no nulls)",
1070 expected_hoff, ctx->tuphdr->t_hoff));
1071 else
1073 psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, no nulls)",
1074 expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
1075 result = false;
1076 }
1077
1078 return result;
1079}
#define MAXALIGN(LEN)
Definition c.h:896
uint32 TransactionId
Definition c.h:736
#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(), result, 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 1114 of file verify_heapam.c.

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

1980{
1982 int32 diff;
1983 FullTransactionId fxid;
1984
1988
1989 if (!TransactionIdIsNormal(xid))
1990 return FullTransactionIdFromEpochAndXid(0, xid);
1991
1993
1994 /* compute the 32bit modulo difference */
1995 diff = (int32) (ctx->next_xid - xid);
1996
1997 /*
1998 * In cases of corruption we might see a 32bit xid that is before epoch 0.
1999 * We can't represent that as a 64bit xid, due to 64bit xids being
2000 * unsigned integers, without the modulo arithmetic of 32bit xid. There's
2001 * no really nice way to deal with that, but it works ok enough to use
2002 * FirstNormalFullTransactionId in that case, as a freshly initdb'd
2003 * cluster already has a newer horizon.
2004 */
2006 {
2009 }
2010 else
2012
2014 return fxid;
2015}
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
uint64_t uint64
Definition c.h:625
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 2048 of file verify_heapam.c.

2049{
2050 return (FullTransactionIdPrecedesOrEquals(ctx->oldest_fxid, fxid) &&
2052}
#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 2113 of file verify_heapam.c.

2115{
2116 FullTransactionId fxid;
2118
2119 /* Quick check for special xids */
2120 if (!TransactionIdIsValid(xid))
2121 return XID_INVALID;
2122 else if (xid == BootstrapTransactionId || xid == FrozenTransactionId)
2123 {
2124 if (status != NULL)
2125 *status = XID_COMMITTED;
2126 return XID_BOUNDS_OK;
2127 }
2128
2129 /* Check if the xid is within bounds */
2130 fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
2131 if (!fxid_in_cached_range(fxid, ctx))
2132 {
2133 /*
2134 * We may have been checking against stale values. Update the cached
2135 * range to be sure, and since we relied on the cached range when we
2136 * performed the full xid conversion, reconvert.
2137 */
2139 fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
2140 }
2141
2143 return XID_IN_FUTURE;
2144 if (FullTransactionIdPrecedes(fxid, ctx->oldest_fxid))
2147 return XID_PRECEDES_RELMIN;
2148
2149 /* Early return if the caller does not request clog checking */
2150 if (status == NULL)
2151 return XID_BOUNDS_OK;
2152
2153 /* Early return if we just checked this xid in a prior call */
2154 if (xid == ctx->cached_xid)
2155 {
2156 *status = ctx->cached_status;
2157 return XID_BOUNDS_OK;
2158 }
2159
2160 *status = XID_COMMITTED;
2162 clog_horizon =
2164 ctx);
2166 {
2168 *status = XID_IS_CURRENT_XID;
2169 else if (TransactionIdIsInProgress(xid))
2170 *status = XID_IN_PROGRESS;
2171 else if (TransactionIdDidCommit(xid))
2172 *status = XID_COMMITTED;
2173 else
2174 *status = XID_ABORTED;
2175 }
2177 ctx->cached_xid = xid;
2178 ctx->cached_status = *status;
2179 return XID_BOUNDS_OK;
2180}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_SHARED
Definition lwlock.h:105
bool TransactionIdIsInProgress(TransactionId xid)
Definition procarray.c:1393
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:37
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 883 of file verify_heapam.c.

886{
887 HeapCheckReadStreamData *p = callback_private_data;
888
889 /* Loops over [current_blocknum, last_exclusive) blocks */
891 {
893
895 {
897 continue;
898 }
899
901 {
903 continue;
904 }
905
906 return i;
907 }
908
909 return InvalidBlockNumber;
910}
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 953 of file verify_heapam.c.

954{
956 ctx->offnum, ctx->attnum, msg);
957 ctx->is_corrupt = true;
958}
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 917 of file verify_heapam.c.

920{
922 bool nulls[HEAPCHECK_RELATION_COLS] = {0};
923 HeapTuple tuple;
924
925 values[0] = Int64GetDatum(blkno);
926 values[1] = Int32GetDatum(offnum);
928 nulls[2] = (attnum < 0);
929 values[3] = CStringGetTextDatum(msg);
930
931 /*
932 * In principle, there is nothing to prevent a scan over a large, highly
933 * corrupted table from using work_mem worth of memory building up the
934 * tuplestore. That's ok, but if we also leak the msg argument memory
935 * until the end of the query, we could exceed work_mem by more than a
936 * trivial amount. Therefore, free the msg argument each time we are
937 * called rather than waiting for our current memory context to be freed.
938 */
939 pfree(msg);
940
941 tuple = heap_form_tuple(tupdesc, values, nulls);
942 tuplestore_puttuple(tupstore, tuple);
943}
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define CStringGetTextDatum(s)
Definition builtins.h:98
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
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 969 of file verify_heapam.c.

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

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 2038 of file verify_heapam.c.

2039{
2041}
void ReadMultiXactIdRange(MultiXactId *oldest, MultiXactId *next)
Definition multixact.c:696

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 252 of file verify_heapam.c.

253{
254 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
256 Buffer vmbuffer = InvalidBuffer;
257 Oid relid;
258 bool on_error_stop;
259 bool check_toast;
260 SkipPages skip_option = SKIP_PAGES_NONE;
263 BlockNumber nblocks;
264 const char *skip;
265 ReadStream *stream;
266 int stream_flags;
268 void *stream_data;
270
271 /* Check supplied arguments */
272 if (PG_ARGISNULL(0))
275 errmsg("relation cannot be null")));
276 relid = PG_GETARG_OID(0);
277
278 if (PG_ARGISNULL(1))
281 errmsg("on_error_stop cannot be null")));
282 on_error_stop = PG_GETARG_BOOL(1);
283
284 if (PG_ARGISNULL(2))
287 errmsg("check_toast cannot be null")));
289
290 if (PG_ARGISNULL(3))
293 errmsg("skip cannot be null")));
295 if (pg_strcasecmp(skip, "all-visible") == 0)
296 skip_option = SKIP_PAGES_ALL_VISIBLE;
297 else if (pg_strcasecmp(skip, "all-frozen") == 0)
298 skip_option = SKIP_PAGES_ALL_FROZEN;
299 else if (pg_strcasecmp(skip, "none") == 0)
300 skip_option = SKIP_PAGES_NONE;
301 else
304 errmsg("invalid skip option"),
305 errhint("Valid skip options are \"all-visible\", \"all-frozen\", and \"none\".")));
306
307 memset(&ctx, 0, sizeof(HeapCheckContext));
310
311 /*
312 * Any xmin newer than the xmin of our snapshot can't become all-visible
313 * while we're running.
314 */
316
317 /*
318 * If we report corruption when not examining some individual attribute,
319 * we need attnum to be reported as NULL. Set that up before any
320 * corruption reporting might happen.
321 */
322 ctx.attnum = -1;
323
324 /* Construct the tuplestore and tuple descriptor */
325 InitMaterializedSRF(fcinfo, 0);
326 ctx.tupdesc = rsinfo->setDesc;
327 ctx.tupstore = rsinfo->setResult;
328
329 /* Open relation, check relkind and access method */
330 ctx.rel = relation_open(relid, AccessShareLock);
331
332 /*
333 * Check that a relation's relkind and access method are both supported.
334 */
335 if (!RELKIND_HAS_TABLE_AM(ctx.rel->rd_rel->relkind) &&
336 ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE)
339 errmsg("cannot check relation \"%s\"",
342
343 /*
344 * Sequences always use heap AM, but they don't show that in the catalogs.
345 * Other relkinds might be using a different AM, so check.
346 */
347 if (ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
348 ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
351 errmsg("only heap AM is supported")));
352
353 /*
354 * Early exit for unlogged relations during recovery. These will have no
355 * relation fork, so there won't be anything to check. We behave as if
356 * the relation is empty.
357 */
358 if (ctx.rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
360 {
363 errmsg("cannot verify unlogged relation \"%s\" during recovery, skipping",
367 }
368
369 /* Early exit if the relation is empty */
370 nblocks = RelationGetNumberOfBlocks(ctx.rel);
371 if (!nblocks)
372 {
375 }
376
378 ctx.buffer = InvalidBuffer;
379 ctx.page = NULL;
380
381 /* Validate block numbers, or handle nulls. */
382 if (PG_ARGISNULL(4))
383 first_block = 0;
384 else
385 {
387
388 if (fb < 0 || fb >= nblocks)
391 errmsg("starting block number must be between 0 and %u",
392 nblocks - 1)));
394 }
395 if (PG_ARGISNULL(5))
396 last_block = nblocks - 1;
397 else
398 {
399 int64 lb = PG_GETARG_INT64(5);
400
401 if (lb < 0 || lb >= nblocks)
404 errmsg("ending block number must be between 0 and %u",
405 nblocks - 1)));
406 last_block = (BlockNumber) lb;
407 }
408
409 /* Optionally open the toast relation, if any. */
410 if (ctx.rel->rd_rel->reltoastrelid && check_toast)
411 {
412 int offset;
413
414 /* Main relation has associated toast relation */
415 ctx.toast_rel = table_open(ctx.rel->rd_rel->reltoastrelid,
417 offset = toast_open_indexes(ctx.toast_rel,
419 &(ctx.toast_indexes),
420 &(ctx.num_toast_indexes));
421 ctx.valid_toast_index = ctx.toast_indexes[offset];
422 }
423 else
424 {
425 /*
426 * Main relation has no associated toast relation, or we're
427 * intentionally skipping it.
428 */
429 ctx.toast_rel = NULL;
430 ctx.toast_indexes = NULL;
431 ctx.num_toast_indexes = 0;
432 }
433
436 ctx.relfrozenxid = ctx.rel->rd_rel->relfrozenxid;
438 ctx.relminmxid = ctx.rel->rd_rel->relminmxid;
439
441 ctx.oldest_xid = ctx.relfrozenxid;
442
443 /* Now that `ctx` is set up, set up the read stream */
444 stream_skip_data.range.current_blocknum = first_block;
445 stream_skip_data.range.last_exclusive = last_block + 1;
446 stream_skip_data.skip_option = skip_option;
447 stream_skip_data.rel = ctx.rel;
448 stream_skip_data.vmbuffer = &vmbuffer;
449
450 if (skip_option == SKIP_PAGES_NONE)
451 {
452 /*
453 * It is safe to use batchmode as block_range_read_stream_cb takes no
454 * locks.
455 */
461 }
462 else
463 {
464 /*
465 * It would not be safe to naively use batchmode, as
466 * heapcheck_read_stream_next_unskippable takes locks. It shouldn't be
467 * too hard to convert though.
468 */
472 }
473
475 ctx.bstrategy,
476 ctx.rel,
478 stream_cb,
480 0);
481
482 while ((ctx.buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
483 {
484 OffsetNumber maxoff;
490
492
494
495 /* Lock the next page. */
498
500 ctx.page = BufferGetPage(ctx.buffer);
501
502 /* Perform tuple checks */
503 maxoff = PageGetMaxOffsetNumber(ctx.page);
504 for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
505 ctx.offnum = OffsetNumberNext(ctx.offnum))
506 {
507 BlockNumber nextblkno;
509
511 lp_valid[ctx.offnum] = false;
512 xmin_commit_status_ok[ctx.offnum] = false;
513 ctx.itemid = PageGetItemId(ctx.page, ctx.offnum);
514
515 /* Skip over unused/dead line pointers */
516 if (!ItemIdIsUsed(ctx.itemid) || ItemIdIsDead(ctx.itemid))
517 continue;
518
519 /*
520 * If this line pointer has been redirected, check that it
521 * redirects to a valid offset within the line pointer array
522 */
523 if (ItemIdIsRedirected(ctx.itemid))
524 {
527
529 {
531 psprintf("line pointer redirection to item at offset %d precedes minimum offset %d",
532 rdoffnum,
534 continue;
535 }
536 if (rdoffnum > maxoff)
537 {
539 psprintf("line pointer redirection to item at offset %d exceeds maximum offset %d",
540 rdoffnum,
541 maxoff));
542 continue;
543 }
544
545 /*
546 * Since we've checked that this redirect points to a line
547 * pointer between FirstOffsetNumber and maxoff, it should now
548 * be safe to fetch the referenced line pointer. We expect it
549 * to be LP_NORMAL; if not, that's corruption.
550 */
552 if (!ItemIdIsUsed(rditem))
553 {
555 psprintf("redirected line pointer points to an unused item at offset %d",
556 rdoffnum));
557 continue;
558 }
559 else if (ItemIdIsDead(rditem))
560 {
562 psprintf("redirected line pointer points to a dead item at offset %d",
563 rdoffnum));
564 continue;
565 }
566 else if (ItemIdIsRedirected(rditem))
567 {
569 psprintf("redirected line pointer points to another redirected line pointer at offset %d",
570 rdoffnum));
571 continue;
572 }
573
574 /*
575 * Record the fact that this line pointer has passed basic
576 * sanity checking, and also the offset number to which it
577 * points.
578 */
579 lp_valid[ctx.offnum] = true;
581 continue;
582 }
583
584 /* Sanity-check the line pointer's offset and length values */
585 ctx.lp_len = ItemIdGetLength(ctx.itemid);
586 ctx.lp_off = ItemIdGetOffset(ctx.itemid);
587
588 if (ctx.lp_off != MAXALIGN(ctx.lp_off))
589 {
591 psprintf("line pointer to page offset %u is not maximally aligned",
592 ctx.lp_off));
593 continue;
594 }
596 {
598 psprintf("line pointer length %u is less than the minimum tuple header size %u",
599 ctx.lp_len,
600 (unsigned) MAXALIGN(SizeofHeapTupleHeader)));
601 continue;
602 }
603 if (ctx.lp_off + ctx.lp_len > BLCKSZ)
604 {
606 psprintf("line pointer to page offset %u with length %u ends beyond maximum page offset %d",
607 ctx.lp_off,
608 ctx.lp_len,
609 BLCKSZ));
610 continue;
611 }
612
613 /* It should be safe to examine the tuple's header, at least */
614 lp_valid[ctx.offnum] = true;
615 ctx.tuphdr = (HeapTupleHeader) PageGetItem(ctx.page, ctx.itemid);
617
618 /* Ok, ready to check this next tuple */
619 check_tuple(&ctx,
622
623 /*
624 * If the CTID field of this tuple seems to point to another tuple
625 * on the same page, record that tuple as the successor of this
626 * one.
627 */
628 nextblkno = ItemPointerGetBlockNumber(&(ctx.tuphdr)->t_ctid);
630 if (nextblkno == ctx.blkno && nextoffnum != ctx.offnum &&
633 }
634
635 /*
636 * Update chain validation. Check each line pointer that's got a valid
637 * successor against that successor.
638 */
639 ctx.attnum = -1;
640 for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
641 ctx.offnum = OffsetNumberNext(ctx.offnum))
642 {
651
652 /*
653 * The current line pointer may not have a successor, either
654 * because it's not valid or because it didn't point to anything.
655 * In either case, we have to give up.
656 *
657 * If the current line pointer does point to something, it's
658 * possible that the target line pointer isn't valid. We have to
659 * give up in that case, too.
660 */
662 continue;
663
664 /* We have two valid line pointers that we can examine. */
665 curr_lp = PageGetItemId(ctx.page, ctx.offnum);
667
668 /* Handle the cases where the current line pointer is a redirect. */
670 {
671 /*
672 * We should not have set successor[ctx.offnum] to a value
673 * other than InvalidOffsetNumber unless that line pointer is
674 * LP_NORMAL.
675 */
677
678 /* Can only redirect to a HOT tuple. */
681 {
683 psprintf("redirected line pointer points to a non-heap-only tuple at offset %d",
684 nextoffnum));
685 }
686
687 /* HOT chains should not intersect. */
689 {
691 psprintf("redirect line pointer points to offset %d, but offset %d also points there",
693 continue;
694 }
695
696 /*
697 * This redirect and the tuple to which it points seem to be
698 * part of an update chain.
699 */
701 continue;
702 }
703
704 /*
705 * If the next line pointer is a redirect, or if it's a tuple but
706 * the XMAX of this tuple doesn't match the XMIN of the next
707 * tuple, then the two aren't part of the same update chain and
708 * there is nothing more to do.
709 */
711 continue;
718 continue;
719
720 /* HOT chains should not intersect. */
722 {
724 psprintf("tuple points to new version at offset %d, but offset %d also points there",
726 continue;
727 }
728
729 /*
730 * This tuple and the tuple to which it points seem to be part of
731 * an update chain.
732 */
734
735 /*
736 * If the current tuple is marked as HOT-updated, then the next
737 * tuple should be marked as a heap-only tuple. Conversely, if the
738 * current tuple isn't marked as HOT-updated, then the next tuple
739 * shouldn't be marked as a heap-only tuple.
740 *
741 * NB: Can't use HeapTupleHeaderIsHotUpdated() as it checks if
742 * hint bits indicate xmin/xmax aborted.
743 */
744 if (!(curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
746 {
748 psprintf("non-heap-only update produced a heap-only tuple at offset %d",
749 nextoffnum));
750 }
751 if ((curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
753 {
755 psprintf("heap-only update produced a non-heap only tuple at offset %d",
756 nextoffnum));
757 }
758
759 /*
760 * If the current tuple's xmin is still in progress but the
761 * successor tuple's xmin is committed, that's corruption.
762 *
763 * NB: We recheck the commit status of the current tuple's xmin
764 * here, because it might have committed after we checked it and
765 * before we checked the commit status of the successor tuple's
766 * xmin. This should be safe because the xmin itself can't have
767 * changed, only its commit status.
768 */
770 if (xmin_commit_status_ok[ctx.offnum] &&
775 {
777 psprintf("tuple with in-progress xmin %u was updated to produce a tuple at offset %d with committed xmin %u",
778 curr_xmin,
779 ctx.offnum,
780 next_xmin));
781 }
782
783 /*
784 * If the current tuple's xmin is aborted but the successor
785 * tuple's xmin is in-progress or committed, that's corruption.
786 */
787 if (xmin_commit_status_ok[ctx.offnum] &&
790 {
793 psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %d with in-progress xmin %u",
794 curr_xmin,
795 ctx.offnum,
796 next_xmin));
799 psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %d with committed xmin %u",
800 curr_xmin,
801 ctx.offnum,
802 next_xmin));
803 }
804 }
805
806 /*
807 * An update chain can start either with a non-heap-only tuple or with
808 * a redirect line pointer, but not with a heap-only tuple.
809 *
810 * (This check is in a separate loop because we need the predecessor
811 * array to be fully populated before we can perform it.)
812 */
813 for (ctx.offnum = FirstOffsetNumber;
814 ctx.offnum <= maxoff;
815 ctx.offnum = OffsetNumberNext(ctx.offnum))
816 {
817 if (xmin_commit_status_ok[ctx.offnum] &&
821 {
823
824 curr_lp = PageGetItemId(ctx.page, ctx.offnum);
826 {
828
833 psprintf("tuple is root of chain but is marked as heap-only tuple"));
834 }
835 }
836 }
837
838 /* clean up */
840
841 /*
842 * Check any toast pointers from the page whose lock we just released
843 */
844 if (ctx.toasted_attributes != NIL)
845 {
846 ListCell *cell;
847
848 foreach(cell, ctx.toasted_attributes)
849 check_toasted_attribute(&ctx, lfirst(cell));
852 }
853
854 if (on_error_stop && ctx.is_corrupt)
855 break;
856 }
857
858 read_stream_end(stream);
859
860 if (vmbuffer != InvalidBuffer)
861 ReleaseBuffer(vmbuffer);
862
863 /* Close the associated toast table and indexes, if any. */
864 if (ctx.toast_indexes)
867 if (ctx.toast_rel)
869
870 /* Close the main relation */
872
874}
int Buffer
Definition buf.h:23
#define InvalidBuffer
Definition buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4446
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5586
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5603
@ BAS_BULKREAD
Definition bufmgr.h:37
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:309
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:468
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:212
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:334
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:419
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:268
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
Definition bufpage.h:378
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:396
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#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:426
void InitMaterializedSRF(FunctionCallInfo fcinfo, uint32 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:125
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:78
#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:550
@ MAIN_FORKNUM
Definition relpath.h:58
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:48
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:6830

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.