Skip to content

Commit

Permalink
amcheck: Fix verify_heapam for tuples where xmin or xmax is 0.
Browse files Browse the repository at this point in the history
In such cases, get_xid_status() doesn't set its output parameter (the
third argument), so we shouldn't fall through to code which will test
the value of that parameter. There are five existing calls to
get_xid_status(), three of which seem to already handle this case
properly.  This commit tries to fix the other two.

If we're checking xmin and find that it is invalid (i.e. 0) just
report that as corruption, similar to what's already done in the
three cases that seem correct. If we're checking xmax and find
that's invalid, that's fine: it just means that the tuple hasn't
been updated or deleted.

Thanks to Andres Freund and valgrind for finding this problem, and
also to Andres for having a look at the patch.  This bug seems to go
all the way back to where verify_heapam was first introduced, but
wasn't detected until recently, possibly because of the new test cases
added for update chain verification.  Back-patch to v14, where this
code showed up.

Discussion: http://postgr.es/m/CA+TgmoZAYzQZqyUparXy_ks3OEOfLD9-bEXt8N-2tS1qghX9gQ@mail.gmail.com
  • Loading branch information
robertmhaas committed Mar 24, 2023
1 parent 9dac02c commit 8fd5aa7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion contrib/amcheck/verify_heapam.c
Expand Up @@ -774,6 +774,9 @@ check_tuple_visibility(HeapCheckContext *ctx)
switch (get_xid_status(xmin, ctx, &xmin_status))
{
case XID_INVALID:
report_corruption(ctx,
pstrdup("xmin is invalid"));
return false;
case XID_BOUNDS_OK:
break;
case XID_IN_FUTURE:
Expand Down Expand Up @@ -1109,6 +1112,9 @@ check_tuple_visibility(HeapCheckContext *ctx)
xmax = HeapTupleHeaderGetRawXmax(tuphdr);
switch (get_xid_status(xmax, ctx, &xmax_status))
{
case XID_INVALID:
ctx->tuple_could_be_pruned = false;
return true;
case XID_IN_FUTURE:
report_corruption(ctx,
psprintf("xmax %u equals or exceeds next valid transaction ID %u:%u",
Expand All @@ -1131,7 +1137,6 @@ check_tuple_visibility(HeapCheckContext *ctx)
XidFromFullTransactionId(ctx->oldest_fxid)));
return false; /* corrupt */
case XID_BOUNDS_OK:
case XID_INVALID:
break;
}

Expand Down

0 comments on commit 8fd5aa7

Please sign in to comment.