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
{ {
......
This diff is collapsed.
...@@ -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;
} }
......
This diff is collapsed.
...@@ -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