Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
311 changes: 273 additions & 38 deletions src/jrd/dpm.epp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,71 @@ void DPM_backout_mark(thread_db* tdbb, record_param* rpb, const jrd_tra* transac
}


namespace
{
// Maximum number of non-full (potentially sparse) data pages to inspect or sample
constexpr ULONG MAX_SAMPLED_NONFULL_PAGES = 32;

// Maximum number of full data pages to inspect or sample in stratified estimation
constexpr ULONG MAX_SAMPLED_FULL_PAGES = 8;

// Maximum number of pointer pages to walk when counting a small relation
// whose remaining data pages are spread across multiple pointer pages
constexpr ULONG MAX_SCANNED_POINTER_PAGES = 32;

// Treat a single-pointer-page relation as dense (single data-page fetch)
// when it has more than MAX_SAMPLED_FULL_PAGES full pages and non-full pages
// are fewer than 1 / DENSE_NONFULL_RATIO of full pages
constexpr ULONG DENSE_NONFULL_RATIO = 8;

inline bool isQualifyingDataPage(const pointer_page* ppage, const UCHAR* bits, USHORT slot) noexcept
{
return ppage->ppg_page[slot] &&
!PPG_DP_BIT_TEST(bits, slot, ppg_dp_secondary) &&
!PPG_DP_BIT_TEST(bits, slot, ppg_dp_empty) &&
!PPG_DP_BIT_TEST(bits, slot, ppg_dp_reserved);
}

ULONG countDataPageRecords(thread_db* tdbb, ULONG pageSpaceId, ULONG pageNum,
ULONG* rawCount = nullptr, ULONG* rawLength = nullptr)
{
WIN dpWindow(pageSpaceId, pageNum);
const auto* dpage = (const data_page*) CCH_FETCH(tdbb, &dpWindow, LCK_read, pag_data);

ULONG primaryCount = 0;
const data_page::dpg_repeat* index = dpage->dpg_rpt;
const data_page::dpg_repeat* const end = index + dpage->dpg_count;

for (; index < end; index++)
{
if (!index->dpg_offset || index->dpg_length < RHD_SIZE)
continue;

if (rawCount)
{
(*rawCount)++;
*rawLength += index->dpg_length - RHD_SIZE;
}

// Back versions, fragments and blobs are not primary records.
// Deleted but not yet garbage-collected primary records are counted
// as they may still be visible to active transactions and must be visited.
const auto* header = (const rhd*) ((const UCHAR*) dpage + index->dpg_offset);
if (!(header->rhd_flags & (rhd_chain | rhd_fragment | rhd_blob)))
primaryCount++;
}

CCH_RELEASE(tdbb, &dpWindow);
return primaryCount;
}

inline ULONG sampleIndex(ULONG sample, ULONG count, ULONG samples) noexcept
{
return (ULONG) (((FB_UINT64) sample * count + count / 2) / samples);
}
}


double DPM_cardinality(thread_db* tdbb, jrd_rel* relation, const Format* format)
{
/**************************************
Expand All @@ -325,75 +390,245 @@ double DPM_cardinality(thread_db* tdbb, jrd_rel* relation, const Format* format)

// Get the number of data pages for this relation

const ULONG dataPages = DPM_data_pages(tdbb, relation);
ULONG dataPages = DPM_data_pages(tdbb, relation);
if (!dataPages)
return 0.0;

const auto relPages = relation->getPages(tdbb);
if (!relPages->hasData())
return 0.0;

// Calculate record count and total compressed record length
// on the first data page
const ULONG pageSpaceId = relPages->getPageSpaceId();

ULONG recordCount = 0, recordLength = 0;
double effectivePages = (double) dataPages;

const auto relPages = relation->getPages(tdbb);
if (relPages->hasData())
WIN window(pageSpaceId);
const pointer_page* ppage =
get_pointer_page(tdbb, relation->getPermanent(), relPages, &window, 0, LCK_read);
if (!ppage)
{
BUGCHECK(243);
// msg 243 missing pointer page in DPM_data_pages
}

if (ppage->ppg_header.pag_flags & ppg_eof)
{
const ULONG pageSpaceId = relPages->getPageSpaceId();
// Single pointer page: we can classify every qualifying data page in memory
// into full (ppg_dp_full) and non-full (potentially sparse) strata.

bool done = false;
for (ULONG sequence = 0; !done; sequence++)
const UCHAR* bits = (UCHAR*)(ppage->ppg_page + dbb->dbb_dp_per_pp);
ULONG fullCount = 0, nonFullCount = 0;
USHORT firstFullSlot = 0;

for (USHORT slot = 0; slot < ppage->ppg_count; slot++)
{
WIN window(pageSpaceId);
if (!isQualifyingDataPage(ppage, bits, slot))
continue;

const pointer_page* ppage =
get_pointer_page(tdbb, relation->getPermanent(), relPages, &window, sequence, LCK_read);
if (!ppage)
if (PPG_DP_BIT_TEST(bits, slot, ppg_dp_full))
{
if (!fullCount)
firstFullSlot = slot;
fullCount++;
}
else
nonFullCount++;
}

if (!fullCount && !nonFullCount)
{
CCH_RELEASE(tdbb, &window);
return 0.0;
}

if (fullCount > MAX_SAMPLED_FULL_PAGES &&
nonFullCount * DENSE_NONFULL_RATIO < fullCount)
{
// Dense relation: keep single-page prepare I/O by inspecting a full page
// and extrapolating via the standard record-size capacity formula.

ULONG checked = 0;
for (USHORT slot = firstFullSlot;
slot < ppage->ppg_count && !recordCount && checked < MAX_SAMPLED_FULL_PAGES;
slot++)
{
if (isQualifyingDataPage(ppage, bits, slot) &&
PPG_DP_BIT_TEST(bits, slot, ppg_dp_full))
{
countDataPageRecords(tdbb, pageSpaceId, ppage->ppg_page[slot],
&recordCount, &recordLength);
checked++;
}
}

CCH_RELEASE(tdbb, &window);
effectivePages = (double) fullCount + 0.5 * nonFullCount;
}
else
{
// Small or sparse relation: count or evenly sample primary records per stratum
// while holding the pointer page read latch.

const ULONG fullSamples = MIN(fullCount, MAX_SAMPLED_FULL_PAGES);
const ULONG nonFullSamples = MIN(nonFullCount, MAX_SAMPLED_NONFULL_PAGES);

ULONG fullSeen = 0, fullNext = 0, fullPrimary = 0;
ULONG nonFullSeen = 0, nonFullNext = 0, nonFullPrimary = 0;
ULONG fullTarget = fullSamples ? sampleIndex(0, fullCount, fullSamples) : 0;
ULONG nonFullTarget = nonFullSamples ? sampleIndex(0, nonFullCount, nonFullSamples) : 0;

for (USHORT slot = 0;
slot < ppage->ppg_count && (fullNext < fullSamples || nonFullNext < nonFullSamples);
slot++)
{
BUGCHECK(243);
// msg 243 missing pointer page in DPM_data_pages
if (!isQualifyingDataPage(ppage, bits, slot))
continue;

if (PPG_DP_BIT_TEST(bits, slot, ppg_dp_full))
{
if (fullNext < fullSamples && fullSeen == fullTarget)
{
fullPrimary += countDataPageRecords(tdbb, pageSpaceId, ppage->ppg_page[slot]);
if (++fullNext < fullSamples)
fullTarget = sampleIndex(fullNext, fullCount, fullSamples);
}
fullSeen++;
}
else
{
if (nonFullNext < nonFullSamples && nonFullSeen == nonFullTarget)
{
nonFullPrimary += countDataPageRecords(tdbb, pageSpaceId, ppage->ppg_page[slot]);
if (++nonFullNext < nonFullSamples)
nonFullTarget = sampleIndex(nonFullNext, nonFullCount, nonFullSamples);
}
nonFullSeen++;
}
}

CCH_RELEASE(tdbb, &window);

double estimate = 0.0;
if (fullSamples)
estimate += (double) fullPrimary * fullCount / fullSamples;
if (nonFullSamples)
estimate += (double) nonFullPrimary * nonFullCount / nonFullSamples;
return estimate;
}
}
else
{
// Multiple pointer pages. If the relation looks small (e.g. shrank after deletions),
// count primary records directly across up to MAX_SAMPLED_NONFULL_PAGES data pages
// and MAX_SCANNED_POINTER_PAGES pointer pages. Otherwise inspect only the first
// non-empty data page for average compressed record length.

bool countAll = (dataPages <= MAX_SAMPLED_NONFULL_PAGES);
ULONG countedPages = 0, primaryCount = 0, firstPagePrimaryCount = 0;
bool morePages = false;

for (ULONG sequence = 0; ; sequence++)
{
if (sequence > 0)
{
ppage = get_pointer_page(tdbb, relation->getPermanent(), relPages, &window, sequence, LCK_read);
if (!ppage)
{
BUGCHECK(243);
// msg 243 missing pointer page in DPM_data_pages
}
}

const UCHAR* bits = (UCHAR*)(ppage->ppg_page + dbb->dbb_dp_per_pp);
for (USHORT slot = 0; slot < ppage->ppg_count; slot++)
bool done = false;

for (USHORT slot = 0; slot < ppage->ppg_count && !done; slot++)
{
if (ppage->ppg_page[slot] &&
!PPG_DP_BIT_TEST(bits, slot, ppg_dp_secondary) &&
!PPG_DP_BIT_TEST(bits, slot, ppg_dp_empty))
if (!isQualifyingDataPage(ppage, bits, slot))
continue;

if (countAll)
{
if (countedPages == MAX_SAMPLED_NONFULL_PAGES)
{
morePages = done = true;
break;
}

const bool needRaw = (recordCount == 0);
const ULONG pagePrimary = countDataPageRecords(tdbb, pageSpaceId,
ppage->ppg_page[slot],
needRaw ? &recordCount : nullptr,
needRaw ? &recordLength : nullptr);

if (needRaw && recordCount)
firstPagePrimaryCount = pagePrimary;

primaryCount += pagePrimary;
countedPages++;
}
else
{
Ods::data_page* dpage =
(Ods::data_page*) CCH_HANDOFF(tdbb, &window, ppage->ppg_page[slot], LCK_read, pag_data);
const ULONG pagePrimary = countDataPageRecords(tdbb, pageSpaceId,
ppage->ppg_page[slot], &recordCount, &recordLength);

const data_page::dpg_repeat* index = dpage->dpg_rpt;
const data_page::dpg_repeat* const end = index + dpage->dpg_count;
for (; index < end; index++)
if (recordCount)
{
if (index->dpg_offset)
{
recordCount++;
recordLength += index->dpg_length - RHD_SIZE;
}
firstPagePrimaryCount = pagePrimary;
done = true;
}
}
}

const bool reachedEof = (ppage->ppg_header.pag_flags & ppg_eof) != 0;
CCH_RELEASE(tdbb, &window);

if (countAll)
{
if (!done && reachedEof)
return (double) primaryCount;

if (done)
break;

if (sequence + 1 >= MAX_SCANNED_POINTER_PAGES)
{
countAll = false;
if (recordCount)
break;
}
}
else if (done || reachedEof)
break;

done = (recordCount != 0) || (ppage->ppg_header.pag_flags & ppg_eof);
CCH_RELEASE(tdbb, &window);
tdbb->checkCancelState();
}

// Refresh outdated cached data-page count if more pages were discovered

if (morePages)
{
relPages->rel_data_pages = 0;
dataPages = DPM_data_pages(tdbb, relation);
}
}

// AB: If we have only 1 data-page then the cardinality calculation
// is too imprecise to be useful, therefore rely on the record count
// from the data-page.
// AB: If we have only 1 data-page then the cardinality calculation
// is too imprecise to be useful, therefore rely on the record count
// from the data-page.

if (dataPages == 1)
return (double) recordCount;
if (dataPages == 1)
return (double) firstPagePrimaryCount;

effectivePages = (double) dataPages;
}

// Estimate total number of records for this relation

if (!format)
format = relation->currentFormat(tdbb);

static const double DEFAULT_COMPRESSION_RATIO = 0.5;
static constexpr double DEFAULT_COMPRESSION_RATIO = 0.5;

const ULONG compressedSize =
recordCount ? recordLength / recordCount :
Expand All @@ -403,7 +638,7 @@ double DPM_cardinality(thread_db* tdbb, jrd_rel* relation, const Format* format)
ROUNDUP(compressedSize + RHD_SIZE, ODS_ALIGNMENT) +
((dbb->dbb_flags & DBB_no_reserve) ? 0 : SPACE_FUDGE);

return (double) dataPages * (dbb->dbb_page_size - DPG_SIZE) / recordSize;
return effectivePages * (dbb->dbb_page_size - DPG_SIZE) / recordSize;
}


Expand Down