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
35 changes: 28 additions & 7 deletions src/backend/commands/analyzeutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,17 +759,38 @@ getNdvBySegHeapTuple(AttStatsSlot * *ndvbsSlots, HeapTuple *heaptupleStats, floa
(void) get_attstatsslot(ndvbsSlots[i], heaptupleStats[i],
STATISTIC_KIND_NDV_BY_SEGMENTS, InvalidOid, ATTSTATSSLOT_VALUES);

if ((InvalidOid != ndvbsSlots[i]->valuetype && // result is not empty
// not empty partition with invalid ndvbs
(relTuples[i] > 0 && DatumGetFloat8(ndvbsSlots[i]->values[0]) == 0)) ||
// not empty partition without ndvbs
(InvalidOid == ndvbsSlots[i]->valuetype && relTuples[i] > 0)) {
if (ndvbsSlots[i]->valuetype != FLOAT8OID)
{
/*
* NDV_BY_SEGMENTS slot not found or has unexpected type.
* Non-empty partitions must have valid NDV_BY_SEGMENTS;
* empty partitions (relTuples == 0) can be skipped.
*/
if (relTuples[i] > 0)
{
valid = false;
break;
}
free_attstatsslot(ndvbsSlots[i]);
pfree(ndvbsSlots[i]);
ndvbsSlots[i] = NULL;
continue;
}

Assert(ndvbsSlots[i]->valuetype == FLOAT8OID);

if (ndvbsSlots[i]->nvalues != 1)
{
valid = false;
break;
}

Assert(ndvbsSlots[i]->valuetype == FLOAT8OID);
Assert(ndvbsSlots[i]->nvalues == 1);
/* Non-empty partition with zero NDV is suspicious */
if (relTuples[i] > 0 && DatumGetFloat8(ndvbsSlots[i]->values[0]) == 0)
{
valid = false;
break;
}
}
return valid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the ndvbsSlots could be release if the valid becomes false. On the other side, the ndvbsSlots and its pointer array could be also freed in aggregate_leaf_partition_ndvbs no matter it's valid or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, aggregate_leaf_partition_ndvbs will free all ndvbsSlots regardless of validity. The early cleanup here for empty partitions (setting slot to NULL after free) is just to keep the slot array consistent during iteration — it won't cause a double-free since the cleanup function checks for NULL.

}
Expand Down
Loading