Commit 9aab83fc authored by Tom Lane's avatar Tom Lane

Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.

The mess cleaned up in commit da075960 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway.  So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.

Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element.  That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger.  We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array.  Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free.  (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them.  At the other end of the size range, it doesn't expand
short-header datums either.  In either case, DatumGetArrayTypeP would have
to make a copy.  We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression.  But that
seems like a very acceptable price for winning in pass-by-ref cases.)

Hence, redesign to take these insights into account.  While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments.  That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.

It's certainly arguable that this is new development and not something to
push post-feature-freeze.  However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later.  Since
we aren't quite at beta phase yet, let's put it in.

Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
parent 1848b73d
...@@ -136,11 +136,7 @@ _int_matchsel(PG_FUNCTION_ARGS) ...@@ -136,11 +136,7 @@ _int_matchsel(PG_FUNCTION_ARGS)
int nmcelems = 0; int nmcelems = 0;
float4 minfreq = 0.0; float4 minfreq = 0.0;
float4 nullfrac = 0.0; float4 nullfrac = 0.0;
Form_pg_statistic stats; AttStatsSlot sslot;
Datum *values = NULL;
int nvalues = 0;
float4 *numbers = NULL;
int nnumbers = 0;
/* /*
* If expression is not "variable @@ something" or "something @@ variable" * If expression is not "variable @@ something" or "something @@ variable"
...@@ -193,6 +189,8 @@ _int_matchsel(PG_FUNCTION_ARGS) ...@@ -193,6 +189,8 @@ _int_matchsel(PG_FUNCTION_ARGS)
*/ */
if (HeapTupleIsValid(vardata.statsTuple)) if (HeapTupleIsValid(vardata.statsTuple))
{ {
Form_pg_statistic stats;
stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
nullfrac = stats->stanullfrac; nullfrac = stats->stanullfrac;
...@@ -200,29 +198,30 @@ _int_matchsel(PG_FUNCTION_ARGS) ...@@ -200,29 +198,30 @@ _int_matchsel(PG_FUNCTION_ARGS)
* For an int4 array, the default array type analyze function will * For an int4 array, the default array type analyze function will
* collect a Most Common Elements list, which is an array of int4s. * collect a Most Common Elements list, which is an array of int4s.
*/ */
if (get_attstatsslot(vardata.statsTuple, if (get_attstatsslot(&sslot, vardata.statsTuple,
INT4OID, -1,
STATISTIC_KIND_MCELEM, InvalidOid, STATISTIC_KIND_MCELEM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
Assert(sslot.valuetype == INT4OID);
/* /*
* There should be three more Numbers than Values, because the * There should be three more Numbers than Values, because the
* last three (for intarray) cells are taken for minimal, maximal * last three (for intarray) cells are taken for minimal, maximal
* and nulls frequency. Punt if not. * and nulls frequency. Punt if not.
*/ */
if (nnumbers == nvalues + 3) if (sslot.nnumbers == sslot.nvalues + 3)
{ {
/* Grab the lowest frequency. */ /* Grab the lowest frequency. */
minfreq = numbers[nnumbers - (nnumbers - nvalues)]; minfreq = sslot.numbers[sslot.nnumbers - (sslot.nnumbers - sslot.nvalues)];
mcelems = values; mcelems = sslot.values;
mcefreqs = numbers; mcefreqs = sslot.numbers;
nmcelems = nvalues; nmcelems = sslot.nvalues;
} }
} }
} }
else
memset(&sslot, 0, sizeof(sslot));
/* Process the logical expression in the query, using the stats */ /* Process the logical expression in the query, using the stats */
selec = int_query_opr_selec(GETQUERY(query) + query->size - 1, selec = int_query_opr_selec(GETQUERY(query) + query->size - 1,
...@@ -231,7 +230,7 @@ _int_matchsel(PG_FUNCTION_ARGS) ...@@ -231,7 +230,7 @@ _int_matchsel(PG_FUNCTION_ARGS)
/* MCE stats count only non-null rows, so adjust for null rows. */ /* MCE stats count only non-null rows, so adjust for null rows. */
selec *= (1.0 - nullfrac); selec *= (1.0 - nullfrac);
free_attstatsslot(INT4OID, values, nvalues, numbers, nnumbers); free_attstatsslot(&sslot);
ReleaseVariableStats(vardata); ReleaseVariableStats(vardata);
CLAMP_PROBABILITY(selec); CLAMP_PROBABILITY(selec);
......
...@@ -1283,10 +1283,7 @@ static void ...@@ -1283,10 +1283,7 @@ static void
ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse) ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
{ {
HeapTupleData *statsTuple; HeapTupleData *statsTuple;
Datum *values; AttStatsSlot sslot;
int nvalues;
float4 *numbers;
int nnumbers;
/* Do nothing if planner didn't identify the outer relation's join key */ /* Do nothing if planner didn't identify the outer relation's join key */
if (!OidIsValid(node->skewTable)) if (!OidIsValid(node->skewTable))
...@@ -1305,19 +1302,17 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse) ...@@ -1305,19 +1302,17 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
if (!HeapTupleIsValid(statsTuple)) if (!HeapTupleIsValid(statsTuple))
return; return;
if (get_attstatsslot(statsTuple, node->skewColType, node->skewColTypmod, if (get_attstatsslot(&sslot, statsTuple,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
double frac; double frac;
int nbuckets; int nbuckets;
FmgrInfo *hashfunctions; FmgrInfo *hashfunctions;
int i; int i;
if (mcvsToUse > nvalues) if (mcvsToUse > sslot.nvalues)
mcvsToUse = nvalues; mcvsToUse = sslot.nvalues;
/* /*
* Calculate the expected fraction of outer relation that will * Calculate the expected fraction of outer relation that will
...@@ -1326,11 +1321,10 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse) ...@@ -1326,11 +1321,10 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
*/ */
frac = 0; frac = 0;
for (i = 0; i < mcvsToUse; i++) for (i = 0; i < mcvsToUse; i++)
frac += numbers[i]; frac += sslot.numbers[i];
if (frac < SKEW_MIN_OUTER_FRACTION) if (frac < SKEW_MIN_OUTER_FRACTION)
{ {
free_attstatsslot(node->skewColType, free_attstatsslot(&sslot);
values, nvalues, numbers, nnumbers);
ReleaseSysCache(statsTuple); ReleaseSysCache(statsTuple);
return; return;
} }
...@@ -1392,7 +1386,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse) ...@@ -1392,7 +1386,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
int bucket; int bucket;
hashvalue = DatumGetUInt32(FunctionCall1(&hashfunctions[0], hashvalue = DatumGetUInt32(FunctionCall1(&hashfunctions[0],
values[i])); sslot.values[i]));
/* /*
* While we have not hit a hole in the hashtable and have not hit * While we have not hit a hole in the hashtable and have not hit
...@@ -1426,8 +1420,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse) ...@@ -1426,8 +1420,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
hashtable->spacePeak = hashtable->spaceUsed; hashtable->spacePeak = hashtable->spaceUsed;
} }
free_attstatsslot(node->skewColType, free_attstatsslot(&sslot);
values, nvalues, numbers, nnumbers);
} }
ReleaseSysCache(statsTuple); ReleaseSysCache(statsTuple);
......
...@@ -163,28 +163,22 @@ tsquerysel(VariableStatData *vardata, Datum constval) ...@@ -163,28 +163,22 @@ tsquerysel(VariableStatData *vardata, Datum constval)
if (HeapTupleIsValid(vardata->statsTuple)) if (HeapTupleIsValid(vardata->statsTuple))
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
Datum *values; AttStatsSlot sslot;
int nvalues;
float4 *numbers;
int nnumbers;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
/* MCELEM will be an array of TEXT elements for a tsvector column */ /* MCELEM will be an array of TEXT elements for a tsvector column */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
TEXTOID, -1,
STATISTIC_KIND_MCELEM, InvalidOid, STATISTIC_KIND_MCELEM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
/* /*
* There is a most-common-elements slot for the tsvector Var, so * There is a most-common-elements slot for the tsvector Var, so
* use that. * use that.
*/ */
selec = mcelem_tsquery_selec(query, values, nvalues, selec = mcelem_tsquery_selec(query, sslot.values, sslot.nvalues,
numbers, nnumbers); sslot.numbers, sslot.nnumbers);
free_attstatsslot(TEXTOID, values, nvalues, numbers, nnumbers); free_attstatsslot(&sslot);
} }
else else
{ {
......
...@@ -137,35 +137,22 @@ scalararraysel_containment(PlannerInfo *root, ...@@ -137,35 +137,22 @@ scalararraysel_containment(PlannerInfo *root,
statistic_proc_security_check(&vardata, cmpfunc->fn_oid)) statistic_proc_security_check(&vardata, cmpfunc->fn_oid))
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
Datum *values; AttStatsSlot sslot;
int nvalues; AttStatsSlot hslot;
float4 *numbers;
int nnumbers;
float4 *hist;
int nhist;
stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
/* MCELEM will be an array of same type as element */ /* MCELEM will be an array of same type as element */
if (get_attstatsslot(vardata.statsTuple, if (get_attstatsslot(&sslot, vardata.statsTuple,
elemtype, vardata.atttypmod,
STATISTIC_KIND_MCELEM, InvalidOid, STATISTIC_KIND_MCELEM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
/* For ALL case, also get histogram of distinct-element counts */ /* For ALL case, also get histogram of distinct-element counts */
if (useOr || if (useOr ||
!get_attstatsslot(vardata.statsTuple, !get_attstatsslot(&hslot, vardata.statsTuple,
elemtype, vardata.atttypmod,
STATISTIC_KIND_DECHIST, InvalidOid, STATISTIC_KIND_DECHIST, InvalidOid,
NULL, ATTSTATSSLOT_NUMBERS))
NULL, NULL, memset(&hslot, 0, sizeof(hslot));
&hist, &nhist))
{
hist = NULL;
nhist = 0;
}
/* /*
* For = ANY, estimate as var @> ARRAY[const]. * For = ANY, estimate as var @> ARRAY[const].
...@@ -173,22 +160,26 @@ scalararraysel_containment(PlannerInfo *root, ...@@ -173,22 +160,26 @@ scalararraysel_containment(PlannerInfo *root,
* For = ALL, estimate as var <@ ARRAY[const]. * For = ALL, estimate as var <@ ARRAY[const].
*/ */
if (useOr) if (useOr)
selec = mcelem_array_contain_overlap_selec(values, nvalues, selec = mcelem_array_contain_overlap_selec(sslot.values,
numbers, nnumbers, sslot.nvalues,
sslot.numbers,
sslot.nnumbers,
&constval, 1, &constval, 1,
OID_ARRAY_CONTAINS_OP, OID_ARRAY_CONTAINS_OP,
cmpfunc); cmpfunc);
else else
selec = mcelem_array_contained_selec(values, nvalues, selec = mcelem_array_contained_selec(sslot.values,
numbers, nnumbers, sslot.nvalues,
sslot.numbers,
sslot.nnumbers,
&constval, 1, &constval, 1,
hist, nhist, hslot.numbers,
hslot.nnumbers,
OID_ARRAY_CONTAINED_OP, OID_ARRAY_CONTAINED_OP,
cmpfunc); cmpfunc);
if (hist) free_attstatsslot(&hslot);
free_attstatsslot(elemtype, NULL, 0, hist, nhist); free_attstatsslot(&sslot);
free_attstatsslot(elemtype, values, nvalues, numbers, nnumbers);
} }
else else
{ {
...@@ -369,49 +360,35 @@ calc_arraycontsel(VariableStatData *vardata, Datum constval, ...@@ -369,49 +360,35 @@ calc_arraycontsel(VariableStatData *vardata, Datum constval,
statistic_proc_security_check(vardata, cmpfunc->fn_oid)) statistic_proc_security_check(vardata, cmpfunc->fn_oid))
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
Datum *values; AttStatsSlot sslot;
int nvalues; AttStatsSlot hslot;
float4 *numbers;
int nnumbers;
float4 *hist;
int nhist;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
/* MCELEM will be an array of same type as column */ /* MCELEM will be an array of same type as column */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
elemtype, vardata->atttypmod,
STATISTIC_KIND_MCELEM, InvalidOid, STATISTIC_KIND_MCELEM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
/* /*
* For "array <@ const" case we also need histogram of distinct * For "array <@ const" case we also need histogram of distinct
* element counts. * element counts.
*/ */
if (operator != OID_ARRAY_CONTAINED_OP || if (operator != OID_ARRAY_CONTAINED_OP ||
!get_attstatsslot(vardata->statsTuple, !get_attstatsslot(&hslot, vardata->statsTuple,
elemtype, vardata->atttypmod,
STATISTIC_KIND_DECHIST, InvalidOid, STATISTIC_KIND_DECHIST, InvalidOid,
NULL, ATTSTATSSLOT_NUMBERS))
NULL, NULL, memset(&hslot, 0, sizeof(hslot));
&hist, &nhist))
{
hist = NULL;
nhist = 0;
}
/* Use the most-common-elements slot for the array Var. */ /* Use the most-common-elements slot for the array Var. */
selec = mcelem_array_selec(array, typentry, selec = mcelem_array_selec(array, typentry,
values, nvalues, sslot.values, sslot.nvalues,
numbers, nnumbers, sslot.numbers, sslot.nnumbers,
hist, nhist, hslot.numbers, hslot.nnumbers,
operator, cmpfunc); operator, cmpfunc);
if (hist) free_attstatsslot(&hslot);
free_attstatsslot(elemtype, NULL, 0, hist, nhist); free_attstatsslot(&sslot);
free_attstatsslot(elemtype, values, nvalues, numbers, nnumbers);
} }
else else
{ {
......
...@@ -88,10 +88,9 @@ networksel(PG_FUNCTION_ARGS) ...@@ -88,10 +88,9 @@ networksel(PG_FUNCTION_ARGS)
Selectivity selec, Selectivity selec,
mcv_selec, mcv_selec,
non_mcv_selec; non_mcv_selec;
Datum constvalue, Datum constvalue;
*hist_values;
int hist_nvalues;
Form_pg_statistic stats; Form_pg_statistic stats;
AttStatsSlot hslot;
double sumcommon, double sumcommon,
nullfrac; nullfrac;
FmgrInfo proc; FmgrInfo proc;
...@@ -146,22 +145,19 @@ networksel(PG_FUNCTION_ARGS) ...@@ -146,22 +145,19 @@ networksel(PG_FUNCTION_ARGS)
* non-MCV population that satisfies the clause. If we don't, apply the * non-MCV population that satisfies the clause. If we don't, apply the
* default selectivity to that population. * default selectivity to that population.
*/ */
if (get_attstatsslot(vardata.statsTuple, if (get_attstatsslot(&hslot, vardata.statsTuple,
vardata.atttype, vardata.atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES))
&hist_values, &hist_nvalues,
NULL, NULL))
{ {
int opr_codenum = inet_opr_codenum(operator); int opr_codenum = inet_opr_codenum(operator);
/* Commute if needed, so we can consider histogram to be on the left */ /* Commute if needed, so we can consider histogram to be on the left */
if (!varonleft) if (!varonleft)
opr_codenum = -opr_codenum; opr_codenum = -opr_codenum;
non_mcv_selec = inet_hist_value_sel(hist_values, hist_nvalues, non_mcv_selec = inet_hist_value_sel(hslot.values, hslot.nvalues,
constvalue, opr_codenum); constvalue, opr_codenum);
free_attstatsslot(vardata.atttype, hist_values, hist_nvalues, NULL, 0); free_attstatsslot(&hslot);
} }
else else
non_mcv_selec = DEFAULT_SEL(operator); non_mcv_selec = DEFAULT_SEL(operator);
...@@ -277,42 +273,33 @@ networkjoinsel_inner(Oid operator, ...@@ -277,42 +273,33 @@ networkjoinsel_inner(Oid operator,
hist1_exists = false, hist1_exists = false,
hist2_exists = false; hist2_exists = false;
int opr_codenum; int opr_codenum;
int mcv1_nvalues, int mcv1_length = 0,
mcv2_nvalues,
mcv1_nnumbers,
mcv2_nnumbers,
hist1_nvalues,
hist2_nvalues,
mcv1_length = 0,
mcv2_length = 0; mcv2_length = 0;
Datum *mcv1_values, AttStatsSlot mcv1_slot;
*mcv2_values, AttStatsSlot mcv2_slot;
*hist1_values, AttStatsSlot hist1_slot;
*hist2_values; AttStatsSlot hist2_slot;
float4 *mcv1_numbers,
*mcv2_numbers;
if (HeapTupleIsValid(vardata1->statsTuple)) if (HeapTupleIsValid(vardata1->statsTuple))
{ {
stats = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
nullfrac1 = stats->stanullfrac; nullfrac1 = stats->stanullfrac;
mcv1_exists = get_attstatsslot(vardata1->statsTuple, mcv1_exists = get_attstatsslot(&mcv1_slot, vardata1->statsTuple,
vardata1->atttype, vardata1->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
&mcv1_values, &mcv1_nvalues, hist1_exists = get_attstatsslot(&hist1_slot, vardata1->statsTuple,
&mcv1_numbers, &mcv1_nnumbers);
hist1_exists = get_attstatsslot(vardata1->statsTuple,
vardata1->atttype, vardata1->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES);
&hist1_values, &hist1_nvalues,
NULL, NULL);
/* Arbitrarily limit number of MCVs considered */ /* Arbitrarily limit number of MCVs considered */
mcv1_length = Min(mcv1_nvalues, MAX_CONSIDERED_ELEMS); mcv1_length = Min(mcv1_slot.nvalues, MAX_CONSIDERED_ELEMS);
if (mcv1_exists) if (mcv1_exists)
sumcommon1 = mcv_population(mcv1_numbers, mcv1_length); sumcommon1 = mcv_population(mcv1_slot.numbers, mcv1_length);
}
else
{
memset(&mcv1_slot, 0, sizeof(mcv1_slot));
memset(&hist1_slot, 0, sizeof(hist1_slot));
} }
if (HeapTupleIsValid(vardata2->statsTuple)) if (HeapTupleIsValid(vardata2->statsTuple))
...@@ -320,22 +307,21 @@ networkjoinsel_inner(Oid operator, ...@@ -320,22 +307,21 @@ networkjoinsel_inner(Oid operator,
stats = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
nullfrac2 = stats->stanullfrac; nullfrac2 = stats->stanullfrac;
mcv2_exists = get_attstatsslot(vardata2->statsTuple, mcv2_exists = get_attstatsslot(&mcv2_slot, vardata2->statsTuple,
vardata2->atttype, vardata2->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
&mcv2_values, &mcv2_nvalues, hist2_exists = get_attstatsslot(&hist2_slot, vardata2->statsTuple,
&mcv2_numbers, &mcv2_nnumbers);
hist2_exists = get_attstatsslot(vardata2->statsTuple,
vardata2->atttype, vardata2->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES);
&hist2_values, &hist2_nvalues,
NULL, NULL);
/* Arbitrarily limit number of MCVs considered */ /* Arbitrarily limit number of MCVs considered */
mcv2_length = Min(mcv2_nvalues, MAX_CONSIDERED_ELEMS); mcv2_length = Min(mcv2_slot.nvalues, MAX_CONSIDERED_ELEMS);
if (mcv2_exists) if (mcv2_exists)
sumcommon2 = mcv_population(mcv2_numbers, mcv2_length); sumcommon2 = mcv_population(mcv2_slot.numbers, mcv2_length);
}
else
{
memset(&mcv2_slot, 0, sizeof(mcv2_slot));
memset(&hist2_slot, 0, sizeof(hist2_slot));
} }
opr_codenum = inet_opr_codenum(operator); opr_codenum = inet_opr_codenum(operator);
...@@ -344,8 +330,10 @@ networkjoinsel_inner(Oid operator, ...@@ -344,8 +330,10 @@ networkjoinsel_inner(Oid operator,
* Calculate selectivity for MCV vs MCV matches. * Calculate selectivity for MCV vs MCV matches.
*/ */
if (mcv1_exists && mcv2_exists) if (mcv1_exists && mcv2_exists)
selec += inet_mcv_join_sel(mcv1_values, mcv1_numbers, mcv1_length, selec += inet_mcv_join_sel(mcv1_slot.values, mcv1_slot.numbers,
mcv2_values, mcv2_numbers, mcv2_length, mcv1_length,
mcv2_slot.values, mcv2_slot.numbers,
mcv2_length,
operator); operator);
/* /*
...@@ -355,13 +343,13 @@ networkjoinsel_inner(Oid operator, ...@@ -355,13 +343,13 @@ networkjoinsel_inner(Oid operator,
*/ */
if (mcv1_exists && hist2_exists) if (mcv1_exists && hist2_exists)
selec += (1.0 - nullfrac2 - sumcommon2) * selec += (1.0 - nullfrac2 - sumcommon2) *
inet_mcv_hist_sel(mcv1_values, mcv1_numbers, mcv1_length, inet_mcv_hist_sel(mcv1_slot.values, mcv1_slot.numbers, mcv1_length,
hist2_values, hist2_nvalues, hist2_slot.values, hist2_slot.nvalues,
opr_codenum); opr_codenum);
if (mcv2_exists && hist1_exists) if (mcv2_exists && hist1_exists)
selec += (1.0 - nullfrac1 - sumcommon1) * selec += (1.0 - nullfrac1 - sumcommon1) *
inet_mcv_hist_sel(mcv2_values, mcv2_numbers, mcv2_length, inet_mcv_hist_sel(mcv2_slot.values, mcv2_slot.numbers, mcv2_length,
hist1_values, hist1_nvalues, hist1_slot.values, hist1_slot.nvalues,
-opr_codenum); -opr_codenum);
/* /*
...@@ -371,8 +359,8 @@ networkjoinsel_inner(Oid operator, ...@@ -371,8 +359,8 @@ networkjoinsel_inner(Oid operator,
if (hist1_exists && hist2_exists) if (hist1_exists && hist2_exists)
selec += (1.0 - nullfrac1 - sumcommon1) * selec += (1.0 - nullfrac1 - sumcommon1) *
(1.0 - nullfrac2 - sumcommon2) * (1.0 - nullfrac2 - sumcommon2) *
inet_hist_inclusion_join_sel(hist1_values, hist1_nvalues, inet_hist_inclusion_join_sel(hist1_slot.values, hist1_slot.nvalues,
hist2_values, hist2_nvalues, hist2_slot.values, hist2_slot.nvalues,
opr_codenum); opr_codenum);
/* /*
...@@ -383,18 +371,10 @@ networkjoinsel_inner(Oid operator, ...@@ -383,18 +371,10 @@ networkjoinsel_inner(Oid operator,
selec = (1.0 - nullfrac1) * (1.0 - nullfrac2) * DEFAULT_SEL(operator); selec = (1.0 - nullfrac1) * (1.0 - nullfrac2) * DEFAULT_SEL(operator);
/* Release stats. */ /* Release stats. */
if (mcv1_exists) free_attstatsslot(&mcv1_slot);
free_attstatsslot(vardata1->atttype, mcv1_values, mcv1_nvalues, free_attstatsslot(&mcv2_slot);
mcv1_numbers, mcv1_nnumbers); free_attstatsslot(&hist1_slot);
if (mcv2_exists) free_attstatsslot(&hist2_slot);
free_attstatsslot(vardata2->atttype, mcv2_values, mcv2_nvalues,
mcv2_numbers, mcv2_nnumbers);
if (hist1_exists)
free_attstatsslot(vardata1->atttype, hist1_values, hist1_nvalues,
NULL, 0);
if (hist2_exists)
free_attstatsslot(vardata2->atttype, hist2_values, hist2_nvalues,
NULL, 0);
return selec; return selec;
} }
...@@ -423,42 +403,33 @@ networkjoinsel_semi(Oid operator, ...@@ -423,42 +403,33 @@ networkjoinsel_semi(Oid operator,
int opr_codenum; int opr_codenum;
FmgrInfo proc; FmgrInfo proc;
int i, int i,
mcv1_nvalues,
mcv2_nvalues,
mcv1_nnumbers,
mcv2_nnumbers,
hist1_nvalues,
hist2_nvalues,
mcv1_length = 0, mcv1_length = 0,
mcv2_length = 0; mcv2_length = 0;
Datum *mcv1_values, AttStatsSlot mcv1_slot;
*mcv2_values, AttStatsSlot mcv2_slot;
*hist1_values, AttStatsSlot hist1_slot;
*hist2_values; AttStatsSlot hist2_slot;
float4 *mcv1_numbers,
*mcv2_numbers;
if (HeapTupleIsValid(vardata1->statsTuple)) if (HeapTupleIsValid(vardata1->statsTuple))
{ {
stats = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
nullfrac1 = stats->stanullfrac; nullfrac1 = stats->stanullfrac;
mcv1_exists = get_attstatsslot(vardata1->statsTuple, mcv1_exists = get_attstatsslot(&mcv1_slot, vardata1->statsTuple,
vardata1->atttype, vardata1->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
&mcv1_values, &mcv1_nvalues, hist1_exists = get_attstatsslot(&hist1_slot, vardata1->statsTuple,
&mcv1_numbers, &mcv1_nnumbers);
hist1_exists = get_attstatsslot(vardata1->statsTuple,
vardata1->atttype, vardata1->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES);
&hist1_values, &hist1_nvalues,
NULL, NULL);
/* Arbitrarily limit number of MCVs considered */ /* Arbitrarily limit number of MCVs considered */
mcv1_length = Min(mcv1_nvalues, MAX_CONSIDERED_ELEMS); mcv1_length = Min(mcv1_slot.nvalues, MAX_CONSIDERED_ELEMS);
if (mcv1_exists) if (mcv1_exists)
sumcommon1 = mcv_population(mcv1_numbers, mcv1_length); sumcommon1 = mcv_population(mcv1_slot.numbers, mcv1_length);
}
else
{
memset(&mcv1_slot, 0, sizeof(mcv1_slot));
memset(&hist1_slot, 0, sizeof(hist1_slot));
} }
if (HeapTupleIsValid(vardata2->statsTuple)) if (HeapTupleIsValid(vardata2->statsTuple))
...@@ -466,22 +437,21 @@ networkjoinsel_semi(Oid operator, ...@@ -466,22 +437,21 @@ networkjoinsel_semi(Oid operator,
stats = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
nullfrac2 = stats->stanullfrac; nullfrac2 = stats->stanullfrac;
mcv2_exists = get_attstatsslot(vardata2->statsTuple, mcv2_exists = get_attstatsslot(&mcv2_slot, vardata2->statsTuple,
vardata2->atttype, vardata2->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
&mcv2_values, &mcv2_nvalues, hist2_exists = get_attstatsslot(&hist2_slot, vardata2->statsTuple,
&mcv2_numbers, &mcv2_nnumbers);
hist2_exists = get_attstatsslot(vardata2->statsTuple,
vardata2->atttype, vardata2->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES);
&hist2_values, &hist2_nvalues,
NULL, NULL);
/* Arbitrarily limit number of MCVs considered */ /* Arbitrarily limit number of MCVs considered */
mcv2_length = Min(mcv2_nvalues, MAX_CONSIDERED_ELEMS); mcv2_length = Min(mcv2_slot.nvalues, MAX_CONSIDERED_ELEMS);
if (mcv2_exists) if (mcv2_exists)
sumcommon2 = mcv_population(mcv2_numbers, mcv2_length); sumcommon2 = mcv_population(mcv2_slot.numbers, mcv2_length);
}
else
{
memset(&mcv2_slot, 0, sizeof(mcv2_slot));
memset(&hist2_slot, 0, sizeof(hist2_slot));
} }
opr_codenum = inet_opr_codenum(operator); opr_codenum = inet_opr_codenum(operator);
...@@ -499,10 +469,11 @@ networkjoinsel_semi(Oid operator, ...@@ -499,10 +469,11 @@ networkjoinsel_semi(Oid operator,
{ {
for (i = 0; i < mcv1_length; i++) for (i = 0; i < mcv1_length; i++)
{ {
selec += mcv1_numbers[i] * selec += mcv1_slot.numbers[i] *
inet_semi_join_sel(mcv1_values[i], inet_semi_join_sel(mcv1_slot.values[i],
mcv2_exists, mcv2_values, mcv2_length, mcv2_exists, mcv2_slot.values, mcv2_length,
hist2_exists, hist2_values, hist2_nvalues, hist2_exists,
hist2_slot.values, hist2_slot.nvalues,
hist2_weight, hist2_weight,
&proc, opr_codenum); &proc, opr_codenum);
} }
...@@ -519,21 +490,22 @@ networkjoinsel_semi(Oid operator, ...@@ -519,21 +490,22 @@ networkjoinsel_semi(Oid operator,
* *
* If there are too many histogram elements, decimate to limit runtime. * If there are too many histogram elements, decimate to limit runtime.
*/ */
if (hist1_exists && hist1_nvalues > 2 && (mcv2_exists || hist2_exists)) if (hist1_exists && hist1_slot.nvalues > 2 && (mcv2_exists || hist2_exists))
{ {
double hist_selec_sum = 0.0; double hist_selec_sum = 0.0;
int k, int k,
n; n;
k = (hist1_nvalues - 3) / MAX_CONSIDERED_ELEMS + 1; k = (hist1_slot.nvalues - 3) / MAX_CONSIDERED_ELEMS + 1;
n = 0; n = 0;
for (i = 1; i < hist1_nvalues - 1; i += k) for (i = 1; i < hist1_slot.nvalues - 1; i += k)
{ {
hist_selec_sum += hist_selec_sum +=
inet_semi_join_sel(hist1_values[i], inet_semi_join_sel(hist1_slot.values[i],
mcv2_exists, mcv2_values, mcv2_length, mcv2_exists, mcv2_slot.values, mcv2_length,
hist2_exists, hist2_values, hist2_nvalues, hist2_exists,
hist2_slot.values, hist2_slot.nvalues,
hist2_weight, hist2_weight,
&proc, opr_codenum); &proc, opr_codenum);
n++; n++;
...@@ -550,18 +522,10 @@ networkjoinsel_semi(Oid operator, ...@@ -550,18 +522,10 @@ networkjoinsel_semi(Oid operator,
selec = (1.0 - nullfrac1) * (1.0 - nullfrac2) * DEFAULT_SEL(operator); selec = (1.0 - nullfrac1) * (1.0 - nullfrac2) * DEFAULT_SEL(operator);
/* Release stats. */ /* Release stats. */
if (mcv1_exists) free_attstatsslot(&mcv1_slot);
free_attstatsslot(vardata1->atttype, mcv1_values, mcv1_nvalues, free_attstatsslot(&mcv2_slot);
mcv1_numbers, mcv1_nnumbers); free_attstatsslot(&hist1_slot);
if (mcv2_exists) free_attstatsslot(&hist2_slot);
free_attstatsslot(vardata2->atttype, mcv2_values, mcv2_nvalues,
mcv2_numbers, mcv2_nnumbers);
if (hist1_exists)
free_attstatsslot(vardata1->atttype, hist1_values, hist1_nvalues,
NULL, 0);
if (hist2_exists)
free_attstatsslot(vardata2->atttype, hist2_values, hist2_nvalues,
NULL, 0);
return selec; return selec;
} }
......
...@@ -239,25 +239,21 @@ calc_rangesel(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -239,25 +239,21 @@ calc_rangesel(TypeCacheEntry *typcache, VariableStatData *vardata,
if (HeapTupleIsValid(vardata->statsTuple)) if (HeapTupleIsValid(vardata->statsTuple))
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
float4 *numbers; AttStatsSlot sslot;
int nnumbers;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
null_frac = stats->stanullfrac; null_frac = stats->stanullfrac;
/* Try to get fraction of empty ranges */ /* Try to get fraction of empty ranges */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
FLOAT8OID, -1,
STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM, STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
InvalidOid, InvalidOid,
NULL, ATTSTATSSLOT_NUMBERS))
NULL, NULL,
&numbers, &nnumbers))
{ {
if (nnumbers != 1) if (sslot.nnumbers != 1)
elog(ERROR, "invalid empty fraction statistic"); /* shouldn't happen */ elog(ERROR, "invalid empty fraction statistic"); /* shouldn't happen */
empty_frac = numbers[0]; empty_frac = sslot.numbers[0];
free_attstatsslot(FLOAT8OID, NULL, 0, numbers, nnumbers); free_attstatsslot(&sslot);
} }
else else
{ {
...@@ -374,10 +370,9 @@ static double ...@@ -374,10 +370,9 @@ static double
calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
RangeType *constval, Oid operator) RangeType *constval, Oid operator)
{ {
Datum *hist_values; AttStatsSlot hslot;
AttStatsSlot lslot;
int nhist; int nhist;
Datum *length_hist_values = NULL;
int length_nhist = 0;
RangeBound *hist_lower; RangeBound *hist_lower;
RangeBound *hist_upper; RangeBound *hist_upper;
int i; int i;
...@@ -397,23 +392,21 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -397,23 +392,21 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
/* Try to get histogram of ranges */ /* Try to get histogram of ranges */
if (!(HeapTupleIsValid(vardata->statsTuple) && if (!(HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(&hslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_BOUNDS_HISTOGRAM, InvalidOid, STATISTIC_KIND_BOUNDS_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES)))
&hist_values, &nhist,
NULL, NULL)))
return -1.0; return -1.0;
/* /*
* Convert histogram of ranges into histograms of its lower and upper * Convert histogram of ranges into histograms of its lower and upper
* bounds. * bounds.
*/ */
nhist = hslot.nvalues;
hist_lower = (RangeBound *) palloc(sizeof(RangeBound) * nhist); hist_lower = (RangeBound *) palloc(sizeof(RangeBound) * nhist);
hist_upper = (RangeBound *) palloc(sizeof(RangeBound) * nhist); hist_upper = (RangeBound *) palloc(sizeof(RangeBound) * nhist);
for (i = 0; i < nhist; i++) for (i = 0; i < nhist; i++)
{ {
range_deserialize(typcache, DatumGetRangeType(hist_values[i]), range_deserialize(typcache, DatumGetRangeType(hslot.values[i]),
&hist_lower[i], &hist_upper[i], &empty); &hist_lower[i], &hist_upper[i], &empty);
/* The histogram should not contain any empty ranges */ /* The histogram should not contain any empty ranges */
if (empty) if (empty)
...@@ -425,27 +418,25 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -425,27 +418,25 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
operator == OID_RANGE_CONTAINED_OP) operator == OID_RANGE_CONTAINED_OP)
{ {
if (!(HeapTupleIsValid(vardata->statsTuple) && if (!(HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(&lslot, vardata->statsTuple,
FLOAT8OID, -1,
STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM, STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
InvalidOid, InvalidOid,
NULL, ATTSTATSSLOT_VALUES)))
&length_hist_values, &length_nhist,
NULL, NULL)))
{ {
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0); free_attstatsslot(&hslot);
return -1.0; return -1.0;
} }
/* check that it's a histogram, not just a dummy entry */ /* check that it's a histogram, not just a dummy entry */
if (length_nhist < 2) if (lslot.nvalues < 2)
{ {
free_attstatsslot(FLOAT8OID, free_attstatsslot(&lslot);
length_hist_values, length_nhist, NULL, 0); free_attstatsslot(&hslot);
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0);
return -1.0; return -1.0;
} }
} }
else
memset(&lslot, 0, sizeof(lslot));
/* Extract the bounds of the constant value. */ /* Extract the bounds of the constant value. */
range_deserialize(typcache, constval, &const_lower, &const_upper, &empty); range_deserialize(typcache, constval, &const_lower, &const_upper, &empty);
...@@ -545,7 +536,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -545,7 +536,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
hist_selec = hist_selec =
calc_hist_selectivity_contains(typcache, &const_lower, calc_hist_selectivity_contains(typcache, &const_lower,
&const_upper, hist_lower, nhist, &const_upper, hist_lower, nhist,
length_hist_values, length_nhist); lslot.values, lslot.nvalues);
break; break;
case OID_RANGE_CONTAINED_OP: case OID_RANGE_CONTAINED_OP:
...@@ -570,7 +561,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -570,7 +561,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
hist_selec = hist_selec =
calc_hist_selectivity_contained(typcache, &const_lower, calc_hist_selectivity_contained(typcache, &const_lower,
&const_upper, hist_lower, nhist, &const_upper, hist_lower, nhist,
length_hist_values, length_nhist); lslot.values, lslot.nvalues);
} }
break; break;
...@@ -580,9 +571,8 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, ...@@ -580,9 +571,8 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
break; break;
} }
free_attstatsslot(FLOAT8OID, free_attstatsslot(&lslot);
length_hist_values, length_nhist, NULL, 0); free_attstatsslot(&hslot);
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0);
return hist_selec; return hist_selec;
} }
......
...@@ -299,10 +299,7 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -299,10 +299,7 @@ var_eq_const(VariableStatData *vardata, Oid operator,
(opfuncoid = get_opcode(operator)))) (opfuncoid = get_opcode(operator))))
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
Datum *values; AttStatsSlot sslot;
int nvalues;
float4 *numbers;
int nnumbers;
bool match = false; bool match = false;
int i; int i;
...@@ -315,30 +312,27 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -315,30 +312,27 @@ var_eq_const(VariableStatData *vardata, Oid operator,
* don't like this, maybe you shouldn't be using eqsel for your * don't like this, maybe you shouldn't be using eqsel for your
* operator...) * operator...)
*/ */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
FmgrInfo eqproc; FmgrInfo eqproc;
fmgr_info(opfuncoid, &eqproc); fmgr_info(opfuncoid, &eqproc);
for (i = 0; i < nvalues; i++) for (i = 0; i < sslot.nvalues; i++)
{ {
/* be careful to apply operator right way 'round */ /* be careful to apply operator right way 'round */
if (varonleft) if (varonleft)
match = DatumGetBool(FunctionCall2Coll(&eqproc, match = DatumGetBool(FunctionCall2Coll(&eqproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values[i], sslot.values[i],
constval)); constval));
else else
match = DatumGetBool(FunctionCall2Coll(&eqproc, match = DatumGetBool(FunctionCall2Coll(&eqproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
constval, constval,
values[i])); sslot.values[i]));
if (match) if (match)
break; break;
} }
...@@ -346,9 +340,7 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -346,9 +340,7 @@ var_eq_const(VariableStatData *vardata, Oid operator,
else else
{ {
/* no most-common-value info available */ /* no most-common-value info available */
values = NULL; i = 0; /* keep compiler quiet */
numbers = NULL;
i = nvalues = nnumbers = 0;
} }
if (match) if (match)
...@@ -357,7 +349,7 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -357,7 +349,7 @@ var_eq_const(VariableStatData *vardata, Oid operator,
* Constant is "=" to this common value. We know selectivity * Constant is "=" to this common value. We know selectivity
* exactly (or as exactly as ANALYZE could calculate it, anyway). * exactly (or as exactly as ANALYZE could calculate it, anyway).
*/ */
selec = numbers[i]; selec = sslot.numbers[i];
} }
else else
{ {
...@@ -369,8 +361,8 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -369,8 +361,8 @@ var_eq_const(VariableStatData *vardata, Oid operator,
double sumcommon = 0.0; double sumcommon = 0.0;
double otherdistinct; double otherdistinct;
for (i = 0; i < nnumbers; i++) for (i = 0; i < sslot.nnumbers; i++)
sumcommon += numbers[i]; sumcommon += sslot.numbers[i];
selec = 1.0 - sumcommon - stats->stanullfrac; selec = 1.0 - sumcommon - stats->stanullfrac;
CLAMP_PROBABILITY(selec); CLAMP_PROBABILITY(selec);
...@@ -379,7 +371,8 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -379,7 +371,8 @@ var_eq_const(VariableStatData *vardata, Oid operator,
* all the not-common values share this remaining fraction * all the not-common values share this remaining fraction
* equally, so we divide by the number of other distinct values. * equally, so we divide by the number of other distinct values.
*/ */
otherdistinct = get_variable_numdistinct(vardata, &isdefault) - nnumbers; otherdistinct = get_variable_numdistinct(vardata, &isdefault) -
sslot.nnumbers;
if (otherdistinct > 1) if (otherdistinct > 1)
selec /= otherdistinct; selec /= otherdistinct;
...@@ -387,12 +380,11 @@ var_eq_const(VariableStatData *vardata, Oid operator, ...@@ -387,12 +380,11 @@ var_eq_const(VariableStatData *vardata, Oid operator,
* Another cross-check: selectivity shouldn't be estimated as more * Another cross-check: selectivity shouldn't be estimated as more
* than the least common "most common value". * than the least common "most common value".
*/ */
if (nnumbers > 0 && selec > numbers[nnumbers - 1]) if (sslot.nnumbers > 0 && selec > sslot.numbers[sslot.nnumbers - 1])
selec = numbers[nnumbers - 1]; selec = sslot.numbers[sslot.nnumbers - 1];
} }
free_attstatsslot(vardata->atttype, values, nvalues, free_attstatsslot(&sslot);
numbers, nnumbers);
} }
else else
{ {
...@@ -435,8 +427,7 @@ var_eq_non_const(VariableStatData *vardata, Oid operator, ...@@ -435,8 +427,7 @@ var_eq_non_const(VariableStatData *vardata, Oid operator,
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
double ndistinct; double ndistinct;
float4 *numbers; AttStatsSlot sslot;
int nnumbers;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
...@@ -459,16 +450,13 @@ var_eq_non_const(VariableStatData *vardata, Oid operator, ...@@ -459,16 +450,13 @@ var_eq_non_const(VariableStatData *vardata, Oid operator,
* Cross-check: selectivity should never be estimated as more than the * Cross-check: selectivity should never be estimated as more than the
* most common value's. * most common value's.
*/ */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_NUMBERS))
NULL, NULL,
&numbers, &nnumbers))
{ {
if (nnumbers > 0 && selec > numbers[0]) if (sslot.nnumbers > 0 && selec > sslot.numbers[0])
selec = numbers[0]; selec = sslot.numbers[0];
free_attstatsslot(vardata->atttype, NULL, 0, numbers, nnumbers); free_attstatsslot(&sslot);
} }
} }
else else
...@@ -620,10 +608,7 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, ...@@ -620,10 +608,7 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
{ {
double mcv_selec, double mcv_selec,
sumcommon; sumcommon;
Datum *values; AttStatsSlot sslot;
int nvalues;
float4 *numbers;
int nnumbers;
int i; int i;
mcv_selec = 0.0; mcv_selec = 0.0;
...@@ -631,29 +616,25 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, ...@@ -631,29 +616,25 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
if (HeapTupleIsValid(vardata->statsTuple) && if (HeapTupleIsValid(vardata->statsTuple) &&
statistic_proc_security_check(vardata, opproc->fn_oid) && statistic_proc_security_check(vardata, opproc->fn_oid) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
&values, &nvalues,
&numbers, &nnumbers))
{ {
for (i = 0; i < nvalues; i++) for (i = 0; i < sslot.nvalues; i++)
{ {
if (varonleft ? if (varonleft ?
DatumGetBool(FunctionCall2Coll(opproc, DatumGetBool(FunctionCall2Coll(opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values[i], sslot.values[i],
constval)) : constval)) :
DatumGetBool(FunctionCall2Coll(opproc, DatumGetBool(FunctionCall2Coll(opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
constval, constval,
values[i]))) sslot.values[i])))
mcv_selec += numbers[i]; mcv_selec += sslot.numbers[i];
sumcommon += numbers[i]; sumcommon += sslot.numbers[i];
} }
free_attstatsslot(vardata->atttype, values, nvalues, free_attstatsslot(&sslot);
numbers, nnumbers);
} }
*sumcommonp = sumcommon; *sumcommonp = sumcommon;
...@@ -699,8 +680,7 @@ histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, ...@@ -699,8 +680,7 @@ histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
int *hist_size) int *hist_size)
{ {
double result; double result;
Datum *values; AttStatsSlot sslot;
int nvalues;
/* check sanity of parameters */ /* check sanity of parameters */
Assert(n_skip >= 0); Assert(n_skip >= 0);
...@@ -708,37 +688,34 @@ histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, ...@@ -708,37 +688,34 @@ histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
if (HeapTupleIsValid(vardata->statsTuple) && if (HeapTupleIsValid(vardata->statsTuple) &&
statistic_proc_security_check(vardata, opproc->fn_oid) && statistic_proc_security_check(vardata, opproc->fn_oid) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, ATTSTATSSLOT_VALUES))
&values, &nvalues,
NULL, NULL))
{ {
*hist_size = nvalues; *hist_size = sslot.nvalues;
if (nvalues >= min_hist_size) if (sslot.nvalues >= min_hist_size)
{ {
int nmatch = 0; int nmatch = 0;
int i; int i;
for (i = n_skip; i < nvalues - n_skip; i++) for (i = n_skip; i < sslot.nvalues - n_skip; i++)
{ {
if (varonleft ? if (varonleft ?
DatumGetBool(FunctionCall2Coll(opproc, DatumGetBool(FunctionCall2Coll(opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values[i], sslot.values[i],
constval)) : constval)) :
DatumGetBool(FunctionCall2Coll(opproc, DatumGetBool(FunctionCall2Coll(opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
constval, constval,
values[i]))) sslot.values[i])))
nmatch++; nmatch++;
} }
result = ((double) nmatch) / ((double) (nvalues - 2 * n_skip)); result = ((double) nmatch) / ((double) (sslot.nvalues - 2 * n_skip));
} }
else else
result = -1; result = -1;
free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0); free_attstatsslot(&sslot);
} }
else else
{ {
...@@ -768,9 +745,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -768,9 +745,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
Datum constval, Oid consttype) Datum constval, Oid consttype)
{ {
double hist_selec; double hist_selec;
Oid hist_op; AttStatsSlot sslot;
Datum *values;
int nvalues;
hist_selec = -1.0; hist_selec = -1.0;
...@@ -786,14 +761,11 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -786,14 +761,11 @@ ineq_histogram_selectivity(PlannerInfo *root,
*/ */
if (HeapTupleIsValid(vardata->statsTuple) && if (HeapTupleIsValid(vardata->statsTuple) &&
statistic_proc_security_check(vardata, opproc->fn_oid) && statistic_proc_security_check(vardata, opproc->fn_oid) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
&hist_op, ATTSTATSSLOT_VALUES))
&values, &nvalues,
NULL, NULL))
{ {
if (nvalues > 1) if (sslot.nvalues > 1)
{ {
/* /*
* Use binary search to find proper location, ie, the first slot * Use binary search to find proper location, ie, the first slot
...@@ -812,7 +784,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -812,7 +784,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
*/ */
double histfrac; double histfrac;
int lobound = 0; /* first possible slot to search */ int lobound = 0; /* first possible slot to search */
int hibound = nvalues; /* last+1 slot to search */ int hibound = sslot.nvalues; /* last+1 slot to search */
bool have_end = false; bool have_end = false;
/* /*
...@@ -821,12 +793,12 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -821,12 +793,12 @@ ineq_histogram_selectivity(PlannerInfo *root,
* one of them to be updated, so we deal with that within the * one of them to be updated, so we deal with that within the
* loop.) * loop.)
*/ */
if (nvalues == 2) if (sslot.nvalues == 2)
have_end = get_actual_variable_range(root, have_end = get_actual_variable_range(root,
vardata, vardata,
hist_op, sslot.staop,
&values[0], &sslot.values[0],
&values[1]); &sslot.values[1]);
while (lobound < hibound) while (lobound < hibound)
{ {
...@@ -838,22 +810,22 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -838,22 +810,22 @@ ineq_histogram_selectivity(PlannerInfo *root,
* histogram entry, first try to replace it with the actual * histogram entry, first try to replace it with the actual
* current min or max (unless we already did so above). * current min or max (unless we already did so above).
*/ */
if (probe == 0 && nvalues > 2) if (probe == 0 && sslot.nvalues > 2)
have_end = get_actual_variable_range(root, have_end = get_actual_variable_range(root,
vardata, vardata,
hist_op, sslot.staop,
&values[0], &sslot.values[0],
NULL); NULL);
else if (probe == nvalues - 1 && nvalues > 2) else if (probe == sslot.nvalues - 1 && sslot.nvalues > 2)
have_end = get_actual_variable_range(root, have_end = get_actual_variable_range(root,
vardata, vardata,
hist_op, sslot.staop,
NULL, NULL,
&values[probe]); &sslot.values[probe]);
ltcmp = DatumGetBool(FunctionCall2Coll(opproc, ltcmp = DatumGetBool(FunctionCall2Coll(opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values[probe], sslot.values[probe],
constval)); constval));
if (isgt) if (isgt)
ltcmp = !ltcmp; ltcmp = !ltcmp;
...@@ -868,7 +840,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -868,7 +840,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
/* Constant is below lower histogram boundary. */ /* Constant is below lower histogram boundary. */
histfrac = 0.0; histfrac = 0.0;
} }
else if (lobound >= nvalues) else if (lobound >= sslot.nvalues)
{ {
/* Constant is above upper histogram boundary. */ /* Constant is above upper histogram boundary. */
histfrac = 1.0; histfrac = 1.0;
...@@ -889,7 +861,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -889,7 +861,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
* interpolation within this bin. * interpolation within this bin.
*/ */
if (convert_to_scalar(constval, consttype, &val, if (convert_to_scalar(constval, consttype, &val,
values[i - 1], values[i], sslot.values[i - 1], sslot.values[i],
vardata->vartype, vardata->vartype,
&low, &high)) &low, &high))
{ {
...@@ -936,7 +908,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -936,7 +908,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
* binfrac partial bin below the constant. * binfrac partial bin below the constant.
*/ */
histfrac = (double) (i - 1) + binfrac; histfrac = (double) (i - 1) + binfrac;
histfrac /= (double) (nvalues - 1); histfrac /= (double) (sslot.nvalues - 1);
} }
/* /*
...@@ -964,7 +936,7 @@ ineq_histogram_selectivity(PlannerInfo *root, ...@@ -964,7 +936,7 @@ ineq_histogram_selectivity(PlannerInfo *root,
} }
} }
free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0); free_attstatsslot(&sslot);
} }
return hist_selec; return hist_selec;
...@@ -1517,21 +1489,15 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, ...@@ -1517,21 +1489,15 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
{ {
Form_pg_statistic stats; Form_pg_statistic stats;
double freq_null; double freq_null;
Datum *values; AttStatsSlot sslot;
int nvalues;
float4 *numbers;
int nnumbers;
stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple); stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
freq_null = stats->stanullfrac; freq_null = stats->stanullfrac;
if (get_attstatsslot(vardata.statsTuple, if (get_attstatsslot(&sslot, vardata.statsTuple,
vardata.atttype, vardata.atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS)
&values, &nvalues, && sslot.nnumbers > 0)
&numbers, &nnumbers)
&& nnumbers > 0)
{ {
double freq_true; double freq_true;
double freq_false; double freq_false;
...@@ -1539,10 +1505,10 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, ...@@ -1539,10 +1505,10 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
/* /*
* Get first MCV frequency and derive frequency for true. * Get first MCV frequency and derive frequency for true.
*/ */
if (DatumGetBool(values[0])) if (DatumGetBool(sslot.values[0]))
freq_true = numbers[0]; freq_true = sslot.numbers[0];
else else
freq_true = 1.0 - numbers[0] - freq_null; freq_true = 1.0 - sslot.numbers[0] - freq_null;
/* /*
* Next derive frequency for false. Then use these as appropriate * Next derive frequency for false. Then use these as appropriate
...@@ -1583,8 +1549,7 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, ...@@ -1583,8 +1549,7 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
break; break;
} }
free_attstatsslot(vardata.atttype, values, nvalues, free_attstatsslot(&sslot);
numbers, nnumbers);
} }
else else
{ {
...@@ -2274,34 +2239,26 @@ eqjoinsel_inner(Oid operator, ...@@ -2274,34 +2239,26 @@ eqjoinsel_inner(Oid operator,
Form_pg_statistic stats1 = NULL; Form_pg_statistic stats1 = NULL;
Form_pg_statistic stats2 = NULL; Form_pg_statistic stats2 = NULL;
bool have_mcvs1 = false; bool have_mcvs1 = false;
Datum *values1 = NULL;
int nvalues1 = 0;
float4 *numbers1 = NULL;
int nnumbers1 = 0;
bool have_mcvs2 = false; bool have_mcvs2 = false;
Datum *values2 = NULL; AttStatsSlot sslot1;
int nvalues2 = 0; AttStatsSlot sslot2;
float4 *numbers2 = NULL;
int nnumbers2 = 0;
nd1 = get_variable_numdistinct(vardata1, &isdefault1); nd1 = get_variable_numdistinct(vardata1, &isdefault1);
nd2 = get_variable_numdistinct(vardata2, &isdefault2); nd2 = get_variable_numdistinct(vardata2, &isdefault2);
opfuncoid = get_opcode(operator); opfuncoid = get_opcode(operator);
memset(&sslot1, 0, sizeof(sslot1));
memset(&sslot2, 0, sizeof(sslot2));
if (HeapTupleIsValid(vardata1->statsTuple)) if (HeapTupleIsValid(vardata1->statsTuple))
{ {
/* note we allow use of nullfrac regardless of security check */ /* note we allow use of nullfrac regardless of security check */
stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple); stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
if (statistic_proc_security_check(vardata1, opfuncoid)) if (statistic_proc_security_check(vardata1, opfuncoid))
have_mcvs1 = get_attstatsslot(vardata1->statsTuple, have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
vardata1->atttype, STATISTIC_KIND_MCV, InvalidOid,
vardata1->atttypmod, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
STATISTIC_KIND_MCV,
InvalidOid,
NULL,
&values1, &nvalues1,
&numbers1, &nnumbers1);
} }
if (HeapTupleIsValid(vardata2->statsTuple)) if (HeapTupleIsValid(vardata2->statsTuple))
...@@ -2309,14 +2266,9 @@ eqjoinsel_inner(Oid operator, ...@@ -2309,14 +2266,9 @@ eqjoinsel_inner(Oid operator,
/* note we allow use of nullfrac regardless of security check */ /* note we allow use of nullfrac regardless of security check */
stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple); stats2 = (Form_pg_statistic) GETSTRUCT(vardata2->statsTuple);
if (statistic_proc_security_check(vardata2, opfuncoid)) if (statistic_proc_security_check(vardata2, opfuncoid))
have_mcvs2 = get_attstatsslot(vardata2->statsTuple, have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
vardata2->atttype, STATISTIC_KIND_MCV, InvalidOid,
vardata2->atttypmod, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
STATISTIC_KIND_MCV,
InvalidOid,
NULL,
&values2, &nvalues2,
&numbers2, &nnumbers2);
} }
if (have_mcvs1 && have_mcvs2) if (have_mcvs1 && have_mcvs2)
...@@ -2351,8 +2303,8 @@ eqjoinsel_inner(Oid operator, ...@@ -2351,8 +2303,8 @@ eqjoinsel_inner(Oid operator,
nmatches; nmatches;
fmgr_info(opfuncoid, &eqproc); fmgr_info(opfuncoid, &eqproc);
hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool)); hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
hasmatch2 = (bool *) palloc0(nvalues2 * sizeof(bool)); hasmatch2 = (bool *) palloc0(sslot2.nvalues * sizeof(bool));
/* /*
* Note we assume that each MCV will match at most one member of the * Note we assume that each MCV will match at most one member of the
...@@ -2362,21 +2314,21 @@ eqjoinsel_inner(Oid operator, ...@@ -2362,21 +2314,21 @@ eqjoinsel_inner(Oid operator,
*/ */
matchprodfreq = 0.0; matchprodfreq = 0.0;
nmatches = 0; nmatches = 0;
for (i = 0; i < nvalues1; i++) for (i = 0; i < sslot1.nvalues; i++)
{ {
int j; int j;
for (j = 0; j < nvalues2; j++) for (j = 0; j < sslot2.nvalues; j++)
{ {
if (hasmatch2[j]) if (hasmatch2[j])
continue; continue;
if (DatumGetBool(FunctionCall2Coll(&eqproc, if (DatumGetBool(FunctionCall2Coll(&eqproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values1[i], sslot1.values[i],
values2[j]))) sslot2.values[j])))
{ {
hasmatch1[i] = hasmatch2[j] = true; hasmatch1[i] = hasmatch2[j] = true;
matchprodfreq += numbers1[i] * numbers2[j]; matchprodfreq += sslot1.numbers[i] * sslot2.numbers[j];
nmatches++; nmatches++;
break; break;
} }
...@@ -2385,22 +2337,22 @@ eqjoinsel_inner(Oid operator, ...@@ -2385,22 +2337,22 @@ eqjoinsel_inner(Oid operator,
CLAMP_PROBABILITY(matchprodfreq); CLAMP_PROBABILITY(matchprodfreq);
/* Sum up frequencies of matched and unmatched MCVs */ /* Sum up frequencies of matched and unmatched MCVs */
matchfreq1 = unmatchfreq1 = 0.0; matchfreq1 = unmatchfreq1 = 0.0;
for (i = 0; i < nvalues1; i++) for (i = 0; i < sslot1.nvalues; i++)
{ {
if (hasmatch1[i]) if (hasmatch1[i])
matchfreq1 += numbers1[i]; matchfreq1 += sslot1.numbers[i];
else else
unmatchfreq1 += numbers1[i]; unmatchfreq1 += sslot1.numbers[i];
} }
CLAMP_PROBABILITY(matchfreq1); CLAMP_PROBABILITY(matchfreq1);
CLAMP_PROBABILITY(unmatchfreq1); CLAMP_PROBABILITY(unmatchfreq1);
matchfreq2 = unmatchfreq2 = 0.0; matchfreq2 = unmatchfreq2 = 0.0;
for (i = 0; i < nvalues2; i++) for (i = 0; i < sslot2.nvalues; i++)
{ {
if (hasmatch2[i]) if (hasmatch2[i])
matchfreq2 += numbers2[i]; matchfreq2 += sslot2.numbers[i];
else else
unmatchfreq2 += numbers2[i]; unmatchfreq2 += sslot2.numbers[i];
} }
CLAMP_PROBABILITY(matchfreq2); CLAMP_PROBABILITY(matchfreq2);
CLAMP_PROBABILITY(unmatchfreq2); CLAMP_PROBABILITY(unmatchfreq2);
...@@ -2425,15 +2377,15 @@ eqjoinsel_inner(Oid operator, ...@@ -2425,15 +2377,15 @@ eqjoinsel_inner(Oid operator,
* MCVs plus non-MCV values. * MCVs plus non-MCV values.
*/ */
totalsel1 = matchprodfreq; totalsel1 = matchprodfreq;
if (nd2 > nvalues2) if (nd2 > sslot2.nvalues)
totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - nvalues2); totalsel1 += unmatchfreq1 * otherfreq2 / (nd2 - sslot2.nvalues);
if (nd2 > nmatches) if (nd2 > nmatches)
totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) / totalsel1 += otherfreq1 * (otherfreq2 + unmatchfreq2) /
(nd2 - nmatches); (nd2 - nmatches);
/* Same estimate from the point of view of relation 2. */ /* Same estimate from the point of view of relation 2. */
totalsel2 = matchprodfreq; totalsel2 = matchprodfreq;
if (nd1 > nvalues1) if (nd1 > sslot1.nvalues)
totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - nvalues1); totalsel2 += unmatchfreq2 * otherfreq1 / (nd1 - sslot1.nvalues);
if (nd1 > nmatches) if (nd1 > nmatches)
totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) / totalsel2 += otherfreq2 * (otherfreq1 + unmatchfreq1) /
(nd1 - nmatches); (nd1 - nmatches);
...@@ -2478,12 +2430,8 @@ eqjoinsel_inner(Oid operator, ...@@ -2478,12 +2430,8 @@ eqjoinsel_inner(Oid operator,
selec /= nd2; selec /= nd2;
} }
if (have_mcvs1) free_attstatsslot(&sslot1);
free_attstatsslot(vardata1->atttype, values1, nvalues1, free_attstatsslot(&sslot2);
numbers1, nnumbers1);
if (have_mcvs2)
free_attstatsslot(vardata2->atttype, values2, nvalues2,
numbers2, nnumbers2);
return selec; return selec;
} }
...@@ -2508,21 +2456,18 @@ eqjoinsel_semi(Oid operator, ...@@ -2508,21 +2456,18 @@ eqjoinsel_semi(Oid operator,
Oid opfuncoid; Oid opfuncoid;
Form_pg_statistic stats1 = NULL; Form_pg_statistic stats1 = NULL;
bool have_mcvs1 = false; bool have_mcvs1 = false;
Datum *values1 = NULL;
int nvalues1 = 0;
float4 *numbers1 = NULL;
int nnumbers1 = 0;
bool have_mcvs2 = false; bool have_mcvs2 = false;
Datum *values2 = NULL; AttStatsSlot sslot1;
int nvalues2 = 0; AttStatsSlot sslot2;
float4 *numbers2 = NULL;
int nnumbers2 = 0;
nd1 = get_variable_numdistinct(vardata1, &isdefault1); nd1 = get_variable_numdistinct(vardata1, &isdefault1);
nd2 = get_variable_numdistinct(vardata2, &isdefault2); nd2 = get_variable_numdistinct(vardata2, &isdefault2);
opfuncoid = OidIsValid(operator) ? get_opcode(operator) : InvalidOid; opfuncoid = OidIsValid(operator) ? get_opcode(operator) : InvalidOid;
memset(&sslot1, 0, sizeof(sslot1));
memset(&sslot2, 0, sizeof(sslot2));
/* /*
* We clamp nd2 to be not more than what we estimate the inner relation's * We clamp nd2 to be not more than what we estimate the inner relation's
* size to be. This is intuitively somewhat reasonable since obviously * size to be. This is intuitively somewhat reasonable since obviously
...@@ -2561,27 +2506,18 @@ eqjoinsel_semi(Oid operator, ...@@ -2561,27 +2506,18 @@ eqjoinsel_semi(Oid operator,
/* note we allow use of nullfrac regardless of security check */ /* note we allow use of nullfrac regardless of security check */
stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple); stats1 = (Form_pg_statistic) GETSTRUCT(vardata1->statsTuple);
if (statistic_proc_security_check(vardata1, opfuncoid)) if (statistic_proc_security_check(vardata1, opfuncoid))
have_mcvs1 = get_attstatsslot(vardata1->statsTuple, have_mcvs1 = get_attstatsslot(&sslot1, vardata1->statsTuple,
vardata1->atttype, STATISTIC_KIND_MCV, InvalidOid,
vardata1->atttypmod, ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS);
STATISTIC_KIND_MCV,
InvalidOid,
NULL,
&values1, &nvalues1,
&numbers1, &nnumbers1);
} }
if (HeapTupleIsValid(vardata2->statsTuple) && if (HeapTupleIsValid(vardata2->statsTuple) &&
statistic_proc_security_check(vardata2, opfuncoid)) statistic_proc_security_check(vardata2, opfuncoid))
{ {
have_mcvs2 = get_attstatsslot(vardata2->statsTuple, have_mcvs2 = get_attstatsslot(&sslot2, vardata2->statsTuple,
vardata2->atttype, STATISTIC_KIND_MCV, InvalidOid,
vardata2->atttypmod, ATTSTATSSLOT_VALUES);
STATISTIC_KIND_MCV, /* note: currently don't need stanumbers from RHS */
InvalidOid,
NULL,
&values2, &nvalues2,
&numbers2, &nnumbers2);
} }
if (have_mcvs1 && have_mcvs2 && OidIsValid(operator)) if (have_mcvs1 && have_mcvs2 && OidIsValid(operator))
...@@ -2607,15 +2543,15 @@ eqjoinsel_semi(Oid operator, ...@@ -2607,15 +2543,15 @@ eqjoinsel_semi(Oid operator,
/* /*
* The clamping above could have resulted in nd2 being less than * The clamping above could have resulted in nd2 being less than
* nvalues2; in which case, we assume that precisely the nd2 most * sslot2.nvalues; in which case, we assume that precisely the nd2
* common values in the relation will appear in the join input, and so * most common values in the relation will appear in the join input,
* compare to only the first nd2 members of the MCV list. Of course * and so compare to only the first nd2 members of the MCV list. Of
* this is frequently wrong, but it's the best bet we can make. * course this is frequently wrong, but it's the best bet we can make.
*/ */
clamped_nvalues2 = Min(nvalues2, nd2); clamped_nvalues2 = Min(sslot2.nvalues, nd2);
fmgr_info(opfuncoid, &eqproc); fmgr_info(opfuncoid, &eqproc);
hasmatch1 = (bool *) palloc0(nvalues1 * sizeof(bool)); hasmatch1 = (bool *) palloc0(sslot1.nvalues * sizeof(bool));
hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool)); hasmatch2 = (bool *) palloc0(clamped_nvalues2 * sizeof(bool));
/* /*
...@@ -2625,7 +2561,7 @@ eqjoinsel_semi(Oid operator, ...@@ -2625,7 +2561,7 @@ eqjoinsel_semi(Oid operator,
* and because the math wouldn't add up... * and because the math wouldn't add up...
*/ */
nmatches = 0; nmatches = 0;
for (i = 0; i < nvalues1; i++) for (i = 0; i < sslot1.nvalues; i++)
{ {
int j; int j;
...@@ -2635,8 +2571,8 @@ eqjoinsel_semi(Oid operator, ...@@ -2635,8 +2571,8 @@ eqjoinsel_semi(Oid operator,
continue; continue;
if (DatumGetBool(FunctionCall2Coll(&eqproc, if (DatumGetBool(FunctionCall2Coll(&eqproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values1[i], sslot1.values[i],
values2[j]))) sslot2.values[j])))
{ {
hasmatch1[i] = hasmatch2[j] = true; hasmatch1[i] = hasmatch2[j] = true;
nmatches++; nmatches++;
...@@ -2646,10 +2582,10 @@ eqjoinsel_semi(Oid operator, ...@@ -2646,10 +2582,10 @@ eqjoinsel_semi(Oid operator,
} }
/* Sum up frequencies of matched MCVs */ /* Sum up frequencies of matched MCVs */
matchfreq1 = 0.0; matchfreq1 = 0.0;
for (i = 0; i < nvalues1; i++) for (i = 0; i < sslot1.nvalues; i++)
{ {
if (hasmatch1[i]) if (hasmatch1[i])
matchfreq1 += numbers1[i]; matchfreq1 += sslot1.numbers[i];
} }
CLAMP_PROBABILITY(matchfreq1); CLAMP_PROBABILITY(matchfreq1);
pfree(hasmatch1); pfree(hasmatch1);
...@@ -2704,12 +2640,8 @@ eqjoinsel_semi(Oid operator, ...@@ -2704,12 +2640,8 @@ eqjoinsel_semi(Oid operator,
selec = 0.5 * (1.0 - nullfrac1); selec = 0.5 * (1.0 - nullfrac1);
} }
if (have_mcvs1) free_attstatsslot(&sslot1);
free_attstatsslot(vardata1->atttype, values1, nvalues1, free_attstatsslot(&sslot2);
numbers1, nnumbers1);
if (have_mcvs2)
free_attstatsslot(vardata2->atttype, values2, nvalues2,
numbers2, nnumbers2);
return selec; return selec;
} }
...@@ -3629,8 +3561,7 @@ estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets) ...@@ -3629,8 +3561,7 @@ estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
mcvfreq, mcvfreq,
avgfreq; avgfreq;
bool isdefault; bool isdefault;
float4 *numbers; AttStatsSlot sslot;
int nnumbers;
examine_variable(root, hashkey, 0, &vardata); examine_variable(root, hashkey, 0, &vardata);
...@@ -3689,20 +3620,16 @@ estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets) ...@@ -3689,20 +3620,16 @@ estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey, double nbuckets)
if (HeapTupleIsValid(vardata.statsTuple)) if (HeapTupleIsValid(vardata.statsTuple))
{ {
if (get_attstatsslot(vardata.statsTuple, if (get_attstatsslot(&sslot, vardata.statsTuple,
vardata.atttype, vardata.atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_NUMBERS))
NULL, NULL,
&numbers, &nnumbers))
{ {
/* /*
* The first MCV stat is for the most common value. * The first MCV stat is for the most common value.
*/ */
if (nnumbers > 0) if (sslot.nnumbers > 0)
mcvfreq = numbers[0]; mcvfreq = sslot.numbers[0];
free_attstatsslot(vardata.atttype, NULL, 0, free_attstatsslot(&sslot);
numbers, nnumbers);
} }
} }
...@@ -4572,7 +4499,7 @@ get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, ...@@ -4572,7 +4499,7 @@ get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
* freefunc: pointer to a function to release statsTuple with. * freefunc: pointer to a function to release statsTuple with.
* vartype: exposed type of the expression; this should always match * vartype: exposed type of the expression; this should always match
* the declared input type of the operator we are estimating for. * the declared input type of the operator we are estimating for.
* atttype, atttypmod: type data to pass to get_attstatsslot(). This is * atttype, atttypmod: actual type/typmod of the "var" expression. This is
* commonly the same as the exposed type of the variable argument, * commonly the same as the exposed type of the variable argument,
* but can be different in binary-compatible-type cases. * but can be different in binary-compatible-type cases.
* isunique: TRUE if we were able to match the var to a unique index or a * isunique: TRUE if we were able to match the var to a unique index or a
...@@ -5125,8 +5052,7 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, ...@@ -5125,8 +5052,7 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
int16 typLen; int16 typLen;
bool typByVal; bool typByVal;
Oid opfuncoid; Oid opfuncoid;
Datum *values; AttStatsSlot sslot;
int nvalues;
int i; int i;
/* /*
...@@ -5167,29 +5093,23 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, ...@@ -5167,29 +5093,23 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
* the one we want, fail --- this suggests that there is data we can't * the one we want, fail --- this suggests that there is data we can't
* use. * use.
*/ */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_HISTOGRAM, sortop, STATISTIC_KIND_HISTOGRAM, sortop,
NULL, ATTSTATSSLOT_VALUES))
&values, &nvalues,
NULL, NULL))
{ {
if (nvalues > 0) if (sslot.nvalues > 0)
{ {
tmin = datumCopy(values[0], typByVal, typLen); tmin = datumCopy(sslot.values[0], typByVal, typLen);
tmax = datumCopy(values[nvalues - 1], typByVal, typLen); tmax = datumCopy(sslot.values[sslot.nvalues - 1], typByVal, typLen);
have_data = true; have_data = true;
} }
free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0); free_attstatsslot(&sslot);
} }
else if (get_attstatsslot(vardata->statsTuple, else if (get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_HISTOGRAM, InvalidOid, STATISTIC_KIND_HISTOGRAM, InvalidOid,
NULL, 0))
&values, &nvalues,
NULL, NULL))
{ {
free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0); free_attstatsslot(&sslot);
return false; return false;
} }
...@@ -5199,12 +5119,9 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, ...@@ -5199,12 +5119,9 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
* the MCVs. However, usually the MCVs will not be the extreme values, so * the MCVs. However, usually the MCVs will not be the extreme values, so
* avoid unnecessary data copying. * avoid unnecessary data copying.
*/ */
if (get_attstatsslot(vardata->statsTuple, if (get_attstatsslot(&sslot, vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
STATISTIC_KIND_MCV, InvalidOid, STATISTIC_KIND_MCV, InvalidOid,
NULL, ATTSTATSSLOT_VALUES))
&values, &nvalues,
NULL, NULL))
{ {
bool tmin_is_mcv = false; bool tmin_is_mcv = false;
bool tmax_is_mcv = false; bool tmax_is_mcv = false;
...@@ -5212,26 +5129,26 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, ...@@ -5212,26 +5129,26 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
fmgr_info(opfuncoid, &opproc); fmgr_info(opfuncoid, &opproc);
for (i = 0; i < nvalues; i++) for (i = 0; i < sslot.nvalues; i++)
{ {
if (!have_data) if (!have_data)
{ {
tmin = tmax = values[i]; tmin = tmax = sslot.values[i];
tmin_is_mcv = tmax_is_mcv = have_data = true; tmin_is_mcv = tmax_is_mcv = have_data = true;
continue; continue;
} }
if (DatumGetBool(FunctionCall2Coll(&opproc, if (DatumGetBool(FunctionCall2Coll(&opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
values[i], tmin))) sslot.values[i], tmin)))
{ {
tmin = values[i]; tmin = sslot.values[i];
tmin_is_mcv = true; tmin_is_mcv = true;
} }
if (DatumGetBool(FunctionCall2Coll(&opproc, if (DatumGetBool(FunctionCall2Coll(&opproc,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
tmax, values[i]))) tmax, sslot.values[i])))
{ {
tmax = values[i]; tmax = sslot.values[i];
tmax_is_mcv = true; tmax_is_mcv = true;
} }
} }
...@@ -5239,7 +5156,7 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop, ...@@ -5239,7 +5156,7 @@ get_variable_range(PlannerInfo *root, VariableStatData *vardata, Oid sortop,
tmin = datumCopy(tmin, typByVal, typLen); tmin = datumCopy(tmin, typByVal, typLen);
if (tmax_is_mcv) if (tmax_is_mcv)
tmax = datumCopy(tmax, typByVal, typLen); tmax = datumCopy(tmax, typByVal, typLen);
free_attstatsslot(vardata->atttype, values, nvalues, NULL, 0); free_attstatsslot(&sslot);
} }
*min = tmin; *min = tmin;
...@@ -6979,25 +6896,21 @@ btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, ...@@ -6979,25 +6896,21 @@ btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
if (HeapTupleIsValid(vardata.statsTuple)) if (HeapTupleIsValid(vardata.statsTuple))
{ {
Oid sortop; Oid sortop;
float4 *numbers; AttStatsSlot sslot;
int nnumbers;
sortop = get_opfamily_member(index->opfamily[0], sortop = get_opfamily_member(index->opfamily[0],
index->opcintype[0], index->opcintype[0],
index->opcintype[0], index->opcintype[0],
BTLessStrategyNumber); BTLessStrategyNumber);
if (OidIsValid(sortop) && if (OidIsValid(sortop) &&
get_attstatsslot(vardata.statsTuple, InvalidOid, 0, get_attstatsslot(&sslot, vardata.statsTuple,
STATISTIC_KIND_CORRELATION, STATISTIC_KIND_CORRELATION, sortop,
sortop, ATTSTATSSLOT_NUMBERS))
NULL,
NULL, NULL,
&numbers, &nnumbers))
{ {
double varCorrelation; double varCorrelation;
Assert(nnumbers == 1); Assert(sslot.nnumbers == 1);
varCorrelation = numbers[0]; varCorrelation = sslot.numbers[0];
if (index->reverse_sort[0]) if (index->reverse_sort[0])
varCorrelation = -varCorrelation; varCorrelation = -varCorrelation;
...@@ -7007,7 +6920,7 @@ btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, ...@@ -7007,7 +6920,7 @@ btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
else else
costs.indexCorrelation = varCorrelation; costs.indexCorrelation = varCorrelation;
free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers); free_attstatsslot(&sslot);
} }
} }
...@@ -7920,25 +7833,21 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count, ...@@ -7920,25 +7833,21 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
if (HeapTupleIsValid(vardata.statsTuple)) if (HeapTupleIsValid(vardata.statsTuple))
{ {
float4 *numbers; AttStatsSlot sslot;
int nnumbers;
if (get_attstatsslot(&sslot, vardata.statsTuple,
if (get_attstatsslot(vardata.statsTuple, InvalidOid, 0, STATISTIC_KIND_CORRELATION, InvalidOid,
STATISTIC_KIND_CORRELATION, ATTSTATSSLOT_NUMBERS))
InvalidOid,
NULL,
NULL, NULL,
&numbers, &nnumbers))
{ {
double varCorrelation = 0.0; double varCorrelation = 0.0;
if (nnumbers > 0) if (sslot.nnumbers > 0)
varCorrelation = Abs(numbers[0]); varCorrelation = Abs(sslot.numbers[0]);
if (varCorrelation > *indexCorrelation) if (varCorrelation > *indexCorrelation)
*indexCorrelation = varCorrelation; *indexCorrelation = varCorrelation;
free_attstatsslot(InvalidOid, NULL, 0, numbers, nnumbers); free_attstatsslot(&sslot);
} }
} }
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#include "catalog/pg_proc.h" #include "catalog/pg_proc.h"
#include "catalog/pg_range.h" #include "catalog/pg_range.h"
#include "catalog/pg_statistic.h" #include "catalog/pg_statistic.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_transform.h" #include "catalog/pg_transform.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "miscadmin.h" #include "miscadmin.h"
...@@ -2865,35 +2864,39 @@ get_attavgwidth(Oid relid, AttrNumber attnum) ...@@ -2865,35 +2864,39 @@ get_attavgwidth(Oid relid, AttrNumber attnum)
* that have been provided by a stats hook and didn't really come from * that have been provided by a stats hook and didn't really come from
* pg_statistic. * pg_statistic.
* *
* sslot: pointer to output area (typically, a local variable in the caller).
* statstuple: pg_statistic tuple to be examined. * statstuple: pg_statistic tuple to be examined.
* atttype: type OID of slot's stavalues (can be InvalidOid if values == NULL).
* atttypmod: typmod of slot's stavalues (can be 0 if values == NULL).
* reqkind: STAKIND code for desired statistics slot kind. * reqkind: STAKIND code for desired statistics slot kind.
* reqop: STAOP value wanted, or InvalidOid if don't care. * reqop: STAOP value wanted, or InvalidOid if don't care.
* actualop: if not NULL, *actualop receives the actual STAOP value. * flags: bitmask of ATTSTATSSLOT_VALUES and/or ATTSTATSSLOT_NUMBERS.
* values, nvalues: if not NULL, the slot's stavalues are extracted.
* numbers, nnumbers: if not NULL, the slot's stanumbers are extracted.
* *
* If assigned, values and numbers are set to point to palloc'd arrays. * If a matching slot is found, TRUE is returned, and *sslot is filled thus:
* If the stavalues datatype is pass-by-reference, the values referenced by * staop: receives the actual STAOP value.
* the values array are themselves palloc'd. The palloc'd stuff can be * valuetype: receives actual datatype of the elements of stavalues.
* freed by calling free_attstatsslot. * values: receives pointer to an array of the slot's stavalues.
* nvalues: receives number of stavalues.
* numbers: receives pointer to an array of the slot's stanumbers (as float4).
* nnumbers: receives number of stanumbers.
* *
* Note: at present, atttype/atttypmod aren't actually used here at all. * valuetype/values/nvalues are InvalidOid/NULL/0 if ATTSTATSSLOT_VALUES
* But the caller must have the correct (or at least binary-compatible) * wasn't specified. Likewise, numbers/nnumbers are NULL/0 if
* type ID to pass to free_attstatsslot later. * ATTSTATSSLOT_NUMBERS wasn't specified.
*
* If no matching slot is found, FALSE is returned, and *sslot is zeroed.
*
* The data referred to by the fields of sslot is locally palloc'd and
* is independent of the original pg_statistic tuple. When the caller
* is done with it, call free_attstatsslot to release the palloc'd data.
*
* If it's desirable to call free_attstatsslot when get_attstatsslot might
* not have been called, memset'ing sslot to zeroes will allow that.
*/ */
bool bool
get_attstatsslot(HeapTuple statstuple, get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
Oid atttype, int32 atttypmod, int reqkind, Oid reqop, int flags)
int reqkind, Oid reqop,
Oid *actualop,
Datum **values, int *nvalues,
float4 **numbers, int *nnumbers)
{ {
Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple); Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
int i, int i;
j;
Datum val; Datum val;
bool isnull; bool isnull;
ArrayType *statarray; ArrayType *statarray;
...@@ -2902,6 +2905,9 @@ get_attstatsslot(HeapTuple statstuple, ...@@ -2902,6 +2905,9 @@ get_attstatsslot(HeapTuple statstuple,
HeapTuple typeTuple; HeapTuple typeTuple;
Form_pg_type typeForm; Form_pg_type typeForm;
/* initialize *sslot properly */
memset(sslot, 0, sizeof(AttStatsSlot));
for (i = 0; i < STATISTIC_NUM_SLOTS; i++) for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
{ {
if ((&stats->stakind1)[i] == reqkind && if ((&stats->stakind1)[i] == reqkind &&
...@@ -2911,26 +2917,29 @@ get_attstatsslot(HeapTuple statstuple, ...@@ -2911,26 +2917,29 @@ get_attstatsslot(HeapTuple statstuple,
if (i >= STATISTIC_NUM_SLOTS) if (i >= STATISTIC_NUM_SLOTS)
return false; /* not there */ return false; /* not there */
if (actualop) sslot->staop = (&stats->staop1)[i];
*actualop = (&stats->staop1)[i];
if (values) if (flags & ATTSTATSSLOT_VALUES)
{ {
val = SysCacheGetAttr(STATRELATTINH, statstuple, val = SysCacheGetAttr(STATRELATTINH, statstuple,
Anum_pg_statistic_stavalues1 + i, Anum_pg_statistic_stavalues1 + i,
&isnull); &isnull);
if (isnull) if (isnull)
elog(ERROR, "stavalues is null"); elog(ERROR, "stavalues is null");
statarray = DatumGetArrayTypeP(val);
/* /*
* Need to get info about the array element type. We look at the * Detoast the array if needed, and in any case make a copy that's
* actual element type embedded in the array, which might be only * under control of this AttStatsSlot.
* binary-compatible with the passed-in atttype. The info we extract
* here should be the same either way, but deconstruct_array is picky
* about having an exact type OID match.
*/ */
arrayelemtype = ARR_ELEMTYPE(statarray); statarray = DatumGetArrayTypePCopy(val);
/*
* Extract the actual array element type, and pass it back in case the
* caller needs it.
*/
sslot->valuetype = arrayelemtype = ARR_ELEMTYPE(statarray);
/* Need info about element type */
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(arrayelemtype)); typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(arrayelemtype));
if (!HeapTupleIsValid(typeTuple)) if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "cache lookup failed for type %u", arrayelemtype); elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
...@@ -2942,40 +2951,35 @@ get_attstatsslot(HeapTuple statstuple, ...@@ -2942,40 +2951,35 @@ get_attstatsslot(HeapTuple statstuple,
typeForm->typlen, typeForm->typlen,
typeForm->typbyval, typeForm->typbyval,
typeForm->typalign, typeForm->typalign,
values, NULL, nvalues); &sslot->values, NULL, &sslot->nvalues);
/* /*
* If the element type is pass-by-reference, we now have a bunch of * If the element type is pass-by-reference, we now have a bunch of
* Datums that are pointers into the syscache value. Copy them to * Datums that are pointers into the statarray, so we need to keep
* avoid problems if syscache decides to drop the entry. * that until free_attstatsslot. Otherwise, all the useful info is in
* sslot->values[], so we can free the array object immediately.
*/ */
if (!typeForm->typbyval) if (!typeForm->typbyval)
{ sslot->values_arr = statarray;
for (j = 0; j < *nvalues; j++) else
{ pfree(statarray);
(*values)[j] = datumCopy((*values)[j],
typeForm->typbyval,
typeForm->typlen);
}
}
ReleaseSysCache(typeTuple); ReleaseSysCache(typeTuple);
/*
* Free statarray if it's a detoasted copy.
*/
if ((Pointer) statarray != DatumGetPointer(val))
pfree(statarray);
} }
if (numbers) if (flags & ATTSTATSSLOT_NUMBERS)
{ {
val = SysCacheGetAttr(STATRELATTINH, statstuple, val = SysCacheGetAttr(STATRELATTINH, statstuple,
Anum_pg_statistic_stanumbers1 + i, Anum_pg_statistic_stanumbers1 + i,
&isnull); &isnull);
if (isnull) if (isnull)
elog(ERROR, "stanumbers is null"); elog(ERROR, "stanumbers is null");
statarray = DatumGetArrayTypeP(val);
/*
* Detoast the array if needed, and in any case make a copy that's
* under control of this AttStatsSlot.
*/
statarray = DatumGetArrayTypePCopy(val);
/* /*
* We expect the array to be a 1-D float4 array; verify that. We don't * We expect the array to be a 1-D float4 array; verify that. We don't
...@@ -2987,15 +2991,13 @@ get_attstatsslot(HeapTuple statstuple, ...@@ -2987,15 +2991,13 @@ get_attstatsslot(HeapTuple statstuple,
ARR_HASNULL(statarray) || ARR_HASNULL(statarray) ||
ARR_ELEMTYPE(statarray) != FLOAT4OID) ARR_ELEMTYPE(statarray) != FLOAT4OID)
elog(ERROR, "stanumbers is not a 1-D float4 array"); elog(ERROR, "stanumbers is not a 1-D float4 array");
*numbers = (float4 *) palloc(narrayelem * sizeof(float4));
memcpy(*numbers, ARR_DATA_PTR(statarray), narrayelem * sizeof(float4));
*nnumbers = narrayelem;
/* /* Give caller a pointer directly into the statarray */
* Free statarray if it's a detoasted copy. sslot->numbers = (float4 *) ARR_DATA_PTR(statarray);
*/ sslot->nnumbers = narrayelem;
if ((Pointer) statarray != DatumGetPointer(val))
pfree(statarray); /* We'll free the statarray in free_attstatsslot */
sslot->numbers_arr = statarray;
} }
return true; return true;
...@@ -3004,28 +3006,19 @@ get_attstatsslot(HeapTuple statstuple, ...@@ -3004,28 +3006,19 @@ get_attstatsslot(HeapTuple statstuple,
/* /*
* free_attstatsslot * free_attstatsslot
* Free data allocated by get_attstatsslot * Free data allocated by get_attstatsslot
*
* atttype is the type of the individual values in values[].
* It need be valid only if values != NULL.
*/ */
void void
free_attstatsslot(Oid atttype, free_attstatsslot(AttStatsSlot *sslot)
Datum *values, int nvalues, {
float4 *numbers, int nnumbers) /* The values[] array was separately palloc'd by deconstruct_array */
{ if (sslot->values)
if (values) pfree(sslot->values);
{ /* The numbers[] array points into numbers_arr, do not pfree it */
if (!get_typbyval(atttype)) /* Free the detoasted array objects, if any */
{ if (sslot->values_arr)
int i; pfree(sslot->values_arr);
if (sslot->numbers_arr)
for (i = 0; i < nvalues; i++) pfree(sslot->numbers_arr);
pfree(DatumGetPointer(values[i]));
}
pfree(values);
}
if (numbers)
pfree(numbers);
} }
/* ---------- PG_NAMESPACE CACHE ---------- */ /* ---------- PG_NAMESPACE CACHE ---------- */
......
...@@ -35,6 +35,28 @@ typedef enum IOFuncSelector ...@@ -35,6 +35,28 @@ typedef enum IOFuncSelector
IOFunc_send IOFunc_send
} IOFuncSelector; } IOFuncSelector;
/* Flag bits for get_attstatsslot */
#define ATTSTATSSLOT_VALUES 0x01
#define ATTSTATSSLOT_NUMBERS 0x02
/* Result struct for get_attstatsslot */
typedef struct AttStatsSlot
{
/* Always filled: */
Oid staop; /* Actual staop for the found slot */
/* Filled if ATTSTATSSLOT_VALUES is specified: */
Oid valuetype; /* Actual datatype of the values */
Datum *values; /* slot's "values" array, or NULL if none */
int nvalues; /* length of values[], or 0 */
/* Filled if ATTSTATSSLOT_NUMBERS is specified: */
float4 *numbers; /* slot's "numbers" array, or NULL if none */
int nnumbers; /* length of numbers[], or 0 */
/* Remaining fields are private to get_attstatsslot/free_attstatsslot */
void *values_arr; /* palloc'd values array, if any */
void *numbers_arr; /* palloc'd numbers array, if any */
} AttStatsSlot;
/* Hook for plugins to get control in get_attavgwidth() */ /* Hook for plugins to get control in get_attavgwidth() */
typedef int32 (*get_attavgwidth_hook_type) (Oid relid, AttrNumber attnum); typedef int32 (*get_attavgwidth_hook_type) (Oid relid, AttrNumber attnum);
extern PGDLLIMPORT get_attavgwidth_hook_type get_attavgwidth_hook; extern PGDLLIMPORT get_attavgwidth_hook_type get_attavgwidth_hook;
...@@ -148,15 +170,9 @@ extern Oid getBaseType(Oid typid); ...@@ -148,15 +170,9 @@ extern Oid getBaseType(Oid typid);
extern Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod); extern Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod);
extern int32 get_typavgwidth(Oid typid, int32 typmod); extern int32 get_typavgwidth(Oid typid, int32 typmod);
extern int32 get_attavgwidth(Oid relid, AttrNumber attnum); extern int32 get_attavgwidth(Oid relid, AttrNumber attnum);
extern bool get_attstatsslot(HeapTuple statstuple, extern bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
Oid atttype, int32 atttypmod, int reqkind, Oid reqop, int flags);
int reqkind, Oid reqop, extern void free_attstatsslot(AttStatsSlot *sslot);
Oid *actualop,
Datum **values, int *nvalues,
float4 **numbers, int *nnumbers);
extern void free_attstatsslot(Oid atttype,
Datum *values, int nvalues,
float4 *numbers, int nnumbers);
extern char *get_namespace_name(Oid nspid); extern char *get_namespace_name(Oid nspid);
extern char *get_namespace_name_or_temp(Oid nspid); extern char *get_namespace_name_or_temp(Oid nspid);
extern Oid get_range_subtype(Oid rangeOid); extern Oid get_range_subtype(Oid rangeOid);
......
...@@ -72,8 +72,8 @@ typedef struct VariableStatData ...@@ -72,8 +72,8 @@ typedef struct VariableStatData
/* NB: if statsTuple!=NULL, it must be freed when caller is done */ /* NB: if statsTuple!=NULL, it must be freed when caller is done */
void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */ void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
Oid vartype; /* exposed type of expression */ Oid vartype; /* exposed type of expression */
Oid atttype; /* type to pass to get_attstatsslot */ Oid atttype; /* actual type (after stripping relabel) */
int32 atttypmod; /* typmod to pass to get_attstatsslot */ int32 atttypmod; /* actual typmod (after stripping relabel) */
bool isunique; /* matches unique index or DISTINCT clause */ bool isunique; /* matches unique index or DISTINCT clause */
bool acl_ok; /* result of ACL check on table or column */ bool acl_ok; /* result of ACL check on table or column */
} VariableStatData; } VariableStatData;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment