LCOV - differential code coverage report
Current view: top level - contrib/amcheck - verify_heapam.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 75.5 % 691 522 4 41 102 22 36 256 100 130 110 349 1 2
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 18 18 15 2 1 17
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 95.8 % 118 113 4 1 2 10 98 3 8 6
Legend: Lines: hit not hit (120,180] days: 100.0 % 1 1 1
(240..) days: 71.3 % 572 408 41 101 22 34 245 2 127 97 239
Function coverage date bins:
[..60] days: 100.0 % 2 2 2
(240..) days: 64.0 % 25 16 15 1 9

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * verify_heapam.c
                                  4                 :  *    Functions to check postgresql heap relations for corruption
                                  5                 :  *
                                  6                 :  * Copyright (c) 2016-2023, PostgreSQL Global Development Group
                                  7                 :  *
                                  8                 :  *    contrib/amcheck/verify_heapam.c
                                  9                 :  *-------------------------------------------------------------------------
                                 10                 :  */
                                 11                 : #include "postgres.h"
                                 12                 : 
                                 13                 : #include "access/detoast.h"
                                 14                 : #include "access/genam.h"
                                 15                 : #include "access/heapam.h"
                                 16                 : #include "access/heaptoast.h"
                                 17                 : #include "access/multixact.h"
                                 18                 : #include "access/toast_internals.h"
                                 19                 : #include "access/visibilitymap.h"
                                 20                 : #include "catalog/pg_am.h"
                                 21                 : #include "funcapi.h"
                                 22                 : #include "miscadmin.h"
                                 23                 : #include "storage/bufmgr.h"
                                 24                 : #include "storage/procarray.h"
                                 25                 : #include "utils/builtins.h"
                                 26                 : #include "utils/fmgroids.h"
                                 27                 : 
  899 rhaas                      28 CBC         290 : PG_FUNCTION_INFO_V1(verify_heapam);
                                 29                 : 
                                 30                 : /* The number of columns in tuples returned by verify_heapam */
                                 31                 : #define HEAPCHECK_RELATION_COLS 4
                                 32                 : 
                                 33                 : /* The largest valid toast va_rawsize */
                                 34                 : #define VARLENA_SIZE_LIMIT 0x3FFFFFFF
                                 35                 : 
                                 36                 : /*
                                 37                 :  * Despite the name, we use this for reporting problems with both XIDs and
                                 38                 :  * MXIDs.
                                 39                 :  */
                                 40                 : typedef enum XidBoundsViolation
                                 41                 : {
                                 42                 :     XID_INVALID,
                                 43                 :     XID_IN_FUTURE,
                                 44                 :     XID_PRECEDES_CLUSTERMIN,
                                 45                 :     XID_PRECEDES_RELMIN,
                                 46                 :     XID_BOUNDS_OK
                                 47                 : } XidBoundsViolation;
                                 48                 : 
                                 49                 : typedef enum XidCommitStatus
                                 50                 : {
                                 51                 :     XID_COMMITTED,
                                 52                 :     XID_IS_CURRENT_XID,
                                 53                 :     XID_IN_PROGRESS,
                                 54                 :     XID_ABORTED
                                 55                 : } XidCommitStatus;
                                 56                 : 
                                 57                 : typedef enum SkipPages
                                 58                 : {
                                 59                 :     SKIP_PAGES_ALL_FROZEN,
                                 60                 :     SKIP_PAGES_ALL_VISIBLE,
                                 61                 :     SKIP_PAGES_NONE
                                 62                 : } SkipPages;
                                 63                 : 
                                 64                 : /*
                                 65                 :  * Struct holding information about a toasted attribute sufficient to both
                                 66                 :  * check the toasted attribute and, if found to be corrupt, to report where it
                                 67                 :  * was encountered in the main table.
                                 68                 :  */
                                 69                 : typedef struct ToastedAttribute
                                 70                 : {
                                 71                 :     struct varatt_external toast_pointer;
                                 72                 :     BlockNumber blkno;          /* block in main table */
                                 73                 :     OffsetNumber offnum;        /* offset in main table */
                                 74                 :     AttrNumber  attnum;         /* attribute in main table */
                                 75                 : } ToastedAttribute;
                                 76                 : 
                                 77                 : /*
                                 78                 :  * Struct holding the running context information during
                                 79                 :  * a lifetime of a verify_heapam execution.
                                 80                 :  */
                                 81                 : typedef struct HeapCheckContext
                                 82                 : {
                                 83                 :     /*
                                 84                 :      * Cached copies of values from ShmemVariableCache and computed values
                                 85                 :      * from them.
                                 86                 :      */
                                 87                 :     FullTransactionId next_fxid;    /* ShmemVariableCache->nextXid */
                                 88                 :     TransactionId next_xid;     /* 32-bit version of next_fxid */
                                 89                 :     TransactionId oldest_xid;   /* ShmemVariableCache->oldestXid */
                                 90                 :     FullTransactionId oldest_fxid;  /* 64-bit version of oldest_xid, computed
                                 91                 :                                      * relative to next_fxid */
                                 92                 :     TransactionId safe_xmin;    /* this XID and newer ones can't become
                                 93                 :                                  * all-visible while we're running */
                                 94                 : 
                                 95                 :     /*
                                 96                 :      * Cached copy of value from MultiXactState
                                 97                 :      */
                                 98                 :     MultiXactId next_mxact;     /* MultiXactState->nextMXact */
                                 99                 :     MultiXactId oldest_mxact;   /* MultiXactState->oldestMultiXactId */
                                100                 : 
                                101                 :     /*
                                102                 :      * Cached copies of the most recently checked xid and its status.
                                103                 :      */
                                104                 :     TransactionId cached_xid;
                                105                 :     XidCommitStatus cached_status;
                                106                 : 
                                107                 :     /* Values concerning the heap relation being checked */
                                108                 :     Relation    rel;
                                109                 :     TransactionId relfrozenxid;
                                110                 :     FullTransactionId relfrozenfxid;
                                111                 :     TransactionId relminmxid;
                                112                 :     Relation    toast_rel;
                                113                 :     Relation   *toast_indexes;
                                114                 :     Relation    valid_toast_index;
                                115                 :     int         num_toast_indexes;
                                116                 : 
                                117                 :     /* Values for iterating over pages in the relation */
                                118                 :     BlockNumber blkno;
                                119                 :     BufferAccessStrategy bstrategy;
                                120                 :     Buffer      buffer;
                                121                 :     Page        page;
                                122                 : 
                                123                 :     /* Values for iterating over tuples within a page */
                                124                 :     OffsetNumber offnum;
                                125                 :     ItemId      itemid;
                                126                 :     uint16      lp_len;
                                127                 :     uint16      lp_off;
                                128                 :     HeapTupleHeader tuphdr;
                                129                 :     int         natts;
                                130                 : 
                                131                 :     /* Values for iterating over attributes within the tuple */
                                132                 :     uint32      offset;         /* offset in tuple data */
                                133                 :     AttrNumber  attnum;
                                134                 : 
                                135                 :     /* True if tuple's xmax makes it eligible for pruning */
                                136                 :     bool        tuple_could_be_pruned;
                                137                 : 
                                138                 :     /*
                                139                 :      * List of ToastedAttribute structs for toasted attributes which are not
                                140                 :      * eligible for pruning and should be checked
                                141                 :      */
                                142                 :     List       *toasted_attributes;
                                143                 : 
                                144                 :     /* Whether verify_heapam has yet encountered any corrupt tuples */
                                145                 :     bool        is_corrupt;
                                146                 : 
                                147                 :     /* The descriptor and tuplestore for verify_heapam's result tuples */
                                148                 :     TupleDesc   tupdesc;
                                149                 :     Tuplestorestate *tupstore;
                                150                 : } HeapCheckContext;
                                151                 : 
                                152                 : /* Internal implementation */
                                153                 : static void check_tuple(HeapCheckContext *ctx,
                                154                 :                         bool *xmin_commit_status_ok,
                                155                 :                         XidCommitStatus *xmin_commit_status);
                                156                 : static void check_toast_tuple(HeapTuple toasttup, HeapCheckContext *ctx,
                                157                 :                               ToastedAttribute *ta, int32 *expected_chunk_seq,
                                158                 :                               uint32 extsize);
                                159                 : 
                                160                 : static bool check_tuple_attribute(HeapCheckContext *ctx);
                                161                 : static void check_toasted_attribute(HeapCheckContext *ctx,
                                162                 :                                     ToastedAttribute *ta);
                                163                 : 
                                164                 : static bool check_tuple_header(HeapCheckContext *ctx);
                                165                 : static bool check_tuple_visibility(HeapCheckContext *ctx,
                                166                 :                                    bool *xmin_commit_status_ok,
                                167                 :                                    XidCommitStatus *xmin_commit_status);
                                168                 : 
                                169                 : static void report_corruption(HeapCheckContext *ctx, char *msg);
                                170                 : static void report_toast_corruption(HeapCheckContext *ctx,
                                171                 :                                     ToastedAttribute *ta, char *msg);
                                172                 : static FullTransactionId FullTransactionIdFromXidAndCtx(TransactionId xid,
                                173                 :                                                         const HeapCheckContext *ctx);
                                174                 : static void update_cached_xid_range(HeapCheckContext *ctx);
                                175                 : static void update_cached_mxid_range(HeapCheckContext *ctx);
                                176                 : static XidBoundsViolation check_mxid_in_range(MultiXactId mxid,
                                177                 :                                               HeapCheckContext *ctx);
                                178                 : static XidBoundsViolation check_mxid_valid_in_rel(MultiXactId mxid,
                                179                 :                                                   HeapCheckContext *ctx);
                                180                 : static XidBoundsViolation get_xid_status(TransactionId xid,
                                181                 :                                          HeapCheckContext *ctx,
                                182                 :                                          XidCommitStatus *status);
                                183                 : 
                                184                 : /*
                                185                 :  * Scan and report corruption in heap pages, optionally reconciling toasted
                                186                 :  * attributes with entries in the associated toast table.  Intended to be
                                187                 :  * called from SQL with the following parameters:
                                188                 :  *
                                189                 :  *   relation:
                                190                 :  *     The Oid of the heap relation to be checked.
                                191                 :  *
                                192                 :  *   on_error_stop:
                                193                 :  *     Whether to stop at the end of the first page for which errors are
                                194                 :  *     detected.  Note that multiple rows may be returned.
                                195                 :  *
                                196                 :  *   check_toast:
                                197                 :  *     Whether to check each toasted attribute against the toast table to
                                198                 :  *     verify that it can be found there.
                                199                 :  *
                                200                 :  *   skip:
                                201                 :  *     What kinds of pages in the heap relation should be skipped.  Valid
                                202                 :  *     options are "all-visible", "all-frozen", and "none".
                                203                 :  *
                                204                 :  * Returns to the SQL caller a set of tuples, each containing the location
                                205                 :  * and a description of a corruption found in the heap.
                                206                 :  *
                                207                 :  * This code goes to some trouble to avoid crashing the server even if the
                                208                 :  * table pages are badly corrupted, but it's probably not perfect. If
                                209                 :  * check_toast is true, we'll use regular index lookups to try to fetch TOAST
                                210                 :  * tuples, which can certainly cause crashes if the right kind of corruption
                                211                 :  * exists in the toast table or index. No matter what parameters you pass,
                                212                 :  * we can't protect against crashes that might occur trying to look up the
                                213                 :  * commit status of transaction IDs (though we avoid trying to do such lookups
                                214                 :  * for transaction IDs that can't legally appear in the table).
                                215                 :  */
                                216                 : Datum
  899 rhaas                     217 GIC        2718 : verify_heapam(PG_FUNCTION_ARGS)
                                218                 : {
                                219            2718 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                                220                 :     HeapCheckContext ctx;
  899 rhaas                     221 CBC        2718 :     Buffer      vmbuffer = InvalidBuffer;
                                222                 :     Oid         relid;
  899 rhaas                     223 ECB             :     bool        on_error_stop;
                                224                 :     bool        check_toast;
  899 rhaas                     225 CBC        2718 :     SkipPages   skip_option = SKIP_PAGES_NONE;
                                226                 :     BlockNumber first_block;
                                227                 :     BlockNumber last_block;
                                228                 :     BlockNumber nblocks;
  899 rhaas                     229 ECB             :     const char *skip;
                                230                 : 
                                231                 :     /* Check supplied arguments */
  899 rhaas                     232 GIC        2718 :     if (PG_ARGISNULL(0))
  899 rhaas                     233 UIC           0 :         ereport(ERROR,
                                234                 :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                235                 :                  errmsg("relation cannot be null")));
  899 rhaas                     236 CBC        2718 :     relid = PG_GETARG_OID(0);
  899 rhaas                     237 EUB             : 
  899 rhaas                     238 GIC        2718 :     if (PG_ARGISNULL(1))
  899 rhaas                     239 UIC           0 :         ereport(ERROR,
  899 rhaas                     240 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                241                 :                  errmsg("on_error_stop cannot be null")));
  899 rhaas                     242 CBC        2718 :     on_error_stop = PG_GETARG_BOOL(1);
  899 rhaas                     243 EUB             : 
  899 rhaas                     244 GIC        2718 :     if (PG_ARGISNULL(2))
  899 rhaas                     245 UIC           0 :         ereport(ERROR,
  899 rhaas                     246 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                247                 :                  errmsg("check_toast cannot be null")));
  899 rhaas                     248 CBC        2718 :     check_toast = PG_GETARG_BOOL(2);
  899 rhaas                     249 EUB             : 
  899 rhaas                     250 GIC        2718 :     if (PG_ARGISNULL(3))
  899 rhaas                     251 UIC           0 :         ereport(ERROR,
  899 rhaas                     252 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                253                 :                  errmsg("skip cannot be null")));
  899 rhaas                     254 CBC        2718 :     skip = text_to_cstring(PG_GETARG_TEXT_PP(3));
  899 rhaas                     255 GBC        2718 :     if (pg_strcasecmp(skip, "all-visible") == 0)
  899 rhaas                     256 GIC          84 :         skip_option = SKIP_PAGES_ALL_VISIBLE;
                                257            2634 :     else if (pg_strcasecmp(skip, "all-frozen") == 0)
  899 rhaas                     258 CBC          87 :         skip_option = SKIP_PAGES_ALL_FROZEN;
                                259            2547 :     else if (pg_strcasecmp(skip, "none") == 0)
                                260            2546 :         skip_option = SKIP_PAGES_NONE;
  899 rhaas                     261 ECB             :     else
  899 rhaas                     262 CBC           1 :         ereport(ERROR,
  899 rhaas                     263 ECB             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                264                 :                  errmsg("invalid skip option"),
                                265                 :                  errhint("Valid skip options are \"all-visible\", \"all-frozen\", and \"none\".")));
                                266                 : 
  899 rhaas                     267 GIC        2717 :     memset(&ctx, 0, sizeof(HeapCheckContext));
                                268            2717 :     ctx.cached_xid = InvalidTransactionId;
  732                           269            2717 :     ctx.toasted_attributes = NIL;
                                270                 : 
  738 rhaas                     271 ECB             :     /*
                                272                 :      * Any xmin newer than the xmin of our snapshot can't become all-visible
                                273                 :      * while we're running.
                                274                 :      */
  738 rhaas                     275 GIC        2717 :     ctx.safe_xmin = GetTransactionSnapshot()->xmin;
                                276                 : 
                                277                 :     /*
                                278                 :      * If we report corruption when not examining some individual attribute,
  898 tgl                       279 ECB             :      * we need attnum to be reported as NULL.  Set that up before any
                                280                 :      * corruption reporting might happen.
                                281                 :      */
  898 tgl                       282 GIC        2717 :     ctx.attnum = -1;
                                283                 : 
                                284                 :     /* Construct the tuplestore and tuple descriptor */
  173 michael                   285            2717 :     InitMaterializedSRF(fcinfo, 0);
  397 michael                   286 CBC        2717 :     ctx.tupdesc = rsinfo->setDesc;
  397 michael                   287 GIC        2717 :     ctx.tupstore = rsinfo->setResult;
                                288                 : 
  657 peter                     289 ECB             :     /* Open relation, check relkind and access method */
  899 rhaas                     290 CBC        2717 :     ctx.rel = relation_open(relid, AccessShareLock);
  640 peter                     291 ECB             : 
                                292                 :     /*
                                293                 :      * Check that a relation's relkind and access method are both supported.
                                294                 :      */
  492 peter                     295 GIC        2717 :     if (!RELKIND_HAS_TABLE_AM(ctx.rel->rd_rel->relkind) &&
  558                           296             195 :         ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE)
  640                           297               4 :         ereport(ERROR,
                                298                 :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
  640 peter                     299 ECB             :                  errmsg("cannot check relation \"%s\"",
                                300                 :                         RelationGetRelationName(ctx.rel)),
                                301                 :                  errdetail_relkind_not_supported(ctx.rel->rd_rel->relkind)));
                                302                 : 
                                303                 :     /*
                                304                 :      * Sequences always use heap AM, but they don't show that in the catalogs.
                                305                 :      * Other relkinds might be using a different AM, so check.
                                306                 :      */
  558 peter                     307 GIC        2713 :     if (ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
                                308            2522 :         ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
  640 peter                     309 UIC           0 :         ereport(ERROR,
                                310                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  640 peter                     311 ECB             :                  errmsg("only heap AM is supported")));
  899 rhaas                     312                 : 
  545 pg                        313 EUB             :     /*
                                314                 :      * Early exit for unlogged relations during recovery.  These will have no
                                315                 :      * relation fork, so there won't be anything to check.  We behave as if
                                316                 :      * the relation is empty.
                                317                 :      */
  545 pg                        318 GIC        2713 :     if (ctx.rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
  545 pg                        319 UIC           0 :         RecoveryInProgress())
                                320                 :     {
                                321               0 :         ereport(DEBUG1,
  545 pg                        322 ECB             :                 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
  545 pg                        323 EUB             :                  errmsg("cannot verify unlogged relation \"%s\" during recovery, skipping",
                                324                 :                         RelationGetRelationName(ctx.rel))));
  545 pg                        325 UBC           0 :         relation_close(ctx.rel, AccessShareLock);
  545 pg                        326 UIC           0 :         PG_RETURN_NULL();
                                327                 :     }
                                328                 : 
  899 rhaas                     329 EUB             :     /* Early exit if the relation is empty */
  899 rhaas                     330 GBC        2713 :     nblocks = RelationGetNumberOfBlocks(ctx.rel);
  899 rhaas                     331 GIC        2696 :     if (!nblocks)
                                332                 :     {
                                333            1495 :         relation_close(ctx.rel, AccessShareLock);
  899 rhaas                     334 CBC        1495 :         PG_RETURN_NULL();
  899 rhaas                     335 ECB             :     }
                                336                 : 
  899 rhaas                     337 CBC        1201 :     ctx.bstrategy = GetAccessStrategy(BAS_BULKREAD);
                                338            1201 :     ctx.buffer = InvalidBuffer;
  899 rhaas                     339 GIC        1201 :     ctx.page = NULL;
                                340                 : 
  899 rhaas                     341 ECB             :     /* Validate block numbers, or handle nulls. */
  899 rhaas                     342 CBC        1201 :     if (PG_ARGISNULL(4))
                                343            1078 :         first_block = 0;
                                344                 :     else
                                345                 :     {
                                346             123 :         int64       fb = PG_GETARG_INT64(4);
  899 rhaas                     347 ECB             : 
  899 rhaas                     348 GIC         123 :         if (fb < 0 || fb >= nblocks)
                                349               1 :             ereport(ERROR,
  899 rhaas                     350 ECB             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                351                 :                      errmsg("starting block number must be between 0 and %u",
                                352                 :                             nblocks - 1)));
  899 rhaas                     353 CBC         122 :         first_block = (BlockNumber) fb;
                                354                 :     }
  899 rhaas                     355 GIC        1200 :     if (PG_ARGISNULL(5))
                                356            1077 :         last_block = nblocks - 1;
  899 rhaas                     357 ECB             :     else
                                358                 :     {
  899 rhaas                     359 CBC         123 :         int64       lb = PG_GETARG_INT64(5);
  899 rhaas                     360 ECB             : 
  899 rhaas                     361 GIC         123 :         if (lb < 0 || lb >= nblocks)
                                362               1 :             ereport(ERROR,
  899 rhaas                     363 ECB             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                364                 :                      errmsg("ending block number must be between 0 and %u",
                                365                 :                             nblocks - 1)));
  899 rhaas                     366 CBC         122 :         last_block = (BlockNumber) lb;
                                367                 :     }
                                368                 : 
                                369                 :     /* Optionally open the toast relation, if any. */
                                370            1199 :     if (ctx.rel->rd_rel->reltoastrelid && check_toast)
  899 rhaas                     371 GIC         558 :     {
                                372                 :         int         offset;
                                373                 : 
  899 rhaas                     374 ECB             :         /* Main relation has associated toast relation */
  899 rhaas                     375 CBC         558 :         ctx.toast_rel = table_open(ctx.rel->rd_rel->reltoastrelid,
                                376                 :                                    AccessShareLock);
  899 rhaas                     377 GIC         558 :         offset = toast_open_indexes(ctx.toast_rel,
                                378                 :                                     AccessShareLock,
  899 rhaas                     379 ECB             :                                     &(ctx.toast_indexes),
                                380                 :                                     &(ctx.num_toast_indexes));
  899 rhaas                     381 CBC         558 :         ctx.valid_toast_index = ctx.toast_indexes[offset];
                                382                 :     }
                                383                 :     else
                                384                 :     {
  899 rhaas                     385 ECB             :         /*
                                386                 :          * Main relation has no associated toast relation, or we're
                                387                 :          * intentionally skipping it.
                                388                 :          */
  899 rhaas                     389 GIC         641 :         ctx.toast_rel = NULL;
                                390             641 :         ctx.toast_indexes = NULL;
                                391             641 :         ctx.num_toast_indexes = 0;
                                392                 :     }
  899 rhaas                     393 ECB             : 
  899 rhaas                     394 CBC        1199 :     update_cached_xid_range(&ctx);
                                395            1199 :     update_cached_mxid_range(&ctx);
  899 rhaas                     396 GIC        1199 :     ctx.relfrozenxid = ctx.rel->rd_rel->relfrozenxid;
                                397            1199 :     ctx.relfrozenfxid = FullTransactionIdFromXidAndCtx(ctx.relfrozenxid, &ctx);
  899 rhaas                     398 CBC        1199 :     ctx.relminmxid = ctx.rel->rd_rel->relminmxid;
  899 rhaas                     399 ECB             : 
  899 rhaas                     400 CBC        1199 :     if (TransactionIdIsNormal(ctx.relfrozenxid))
                                401            1008 :         ctx.oldest_xid = ctx.relfrozenxid;
  899 rhaas                     402 ECB             : 
  899 rhaas                     403 GIC       10423 :     for (ctx.blkno = first_block; ctx.blkno <= last_block; ctx.blkno++)
  899 rhaas                     404 ECB             :     {
                                405                 :         OffsetNumber maxoff;
                                406                 :         OffsetNumber predecessor[MaxOffsetNumber];
                                407                 :         OffsetNumber successor[MaxOffsetNumber];
                                408                 :         bool        lp_valid[MaxOffsetNumber];
                                409                 :         bool        xmin_commit_status_ok[MaxOffsetNumber];
                                410                 :         XidCommitStatus xmin_commit_status[MaxOffsetNumber];
                                411                 : 
  591 pg                        412 CBC        9227 :         CHECK_FOR_INTERRUPTS();
                                413                 : 
   18 rhaas                     414 GNC        9227 :         memset(predecessor, 0, sizeof(OffsetNumber) * MaxOffsetNumber);
                                415                 : 
                                416                 :         /* Optionally skip over all-frozen or all-visible blocks */
  899 rhaas                     417 GIC        9227 :         if (skip_option != SKIP_PAGES_NONE)
                                418                 :         {
                                419                 :             int32       mapbits;
                                420                 : 
                                421             735 :             mapbits = (int32) visibilitymap_get_status(ctx.rel, ctx.blkno,
                                422                 :                                                        &vmbuffer);
  899 rhaas                     423 CBC         735 :             if (skip_option == SKIP_PAGES_ALL_FROZEN)
                                424                 :             {
                                425             384 :                 if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0)
  899 rhaas                     426 GIC          33 :                     continue;
                                427                 :             }
  899 rhaas                     428 ECB             : 
  899 rhaas                     429 GIC         703 :             if (skip_option == SKIP_PAGES_ALL_VISIBLE)
                                430                 :             {
                                431             351 :                 if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0)
  899 rhaas                     432 CBC           1 :                     continue;
                                433                 :             }
  899 rhaas                     434 ECB             :         }
                                435                 : 
                                436                 :         /* Read and lock the next page. */
  899 rhaas                     437 CBC        9194 :         ctx.buffer = ReadBufferExtended(ctx.rel, MAIN_FORKNUM, ctx.blkno,
                                438                 :                                         RBM_NORMAL, ctx.bstrategy);
  899 rhaas                     439 GIC        9194 :         LockBuffer(ctx.buffer, BUFFER_LOCK_SHARE);
  899 rhaas                     440 CBC        9194 :         ctx.page = BufferGetPage(ctx.buffer);
                                441                 : 
  899 rhaas                     442 ECB             :         /* Perform tuple checks */
  899 rhaas                     443 CBC        9194 :         maxoff = PageGetMaxOffsetNumber(ctx.page);
  899 rhaas                     444 GIC      421498 :         for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
                                445          412304 :              ctx.offnum = OffsetNumberNext(ctx.offnum))
                                446                 :         {
                                447                 :             BlockNumber nextblkno;
                                448                 :             OffsetNumber nextoffnum;
                                449                 : 
   18 rhaas                     450 GNC      412304 :             successor[ctx.offnum] = InvalidOffsetNumber;
                                451          412304 :             lp_valid[ctx.offnum] = false;
                                452          412304 :             xmin_commit_status_ok[ctx.offnum] = false;
  899 rhaas                     453 GIC      412304 :             ctx.itemid = PageGetItemId(ctx.page, ctx.offnum);
  899 rhaas                     454 ECB             : 
                                455                 :             /* Skip over unused/dead line pointers */
  899 rhaas                     456 CBC      412304 :             if (!ItemIdIsUsed(ctx.itemid) || ItemIdIsDead(ctx.itemid))
                                457            3902 :                 continue;
                                458                 : 
                                459                 :             /*
  899 rhaas                     460 ECB             :              * If this line pointer has been redirected, check that it
  898 tgl                       461                 :              * redirects to a valid offset within the line pointer array
  899 rhaas                     462                 :              */
  899 rhaas                     463 GIC      408402 :             if (ItemIdIsRedirected(ctx.itemid))
                                464            3988 :             {
                                465            4009 :                 OffsetNumber rdoffnum = ItemIdGetRedirect(ctx.itemid);
                                466                 :                 ItemId      rditem;
  899 rhaas                     467 ECB             : 
  898 tgl                       468 CBC        4009 :                 if (rdoffnum < FirstOffsetNumber)
  898 tgl                       469 ECB             :                 {
  898 tgl                       470 CBC           6 :                     report_corruption(&ctx,
                                471                 :                                       psprintf("line pointer redirection to item at offset %u precedes minimum offset %u",
                                472                 :                                                (unsigned) rdoffnum,
  898 tgl                       473 ECB             :                                                (unsigned) FirstOffsetNumber));
  898 tgl                       474 CBC           6 :                     continue;
                                475                 :                 }
  898 tgl                       476 GIC        4003 :                 if (rdoffnum > maxoff)
                                477                 :                 {
  899 rhaas                     478              14 :                     report_corruption(&ctx,
                                479                 :                                       psprintf("line pointer redirection to item at offset %u exceeds maximum offset %u",
  899 rhaas                     480 ECB             :                                                (unsigned) rdoffnum,
                                481                 :                                                (unsigned) maxoff));
  899 rhaas                     482 CBC          14 :                     continue;
                                483                 :                 }
                                484                 : 
                                485                 :                 /*
                                486                 :                  * Since we've checked that this redirect points to a line
                                487                 :                  * pointer between FirstOffsetNumber and maxoff, it should
                                488                 :                  * now be safe to fetch the referenced line pointer. We expect
                                489                 :                  * it to be LP_NORMAL; if not, that's corruption.
                                490                 :                  */
  899 rhaas                     491 GIC        3989 :                 rditem = PageGetItemId(ctx.page, rdoffnum);
  899 rhaas                     492 CBC        3989 :                 if (!ItemIdIsUsed(rditem))
                                493                 :                 {
  899 rhaas                     494 UIC           0 :                     report_corruption(&ctx,
                                495                 :                                       psprintf("redirected line pointer points to an unused item at offset %u",
                                496                 :                                                (unsigned) rdoffnum));
   13 rhaas                     497 UNC           0 :                     continue;
                                498                 :                 }
   13 rhaas                     499 GNC        3989 :                 else if (ItemIdIsDead(rditem))
                                500                 :                 {
   13 rhaas                     501 UNC           0 :                     report_corruption(&ctx,
                                502                 :                                       psprintf("redirected line pointer points to a dead item at offset %u",
                                503                 :                                                (unsigned) rdoffnum));
                                504               0 :                     continue;
                                505                 :                 }
   13 rhaas                     506 GNC        3989 :                 else if (ItemIdIsRedirected(rditem))
                                507                 :                 {
                                508               1 :                     report_corruption(&ctx,
                                509                 :                                       psprintf("redirected line pointer points to another redirected line pointer at offset %u",
                                510                 :                                                (unsigned) rdoffnum));
                                511               1 :                     continue;
                                512                 :                 }
                                513                 : 
                                514                 :                 /*
                                515                 :                  * Record the fact that this line pointer has passed basic
                                516                 :                  * sanity checking, and also the offset number to which it
                                517                 :                  * points.
                                518                 :                  */
   18                           519            3988 :                 lp_valid[ctx.offnum] = true;
                                520            3988 :                 successor[ctx.offnum] = rdoffnum;
  899 rhaas                     521 GIC        3988 :                 continue;
                                522                 :             }
  899 rhaas                     523 ECB             : 
                                524                 :             /* Sanity-check the line pointer's offset and length values */
  899 rhaas                     525 CBC      404393 :             ctx.lp_len = ItemIdGetLength(ctx.itemid);
  898 tgl                       526 GIC      404393 :             ctx.lp_off = ItemIdGetOffset(ctx.itemid);
  898 tgl                       527 ECB             : 
  898 tgl                       528 GIC      404393 :             if (ctx.lp_off != MAXALIGN(ctx.lp_off))
                                529                 :             {
                                530               6 :                 report_corruption(&ctx,
  898 tgl                       531 ECB             :                                   psprintf("line pointer to page offset %u is not maximally aligned",
  898 tgl                       532 GIC           6 :                                            ctx.lp_off));
                                533               6 :                 continue;
                                534                 :             }
                                535          404387 :             if (ctx.lp_len < MAXALIGN(SizeofHeapTupleHeader))
                                536                 :             {
                                537              12 :                 report_corruption(&ctx,
                                538                 :                                   psprintf("line pointer length %u is less than the minimum tuple header size %u",
                                539              12 :                                            ctx.lp_len,
  898 tgl                       540 ECB             :                                            (unsigned) MAXALIGN(SizeofHeapTupleHeader)));
  898 tgl                       541 CBC          12 :                 continue;
                                542                 :             }
  898 tgl                       543 GBC      404375 :             if (ctx.lp_off + ctx.lp_len > BLCKSZ)
                                544                 :             {
  898 tgl                       545 GIC          14 :                 report_corruption(&ctx,
  898 tgl                       546 EUB             :                                   psprintf("line pointer to page offset %u with length %u ends beyond maximum page offset %u",
  898 tgl                       547 GIC          14 :                                            ctx.lp_off,
  898 tgl                       548 CBC          14 :                                            ctx.lp_len,
                                549                 :                                            (unsigned) BLCKSZ));
  898 tgl                       550 GBC          14 :                 continue;
                                551                 :             }
                                552                 : 
  898 tgl                       553 EUB             :             /* It should be safe to examine the tuple's header, at least */
   18 rhaas                     554 GNC      404361 :             lp_valid[ctx.offnum] = true;
  899 rhaas                     555 GIC      404361 :             ctx.tuphdr = (HeapTupleHeader) PageGetItem(ctx.page, ctx.itemid);
  899 rhaas                     556 CBC      404361 :             ctx.natts = HeapTupleHeaderGetNatts(ctx.tuphdr);
                                557                 : 
  899 rhaas                     558 ECB             :             /* Ok, ready to check this next tuple */
   18 rhaas                     559 GNC      404361 :             check_tuple(&ctx,
                                560          404361 :                         &xmin_commit_status_ok[ctx.offnum],
                                561          404361 :                         &xmin_commit_status[ctx.offnum]);
                                562                 : 
                                563                 :             /*
                                564                 :              * If the CTID field of this tuple seems to point to another tuple
                                565                 :              * on the same page, record that tuple as the successor of this
                                566                 :              * one.
                                567                 :              */
                                568          404361 :             nextblkno = ItemPointerGetBlockNumber(&(ctx.tuphdr)->t_ctid);
                                569          404361 :             nextoffnum = ItemPointerGetOffsetNumber(&(ctx.tuphdr)->t_ctid);
   17                           570          404361 :             if (nextblkno == ctx.blkno && nextoffnum != ctx.offnum &&
                                571             128 :                 nextoffnum >= FirstOffsetNumber && nextoffnum <= maxoff)
   18                           572             128 :                 successor[ctx.offnum] = nextoffnum;
                                573                 :         }
                                574                 : 
                                575                 :         /*
                                576                 :          * Update chain validation. Check each line pointer that's got a valid
                                577                 :          * successor against that successor.
                                578                 :          */
                                579            9194 :         ctx.attnum = -1;
                                580          421498 :         for (ctx.offnum = FirstOffsetNumber; ctx.offnum <= maxoff;
                                581          412304 :              ctx.offnum = OffsetNumberNext(ctx.offnum))
                                582                 :         {
                                583                 :             ItemId      curr_lp;
                                584                 :             ItemId      next_lp;
                                585                 :             HeapTupleHeader curr_htup;
                                586                 :             HeapTupleHeader next_htup;
                                587                 :             TransactionId curr_xmin;
                                588                 :             TransactionId curr_xmax;
                                589                 :             TransactionId next_xmin;
                                590          412304 :             OffsetNumber nextoffnum = successor[ctx.offnum];
                                591                 : 
                                592                 :             /*
                                593                 :              * The current line pointer may not have a successor, either
                                594                 :              * because it's not valid or because it didn't point to anything.
                                595                 :              * In either case, we have to give up.
                                596                 :              *
                                597                 :              * If the current line pointer does point to something, it's
                                598                 :              * possible that the target line pointer isn't valid. We have to
                                599                 :              * give up in that case, too.
                                600                 :              */
                                601          412304 :             if (nextoffnum == InvalidOffsetNumber || !lp_valid[nextoffnum])
                                602          408188 :                 continue;
                                603                 : 
                                604                 :             /* We have two valid line pointers that we can examine. */
                                605            4116 :             curr_lp = PageGetItemId(ctx.page, ctx.offnum);
                                606            4116 :             next_lp = PageGetItemId(ctx.page, nextoffnum);
                                607                 : 
                                608                 :             /* Handle the cases where the current line pointer is a redirect. */
                                609            4116 :             if (ItemIdIsRedirected(curr_lp))
                                610                 :             {
                                611                 :                 /*
                                612                 :                  * We should not have set successor[ctx.offnum] to a value
                                613                 :                  * other than InvalidOffsetNumber unless that line pointer
                                614                 :                  * is LP_NORMAL.
                                615                 :                  */
   13                           616            3988 :                 Assert(ItemIdIsNormal(next_lp));
                                617                 : 
                                618                 :                 /* Can only redirect to a HOT tuple. */
   18                           619            3988 :                 next_htup = (HeapTupleHeader) PageGetItem(ctx.page, next_lp);
                                620            3988 :                 if (!HeapTupleHeaderIsHeapOnly(next_htup))
                                621                 :                 {
                                622               1 :                     report_corruption(&ctx,
                                623                 :                                       psprintf("redirected line pointer points to a non-heap-only tuple at offset %u",
                                624                 :                                                (unsigned) nextoffnum));
                                625                 :                 }
                                626                 : 
                                627                 :                 /* HOT chains should not intersect. */
                                628            3988 :                 if (predecessor[nextoffnum] != InvalidOffsetNumber)
                                629                 :                 {
                                630               1 :                     report_corruption(&ctx,
                                631                 :                                       psprintf("redirect line pointer points to offset %u, but offset %u also points there",
                                632               1 :                                                (unsigned) nextoffnum, (unsigned) predecessor[nextoffnum]));
                                633               1 :                     continue;
                                634                 :                 }
                                635                 : 
                                636                 :                 /*
                                637                 :                  * This redirect and the tuple to which it points seem to be
                                638                 :                  * part of an update chain.
                                639                 :                  */
                                640            3987 :                 predecessor[nextoffnum] = ctx.offnum;
                                641            3987 :                 continue;
                                642                 :             }
                                643                 : 
                                644                 :             /*
                                645                 :              * If the next line pointer is a redirect, or if it's a tuple
                                646                 :              * but the XMAX of this tuple doesn't match the XMIN of the next
                                647                 :              * tuple, then the two aren't part of the same update chain and
                                648                 :              * there is nothing more to do.
                                649                 :              */
                                650             128 :             if (ItemIdIsRedirected(next_lp))
   18 rhaas                     651 UNC           0 :                 continue;
   18 rhaas                     652 GNC         128 :             curr_htup = (HeapTupleHeader) PageGetItem(ctx.page, curr_lp);
                                653             128 :             curr_xmax = HeapTupleHeaderGetUpdateXid(curr_htup);
                                654             128 :             next_htup = (HeapTupleHeader) PageGetItem(ctx.page, next_lp);
                                655             128 :             next_xmin = HeapTupleHeaderGetXmin(next_htup);
                                656             128 :             if (!TransactionIdIsValid(curr_xmax) ||
                                657                 :                 !TransactionIdEquals(curr_xmax, next_xmin))
                                658               4 :                 continue;
                                659                 : 
                                660                 :             /* HOT chains should not intersect. */
                                661             124 :             if (predecessor[nextoffnum] != InvalidOffsetNumber)
                                662                 :             {
                                663               1 :                 report_corruption(&ctx,
                                664                 :                                   psprintf("tuple points to new version at offset %u, but offset %u also points there",
                                665               1 :                                            (unsigned) nextoffnum, (unsigned) predecessor[nextoffnum]));
                                666               1 :                 continue;
                                667                 :             }
                                668                 : 
                                669                 :             /*
                                670                 :              * This tuple and the tuple to which it points seem to be part
                                671                 :              * of an update chain.
                                672                 :              */
                                673             123 :             predecessor[nextoffnum] = ctx.offnum;
                                674                 : 
                                675                 :             /*
                                676                 :              * If the current tuple is marked as HOT-updated, then the next
                                677                 :              * tuple should be marked as a heap-only tuple. Conversely, if the
                                678                 :              * current tuple isn't marked as HOT-updated, then the next tuple
                                679                 :              * shouldn't be marked as a heap-only tuple.
                                680                 :              *
                                681                 :              * NB: Can't use HeapTupleHeaderIsHotUpdated() as it checks if
                                682                 :              * hint bits indicate xmin/xmax aborted.
                                683                 :              */
   17                           684             123 :             if (!(curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
   18                           685               1 :                 HeapTupleHeaderIsHeapOnly(next_htup))
                                686                 :             {
                                687               1 :                 report_corruption(&ctx,
                                688                 :                                   psprintf("non-heap-only update produced a heap-only tuple at offset %u",
                                689                 :                                            (unsigned) nextoffnum));
                                690                 :             }
   17                           691             123 :             if ((curr_htup->t_infomask2 & HEAP_HOT_UPDATED) &&
   18                           692             122 :                 !HeapTupleHeaderIsHeapOnly(next_htup))
                                693                 :             {
                                694               1 :                 report_corruption(&ctx,
                                695                 :                                   psprintf("heap-only update produced a non-heap only tuple at offset %u",
                                696                 :                                            (unsigned) nextoffnum));
                                697                 :             }
                                698                 : 
                                699                 :             /*
                                700                 :              * If the current tuple's xmin is still in progress but the
                                701                 :              * successor tuple's xmin is committed, that's corruption.
                                702                 :              *
                                703                 :              * NB: We recheck the commit status of the current tuple's xmin
                                704                 :              * here, because it might have committed after we checked it and
                                705                 :              * before we checked the commit status of the successor tuple's
                                706                 :              * xmin. This should be safe because the xmin itself can't have
                                707                 :              * changed, only its commit status.
                                708                 :              */
                                709             123 :             curr_xmin = HeapTupleHeaderGetXmin(curr_htup);
                                710             123 :             if (xmin_commit_status_ok[ctx.offnum] &&
                                711             123 :                 xmin_commit_status[ctx.offnum] == XID_IN_PROGRESS &&
                                712               1 :                 xmin_commit_status_ok[nextoffnum] &&
                                713               2 :                 xmin_commit_status[nextoffnum] == XID_COMMITTED &&
                                714               1 :                 TransactionIdIsInProgress(curr_xmin))
                                715                 :             {
                                716               1 :                 report_corruption(&ctx,
                                717                 :                                   psprintf("tuple with in-progress xmin %u was updated to produce a tuple at offset %u with committed xmin %u",
                                718                 :                                            (unsigned) curr_xmin,
                                719               1 :                                            (unsigned) ctx.offnum,
                                720                 :                                            (unsigned) next_xmin));
                                721                 :             }
                                722                 : 
                                723                 :             /*
                                724                 :              * If the current tuple's xmin is aborted but the successor tuple's
                                725                 :              * xmin is in-progress or committed, that's corruption.
                                726                 :              */
                                727             123 :             if (xmin_commit_status_ok[ctx.offnum] &&
                                728             123 :                 xmin_commit_status[ctx.offnum] == XID_ABORTED &&
                                729               2 :                 xmin_commit_status_ok[nextoffnum])
                                730                 :             {
                                731               2 :                 if (xmin_commit_status[nextoffnum] == XID_IN_PROGRESS)
                                732               1 :                     report_corruption(&ctx,
                                733                 :                                       psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %u with in-progress xmin %u",
                                734                 :                                                (unsigned) curr_xmin,
                                735               1 :                                                (unsigned) ctx.offnum,
                                736                 :                                                (unsigned) next_xmin));
                                737               1 :                 else if (xmin_commit_status[nextoffnum] == XID_COMMITTED)
                                738               1 :                     report_corruption(&ctx,
                                739                 :                                       psprintf("tuple with aborted xmin %u was updated to produce a tuple at offset %u with committed xmin %u",
                                740                 :                                                (unsigned) curr_xmin,
                                741               1 :                                                (unsigned) ctx.offnum,
                                742                 :                                                (unsigned) next_xmin));
                                743                 :             }
                                744                 :         }
                                745                 : 
                                746                 :         /*
                                747                 :          * An update chain can start either with a non-heap-only tuple or with
                                748                 :          * a redirect line pointer, but not with a heap-only tuple.
                                749                 :          *
                                750                 :          * (This check is in a separate loop because we need the predecessor
                                751                 :          * array to be fully populated before we can perform it.)
                                752                 :          */
                                753            9194 :         for (ctx.offnum = FirstOffsetNumber;
                                754          421498 :              ctx.offnum <= maxoff;
                                755          412304 :              ctx.offnum = OffsetNumberNext(ctx.offnum))
                                756                 :         {
                                757          412304 :             if (xmin_commit_status_ok[ctx.offnum] &&
                                758          404352 :                 (xmin_commit_status[ctx.offnum] == XID_COMMITTED ||
                                759               7 :                  xmin_commit_status[ctx.offnum] == XID_IN_PROGRESS) &&
                                760          404347 :                 predecessor[ctx.offnum] == InvalidOffsetNumber)
                                761                 :             {
                                762                 :                 ItemId      curr_lp;
                                763                 : 
                                764          400240 :                 curr_lp = PageGetItemId(ctx.page, ctx.offnum);
                                765          400240 :                 if (!ItemIdIsRedirected(curr_lp))
                                766                 :                 {
                                767                 :                     HeapTupleHeader curr_htup;
                                768                 : 
                                769                 :                     curr_htup = (HeapTupleHeader)
                                770          400240 :                         PageGetItem(ctx.page, curr_lp);
                                771          400240 :                     if (HeapTupleHeaderIsHeapOnly(curr_htup))
                                772               4 :                         report_corruption(&ctx,
                                773                 :                                           psprintf("tuple is root of chain but is marked as heap-only tuple"));
                                774                 :                 }
                                775                 :             }
                                776                 :         }
  899 rhaas                     777 ECB             : 
                                778                 :         /* clean up */
  899 rhaas                     779 GIC        9194 :         UnlockReleaseBuffer(ctx.buffer);
                                780                 : 
                                781                 :         /*
                                782                 :          * Check any toast pointers from the page whose lock we just released
                                783                 :          */
  732                           784            9194 :         if (ctx.toasted_attributes != NIL)
  732 rhaas                     785 ECB             :         {
                                786                 :             ListCell   *cell;
                                787                 : 
  732 rhaas                     788 GIC       12029 :             foreach(cell, ctx.toasted_attributes)
                                789           11305 :                 check_toasted_attribute(&ctx, lfirst(cell));
                                790             724 :             list_free_deep(ctx.toasted_attributes);
  732 rhaas                     791 CBC         724 :             ctx.toasted_attributes = NIL;
  732 rhaas                     792 ECB             :         }
                                793                 : 
  899 rhaas                     794 CBC        9191 :         if (on_error_stop && ctx.is_corrupt)
  899 rhaas                     795 UIC           0 :             break;
  899 rhaas                     796 ECB             :     }
                                797                 : 
  899 rhaas                     798 CBC        1196 :     if (vmbuffer != InvalidBuffer)
                                799               3 :         ReleaseBuffer(vmbuffer);
                                800                 : 
  899 rhaas                     801 ECB             :     /* Close the associated toast table and indexes, if any. */
  899 rhaas                     802 GIC        1196 :     if (ctx.toast_indexes)
  899 rhaas                     803 CBC         555 :         toast_close_indexes(ctx.toast_indexes, ctx.num_toast_indexes,
                                804                 :                             AccessShareLock);
                                805            1196 :     if (ctx.toast_rel)
  899 rhaas                     806 GIC         555 :         table_close(ctx.toast_rel, AccessShareLock);
  899 rhaas                     807 ECB             : 
                                808                 :     /* Close the main relation */
  899 rhaas                     809 CBC        1196 :     relation_close(ctx.rel, AccessShareLock);
                                810                 : 
                                811            1196 :     PG_RETURN_NULL();
                                812                 : }
  899 rhaas                     813 ECB             : 
                                814                 : /*
                                815                 :  * Shared internal implementation for report_corruption and
  732                           816                 :  * report_toast_corruption.
                                817                 :  */
                                818                 : static void
  732 rhaas                     819 GIC          86 : report_corruption_internal(Tuplestorestate *tupstore, TupleDesc tupdesc,
  732 rhaas                     820 ECB             :                            BlockNumber blkno, OffsetNumber offnum,
                                821                 :                            AttrNumber attnum, char *msg)
  899                           822                 : {
  267 peter                     823 GNC          86 :     Datum       values[HEAPCHECK_RELATION_COLS] = {0};
                                824              86 :     bool        nulls[HEAPCHECK_RELATION_COLS] = {0};
  899 rhaas                     825 ECB             :     HeapTuple   tuple;
                                826                 : 
  732 rhaas                     827 GIC          86 :     values[0] = Int64GetDatum(blkno);
                                828              86 :     values[1] = Int32GetDatum(offnum);
                                829              86 :     values[2] = Int32GetDatum(attnum);
                                830              86 :     nulls[2] = (attnum < 0);
  899                           831              86 :     values[3] = CStringGetTextDatum(msg);
  899 rhaas                     832 ECB             : 
                                833                 :     /*
                                834                 :      * In principle, there is nothing to prevent a scan over a large, highly
                                835                 :      * corrupted table from using work_mem worth of memory building up the
                                836                 :      * tuplestore.  That's ok, but if we also leak the msg argument memory
                                837                 :      * until the end of the query, we could exceed work_mem by more than a
                                838                 :      * trivial amount.  Therefore, free the msg argument each time we are
                                839                 :      * called rather than waiting for our current memory context to be freed.
                                840                 :      */
  899 rhaas                     841 GIC          86 :     pfree(msg);
                                842                 : 
  732 rhaas                     843 CBC          86 :     tuple = heap_form_tuple(tupdesc, values, nulls);
                                844              86 :     tuplestore_puttuple(tupstore, tuple);
                                845              86 : }
                                846                 : 
                                847                 : /*
                                848                 :  * Record a single corruption found in the main table.  The values in ctx should
                                849                 :  * indicate the location of the corruption, and the msg argument should contain
                                850                 :  * a human-readable description of the corruption.
                                851                 :  *
                                852                 :  * The msg argument is pfree'd by this function.
                                853                 :  */
  732 rhaas                     854 ECB             : static void
  732 rhaas                     855 GIC          85 : report_corruption(HeapCheckContext *ctx, char *msg)
                                856                 : {
                                857              85 :     report_corruption_internal(ctx->tupstore, ctx->tupdesc, ctx->blkno,
                                858              85 :                                ctx->offnum, ctx->attnum, msg);
                                859              85 :     ctx->is_corrupt = true;
                                860              85 : }
                                861                 : 
                                862                 : /*
                                863                 :  * Record corruption found in the toast table.  The values in ta should
                                864                 :  * indicate the location in the main table where the toast pointer was
  732 rhaas                     865 ECB             :  * encountered, and the msg argument should contain a human-readable
                                866                 :  * description of the toast table corruption.
                                867                 :  *
                                868                 :  * As above, the msg argument is pfree'd by this function.
                                869                 :  */
                                870                 : static void
  732 rhaas                     871 GIC           1 : report_toast_corruption(HeapCheckContext *ctx, ToastedAttribute *ta,
                                872                 :                         char *msg)
  732 rhaas                     873 ECB             : {
  732 rhaas                     874 GIC           1 :     report_corruption_internal(ctx->tupstore, ctx->tupdesc, ta->blkno,
                                875               1 :                                ta->offnum, ta->attnum, msg);
  899                           876               1 :     ctx->is_corrupt = true;
                                877               1 : }
                                878                 : 
                                879                 : /*
  738 rhaas                     880 ECB             :  * Check for tuple header corruption.
                                881                 :  *
                                882                 :  * Some kinds of corruption make it unsafe to check the tuple attributes, for
  899                           883                 :  * example when the line pointer refers to a range of bytes outside the page.
  738                           884                 :  * In such cases, we return false (not checkable) after recording appropriate
                                885                 :  * corruption messages.
  899                           886                 :  *
                                887                 :  * Some other kinds of tuple header corruption confuse the question of where
                                888                 :  * the tuple attributes begin, or how long the nulls bitmap is, etc., making it
                                889                 :  * unreasonable to attempt to check attributes, even if all candidate answers
                                890                 :  * to those questions would not result in reading past the end of the line
                                891                 :  * pointer or page.  In such cases, like above, we record corruption messages
                                892                 :  * about the header and then return false.
                                893                 :  *
                                894                 :  * Other kinds of tuple header corruption do not bear on the question of
                                895                 :  * whether the tuple attributes can be checked, so we record corruption
  738                           896                 :  * messages for them but we do not return false merely because we detected
                                897                 :  * them.
                                898                 :  *
                                899                 :  * Returns whether the tuple is sufficiently sensible to undergo visibility and
                                900                 :  * attribute checks.
                                901                 :  */
                                902                 : static bool
  738 rhaas                     903 GIC      404361 : check_tuple_header(HeapCheckContext *ctx)
  899 rhaas                     904 ECB             : {
  738 rhaas                     905 CBC      404361 :     HeapTupleHeader tuphdr = ctx->tuphdr;
  899 rhaas                     906 GIC      404361 :     uint16      infomask = tuphdr->t_infomask;
   18 rhaas                     907 GNC      404361 :     TransactionId curr_xmax = HeapTupleHeaderGetUpdateXid(tuphdr);
  738 rhaas                     908 GIC      404361 :     bool        result = true;
                                909                 :     unsigned    expected_hoff;
                                910                 : 
  899                           911          404361 :     if (ctx->tuphdr->t_hoff > ctx->lp_len)
                                912                 :     {
                                913               1 :         report_corruption(ctx,
                                914                 :                           psprintf("data begins at offset %u beyond the tuple length %u",
  899 rhaas                     915 CBC           1 :                                    ctx->tuphdr->t_hoff, ctx->lp_len));
  738 rhaas                     916 GBC           1 :         result = false;
  899 rhaas                     917 ECB             :     }
                                918                 : 
  899 rhaas                     919 CBC      404361 :     if ((ctx->tuphdr->t_infomask & HEAP_XMAX_COMMITTED) &&
                                920              89 :         (ctx->tuphdr->t_infomask & HEAP_XMAX_IS_MULTI))
  899 rhaas                     921 ECB             :     {
  899 rhaas                     922 GIC           2 :         report_corruption(ctx,
  899 rhaas                     923 ECB             :                           pstrdup("multixact should not be marked committed"));
                                924                 : 
                                925                 :         /*
  738                           926                 :          * This condition is clearly wrong, but it's not enough to justify
                                927                 :          * skipping further checks, because we don't rely on this to determine
                                928                 :          * whether the tuple is visible or to interpret other relevant header
                                929                 :          * fields.
  899                           930                 :          */
                                931                 :     }
                                932                 : 
   18 rhaas                     933 GNC      404361 :     if (!TransactionIdIsValid(curr_xmax) &&
                                934          404172 :         HeapTupleHeaderIsHotUpdated(tuphdr))
                                935                 :     {
                                936               1 :         report_corruption(ctx,
                                937                 :                           psprintf("tuple has been HOT updated, but xmax is 0"));
                                938                 : 
                                939                 :         /*
                                940                 :          * As above, even though this shouldn't happen, it's not sufficient
                                941                 :          * justification for skipping further checks, we should still be able
                                942                 :          * to perform sensibly.
                                943                 :          */
                                944                 :     }
                                945                 : 
   13                           946          404361 :     if (HeapTupleHeaderIsHeapOnly(tuphdr) &&
                                947            4112 :         ((tuphdr->t_infomask & HEAP_UPDATED) == 0))
                                948                 :     {
                                949               1 :         report_corruption(ctx,
                                950                 :                           psprintf("tuple is heap only, but not the result of an update"));
                                951                 : 
                                952                 :         /* Here again, we can still perform further checks. */
                                953                 :     }
                                954                 : 
  899 rhaas                     955 GIC      404361 :     if (infomask & HEAP_HASNULL)
                                956          181974 :         expected_hoff = MAXALIGN(SizeofHeapTupleHeader + BITMAPLEN(ctx->natts));
                                957                 :     else
                                958          222387 :         expected_hoff = MAXALIGN(SizeofHeapTupleHeader);
                                959          404361 :     if (ctx->tuphdr->t_hoff != expected_hoff)
  899 rhaas                     960 ECB             :     {
  899 rhaas                     961 GIC           5 :         if ((infomask & HEAP_HASNULL) && ctx->natts == 1)
  899 rhaas                     962 UIC           0 :             report_corruption(ctx,
                                963                 :                               psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, has nulls)",
                                964               0 :                                        expected_hoff, ctx->tuphdr->t_hoff));
  899 rhaas                     965 GIC           5 :         else if ((infomask & HEAP_HASNULL))
                                966               1 :             report_corruption(ctx,
                                967                 :                               psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, has nulls)",
                                968               1 :                                        expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
                                969               4 :         else if (ctx->natts == 1)
  899 rhaas                     970 UIC           0 :             report_corruption(ctx,
  899 rhaas                     971 ECB             :                               psprintf("tuple data should begin at byte %u, but actually begins at byte %u (1 attribute, no nulls)",
  899 rhaas                     972 LBC           0 :                                        expected_hoff, ctx->tuphdr->t_hoff));
                                973                 :         else
  899 rhaas                     974 CBC           4 :             report_corruption(ctx,
                                975                 :                               psprintf("tuple data should begin at byte %u, but actually begins at byte %u (%u attributes, no nulls)",
  899 rhaas                     976 GIC           4 :                                        expected_hoff, ctx->tuphdr->t_hoff, ctx->natts));
  738                           977               5 :         result = false;
  899 rhaas                     978 ECB             :     }
                                979                 : 
  738 rhaas                     980 GIC      404361 :     return result;
  738 rhaas                     981 ECB             : }
                                982                 : 
                                983                 : /*
                                984                 :  * Checks tuple visibility so we know which further checks are safe to
                                985                 :  * perform.
                                986                 :  *
                                987                 :  * If a tuple could have been inserted by a transaction that also added a
                                988                 :  * column to the table, but which ultimately did not commit, or which has not
                                989                 :  * yet committed, then the table's current TupleDesc might differ from the one
                                990                 :  * used to construct this tuple, so we must not check it.
                                991                 :  *
                                992                 :  * As a special case, if our own transaction inserted the tuple, even if we
                                993                 :  * added a column to the table, our TupleDesc should match.  We could check the
                                994                 :  * tuple, but choose not to do so.
                                995                 :  *
                                996                 :  * If a tuple has been updated or deleted, we can still read the old tuple for
                                997                 :  * corruption checking purposes, as long as we are careful about concurrent
                                998                 :  * vacuums.  The main table tuple itself cannot be vacuumed away because we
                                999                 :  * hold a buffer lock on the page, but if the deleting transaction is older
                               1000                 :  * than our transaction snapshot's xmin, then vacuum could remove the toast at
                               1001                 :  * any time, so we must not try to follow TOAST pointers.
                               1002                 :  *
                               1003                 :  * If xmin or xmax values are older than can be checked against clog, or appear
                               1004                 :  * to be in the future (possibly due to wrap-around), then we cannot make a
                               1005                 :  * determination about the visibility of the tuple, so we skip further checks.
                               1006                 :  *
                               1007                 :  * Returns true if the tuple itself should be checked, false otherwise.  Sets
                               1008                 :  * ctx->tuple_could_be_pruned if the tuple -- and thus also any associated
                               1009                 :  * TOAST tuples -- are eligible for pruning.
                               1010                 :  *
                               1011                 :  * Sets *xmin_commit_status_ok to true if the commit status of xmin is known
                               1012                 :  * and false otherwise. If it's set to true, then also set *xid_commit_status
                               1013                 :  * to the actual commit status.
                               1014                 :  */
                               1015                 : static bool
   18 rhaas                    1016 GNC      404356 : check_tuple_visibility(HeapCheckContext *ctx, bool *xmin_commit_status_ok,
                               1017                 :                        XidCommitStatus *xmin_commit_status)
                               1018                 : {
  738 rhaas                    1019 ECB             :     TransactionId xmin;
                               1020                 :     TransactionId xvac;
                               1021                 :     TransactionId xmax;
                               1022                 :     XidCommitStatus xmin_status;
                               1023                 :     XidCommitStatus xvac_status;
                               1024                 :     XidCommitStatus xmax_status;
  738 rhaas                    1025 GIC      404356 :     HeapTupleHeader tuphdr = ctx->tuphdr;
                               1026                 : 
  738 rhaas                    1027 CBC      404356 :     ctx->tuple_could_be_pruned = true;   /* have not yet proven otherwise */
   18 rhaas                    1028 GNC      404356 :     *xmin_commit_status_ok = false;     /* have not yet proven otherwise */
                               1029                 : 
  738 rhaas                    1030 ECB             :     /* If xmin is normal, it should be within valid range */
  738 rhaas                    1031 CBC      404356 :     xmin = HeapTupleHeaderGetXmin(tuphdr);
  738 rhaas                    1032 GIC      404356 :     switch (get_xid_status(xmin, ctx, &xmin_status))
                               1033                 :     {
  738 rhaas                    1034 LBC           0 :         case XID_INVALID:
                               1035                 :             /* Could be the result of a speculative insertion that aborted. */
   17 rhaas                    1036 UIC           0 :             return false;
  738 rhaas                    1037 GIC      404352 :         case XID_BOUNDS_OK:
   18 rhaas                    1038 GNC      404352 :             *xmin_commit_status_ok = true;
                               1039          404352 :             *xmin_commit_status = xmin_status;
  738 rhaas                    1040 GIC      404352 :             break;
                               1041               1 :         case XID_IN_FUTURE:
                               1042               1 :             report_corruption(ctx,
                               1043                 :                               psprintf("xmin %u equals or exceeds next valid transaction ID %u:%u",
                               1044                 :                                        xmin,
                               1045               1 :                                        EpochFromFullTransactionId(ctx->next_fxid),
                               1046               1 :                                        XidFromFullTransactionId(ctx->next_fxid)));
                               1047               1 :             return false;
  738 rhaas                    1048 CBC           2 :         case XID_PRECEDES_CLUSTERMIN:
                               1049               2 :             report_corruption(ctx,
  738 rhaas                    1050 ECB             :                               psprintf("xmin %u precedes oldest valid transaction ID %u:%u",
                               1051                 :                                        xmin,
  738 rhaas                    1052 CBC           2 :                                        EpochFromFullTransactionId(ctx->oldest_fxid),
                               1053               2 :                                        XidFromFullTransactionId(ctx->oldest_fxid)));
                               1054               2 :             return false;
                               1055               1 :         case XID_PRECEDES_RELMIN:
  738 rhaas                    1056 GIC           1 :             report_corruption(ctx,
                               1057                 :                               psprintf("xmin %u precedes relation freeze threshold %u:%u",
                               1058                 :                                        xmin,
  738 rhaas                    1059 CBC           1 :                                        EpochFromFullTransactionId(ctx->relfrozenfxid),
                               1060               1 :                                        XidFromFullTransactionId(ctx->relfrozenfxid)));
  738 rhaas                    1061 GIC           1 :             return false;
                               1062                 :     }
                               1063                 : 
                               1064                 :     /*
  738 rhaas                    1065 ECB             :      * Has inserting transaction committed?
  899                          1066                 :      */
  899 rhaas                    1067 CBC      404352 :     if (!HeapTupleHeaderXminCommitted(tuphdr))
                               1068                 :     {
  899 rhaas                    1069 GIC       13801 :         if (HeapTupleHeaderXminInvalid(tuphdr))
  738 rhaas                    1070 UIC           0 :             return false;       /* inserter aborted, don't check */
                               1071                 :         /* Used by pre-9.0 binary upgrades */
  738 rhaas                    1072 GIC       13801 :         else if (tuphdr->t_infomask & HEAP_MOVED_OFF)
                               1073                 :         {
  738 rhaas                    1074 LBC           0 :             xvac = HeapTupleHeaderGetXvac(tuphdr);
                               1075                 : 
  738 rhaas                    1076 UIC           0 :             switch (get_xid_status(xvac, ctx, &xvac_status))
                               1077                 :             {
  899                          1078               0 :                 case XID_INVALID:
  899 rhaas                    1079 LBC           0 :                     report_corruption(ctx,
                               1080                 :                                       pstrdup("old-style VACUUM FULL transaction ID for moved off tuple is invalid"));
  738 rhaas                    1081 UIC           0 :                     return false;
  899                          1082               0 :                 case XID_IN_FUTURE:
  899 rhaas                    1083 LBC           0 :                     report_corruption(ctx,
  738 rhaas                    1084 ECB             :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple equals or exceeds next valid transaction ID %u:%u",
  899                          1085                 :                                                xvac,
  899 rhaas                    1086 LBC           0 :                                                EpochFromFullTransactionId(ctx->next_fxid),
  899 rhaas                    1087 UIC           0 :                                                XidFromFullTransactionId(ctx->next_fxid)));
  738                          1088               0 :                     return false;
  899 rhaas                    1089 LBC           0 :                 case XID_PRECEDES_RELMIN:
  899 rhaas                    1090 UBC           0 :                     report_corruption(ctx,
                               1091                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple precedes relation freeze threshold %u:%u",
                               1092                 :                                                xvac,
  899 rhaas                    1093 LBC           0 :                                                EpochFromFullTransactionId(ctx->relfrozenfxid),
                               1094               0 :                                                XidFromFullTransactionId(ctx->relfrozenfxid)));
  738 rhaas                    1095 UIC           0 :                     return false;
  899                          1096               0 :                 case XID_PRECEDES_CLUSTERMIN:
  899 rhaas                    1097 LBC           0 :                     report_corruption(ctx,
  738 rhaas                    1098 ECB             :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple precedes oldest valid transaction ID %u:%u",
                               1099                 :                                                xvac,
  899 rhaas                    1100 LBC           0 :                                                EpochFromFullTransactionId(ctx->oldest_fxid),
                               1101               0 :                                                XidFromFullTransactionId(ctx->oldest_fxid)));
  738 rhaas                    1102 UIC           0 :                     return false;
  899                          1103               0 :                 case XID_BOUNDS_OK:
  738 rhaas                    1104 LBC           0 :                     break;
                               1105                 :             }
  738 rhaas                    1106 ECB             : 
  738 rhaas                    1107 UIC           0 :             switch (xvac_status)
                               1108                 :             {
                               1109               0 :                 case XID_IS_CURRENT_XID:
                               1110               0 :                     report_corruption(ctx,
                               1111                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple matches our current transaction ID",
                               1112                 :                                                xvac));
                               1113               0 :                     return false;
  738 rhaas                    1114 LBC           0 :                 case XID_IN_PROGRESS:
  738 rhaas                    1115 UIC           0 :                     report_corruption(ctx,
                               1116                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved off tuple appears to be in progress",
                               1117                 :                                                xvac));
  738 rhaas                    1118 LBC           0 :                     return false;
  738 rhaas                    1119 ECB             : 
  738 rhaas                    1120 UIC           0 :                 case XID_COMMITTED:
                               1121                 : 
  738 rhaas                    1122 ECB             :                     /*
                               1123                 :                      * The tuple is dead, because the xvac transaction moved
  697 tgl                      1124                 :                      * it off and committed. It's checkable, but also
                               1125                 :                      * prunable.
  738 rhaas                    1126                 :                      */
  738 rhaas                    1127 UIC           0 :                     return true;
                               1128                 : 
                               1129               0 :                 case XID_ABORTED:
                               1130                 : 
                               1131                 :                     /*
                               1132                 :                      * The original xmin must have committed, because the xvac
                               1133                 :                      * transaction tried to move it later. Since xvac is
                               1134                 :                      * aborted, whether it's still alive now depends on the
                               1135                 :                      * status of xmax.
  738 rhaas                    1136 ECB             :                      */
  738 rhaas                    1137 UIC           0 :                     break;
  899 rhaas                    1138 ECB             :             }
                               1139                 :         }
  738                          1140                 :         /* Used by pre-9.0 binary upgrades */
  738 rhaas                    1141 GIC       13801 :         else if (tuphdr->t_infomask & HEAP_MOVED_IN)
                               1142                 :         {
  738 rhaas                    1143 UIC           0 :             xvac = HeapTupleHeaderGetXvac(tuphdr);
                               1144                 : 
                               1145               0 :             switch (get_xid_status(xvac, ctx, &xvac_status))
                               1146                 :             {
  899                          1147               0 :                 case XID_INVALID:
                               1148               0 :                     report_corruption(ctx,
                               1149                 :                                       pstrdup("old-style VACUUM FULL transaction ID for moved in tuple is invalid"));
  899 rhaas                    1150 LBC           0 :                     return false;
  899 rhaas                    1151 UIC           0 :                 case XID_IN_FUTURE:
  899 rhaas                    1152 LBC           0 :                     report_corruption(ctx,
  738 rhaas                    1153 ECB             :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple equals or exceeds next valid transaction ID %u:%u",
                               1154                 :                                                xvac,
  899 rhaas                    1155 LBC           0 :                                                EpochFromFullTransactionId(ctx->next_fxid),
  899 rhaas                    1156 UIC           0 :                                                XidFromFullTransactionId(ctx->next_fxid)));
  738                          1157               0 :                     return false;
  899                          1158               0 :                 case XID_PRECEDES_RELMIN:
                               1159               0 :                     report_corruption(ctx,
                               1160                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple precedes relation freeze threshold %u:%u",
                               1161                 :                                                xvac,
                               1162               0 :                                                EpochFromFullTransactionId(ctx->relfrozenfxid),
                               1163               0 :                                                XidFromFullTransactionId(ctx->relfrozenfxid)));
  738                          1164               0 :                     return false;
  899                          1165               0 :                 case XID_PRECEDES_CLUSTERMIN:
  899 rhaas                    1166 LBC           0 :                     report_corruption(ctx,
                               1167                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple precedes oldest valid transaction ID %u:%u",
                               1168                 :                                                xvac,
                               1169               0 :                                                EpochFromFullTransactionId(ctx->oldest_fxid),
                               1170               0 :                                                XidFromFullTransactionId(ctx->oldest_fxid)));
  738                          1171               0 :                     return false;
  899                          1172               0 :                 case XID_BOUNDS_OK:
  738 rhaas                    1173 UIC           0 :                     break;
                               1174                 :             }
                               1175                 : 
                               1176               0 :             switch (xvac_status)
                               1177                 :             {
                               1178               0 :                 case XID_IS_CURRENT_XID:
                               1179               0 :                     report_corruption(ctx,
                               1180                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple matches our current transaction ID",
                               1181                 :                                                xvac));
                               1182               0 :                     return false;
                               1183               0 :                 case XID_IN_PROGRESS:
                               1184               0 :                     report_corruption(ctx,
                               1185                 :                                       psprintf("old-style VACUUM FULL transaction ID %u for moved in tuple appears to be in progress",
                               1186                 :                                                xvac));
                               1187               0 :                     return false;
                               1188                 : 
                               1189               0 :                 case XID_COMMITTED:
                               1190                 : 
                               1191                 :                     /*
                               1192                 :                      * The original xmin must have committed, because the xvac
                               1193                 :                      * transaction moved it later. Whether it's still alive
                               1194                 :                      * now depends on the status of xmax.
                               1195                 :                      */
                               1196               0 :                     break;
                               1197                 : 
  738 rhaas                    1198 LBC           0 :                 case XID_ABORTED:
                               1199                 : 
  738 rhaas                    1200 ECB             :                     /*
                               1201                 :                      * The tuple is dead, because the xvac transaction moved
  697 tgl                      1202                 :                      * it off and committed. It's checkable, but also
                               1203                 :                      * prunable.
                               1204                 :                      */
  738 rhaas                    1205 UIC           0 :                     return true;
  738 rhaas                    1206 ECB             :             }
                               1207                 :         }
  738 rhaas                    1208 CBC       13801 :         else if (xmin_status != XID_COMMITTED)
                               1209                 :         {
  738 rhaas                    1210 ECB             :             /*
                               1211                 :              * Inserting transaction is not in progress, and not committed, so
                               1212                 :              * it might have changed the TupleDesc in ways we don't know
                               1213                 :              * about. Thus, don't try to check the tuple structure.
                               1214                 :              *
                               1215                 :              * If xmin_status happens to be XID_IS_CURRENT_XID, then in theory
                               1216                 :              * any such DDL changes ought to be visible to us, so perhaps we
  697 tgl                      1217                 :              * could check anyway in that case. But, for now, let's be
                               1218                 :              * conservative and treat this like any other uncommitted insert.
                               1219                 :              */
  738 rhaas                    1220 GIC           7 :             return false;
                               1221                 :         }
                               1222                 :     }
                               1223                 : 
                               1224                 :     /*
                               1225                 :      * Okay, the inserter committed, so it was good at some point.  Now what
                               1226                 :      * about the deleting transaction?
                               1227                 :      */
  738 rhaas                    1228 ECB             : 
  738 rhaas                    1229 CBC      404345 :     if (tuphdr->t_infomask & HEAP_XMAX_IS_MULTI)
                               1230                 :     {
  738 rhaas                    1231 ECB             :         /*
                               1232                 :          * xmax is a multixact, so sanity-check the MXID. Note that we do this
                               1233                 :          * prior to checking for HEAP_XMAX_INVALID or
                               1234                 :          * HEAP_XMAX_IS_LOCKED_ONLY. This might therefore complain about
                               1235                 :          * things that wouldn't actually be a problem during a normal scan,
                               1236                 :          * but eventually we're going to have to freeze, and that process will
                               1237                 :          * ignore hint bits.
                               1238                 :          *
                               1239                 :          * Even if the MXID is out of range, we still know that the original
                               1240                 :          * insert committed, so we can check the tuple itself. However, we
                               1241                 :          * can't rule out the possibility that this tuple is dead, so don't
                               1242                 :          * clear ctx->tuple_could_be_pruned. Possibly we should go ahead and
                               1243                 :          * clear that flag anyway if HEAP_XMAX_INVALID is set or if
  697 tgl                      1244                 :          * HEAP_XMAX_IS_LOCKED_ONLY is true, but for now we err on the side of
                               1245                 :          * avoiding possibly-bogus complaints about missing TOAST entries.
                               1246                 :          */
  738 rhaas                    1247 GIC          58 :         xmax = HeapTupleHeaderGetRawXmax(tuphdr);
                               1248              58 :         switch (check_mxid_valid_in_rel(xmax, ctx))
                               1249                 :         {
  738 rhaas                    1250 LBC           0 :             case XID_INVALID:
                               1251               0 :                 report_corruption(ctx,
                               1252                 :                                   pstrdup("multitransaction ID is invalid"));
                               1253               0 :                 return true;
  738 rhaas                    1254 CBC           1 :             case XID_PRECEDES_RELMIN:
  738 rhaas                    1255 GIC           1 :                 report_corruption(ctx,
  738 rhaas                    1256 ECB             :                                   psprintf("multitransaction ID %u precedes relation minimum multitransaction ID threshold %u",
  738 rhaas                    1257 EUB             :                                            xmax, ctx->relminmxid));
  738 rhaas                    1258 GIC           1 :                 return true;
  738 rhaas                    1259 UBC           0 :             case XID_PRECEDES_CLUSTERMIN:
  738 rhaas                    1260 LBC           0 :                 report_corruption(ctx,
  738 rhaas                    1261 ECB             :                                   psprintf("multitransaction ID %u precedes oldest valid multitransaction ID threshold %u",
                               1262                 :                                            xmax, ctx->oldest_mxact));
  738 rhaas                    1263 LBC           0 :                 return true;
  738 rhaas                    1264 CBC           1 :             case XID_IN_FUTURE:
  738 rhaas                    1265 GBC           1 :                 report_corruption(ctx,
                               1266                 :                                   psprintf("multitransaction ID %u equals or exceeds next valid multitransaction ID %u",
  738 rhaas                    1267 EUB             :                                            xmax,
                               1268                 :                                            ctx->next_mxact));
  738 rhaas                    1269 CBC           1 :                 return true;
  738 rhaas                    1270 GIC          56 :             case XID_BOUNDS_OK:
  738 rhaas                    1271 CBC          56 :                 break;
  738 rhaas                    1272 ECB             :         }
                               1273                 :     }
                               1274                 : 
  738 rhaas                    1275 CBC      404343 :     if (tuphdr->t_infomask & HEAP_XMAX_INVALID)
                               1276                 :     {
                               1277                 :         /*
                               1278                 :          * This tuple is live.  A concurrently running transaction could
                               1279                 :          * delete it before we get around to checking the toast, but any such
                               1280                 :          * running transaction is surely not less than our safe_xmin, so the
                               1281                 :          * toast cannot be vacuumed out from under us.
                               1282                 :          */
  738 rhaas                    1283 GIC      404159 :         ctx->tuple_could_be_pruned = false;
                               1284          404159 :         return true;
                               1285                 :     }
                               1286                 : 
                               1287             184 :     if (HEAP_XMAX_IS_LOCKED_ONLY(tuphdr->t_infomask))
                               1288                 :     {
                               1289                 :         /*
                               1290                 :          * "Deleting" xact really only locked it, so the tuple is live in any
                               1291                 :          * case.  As above, a concurrently running transaction could delete
                               1292                 :          * it, but it cannot be vacuumed out from under us.
                               1293                 :          */
                               1294              28 :         ctx->tuple_could_be_pruned = false;
                               1295              28 :         return true;
                               1296                 :     }
                               1297                 : 
                               1298             156 :     if (tuphdr->t_infomask & HEAP_XMAX_IS_MULTI)
                               1299                 :     {
                               1300                 :         /*
                               1301                 :          * We already checked above that this multixact is within limits for
                               1302                 :          * this table.  Now check the update xid from this multixact.
                               1303                 :          */
                               1304              28 :         xmax = HeapTupleGetUpdateXid(tuphdr);
                               1305              28 :         switch (get_xid_status(xmax, ctx, &xmax_status))
                               1306                 :         {
  738 rhaas                    1307 UIC           0 :             case XID_INVALID:
                               1308                 :                 /* not LOCKED_ONLY, so it has to have an xmax */
                               1309               0 :                 report_corruption(ctx,
                               1310                 :                                   pstrdup("update xid is invalid"));
  738 rhaas                    1311 LBC           0 :                 return true;
  738 rhaas                    1312 UIC           0 :             case XID_IN_FUTURE:
                               1313               0 :                 report_corruption(ctx,
                               1314                 :                                   psprintf("update xid %u equals or exceeds next valid transaction ID %u:%u",
                               1315                 :                                            xmax,
                               1316               0 :                                            EpochFromFullTransactionId(ctx->next_fxid),
                               1317               0 :                                            XidFromFullTransactionId(ctx->next_fxid)));
                               1318               0 :                 return true;
                               1319               0 :             case XID_PRECEDES_RELMIN:
  738 rhaas                    1320 LBC           0 :                 report_corruption(ctx,
                               1321                 :                                   psprintf("update xid %u precedes relation freeze threshold %u:%u",
  738 rhaas                    1322 ECB             :                                            xmax,
  738 rhaas                    1323 LBC           0 :                                            EpochFromFullTransactionId(ctx->relfrozenfxid),
  738 rhaas                    1324 UIC           0 :                                            XidFromFullTransactionId(ctx->relfrozenfxid)));
                               1325               0 :                 return true;
  738 rhaas                    1326 LBC           0 :             case XID_PRECEDES_CLUSTERMIN:
                               1327               0 :                 report_corruption(ctx,
                               1328                 :                                   psprintf("update xid %u precedes oldest valid transaction ID %u:%u",
  738 rhaas                    1329 EUB             :                                            xmax,
  738 rhaas                    1330 UIC           0 :                                            EpochFromFullTransactionId(ctx->oldest_fxid),
  738 rhaas                    1331 UBC           0 :                                            XidFromFullTransactionId(ctx->oldest_fxid)));
  738 rhaas                    1332 LBC           0 :                 return true;
  738 rhaas                    1333 CBC          28 :             case XID_BOUNDS_OK:
                               1334              28 :                 break;
  899 rhaas                    1335 ECB             :         }
  738                          1336                 : 
  738 rhaas                    1337 CBC          28 :         switch (xmax_status)
                               1338                 :         {
  738 rhaas                    1339 UIC           0 :             case XID_IS_CURRENT_XID:
  738 rhaas                    1340 ECB             :             case XID_IN_PROGRESS:
                               1341                 : 
                               1342                 :                 /*
                               1343                 :                  * The delete is in progress, so it cannot be visible to our
                               1344                 :                  * snapshot.
                               1345                 :                  */
  738 rhaas                    1346 UIC           0 :                 ctx->tuple_could_be_pruned = false;
  738 rhaas                    1347 LBC           0 :                 break;
  738 rhaas                    1348 CBC          28 :             case XID_COMMITTED:
  738 rhaas                    1349 ECB             : 
                               1350                 :                 /*
                               1351                 :                  * The delete committed.  Whether the toast can be vacuumed
                               1352                 :                  * away depends on how old the deleting transaction is.
                               1353                 :                  */
  738 rhaas                    1354 CBC          28 :                 ctx->tuple_could_be_pruned = TransactionIdPrecedes(xmax,
  697 tgl                      1355 ECB             :                                                                    ctx->safe_xmin);
  738 rhaas                    1356 CBC          28 :                 break;
  738 rhaas                    1357 UIC           0 :             case XID_ABORTED:
                               1358                 : 
                               1359                 :                 /*
                               1360                 :                  * The delete aborted or crashed.  The tuple is still live.
                               1361                 :                  */
  738 rhaas                    1362 LBC           0 :                 ctx->tuple_could_be_pruned = false;
  738 rhaas                    1363 UIC           0 :                 break;
  738 rhaas                    1364 ECB             :         }
  738 rhaas                    1365 EUB             : 
                               1366                 :         /* Tuple itself is checkable even if it's dead. */
  738 rhaas                    1367 CBC          28 :         return true;
                               1368                 :     }
  738 rhaas                    1369 EUB             : 
                               1370                 :     /* xmax is an XID, not a MXID. Sanity check it. */
  738 rhaas                    1371 GBC         128 :     xmax = HeapTupleHeaderGetRawXmax(tuphdr);
  738 rhaas                    1372 GIC         128 :     switch (get_xid_status(xmax, ctx, &xmax_status))
  738 rhaas                    1373 EUB             :     {
   17 rhaas                    1374 GBC           1 :         case XID_INVALID:
   17 rhaas                    1375 GIC           1 :             ctx->tuple_could_be_pruned = false;
   17 rhaas                    1376 GBC           1 :             return true;
  738 rhaas                    1377 UBC           0 :         case XID_IN_FUTURE:
                               1378               0 :             report_corruption(ctx,
                               1379                 :                               psprintf("xmax %u equals or exceeds next valid transaction ID %u:%u",
                               1380                 :                                        xmax,
                               1381               0 :                                        EpochFromFullTransactionId(ctx->next_fxid),
                               1382               0 :                                        XidFromFullTransactionId(ctx->next_fxid)));
                               1383               0 :             return false;       /* corrupt */
                               1384               0 :         case XID_PRECEDES_RELMIN:
                               1385               0 :             report_corruption(ctx,
                               1386                 :                               psprintf("xmax %u precedes relation freeze threshold %u:%u",
                               1387                 :                                        xmax,
                               1388               0 :                                        EpochFromFullTransactionId(ctx->relfrozenfxid),
                               1389               0 :                                        XidFromFullTransactionId(ctx->relfrozenfxid)));
                               1390               0 :             return false;       /* corrupt */
  738 rhaas                    1391 GBC           1 :         case XID_PRECEDES_CLUSTERMIN:
                               1392               1 :             report_corruption(ctx,
                               1393                 :                               psprintf("xmax %u precedes oldest valid transaction ID %u:%u",
                               1394                 :                                        xmax,
                               1395               1 :                                        EpochFromFullTransactionId(ctx->oldest_fxid),
                               1396               1 :                                        XidFromFullTransactionId(ctx->oldest_fxid)));
                               1397               1 :             return false;       /* corrupt */
                               1398             126 :         case XID_BOUNDS_OK:
                               1399             126 :             break;
                               1400                 :     }
                               1401                 : 
  738 rhaas                    1402 EUB             :     /*
                               1403                 :      * Whether the toast can be vacuumed away depends on how old the deleting
                               1404                 :      * transaction is.
                               1405                 :      */
  738 rhaas                    1406 GIC         126 :     switch (xmax_status)
                               1407                 :     {
  738 rhaas                    1408 UBC           0 :         case XID_IS_CURRENT_XID:
  738 rhaas                    1409 EUB             :         case XID_IN_PROGRESS:
                               1410                 : 
                               1411                 :             /*
                               1412                 :              * The delete is in progress, so it cannot be visible to our
                               1413                 :              * snapshot.
                               1414                 :              */
  738 rhaas                    1415 UBC           0 :             ctx->tuple_could_be_pruned = false;
  738 rhaas                    1416 UIC           0 :             break;
                               1417                 : 
  738 rhaas                    1418 GIC         123 :         case XID_COMMITTED:
                               1419                 : 
                               1420                 :             /*
                               1421                 :              * The delete committed.  Whether the toast can be vacuumed away
  738 rhaas                    1422 EUB             :              * depends on how old the deleting transaction is.
                               1423                 :              */
  738 rhaas                    1424 GBC         123 :             ctx->tuple_could_be_pruned = TransactionIdPrecedes(xmax,
                               1425                 :                                                                ctx->safe_xmin);
  738 rhaas                    1426 GIC         123 :             break;
                               1427                 : 
                               1428               3 :         case XID_ABORTED:
                               1429                 : 
                               1430                 :             /*
                               1431                 :              * The delete aborted or crashed.  The tuple is still live.
  738 rhaas                    1432 EUB             :              */
  738 rhaas                    1433 GIC           3 :             ctx->tuple_could_be_pruned = false;
                               1434               3 :             break;
                               1435                 :     }
  738 rhaas                    1436 ECB             : 
                               1437                 :     /* Tuple itself is checkable even if it's dead. */
  738 rhaas                    1438 GBC         126 :     return true;
                               1439                 : }
  899 rhaas                    1440 EUB             : 
                               1441                 : 
                               1442                 : /*
                               1443                 :  * Check the current toast tuple against the state tracked in ctx, recording
                               1444                 :  * any corruption found in ctx->tupstore.
                               1445                 :  *
                               1446                 :  * This is not equivalent to running verify_heapam on the toast table itself,
                               1447                 :  * and is not hardened against corruption of the toast table.  Rather, when
                               1448                 :  * validating a toasted attribute in the main table, the sequence of toast
                               1449                 :  * tuples that store the toasted value are retrieved and checked in order, with
                               1450                 :  * each toast tuple being checked against where we are in the sequence, as well
                               1451                 :  * as each toast tuple having its varlena structure sanity checked.
  732                          1452                 :  *
  706                          1453                 :  * On entry, *expected_chunk_seq should be the chunk_seq value that we expect
                               1454                 :  * to find in toasttup. On exit, it will be updated to the value the next call
                               1455                 :  * to this function should expect to see.
                               1456                 :  */
  899                          1457                 : static void
  732 rhaas                    1458 GBC       39989 : check_toast_tuple(HeapTuple toasttup, HeapCheckContext *ctx,
  706 rhaas                    1459 EUB             :                   ToastedAttribute *ta, int32 *expected_chunk_seq,
                               1460                 :                   uint32 extsize)
  899                          1461                 : {
                               1462                 :     int32       chunk_seq;
  706 rhaas                    1463 GIC       39989 :     int32       last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
  899 rhaas                    1464 EUB             :     Pointer     chunk;
                               1465                 :     bool        isnull;
                               1466                 :     int32       chunksize;
                               1467                 :     int32       expected_size;
                               1468                 : 
                               1469                 :     /* Sanity-check the sequence number. */
  706 rhaas                    1470 GIC       39989 :     chunk_seq = DatumGetInt32(fastgetattr(toasttup, 2,
  706 rhaas                    1471 GBC       39989 :                                           ctx->toast_rel->rd_att, &isnull));
  899 rhaas                    1472 GIC       39989 :     if (isnull)
  899 rhaas                    1473 EUB             :     {
  732 rhaas                    1474 UBC           0 :         report_toast_corruption(ctx, ta,
                               1475                 :                                 psprintf("toast value %u has toast chunk with null sequence number",
                               1476                 :                                          ta->toast_pointer.va_valueid));
  899                          1477               0 :         return;
  899 rhaas                    1478 EUB             :     }
  706 rhaas                    1479 GBC       39989 :     if (chunk_seq != *expected_chunk_seq)
                               1480                 :     {
                               1481                 :         /* Either the TOAST index is corrupt, or we don't have all chunks. */
  706 rhaas                    1482 UBC           0 :         report_toast_corruption(ctx, ta,
                               1483                 :                                 psprintf("toast value %u index scan returned chunk %d when expecting chunk %d",
  706 rhaas                    1484 EUB             :                                          ta->toast_pointer.va_valueid,
                               1485                 :                                          chunk_seq, *expected_chunk_seq));
                               1486                 :     }
  706 rhaas                    1487 GIC       39989 :     *expected_chunk_seq = chunk_seq + 1;
                               1488                 : 
                               1489                 :     /* Sanity-check the chunk data. */
  899                          1490           39989 :     chunk = DatumGetPointer(fastgetattr(toasttup, 3,
  899 rhaas                    1491 GBC       39989 :                                         ctx->toast_rel->rd_att, &isnull));
  899 rhaas                    1492 GIC       39989 :     if (isnull)
  899 rhaas                    1493 EUB             :     {
  732 rhaas                    1494 UIC           0 :         report_toast_corruption(ctx, ta,
                               1495                 :                                 psprintf("toast value %u chunk %d has null data",
                               1496                 :                                          ta->toast_pointer.va_valueid,
                               1497                 :                                          chunk_seq));
  899                          1498               0 :         return;
                               1499                 :     }
  899 rhaas                    1500 GBC       39989 :     if (!VARATT_IS_EXTENDED(chunk))
  899 rhaas                    1501 GIC       39989 :         chunksize = VARSIZE(chunk) - VARHDRSZ;
  899 rhaas                    1502 UIC           0 :     else if (VARATT_IS_SHORT(chunk))
  899 rhaas                    1503 ECB             :     {
                               1504                 :         /*
                               1505                 :          * could happen due to heap_form_tuple doing its thing
                               1506                 :          */
  899 rhaas                    1507 UIC           0 :         chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
                               1508                 :     }
                               1509                 :     else
                               1510                 :     {
                               1511                 :         /* should never happen */
                               1512               0 :         uint32      header = ((varattrib_4b *) chunk)->va_4byte.va_header;
                               1513                 : 
  732                          1514               0 :         report_toast_corruption(ctx, ta,
  725 rhaas                    1515 ECB             :                                 psprintf("toast value %u chunk %d has invalid varlena header %0x",
                               1516                 :                                          ta->toast_pointer.va_valueid,
                               1517                 :                                          chunk_seq, header));
  899 rhaas                    1518 UIC           0 :         return;
                               1519                 :     }
                               1520                 : 
                               1521                 :     /*
                               1522                 :      * Some checks on the data we've found
                               1523                 :      */
  706 rhaas                    1524 CBC       39989 :     if (chunk_seq > last_chunk_seq)
                               1525                 :     {
  732 rhaas                    1526 UIC           0 :         report_toast_corruption(ctx, ta,
                               1527                 :                                 psprintf("toast value %u chunk %d follows last expected chunk %d",
                               1528                 :                                          ta->toast_pointer.va_valueid,
                               1529                 :                                          chunk_seq, last_chunk_seq));
  899                          1530               0 :         return;
                               1531                 :     }
                               1532                 : 
  706 rhaas                    1533 GIC       39989 :     expected_size = chunk_seq < last_chunk_seq ? TOAST_MAX_CHUNK_SIZE
                               1534           11301 :         : extsize - (last_chunk_seq * TOAST_MAX_CHUNK_SIZE);
                               1535                 : 
  899                          1536           39989 :     if (chunksize != expected_size)
  732 rhaas                    1537 UIC           0 :         report_toast_corruption(ctx, ta,
                               1538                 :                                 psprintf("toast value %u chunk %d has size %u, but expected size %u",
                               1539                 :                                          ta->toast_pointer.va_valueid,
                               1540                 :                                          chunk_seq, chunksize, expected_size));
                               1541                 : }
  697 tgl                      1542 ECB             : 
  899 rhaas                    1543                 : /*
                               1544                 :  * Check the current attribute as tracked in ctx, recording any corruption
  899 rhaas                    1545 EUB             :  * found in ctx->tupstore.
                               1546                 :  *
                               1547                 :  * This function follows the logic performed by heap_deform_tuple(), and in the
  732                          1548                 :  * case of a toasted value, optionally stores the toast pointer so later it can
  732 rhaas                    1549 ECB             :  * be checked following the logic of detoast_external_attr(), checking for any
                               1550                 :  * conditions that would result in either of those functions Asserting or
                               1551                 :  * crashing the backend.  The checks performed by Asserts present in those two
                               1552                 :  * functions are also performed here and in check_toasted_attribute.  In cases
                               1553                 :  * where those two functions are a bit cavalier in their assumptions about data
  732 rhaas                    1554 EUB             :  * being correct, we perform additional checks not present in either of those
                               1555                 :  * two functions.  Where some condition is checked in both of those functions,
                               1556                 :  * we perform it here twice, as we parallel the logical flow of those two
                               1557                 :  * functions.  The presence of duplicate checks seems a reasonable price to pay
                               1558                 :  * for keeping this code tightly coupled with the code it protects.
  899 rhaas                    1559 ECB             :  *
                               1560                 :  * Returns true if the tuple attribute is sane enough for processing to
                               1561                 :  * continue on to the next attribute, false otherwise.
                               1562                 :  */
                               1563                 : static bool
  899 rhaas                    1564 CBC     5694900 : check_tuple_attribute(HeapCheckContext *ctx)
  899 rhaas                    1565 ECB             : {
                               1566                 :     Datum       attdatum;
                               1567                 :     struct varlena *attr;
                               1568                 :     char       *tp;             /* pointer to the tuple data */
                               1569                 :     uint16      infomask;
                               1570                 :     Form_pg_attribute thisatt;
                               1571                 :     struct varatt_external toast_pointer;
                               1572                 : 
  899 rhaas                    1573 GIC     5694900 :     infomask = ctx->tuphdr->t_infomask;
                               1574         5694900 :     thisatt = TupleDescAttr(RelationGetDescr(ctx->rel), ctx->attnum);
                               1575                 : 
                               1576         5694900 :     tp = (char *) ctx->tuphdr + ctx->tuphdr->t_hoff;
                               1577                 : 
  899 rhaas                    1578 CBC     5694900 :     if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
  899 rhaas                    1579 ECB             :     {
  899 rhaas                    1580 UIC           0 :         report_corruption(ctx,
                               1581                 :                           psprintf("attribute with length %u starts at offset %u beyond total tuple length %u",
  899 rhaas                    1582 LBC           0 :                                    thisatt->attlen,
  899 rhaas                    1583 UIC           0 :                                    ctx->tuphdr->t_hoff + ctx->offset,
                               1584               0 :                                    ctx->lp_len));
                               1585               0 :         return false;
                               1586                 :     }
                               1587                 : 
                               1588                 :     /* Skip null values */
  899 rhaas                    1589 CBC     5694900 :     if (infomask & HEAP_HASNULL && att_isnull(ctx->attnum, ctx->tuphdr->t_bits))
                               1590          900557 :         return true;
                               1591                 : 
                               1592                 :     /* Skip non-varlena values, but update offset first */
                               1593         4794343 :     if (thisatt->attlen != -1)
                               1594                 :     {
  899 rhaas                    1595 GIC     4374081 :         ctx->offset = att_align_nominal(ctx->offset, thisatt->attalign);
                               1596         4374081 :         ctx->offset = att_addlength_pointer(ctx->offset, thisatt->attlen,
                               1597                 :                                             tp + ctx->offset);
                               1598         4374081 :         if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
  899 rhaas                    1599 ECB             :         {
  899 rhaas                    1600 LBC           0 :             report_corruption(ctx,
                               1601                 :                               psprintf("attribute with length %u ends at offset %u beyond total tuple length %u",
  899 rhaas                    1602 UBC           0 :                                        thisatt->attlen,
  899 rhaas                    1603 UIC           0 :                                        ctx->tuphdr->t_hoff + ctx->offset,
  899 rhaas                    1604 UBC           0 :                                        ctx->lp_len));
  899 rhaas                    1605 UIC           0 :             return false;
  899 rhaas                    1606 EUB             :         }
  899 rhaas                    1607 GBC     4374081 :         return true;
  899 rhaas                    1608 EUB             :     }
                               1609                 : 
                               1610                 :     /* Ok, we're looking at a varlena attribute. */
  899 rhaas                    1611 GBC      420262 :     ctx->offset = att_align_pointer(ctx->offset, thisatt->attalign, -1,
  899 rhaas                    1612 EUB             :                                     tp + ctx->offset);
                               1613                 : 
                               1614                 :     /* Get the (possibly corrupt) varlena datum */
  899 rhaas                    1615 GBC      420262 :     attdatum = fetchatt(thisatt, tp + ctx->offset);
                               1616                 : 
                               1617                 :     /*
  899 rhaas                    1618 EUB             :      * We have the datum, but we cannot decode it carelessly, as it may still
                               1619                 :      * be corrupt.
                               1620                 :      */
                               1621                 : 
                               1622                 :     /*
                               1623                 :      * Check that VARTAG_SIZE won't hit an Assert on a corrupt va_tag before
                               1624                 :      * risking a call into att_addlength_pointer
                               1625                 :      */
  899 rhaas                    1626 GBC      420262 :     if (VARATT_IS_EXTERNAL(tp + ctx->offset))
  899 rhaas                    1627 EUB             :     {
  899 rhaas                    1628 CBC       25742 :         uint8       va_tag = VARTAG_EXTERNAL(tp + ctx->offset);
  899 rhaas                    1629 ECB             : 
  899 rhaas                    1630 GIC       25742 :         if (va_tag != VARTAG_ONDISK)
                               1631                 :         {
  899 rhaas                    1632 LBC           0 :             report_corruption(ctx,
                               1633                 :                               psprintf("toasted attribute has unexpected TOAST tag %u",
  899 rhaas                    1634 EUB             :                                        va_tag));
                               1635                 :             /* We can't know where the next attribute begins */
  899 rhaas                    1636 UIC           0 :             return false;
                               1637                 :         }
                               1638                 :     }
                               1639                 : 
                               1640                 :     /* Ok, should be safe now */
  899 rhaas                    1641 GBC      420262 :     ctx->offset = att_addlength_pointer(ctx->offset, thisatt->attlen,
  899 rhaas                    1642 EUB             :                                         tp + ctx->offset);
  899 rhaas                    1643 ECB             : 
  899 rhaas                    1644 GIC      420262 :     if (ctx->tuphdr->t_hoff + ctx->offset > ctx->lp_len)
                               1645                 :     {
                               1646               1 :         report_corruption(ctx,
                               1647                 :                           psprintf("attribute with length %u ends at offset %u beyond total tuple length %u",
                               1648               1 :                                    thisatt->attlen,
  899 rhaas                    1649 CBC           1 :                                    ctx->tuphdr->t_hoff + ctx->offset,
  899 rhaas                    1650 GIC           1 :                                    ctx->lp_len));
  899 rhaas                    1651 ECB             : 
  899 rhaas                    1652 GBC           1 :         return false;
                               1653                 :     }
                               1654                 : 
                               1655                 :     /*
                               1656                 :      * heap_deform_tuple would be done with this attribute at this point,
  899 rhaas                    1657 EUB             :      * having stored it in values[], and would continue to the next attribute.
                               1658                 :      * We go further, because we need to check if the toast datum is corrupt.
                               1659                 :      */
                               1660                 : 
  899 rhaas                    1661 GIC      420261 :     attr = (struct varlena *) DatumGetPointer(attdatum);
  899 rhaas                    1662 ECB             : 
                               1663                 :     /*
                               1664                 :      * Now we follow the logic of detoast_external_attr(), with the same
                               1665                 :      * caveats about being paranoid about corruption.
                               1666                 :      */
                               1667                 : 
                               1668                 :     /* Skip values that are not external */
  899 rhaas                    1669 CBC      420261 :     if (!VARATT_IS_EXTERNAL(attr))
                               1670          394519 :         return true;
  899 rhaas                    1671 ECB             : 
  899 rhaas                    1672 EUB             :     /* It is external, and we're looking at a page on disk */
                               1673                 : 
                               1674                 :     /*
                               1675                 :      * Must copy attr into toast_pointer for alignment considerations
  725                          1676                 :      */
  725 rhaas                    1677 GBC       25742 :     VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
  725 rhaas                    1678 EUB             : 
  520                          1679                 :     /* Toasted attributes too large to be untoasted should never be stored */
  520 rhaas                    1680 GBC       25742 :     if (toast_pointer.va_rawsize > VARLENA_SIZE_LIMIT)
  520 rhaas                    1681 UIC           0 :         report_corruption(ctx,
                               1682                 :                           psprintf("toast value %u rawsize %d exceeds limit %d",
  520 rhaas                    1683 EUB             :                                    toast_pointer.va_valueid,
                               1684                 :                                    toast_pointer.va_rawsize,
                               1685                 :                                    VARLENA_SIZE_LIMIT));
  520 rhaas                    1686 ECB             : 
  291 rhaas                    1687 CBC       25742 :     if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
                               1688                 :     {
                               1689                 :         ToastCompressionId cmid;
  520                          1690            1520 :         bool        valid = false;
  520 rhaas                    1691 ECB             : 
                               1692                 :         /* Compressed attributes should have a valid compression method */
  520 rhaas                    1693 CBC        1520 :         cmid = TOAST_COMPRESS_METHOD(&toast_pointer);
                               1694            1520 :         switch (cmid)
                               1695                 :         {
                               1696                 :                 /* List of all valid compression method IDs */
  520 rhaas                    1697 GIC        1520 :             case TOAST_PGLZ_COMPRESSION_ID:
                               1698                 :             case TOAST_LZ4_COMPRESSION_ID:
                               1699            1520 :                 valid = true;
                               1700            1520 :                 break;
  520 rhaas                    1701 ECB             : 
                               1702                 :                 /* Recognized but invalid compression method ID */
  520 rhaas                    1703 UBC           0 :             case TOAST_INVALID_COMPRESSION_ID:
  520 rhaas                    1704 UIC           0 :                 break;
                               1705                 : 
                               1706                 :                 /* Intentionally no default here */
                               1707                 :         }
  520 rhaas                    1708 GIC        1520 :         if (!valid)
  520 rhaas                    1709 UIC           0 :             report_corruption(ctx,
  520 rhaas                    1710 EUB             :                               psprintf("toast value %u has invalid compression method id %d",
                               1711                 :                                        toast_pointer.va_valueid, cmid));
                               1712                 :     }
  520 rhaas                    1713 ECB             : 
                               1714                 :     /* The tuple header better claim to contain toasted values */
  899 rhaas                    1715 GIC       25742 :     if (!(infomask & HEAP_HASEXTERNAL))
                               1716                 :     {
  899 rhaas                    1717 UIC           0 :         report_corruption(ctx,
                               1718                 :                           psprintf("toast value %u is external but tuple header flag HEAP_HASEXTERNAL not set",
  725 rhaas                    1719 ECB             :                                    toast_pointer.va_valueid));
  899 rhaas                    1720 UIC           0 :         return true;
  899 rhaas                    1721 ECB             :     }
                               1722                 : 
                               1723                 :     /* The relation better have a toast table */
  899 rhaas                    1724 GIC       25742 :     if (!ctx->rel->rd_rel->reltoastrelid)
                               1725                 :     {
  899 rhaas                    1726 UIC           0 :         report_corruption(ctx,
                               1727                 :                           psprintf("toast value %u is external but relation has no toast relation",
  725 rhaas                    1728 ECB             :                                    toast_pointer.va_valueid));
  899 rhaas                    1729 LBC           0 :         return true;
                               1730                 :     }
                               1731                 : 
                               1732                 :     /* If we were told to skip toast checking, then we're done. */
  899 rhaas                    1733 CBC       25742 :     if (ctx->toast_rel == NULL)
  899 rhaas                    1734 GIC       14429 :         return true;
                               1735                 : 
                               1736                 :     /*
                               1737                 :      * If this tuple is eligible to be pruned, we cannot check the toast.
                               1738                 :      * Otherwise, we push a copy of the toast tuple so we can check it after
                               1739                 :      * releasing the main table buffer lock.
                               1740                 :      */
  732                          1741           11313 :     if (!ctx->tuple_could_be_pruned)
                               1742                 :     {
                               1743                 :         ToastedAttribute *ta;
                               1744                 : 
                               1745           11311 :         ta = (ToastedAttribute *) palloc0(sizeof(ToastedAttribute));
                               1746                 : 
                               1747           11311 :         VARATT_EXTERNAL_GET_POINTER(ta->toast_pointer, attr);
                               1748           11311 :         ta->blkno = ctx->blkno;
                               1749           11311 :         ta->offnum = ctx->offnum;
                               1750           11311 :         ta->attnum = ctx->attnum;
                               1751           11311 :         ctx->toasted_attributes = lappend(ctx->toasted_attributes, ta);
                               1752                 :     }
  732 rhaas                    1753 ECB             : 
  732 rhaas                    1754 GIC       11313 :     return true;
                               1755                 : }
                               1756                 : 
                               1757                 : /*
  732 rhaas                    1758 ECB             :  * For each attribute collected in ctx->toasted_attributes, look up the value
                               1759                 :  * in the toast table and perform checks on it.  This function should only be
                               1760                 :  * called on toast pointers which cannot be vacuumed away during our
                               1761                 :  * processing.
                               1762                 :  */
                               1763                 : static void
  732 rhaas                    1764 GIC       11305 : check_toasted_attribute(HeapCheckContext *ctx, ToastedAttribute *ta)
  732 rhaas                    1765 ECB             : {
                               1766                 :     SnapshotData SnapshotToast;
                               1767                 :     ScanKeyData toastkey;
                               1768                 :     SysScanDesc toastscan;
  732 rhaas                    1769 EUB             :     bool        found_toasttup;
                               1770                 :     HeapTuple   toasttup;
                               1771                 :     uint32      extsize;
  706 rhaas                    1772 GBC       11305 :     int32       expected_chunk_seq = 0;
                               1773                 :     int32       last_chunk_seq;
  899 rhaas                    1774 ECB             : 
  706 rhaas                    1775 GIC       11305 :     extsize = VARATT_EXTERNAL_GET_EXTSIZE(ta->toast_pointer);
                               1776           11305 :     last_chunk_seq = (extsize - 1) / TOAST_MAX_CHUNK_SIZE;
  899 rhaas                    1777 EUB             : 
                               1778                 :     /*
                               1779                 :      * Setup a scan key to find chunks in toast table with matching va_valueid
                               1780                 :      */
  899 rhaas                    1781 GIC       11305 :     ScanKeyInit(&toastkey,
  899 rhaas                    1782 ECB             :                 (AttrNumber) 1,
                               1783                 :                 BTEqualStrategyNumber, F_OIDEQ,
                               1784                 :                 ObjectIdGetDatum(ta->toast_pointer.va_valueid));
                               1785                 : 
                               1786                 :     /*
                               1787                 :      * Check if any chunks for this toasted object exist in the toast table,
                               1788                 :      * accessible via the index.
  899 rhaas                    1789 EUB             :      */
  899 rhaas                    1790 GIC       11305 :     init_toast_snapshot(&SnapshotToast);
                               1791           11305 :     toastscan = systable_beginscan_ordered(ctx->toast_rel,
                               1792                 :                                            ctx->valid_toast_index,
  899 rhaas                    1793 EUB             :                                            &SnapshotToast, 1,
                               1794                 :                                            &toastkey);
  899 rhaas                    1795 CBC       11305 :     found_toasttup = false;
                               1796           11305 :     while ((toasttup =
  899 rhaas                    1797 GBC       51294 :             systable_getnext_ordered(toastscan,
  899 rhaas                    1798 GIC       51291 :                                      ForwardScanDirection)) != NULL)
                               1799                 :     {
                               1800           39989 :         found_toasttup = true;
  706                          1801           39989 :         check_toast_tuple(toasttup, ctx, ta, &expected_chunk_seq, extsize);
  899 rhaas                    1802 EUB             :     }
  899 rhaas                    1803 GIC       11302 :     systable_endscan_ordered(toastscan);
                               1804                 : 
  732                          1805           11302 :     if (!found_toasttup)
                               1806               1 :         report_toast_corruption(ctx, ta,
  725 rhaas                    1807 EUB             :                                 psprintf("toast value %u not found in toast table",
                               1808                 :                                          ta->toast_pointer.va_valueid));
  706 rhaas                    1809 GBC       11301 :     else if (expected_chunk_seq <= last_chunk_seq)
  732 rhaas                    1810 UIC           0 :         report_toast_corruption(ctx, ta,
                               1811                 :                                 psprintf("toast value %u was expected to end at chunk %d, but ended while expecting chunk %d",
                               1812                 :                                          ta->toast_pointer.va_valueid,
  706 rhaas                    1813 EUB             :                                          last_chunk_seq, expected_chunk_seq));
  899 rhaas                    1814 GIC       11302 : }
                               1815                 : 
                               1816                 : /*
                               1817                 :  * Check the current tuple as tracked in ctx, recording any corruption found in
                               1818                 :  * ctx->tupstore.
                               1819                 :  *
                               1820                 :  * We return some information about the status of xmin to aid in validating
                               1821                 :  * update chains.
  899 rhaas                    1822 ECB             :  */
                               1823                 : static void
   18 rhaas                    1824 GNC      404361 : check_tuple(HeapCheckContext *ctx, bool *xmin_commit_status_ok,
                               1825                 :             XidCommitStatus *xmin_commit_status)
                               1826                 : {
                               1827                 :     /*
                               1828                 :      * Check various forms of tuple header corruption, and if the header is
  732 rhaas                    1829 EUB             :      * too corrupt, do not continue with other checks.
                               1830                 :      */
  732 rhaas                    1831 GIC      404361 :     if (!check_tuple_header(ctx))
  899 rhaas                    1832 CBC           5 :         return;
  899 rhaas                    1833 ECB             : 
                               1834                 :     /*
  732                          1835                 :      * Check tuple visibility.  If the inserting transaction aborted, we
  732 rhaas                    1836 EUB             :      * cannot assume our relation description matches the tuple structure, and
                               1837                 :      * therefore cannot check it.
                               1838                 :      */
   18 rhaas                    1839 GNC      404356 :     if (!check_tuple_visibility(ctx, xmin_commit_status_ok,
                               1840                 :                             xmin_commit_status))
  899 rhaas                    1841 GIC          12 :         return;
                               1842                 : 
                               1843                 :     /*
                               1844                 :      * The tuple is visible, so it must be compatible with the current version
                               1845                 :      * of the relation descriptor. It might have fewer columns than are
                               1846                 :      * present in the relation descriptor, but it cannot have more.
                               1847                 :      */
                               1848          404344 :     if (RelationGetDescr(ctx->rel)->natts < ctx->natts)
                               1849                 :     {
                               1850               2 :         report_corruption(ctx,
                               1851                 :                           psprintf("number of attributes %u exceeds maximum expected for table %u",
                               1852                 :                                    ctx->natts,
                               1853               2 :                                    RelationGetDescr(ctx->rel)->natts));
                               1854               2 :         return;
                               1855                 :     }
                               1856                 : 
                               1857                 :     /*
                               1858                 :      * Check each attribute unless we hit corruption that confuses what to do
                               1859                 :      * next, at which point we abort further attribute checks for this tuple.
                               1860                 :      * Note that we don't abort for all types of corruption, only for those
                               1861                 :      * types where we don't know how to continue.  We also don't abort the
                               1862                 :      * checking of toasted attributes collected from the tuple prior to
                               1863                 :      * aborting.  Those will still be checked later along with other toasted
  732 rhaas                    1864 ECB             :      * attributes collected from the page.
                               1865                 :      */
  899 rhaas                    1866 GIC      404342 :     ctx->offset = 0;
                               1867         6099241 :     for (ctx->attnum = 0; ctx->attnum < ctx->natts; ctx->attnum++)
                               1868         5694900 :         if (!check_tuple_attribute(ctx))
                               1869               1 :             break;              /* cannot continue */
                               1870                 : 
                               1871                 :     /* revert attnum to -1 until we again examine individual attributes */
  898 tgl                      1872          404342 :     ctx->attnum = -1;
  899 rhaas                    1873 ECB             : }
                               1874                 : 
                               1875                 : /*
                               1876                 :  * Convert a TransactionId into a FullTransactionId using our cached values of
                               1877                 :  * the valid transaction ID range.  It is the caller's responsibility to have
                               1878                 :  * already updated the cached values, if necessary.
                               1879                 :  */
  899 rhaas                    1880 EUB             : static FullTransactionId
  899 rhaas                    1881 GIC       59506 : FullTransactionIdFromXidAndCtx(TransactionId xid, const HeapCheckContext *ctx)
  899 rhaas                    1882 EUB             : {
   29 andres                   1883                 :     uint64      nextfxid_i;
                               1884                 :     int32       diff;
                               1885                 :     FullTransactionId fxid;
                               1886                 : 
   29 andres                   1887 GIC       59506 :     Assert(TransactionIdIsNormal(ctx->next_xid));
                               1888           59506 :     Assert(FullTransactionIdIsNormal(ctx->next_fxid));
   29 andres                   1889 CBC       59506 :     Assert(XidFromFullTransactionId(ctx->next_fxid) == ctx->next_xid);
   29 andres                   1890 ECB             : 
  899 rhaas                    1891 GIC       59506 :     if (!TransactionIdIsNormal(xid))
                               1892             191 :         return FullTransactionIdFromEpochAndXid(0, xid);
   29 andres                   1893 ECB             : 
   29 andres                   1894 GIC       59315 :     nextfxid_i = U64FromFullTransactionId(ctx->next_fxid);
   29 andres                   1895 ECB             : 
                               1896                 :     /* compute the 32bit modulo difference */
   29 andres                   1897 GIC       59315 :     diff = (int32) (ctx->next_xid - xid);
   29 andres                   1898 ECB             : 
                               1899                 :     /*
   29 andres                   1900 EUB             :      * In cases of corruption we might see a 32bit xid that is before epoch
                               1901                 :      * 0. We can't represent that as a 64bit xid, due to 64bit xids being
                               1902                 :      * unsigned integers, without the modulo arithmetic of 32bit xid. There's
                               1903                 :      * no really nice way to deal with that, but it works ok enough to use
                               1904                 :      * FirstNormalFullTransactionId in that case, as a freshly initdb'd
                               1905                 :      * cluster already has a newer horizon.
                               1906                 :      */
   29 andres                   1907 CBC       59315 :     if (diff > 0 && (nextfxid_i - FirstNormalTransactionId) < (int64) diff)
                               1908                 :     {
   29 andres                   1909 GIC           4 :         Assert(EpochFromFullTransactionId(ctx->next_fxid) == 0);
                               1910               4 :         fxid = FirstNormalFullTransactionId;
   29 andres                   1911 ECB             :     }
                               1912                 :     else
   29 andres                   1913 GIC       59311 :         fxid = FullTransactionIdFromU64(nextfxid_i - diff);
                               1914                 : 
   29 andres                   1915 CBC       59315 :     Assert(FullTransactionIdIsNormal(fxid));
   29 andres                   1916 GIC       59315 :     return fxid;
                               1917                 : }
                               1918                 : 
                               1919                 : /*
                               1920                 :  * Update our cached range of valid transaction IDs.
                               1921                 :  */
                               1922                 : static void
  899 rhaas                    1923            1203 : update_cached_xid_range(HeapCheckContext *ctx)
                               1924                 : {
                               1925                 :     /* Make cached copies */
  899 rhaas                    1926 CBC        1203 :     LWLockAcquire(XidGenLock, LW_SHARED);
  899 rhaas                    1927 GIC        1203 :     ctx->next_fxid = ShmemVariableCache->nextXid;
  899 rhaas                    1928 CBC        1203 :     ctx->oldest_xid = ShmemVariableCache->oldestXid;
  899 rhaas                    1929 GIC        1203 :     LWLockRelease(XidGenLock);
  899 rhaas                    1930 ECB             : 
                               1931                 :     /* And compute alternate versions of the same */
  899 rhaas                    1932 GBC        1203 :     ctx->next_xid = XidFromFullTransactionId(ctx->next_fxid);
   29 andres                   1933 GIC        1203 :     ctx->oldest_fxid = FullTransactionIdFromXidAndCtx(ctx->oldest_xid, ctx);
  899 rhaas                    1934            1203 : }
                               1935                 : 
  899 rhaas                    1936 EUB             : /*
                               1937                 :  * Update our cached range of valid multitransaction IDs.
                               1938                 :  */
                               1939                 : static void
  899 rhaas                    1940 GIC        1201 : update_cached_mxid_range(HeapCheckContext *ctx)
  899 rhaas                    1941 ECB             : {
  899 rhaas                    1942 GIC        1201 :     ReadMultiXactIdRange(&ctx->oldest_mxact, &ctx->next_mxact);
                               1943            1201 : }
  899 rhaas                    1944 ECB             : 
                               1945                 : /*
                               1946                 :  * Return whether the given FullTransactionId is within our cached valid
                               1947                 :  * transaction ID range.
                               1948                 :  */
                               1949                 : static inline bool
  899 rhaas                    1950 CBC       50841 : fxid_in_cached_range(FullTransactionId fxid, const HeapCheckContext *ctx)
                               1951                 : {
                               1952          101679 :     return (FullTransactionIdPrecedesOrEquals(ctx->oldest_fxid, fxid) &&
  899 rhaas                    1953 GIC       50838 :             FullTransactionIdPrecedes(fxid, ctx->next_fxid));
                               1954                 : }
                               1955                 : 
                               1956                 : /*
                               1957                 :  * Checks whether a multitransaction ID is in the cached valid range, returning
                               1958                 :  * the nature of the range violation, if any.
                               1959                 :  */
                               1960                 : static XidBoundsViolation
  899 rhaas                    1961 CBC          60 : check_mxid_in_range(MultiXactId mxid, HeapCheckContext *ctx)
                               1962                 : {
  899 rhaas                    1963 GIC          60 :     if (!TransactionIdIsValid(mxid))
  899 rhaas                    1964 UIC           0 :         return XID_INVALID;
  899 rhaas                    1965 GIC          60 :     if (MultiXactIdPrecedes(mxid, ctx->relminmxid))
                               1966               2 :         return XID_PRECEDES_RELMIN;
                               1967              58 :     if (MultiXactIdPrecedes(mxid, ctx->oldest_mxact))
  899 rhaas                    1968 UIC           0 :         return XID_PRECEDES_CLUSTERMIN;
  899 rhaas                    1969 CBC          58 :     if (MultiXactIdPrecedesOrEquals(ctx->next_mxact, mxid))
                               1970               2 :         return XID_IN_FUTURE;
  899 rhaas                    1971 GIC          56 :     return XID_BOUNDS_OK;
                               1972                 : }
                               1973                 : 
                               1974                 : /*
                               1975                 :  * Checks whether the given mxid is valid to appear in the heap being checked,
                               1976                 :  * returning the nature of the range violation, if any.
  899 rhaas                    1977 ECB             :  *
                               1978                 :  * This function attempts to return quickly by caching the known valid mxid
                               1979                 :  * range in ctx.  Callers should already have performed the initial setup of
                               1980                 :  * the cache prior to the first call to this function.
  899 rhaas                    1981 EUB             :  */
                               1982                 : static XidBoundsViolation
  899 rhaas                    1983 GIC          58 : check_mxid_valid_in_rel(MultiXactId mxid, HeapCheckContext *ctx)
                               1984                 : {
                               1985                 :     XidBoundsViolation result;
                               1986                 : 
  899 rhaas                    1987 CBC          58 :     result = check_mxid_in_range(mxid, ctx);
  899 rhaas                    1988 GIC          58 :     if (result == XID_BOUNDS_OK)
                               1989              56 :         return XID_BOUNDS_OK;
  899 rhaas                    1990 ECB             : 
                               1991                 :     /* The range may have advanced.  Recheck. */
  899 rhaas                    1992 GIC           2 :     update_cached_mxid_range(ctx);
  899 rhaas                    1993 CBC           2 :     return check_mxid_in_range(mxid, ctx);
  899 rhaas                    1994 ECB             : }
                               1995                 : 
                               1996                 : /*
                               1997                 :  * Checks whether the given transaction ID is (or was recently) valid to appear
                               1998                 :  * in the heap being checked, or whether it is too old or too new to appear in
                               1999                 :  * the relation, returning information about the nature of the bounds violation.
                               2000                 :  *
                               2001                 :  * We cache the range of valid transaction IDs.  If xid is in that range, we
                               2002                 :  * conclude that it is valid, even though concurrent changes to the table might
  899 rhaas                    2003 EUB             :  * invalidate it under certain corrupt conditions.  (For example, if the table
                               2004                 :  * contains corrupt all-frozen bits, a concurrent vacuum might skip the page(s)
                               2005                 :  * containing the xid and then truncate clog and advance the relfrozenxid
                               2006                 :  * beyond xid.) Reporting the xid as valid under such conditions seems
                               2007                 :  * acceptable, since if we had checked it earlier in our scan it would have
  899 rhaas                    2008 ECB             :  * truly been valid at that time.
  899 rhaas                    2009 EUB             :  *
                               2010                 :  * If the status argument is not NULL, and if and only if the transaction ID
                               2011                 :  * appears to be valid in this relation, the status argument will be set with
                               2012                 :  * the commit status of the transaction ID.
                               2013                 :  */
                               2014                 : static XidBoundsViolation
  899 rhaas                    2015 CBC      404512 : get_xid_status(TransactionId xid, HeapCheckContext *ctx,
                               2016                 :                XidCommitStatus *status)
  899 rhaas                    2017 EUB             : {
                               2018                 :     FullTransactionId fxid;
                               2019                 :     FullTransactionId clog_horizon;
                               2020                 : 
                               2021                 :     /* Quick check for special xids */
  899 rhaas                    2022 GIC      404512 :     if (!TransactionIdIsValid(xid))
                               2023               1 :         return XID_INVALID;
  899 rhaas                    2024 CBC      404511 :     else if (xid == BootstrapTransactionId || xid == FrozenTransactionId)
                               2025                 :     {
  899 rhaas                    2026 GBC      353670 :         if (status != NULL)
  899 rhaas                    2027 GIC      353670 :             *status = XID_COMMITTED;
                               2028          353670 :         return XID_BOUNDS_OK;
  899 rhaas                    2029 EUB             :     }
                               2030                 : 
                               2031                 :     /* Check if the xid is within bounds */
  899 rhaas                    2032 GIC       50841 :     fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
  899 rhaas                    2033 CBC       50841 :     if (!fxid_in_cached_range(fxid, ctx))
  899 rhaas                    2034 ECB             :     {
                               2035                 :         /*
                               2036                 :          * We may have been checking against stale values.  Update the cached
                               2037                 :          * range to be sure, and since we relied on the cached range when we
                               2038                 :          * performed the full xid conversion, reconvert.
                               2039                 :          */
  899 rhaas                    2040 GIC           4 :         update_cached_xid_range(ctx);
  899 rhaas                    2041 CBC           4 :         fxid = FullTransactionIdFromXidAndCtx(xid, ctx);
                               2042                 :     }
                               2043                 : 
  899 rhaas                    2044 GIC       50841 :     if (FullTransactionIdPrecedesOrEquals(ctx->next_fxid, fxid))
  899 rhaas                    2045 CBC           1 :         return XID_IN_FUTURE;
  899 rhaas                    2046 GIC       50840 :     if (FullTransactionIdPrecedes(fxid, ctx->oldest_fxid))
  899 rhaas                    2047 CBC           3 :         return XID_PRECEDES_CLUSTERMIN;
                               2048           50837 :     if (FullTransactionIdPrecedes(fxid, ctx->relfrozenfxid))
                               2049               1 :         return XID_PRECEDES_RELMIN;
  899 rhaas                    2050 ECB             : 
                               2051                 :     /* Early return if the caller does not request clog checking */
  899 rhaas                    2052 GIC       50836 :     if (status == NULL)
  899 rhaas                    2053 UIC           0 :         return XID_BOUNDS_OK;
  899 rhaas                    2054 ECB             : 
                               2055                 :     /* Early return if we just checked this xid in a prior call */
  899 rhaas                    2056 GIC       50836 :     if (xid == ctx->cached_xid)
                               2057                 :     {
                               2058           44577 :         *status = ctx->cached_status;
                               2059           44577 :         return XID_BOUNDS_OK;
                               2060                 :     }
                               2061                 : 
                               2062            6259 :     *status = XID_COMMITTED;
                               2063            6259 :     LWLockAcquire(XactTruncationLock, LW_SHARED);
  899 rhaas                    2064 ECB             :     clog_horizon =
  899 rhaas                    2065 GIC        6259 :         FullTransactionIdFromXidAndCtx(ShmemVariableCache->oldestClogXid,
                               2066                 :                                        ctx);
                               2067            6259 :     if (FullTransactionIdPrecedesOrEquals(clog_horizon, fxid))
                               2068                 :     {
                               2069            6259 :         if (TransactionIdIsCurrentTransactionId(xid))
  738 rhaas                    2070 UIC           0 :             *status = XID_IS_CURRENT_XID;
  738 rhaas                    2071 GIC        6259 :         else if (TransactionIdIsInProgress(xid))
  899 rhaas                    2072 CBC           2 :             *status = XID_IN_PROGRESS;
  899 rhaas                    2073 GIC        6257 :         else if (TransactionIdDidCommit(xid))
                               2074            6250 :             *status = XID_COMMITTED;
  899 rhaas                    2075 ECB             :         else
  738 rhaas                    2076 CBC           7 :             *status = XID_ABORTED;
                               2077                 :     }
  899 rhaas                    2078 GIC        6259 :     LWLockRelease(XactTruncationLock);
                               2079            6259 :     ctx->cached_xid = xid;
                               2080            6259 :     ctx->cached_status = *status;
  899 rhaas                    2081 CBC        6259 :     return XID_BOUNDS_OK;
                               2082                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a