diff --git a/src/jrd/vio.cpp b/src/jrd/vio.cpp index c427a14d9ad..0b1bceea62f 100644 --- a/src/jrd/vio.cpp +++ b/src/jrd/vio.cpp @@ -1370,9 +1370,15 @@ bool VIO_chase_record_version(thread_db* tdbb, record_param* rpb, return false; } - record_param temp = *rpb; - if ((!(rpb->rpb_flags & rpb_deleted)) || (rpb->rpb_flags & rpb_delta)) + if (rpb->rpb_flags & rpb_chained) { + // Keep the back version's page locked, see the tra_active case below + if ((!(rpb->rpb_flags & rpb_deleted)) || (rpb->rpb_flags & rpb_delta)) + VIO_data(tdbb, rpb, pool, true); + } + else if ((!(rpb->rpb_flags & rpb_deleted)) || (rpb->rpb_flags & rpb_delta)) + { + record_param temp = *rpb; VIO_data(tdbb, rpb, pool); rpb->rpb_page = temp.rpb_page; rpb->rpb_line = temp.rpb_line; @@ -1499,22 +1505,26 @@ bool VIO_chase_record_version(thread_db* tdbb, record_param* rpb, // the first record back may disappear because the primary record was backed // out, and now the first backup back in the primary record's place. - record_param temp = *rpb; - VIO_data(tdbb, rpb, pool); - if (temp.rpb_flags & rpb_chained) + if (rpb->rpb_flags & rpb_chained) { - rpb->rpb_page = temp.rpb_b_page; - rpb->rpb_line = temp.rpb_b_line; - if (!DPM_fetch(tdbb, rpb, LCK_read)) + // A back version cannot be re-checked after its page is released: + // intermediate GC of another attachment may replace the version + // chain meanwhile, and the slot at the saved back pointer may get + // a version of another record. So keep the page latched and + // follow the back pointer with a handoff. + + VIO_data(tdbb, rpb, pool, true); + + if (!DPM_fetch_back(tdbb, rpb, LCK_read, -1)) { - // Things have changed, start all over again. if (!DPM_get(tdbb, rpb, LCK_read)) - return false; // entire record disappeared - break; // start from the primary version again + return false; } } else { + record_param temp = *rpb; + VIO_data(tdbb, rpb, pool); rpb->rpb_page = temp.rpb_page; rpb->rpb_line = temp.rpb_line; if (!DPM_fetch(tdbb, rpb, LCK_read)) @@ -1758,7 +1768,7 @@ void VIO_copy_record(thread_db* tdbb, jrd_rel* relation, Record* orgRecord, Reco } -void VIO_data(thread_db* tdbb, record_param* rpb, MemoryPool* pool) +void VIO_data(thread_db* tdbb, record_param* rpb, MemoryPool* pool, bool keepActive) { /************************************** * @@ -1773,6 +1783,10 @@ void VIO_data(thread_db* tdbb, record_param* rpb, MemoryPool* pool) * an INactive record_param. Yes, Virginia, getting the data for a * record means losing control of the record. This turns out * to matter a lot. + * + * If keepActive is true, the record_param stays active: the page with + * the last fragment of the record stays locked, so the caller can + * follow the back pointer with a handoff. **************************************/ SET_TDBB(tdbb); @@ -1857,7 +1871,8 @@ void VIO_data(thread_db* tdbb, record_param* rpb, MemoryPool* pool) rpb->rpb_f_line = save_f_line; } - CCH_RELEASE(tdbb, &rpb->getWindow(tdbb)); + if (!keepActive) + CCH_RELEASE(tdbb, &rpb->getWindow(tdbb)); // If this is a delta version, apply changes ULONG length; diff --git a/src/jrd/vio_proto.h b/src/jrd/vio_proto.h index f9bbdc75599..c627b5e36ba 100644 --- a/src/jrd/vio_proto.h +++ b/src/jrd/vio_proto.h @@ -56,7 +56,7 @@ void VIO_backout(Jrd::thread_db*, Jrd::record_param*, const Jrd::jrd_tra*); bool VIO_chase_record_version(Jrd::thread_db*, Jrd::record_param*, Jrd::jrd_tra*, MemoryPool*, bool, bool); void VIO_copy_record(Jrd::thread_db*, Jrd::jrd_rel*, Jrd::Record*, Jrd::Record*); -void VIO_data(Jrd::thread_db*, Jrd::record_param*, MemoryPool*); +void VIO_data(Jrd::thread_db*, Jrd::record_param*, MemoryPool*, bool keepActive = false); bool VIO_erase(Jrd::thread_db*, Jrd::record_param*, Jrd::jrd_tra*); void VIO_fini(Jrd::thread_db*); bool VIO_garbage_collect(Jrd::thread_db*, Jrd::record_param*, Jrd::jrd_tra*);