Skip to content

Commit c590e51

Browse files
committed
Prevent access to no-longer-pinned buffer in heapam_tuple_lock().
heap_fetch() used to have a "keep_buf" parameter that told it to return ownership of the buffer pin to the caller after finding that the requested tuple TID exists but is invisible to the specified snapshot. This was thoughtlessly removed in commit 5db6df0, which broke heapam_tuple_lock() (formerly EvalPlanQualFetch) because that function needs to do more accesses to the tuple even if it's invisible. The net effect is that we would continue to touch the page for a microsecond or two after releasing pin on the buffer. Usually no harm would result; but if a different session decided to defragment the page concurrently, we could see garbage data and mistakenly conclude that there's no newer tuple version to chain up to. (It's hard to say whether this has happened in the field. The bug was actually found thanks to a later change that allowed valgrind to detect accesses to non-pinned buffers.) The most reasonable way to fix this is to reintroduce keep_buf, although I made it behave slightly differently: buffer ownership is passed back only if there is a valid tuple at the requested TID. In HEAD, we can just add the parameter back to heap_fetch(). To avoid an API break in the back branches, introduce an additional function heap_fetch_extended() in those branches. In HEAD there is an additional, less obvious API change: tuple->t_data will be set to NULL in all cases where buffer ownership is not returned, in particular when the tuple exists but fails the time qual (and !keep_buf). This is to defend against any other callers attempting to access non-pinned buffers. We concluded that making that change in back branches would be more likely to introduce problems than cure any. In passing, remove a comment about heap_fetch that was obsoleted by 9a8ee1d. Per bug #17462 from Daniil Anisimov. Back-patch to v12 where the bug was introduced. Discussion: https://postgr.es/m/17462-9c98a0f00df9bd36@postgresql.org
1 parent ea669b8 commit c590e51

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/backend/access/heap/heapam.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,10 +1576,13 @@ heap_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction,
15761576
* must unpin the buffer when done with the tuple.
15771577
*
15781578
* If the tuple is not found (ie, item number references a deleted slot),
1579-
* then tuple->t_data is set to NULL and false is returned.
1579+
* then tuple->t_data is set to NULL, *userbuf is set to InvalidBuffer,
1580+
* and false is returned.
15801581
*
15811582
* If the tuple is found but fails the time qual check, then false is returned
1582-
* but tuple->t_data is left pointing to the tuple.
1583+
* and *userbuf is set to InvalidBuffer, but tuple->t_data is left pointing
1584+
* to the tuple. (Note that it is unsafe to dereference tuple->t_data in
1585+
* this case, but callers might choose to test it for NULL-ness.)
15831586
*
15841587
* heap_fetch does not follow HOT chains: only the exact TID requested will
15851588
* be fetched.
@@ -1598,6 +1601,25 @@ heap_fetch(Relation relation,
15981601
Snapshot snapshot,
15991602
HeapTuple tuple,
16001603
Buffer *userbuf)
1604+
{
1605+
return heap_fetch_extended(relation, snapshot, tuple, userbuf, false);
1606+
}
1607+
1608+
/*
1609+
* heap_fetch_extended - fetch tuple even if it fails snapshot test
1610+
*
1611+
* If keep_buf is true, then upon finding a tuple that is valid but fails
1612+
* the snapshot check, we return the tuple pointer in tuple->t_data and the
1613+
* buffer ID in *userbuf, keeping the buffer pin, just as if it had passed
1614+
* the snapshot. (The function result is still "false" though.)
1615+
* If keep_buf is false then this behaves identically to heap_fetch().
1616+
*/
1617+
bool
1618+
heap_fetch_extended(Relation relation,
1619+
Snapshot snapshot,
1620+
HeapTuple tuple,
1621+
Buffer *userbuf,
1622+
bool keep_buf)
16011623
{
16021624
ItemPointer tid = &(tuple->t_self);
16031625
ItemId lp;
@@ -1680,9 +1702,14 @@ heap_fetch(Relation relation,
16801702
return true;
16811703
}
16821704

1683-
/* Tuple failed time qual */
1684-
ReleaseBuffer(buffer);
1685-
*userbuf = InvalidBuffer;
1705+
/* Tuple failed time qual, but maybe caller wants to see it anyway. */
1706+
if (keep_buf)
1707+
*userbuf = buffer;
1708+
else
1709+
{
1710+
ReleaseBuffer(buffer);
1711+
*userbuf = InvalidBuffer;
1712+
}
16861713

16871714
return false;
16881715
}
@@ -1705,8 +1732,7 @@ heap_fetch(Relation relation,
17051732
* are vacuumable, false if not.
17061733
*
17071734
* Unlike heap_fetch, the caller must already have pin and (at least) share
1708-
* lock on the buffer; it is still pinned/locked at exit. Also unlike
1709-
* heap_fetch, we do not report any pgstats count; caller may do so if wanted.
1735+
* lock on the buffer; it is still pinned/locked at exit.
17101736
*/
17111737
bool
17121738
heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer,

src/backend/access/heap/heapam_handler.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
401401
errmsg("tuple to be locked was already moved to another partition due to concurrent update")));
402402

403403
tuple->t_self = *tid;
404-
if (heap_fetch(relation, &SnapshotDirty, tuple, &buffer))
404+
if (heap_fetch_extended(relation, &SnapshotDirty, tuple,
405+
&buffer, true))
405406
{
406407
/*
407408
* If xmin isn't what we're expecting, the slot must have
@@ -500,6 +501,7 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
500501
*/
501502
if (tuple->t_data == NULL)
502503
{
504+
Assert(!BufferIsValid(buffer));
503505
return TM_Deleted;
504506
}
505507

@@ -509,8 +511,7 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
509511
if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple->t_data),
510512
priorXmax))
511513
{
512-
if (BufferIsValid(buffer))
513-
ReleaseBuffer(buffer);
514+
ReleaseBuffer(buffer);
514515
return TM_Deleted;
515516
}
516517

@@ -526,22 +527,20 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
526527
*
527528
* As above, it should be safe to examine xmax and t_ctid
528529
* without the buffer content lock, because they can't be
529-
* changing.
530+
* changing. We'd better hold a buffer pin though.
530531
*/
531532
if (ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
532533
{
533534
/* deleted, so forget about it */
534-
if (BufferIsValid(buffer))
535-
ReleaseBuffer(buffer);
535+
ReleaseBuffer(buffer);
536536
return TM_Deleted;
537537
}
538538

539539
/* updated, so look at the updated row */
540540
*tid = tuple->t_data->t_ctid;
541541
/* updated row should have xmin matching this xmax */
542542
priorXmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
543-
if (BufferIsValid(buffer))
544-
ReleaseBuffer(buffer);
543+
ReleaseBuffer(buffer);
545544
/* loop back to fetch next in chain */
546545
}
547546
}

src/include/access/heapam.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ extern bool heap_getnextslot_tidrange(TableScanDesc sscan,
134134
TupleTableSlot *slot);
135135
extern bool heap_fetch(Relation relation, Snapshot snapshot,
136136
HeapTuple tuple, Buffer *userbuf);
137+
extern bool heap_fetch_extended(Relation relation, Snapshot snapshot,
138+
HeapTuple tuple, Buffer *userbuf,
139+
bool keep_buf);
137140
extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation,
138141
Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
139142
bool *all_dead, bool first_call);

0 commit comments

Comments
 (0)