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)
int nmcelems = 0;
float4 minfreq = 0.0;
float4 nullfrac = 0.0;
Form_pg_statistic stats;
Datum *values = NULL;
int nvalues = 0;
float4 *numbers = NULL;
int nnumbers = 0;
AttStatsSlot sslot;
/*
* If expression is not "variable @@ something" or "something @@ variable"
......@@ -193,6 +189,8 @@ _int_matchsel(PG_FUNCTION_ARGS)
*/
if (HeapTupleIsValid(vardata.statsTuple))
{
Form_pg_statistic stats;
stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
nullfrac = stats->stanullfrac;
......@@ -200,29 +198,30 @@ _int_matchsel(PG_FUNCTION_ARGS)
* For an int4 array, the default array type analyze function will
* collect a Most Common Elements list, which is an array of int4s.
*/
if (get_attstatsslot(vardata.statsTuple,
INT4OID, -1,
if (get_attstatsslot(&sslot, vardata.statsTuple,
STATISTIC_KIND_MCELEM, InvalidOid,
NULL,
&values, &nvalues,
&numbers, &nnumbers))
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
{
Assert(sslot.valuetype == INT4OID);
/*
* There should be three more Numbers than Values, because the
* last three (for intarray) cells are taken for minimal, maximal
* and nulls frequency. Punt if not.
*/
if (nnumbers == nvalues + 3)
if (sslot.nnumbers == sslot.nvalues + 3)
{
/* Grab the lowest frequency. */
minfreq = numbers[nnumbers - (nnumbers - nvalues)];
minfreq = sslot.numbers[sslot.nnumbers - (sslot.nnumbers - sslot.nvalues)];
mcelems = values;
mcefreqs = numbers;
nmcelems = nvalues;
mcelems = sslot.values;
mcefreqs = sslot.numbers;
nmcelems = sslot.nvalues;
}
}
}
else
memset(&sslot, 0, sizeof(sslot));
/* Process the logical expression in the query, using the stats */
selec = int_query_opr_selec(GETQUERY(query) + query->size - 1,
......@@ -231,7 +230,7 @@ _int_matchsel(PG_FUNCTION_ARGS)
/* MCE stats count only non-null rows, so adjust for null rows. */
selec *= (1.0 - nullfrac);
free_attstatsslot(INT4OID, values, nvalues, numbers, nnumbers);
free_attstatsslot(&sslot);
ReleaseVariableStats(vardata);
CLAMP_PROBABILITY(selec);
......
......@@ -1283,10 +1283,7 @@ static void
ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
{
HeapTupleData *statsTuple;
Datum *values;
int nvalues;
float4 *numbers;
int nnumbers;
AttStatsSlot sslot;
/* Do nothing if planner didn't identify the outer relation's join key */
if (!OidIsValid(node->skewTable))
......@@ -1305,19 +1302,17 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
if (!HeapTupleIsValid(statsTuple))
return;
if (get_attstatsslot(statsTuple, node->skewColType, node->skewColTypmod,
if (get_attstatsslot(&sslot, statsTuple,
STATISTIC_KIND_MCV, InvalidOid,
NULL,
&values, &nvalues,
&numbers, &nnumbers))
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
{
double frac;
int nbuckets;
FmgrInfo *hashfunctions;
int i;
if (mcvsToUse > nvalues)
mcvsToUse = nvalues;
if (mcvsToUse > sslot.nvalues)
mcvsToUse = sslot.nvalues;
/*
* Calculate the expected fraction of outer relation that will
......@@ -1326,11 +1321,10 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
*/
frac = 0;
for (i = 0; i < mcvsToUse; i++)
frac += numbers[i];
frac += sslot.numbers[i];
if (frac < SKEW_MIN_OUTER_FRACTION)
{
free_attstatsslot(node->skewColType,
values, nvalues, numbers, nnumbers);
free_attstatsslot(&sslot);
ReleaseSysCache(statsTuple);
return;
}
......@@ -1392,7 +1386,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
int bucket;
hashvalue = DatumGetUInt32(FunctionCall1(&hashfunctions[0],
values[i]));
sslot.values[i]));
/*
* 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)
hashtable->spacePeak = hashtable->spaceUsed;
}
free_attstatsslot(node->skewColType,
values, nvalues, numbers, nnumbers);
free_attstatsslot(&sslot);
}
ReleaseSysCache(statsTuple);
......
......@@ -163,28 +163,22 @@ tsquerysel(VariableStatData *vardata, Datum constval)
if (HeapTupleIsValid(vardata->statsTuple))
{
Form_pg_statistic stats;
Datum *values;
int nvalues;
float4 *numbers;
int nnumbers;
AttStatsSlot sslot;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
/* MCELEM will be an array of TEXT elements for a tsvector column */
if (get_attstatsslot(vardata->statsTuple,
TEXTOID, -1,
if (get_attstatsslot(&sslot, vardata->statsTuple,
STATISTIC_KIND_MCELEM, InvalidOid,
NULL,
&values, &nvalues,
&numbers, &nnumbers))
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
{
/*
* There is a most-common-elements slot for the tsvector Var, so
* use that.
*/
selec = mcelem_tsquery_selec(query, values, nvalues,
numbers, nnumbers);
free_attstatsslot(TEXTOID, values, nvalues, numbers, nnumbers);
selec = mcelem_tsquery_selec(query, sslot.values, sslot.nvalues,
sslot.numbers, sslot.nnumbers);
free_attstatsslot(&sslot);
}
else
{
......
......@@ -137,35 +137,22 @@ scalararraysel_containment(PlannerInfo *root,
statistic_proc_security_check(&vardata, cmpfunc->fn_oid))
{
Form_pg_statistic stats;
Datum *values;
int nvalues;
float4 *numbers;
int nnumbers;
float4 *hist;
int nhist;
AttStatsSlot sslot;
AttStatsSlot hslot;
stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
/* MCELEM will be an array of same type as element */
if (get_attstatsslot(vardata.statsTuple,
elemtype, vardata.atttypmod,
if (get_attstatsslot(&sslot, vardata.statsTuple,
STATISTIC_KIND_MCELEM, InvalidOid,
NULL,
&values, &nvalues,
&numbers, &nnumbers))
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
{
/* For ALL case, also get histogram of distinct-element counts */
if (useOr ||
!get_attstatsslot(vardata.statsTuple,
elemtype, vardata.atttypmod,
!get_attstatsslot(&hslot, vardata.statsTuple,
STATISTIC_KIND_DECHIST, InvalidOid,
NULL,
NULL, NULL,
&hist, &nhist))
{
hist = NULL;
nhist = 0;
}
ATTSTATSSLOT_NUMBERS))
memset(&hslot, 0, sizeof(hslot));
/*
* For = ANY, estimate as var @> ARRAY[const].
......@@ -173,22 +160,26 @@ scalararraysel_containment(PlannerInfo *root,
* For = ALL, estimate as var <@ ARRAY[const].
*/
if (useOr)
selec = mcelem_array_contain_overlap_selec(values, nvalues,
numbers, nnumbers,
selec = mcelem_array_contain_overlap_selec(sslot.values,
sslot.nvalues,
sslot.numbers,
sslot.nnumbers,
&constval, 1,
OID_ARRAY_CONTAINS_OP,
cmpfunc);
else
selec = mcelem_array_contained_selec(values, nvalues,
numbers, nnumbers,
selec = mcelem_array_contained_selec(sslot.values,
sslot.nvalues,
sslot.numbers,
sslot.nnumbers,
&constval, 1,
hist, nhist,
hslot.numbers,
hslot.nnumbers,
OID_ARRAY_CONTAINED_OP,
cmpfunc);
if (hist)
free_attstatsslot(elemtype, NULL, 0, hist, nhist);
free_attstatsslot(elemtype, values, nvalues, numbers, nnumbers);
free_attstatsslot(&hslot);
free_attstatsslot(&sslot);
}
else
{
......@@ -369,49 +360,35 @@ calc_arraycontsel(VariableStatData *vardata, Datum constval,
statistic_proc_security_check(vardata, cmpfunc->fn_oid))
{
Form_pg_statistic stats;
Datum *values;
int nvalues;
float4 *numbers;
int nnumbers;
float4 *hist;
int nhist;
AttStatsSlot sslot;
AttStatsSlot hslot;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
/* MCELEM will be an array of same type as column */
if (get_attstatsslot(vardata->statsTuple,
elemtype, vardata->atttypmod,
if (get_attstatsslot(&sslot, vardata->statsTuple,
STATISTIC_KIND_MCELEM, InvalidOid,
NULL,
&values, &nvalues,
&numbers, &nnumbers))
ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
{
/*
* For "array <@ const" case we also need histogram of distinct
* element counts.
*/
if (operator != OID_ARRAY_CONTAINED_OP ||
!get_attstatsslot(vardata->statsTuple,
elemtype, vardata->atttypmod,
!get_attstatsslot(&hslot, vardata->statsTuple,
STATISTIC_KIND_DECHIST, InvalidOid,
NULL,
NULL, NULL,
&hist, &nhist))
{
hist = NULL;
nhist = 0;
}
ATTSTATSSLOT_NUMBERS))
memset(&hslot, 0, sizeof(hslot));
/* Use the most-common-elements slot for the array Var. */
selec = mcelem_array_selec(array, typentry,
values, nvalues,
numbers, nnumbers,
hist, nhist,
sslot.values, sslot.nvalues,
sslot.numbers, sslot.nnumbers,
hslot.numbers, hslot.nnumbers,
operator, cmpfunc);
if (hist)
free_attstatsslot(elemtype, NULL, 0, hist, nhist);
free_attstatsslot(elemtype, values, nvalues, numbers, nnumbers);
free_attstatsslot(&hslot);
free_attstatsslot(&sslot);
}
else
{
......
This diff is collapsed.
......@@ -239,25 +239,21 @@ calc_rangesel(TypeCacheEntry *typcache, VariableStatData *vardata,
if (HeapTupleIsValid(vardata->statsTuple))
{
Form_pg_statistic stats;
float4 *numbers;
int nnumbers;
AttStatsSlot sslot;
stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
null_frac = stats->stanullfrac;
/* Try to get fraction of empty ranges */
if (get_attstatsslot(vardata->statsTuple,
FLOAT8OID, -1,
if (get_attstatsslot(&sslot, vardata->statsTuple,
STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
InvalidOid,
NULL,
NULL, NULL,
&numbers, &nnumbers))
ATTSTATSSLOT_NUMBERS))
{
if (nnumbers != 1)
if (sslot.nnumbers != 1)
elog(ERROR, "invalid empty fraction statistic"); /* shouldn't happen */
empty_frac = numbers[0];
free_attstatsslot(FLOAT8OID, NULL, 0, numbers, nnumbers);
empty_frac = sslot.numbers[0];
free_attstatsslot(&sslot);
}
else
{
......@@ -374,10 +370,9 @@ static double
calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
RangeType *constval, Oid operator)
{
Datum *hist_values;
AttStatsSlot hslot;
AttStatsSlot lslot;
int nhist;
Datum *length_hist_values = NULL;
int length_nhist = 0;
RangeBound *hist_lower;
RangeBound *hist_upper;
int i;
......@@ -397,23 +392,21 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
/* Try to get histogram of ranges */
if (!(HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
get_attstatsslot(&hslot, vardata->statsTuple,
STATISTIC_KIND_BOUNDS_HISTOGRAM, InvalidOid,
NULL,
&hist_values, &nhist,
NULL, NULL)))
ATTSTATSSLOT_VALUES)))
return -1.0;
/*
* Convert histogram of ranges into histograms of its lower and upper
* bounds.
*/
nhist = hslot.nvalues;
hist_lower = (RangeBound *) palloc(sizeof(RangeBound) * nhist);
hist_upper = (RangeBound *) palloc(sizeof(RangeBound) * nhist);
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);
/* The histogram should not contain any empty ranges */
if (empty)
......@@ -425,27 +418,25 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
operator == OID_RANGE_CONTAINED_OP)
{
if (!(HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple,
FLOAT8OID, -1,
get_attstatsslot(&lslot, vardata->statsTuple,
STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM,
InvalidOid,
NULL,
&length_hist_values, &length_nhist,
NULL, NULL)))
ATTSTATSSLOT_VALUES)))
{
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0);
free_attstatsslot(&hslot);
return -1.0;
}
/* check that it's a histogram, not just a dummy entry */
if (length_nhist < 2)
if (lslot.nvalues < 2)
{
free_attstatsslot(FLOAT8OID,
length_hist_values, length_nhist, NULL, 0);
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0);
free_attstatsslot(&lslot);
free_attstatsslot(&hslot);
return -1.0;
}
}
else
memset(&lslot, 0, sizeof(lslot));
/* Extract the bounds of the constant value. */
range_deserialize(typcache, constval, &const_lower, &const_upper, &empty);
......@@ -545,7 +536,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
hist_selec =
calc_hist_selectivity_contains(typcache, &const_lower,
&const_upper, hist_lower, nhist,
length_hist_values, length_nhist);
lslot.values, lslot.nvalues);
break;
case OID_RANGE_CONTAINED_OP:
......@@ -570,7 +561,7 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
hist_selec =
calc_hist_selectivity_contained(typcache, &const_lower,
&const_upper, hist_lower, nhist,
length_hist_values, length_nhist);
lslot.values, lslot.nvalues);
}
break;
......@@ -580,9 +571,8 @@ calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata,
break;
}
free_attstatsslot(FLOAT8OID,
length_hist_values, length_nhist, NULL, 0);
free_attstatsslot(vardata->atttype, hist_values, nhist, NULL, 0);
free_attstatsslot(&lslot);
free_attstatsslot(&hslot);
return hist_selec;
}
......
This diff is collapsed.
......@@ -32,7 +32,6 @@
#include "catalog/pg_proc.h"
#include "catalog/pg_range.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
......@@ -2865,35 +2864,39 @@ get_attavgwidth(Oid relid, AttrNumber attnum)
* that have been provided by a stats hook and didn't really come from
* pg_statistic.
*
* sslot: pointer to output area (typically, a local variable in the caller).
* 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.
* reqop: STAOP value wanted, or InvalidOid if don't care.
* actualop: if not NULL, *actualop receives the actual STAOP value.
* values, nvalues: if not NULL, the slot's stavalues are extracted.
* numbers, nnumbers: if not NULL, the slot's stanumbers are extracted.
* flags: bitmask of ATTSTATSSLOT_VALUES and/or ATTSTATSSLOT_NUMBERS.
*
* If assigned, values and numbers are set to point to palloc'd arrays.
* If the stavalues datatype is pass-by-reference, the values referenced by
* the values array are themselves palloc'd. The palloc'd stuff can be
* freed by calling free_attstatsslot.
* If a matching slot is found, TRUE is returned, and *sslot is filled thus:
* staop: receives the actual STAOP value.
* valuetype: receives actual datatype of the elements of stavalues.
* 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.
* But the caller must have the correct (or at least binary-compatible)
* type ID to pass to free_attstatsslot later.
* valuetype/values/nvalues are InvalidOid/NULL/0 if ATTSTATSSLOT_VALUES
* wasn't specified. Likewise, numbers/nnumbers are NULL/0 if
* 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
get_attstatsslot(HeapTuple statstuple,
Oid atttype, int32 atttypmod,
int reqkind, Oid reqop,
Oid *actualop,
Datum **values, int *nvalues,
float4 **numbers, int *nnumbers)
get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
int reqkind, Oid reqop, int flags)
{
Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
int i,
j;
int i;
Datum val;
bool isnull;
ArrayType *statarray;
......@@ -2902,6 +2905,9 @@ get_attstatsslot(HeapTuple statstuple,
HeapTuple typeTuple;
Form_pg_type typeForm;
/* initialize *sslot properly */
memset(sslot, 0, sizeof(AttStatsSlot));
for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
{
if ((&stats->stakind1)[i] == reqkind &&
......@@ -2911,26 +2917,29 @@ get_attstatsslot(HeapTuple statstuple,
if (i >= STATISTIC_NUM_SLOTS)
return false; /* not there */
if (actualop)
*actualop = (&stats->staop1)[i];
sslot->staop = (&stats->staop1)[i];
if (values)
if (flags & ATTSTATSSLOT_VALUES)
{
val = SysCacheGetAttr(STATRELATTINH, statstuple,
Anum_pg_statistic_stavalues1 + i,
&isnull);
if (isnull)
elog(ERROR, "stavalues is null");
statarray = DatumGetArrayTypeP(val);
/*
* Need to get info about the array element type. We look at the
* actual element type embedded in the array, which might be only
* 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.
* Detoast the array if needed, and in any case make a copy that's
* under control of this AttStatsSlot.
*/
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));
if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
......@@ -2942,40 +2951,35 @@ get_attstatsslot(HeapTuple statstuple,
typeForm->typlen,
typeForm->typbyval,
typeForm->typalign,
values, NULL, nvalues);
&sslot->values, NULL, &sslot->nvalues);
/*
* 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
* avoid problems if syscache decides to drop the entry.
* Datums that are pointers into the statarray, so we need to keep
* that until free_attstatsslot. Otherwise, all the useful info is in
* sslot->values[], so we can free the array object immediately.
*/
if (!typeForm->typbyval)
{
for (j = 0; j < *nvalues; j++)
{
(*values)[j] = datumCopy((*values)[j],
typeForm->typbyval,
typeForm->typlen);
}
}
sslot->values_arr = statarray;
else
pfree(statarray);
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,
Anum_pg_statistic_stanumbers1 + i,
&isnull);
if (isnull)
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
......@@ -2987,15 +2991,13 @@ get_attstatsslot(HeapTuple statstuple,
ARR_HASNULL(statarray) ||
ARR_ELEMTYPE(statarray) != FLOAT4OID)
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;
/*
* Free statarray if it's a detoasted copy.
*/
if ((Pointer) statarray != DatumGetPointer(val))
pfree(statarray);
/* Give caller a pointer directly into the statarray */
sslot->numbers = (float4 *) ARR_DATA_PTR(statarray);
sslot->nnumbers = narrayelem;
/* We'll free the statarray in free_attstatsslot */
sslot->numbers_arr = statarray;
}
return true;
......@@ -3004,28 +3006,19 @@ get_attstatsslot(HeapTuple statstuple,
/*
* free_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
free_attstatsslot(Oid atttype,
Datum *values, int nvalues,
float4 *numbers, int nnumbers)
{
if (values)
{
if (!get_typbyval(atttype))
{
int i;
for (i = 0; i < nvalues; i++)
pfree(DatumGetPointer(values[i]));
}
pfree(values);
}
if (numbers)
pfree(numbers);
free_attstatsslot(AttStatsSlot *sslot)
{
/* The values[] array was separately palloc'd by deconstruct_array */
if (sslot->values)
pfree(sslot->values);
/* The numbers[] array points into numbers_arr, do not pfree it */
/* Free the detoasted array objects, if any */
if (sslot->values_arr)
pfree(sslot->values_arr);
if (sslot->numbers_arr)
pfree(sslot->numbers_arr);
}
/* ---------- PG_NAMESPACE CACHE ---------- */
......
......@@ -35,6 +35,28 @@ typedef enum IOFuncSelector
IOFunc_send
} 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() */
typedef int32 (*get_attavgwidth_hook_type) (Oid relid, AttrNumber attnum);
extern PGDLLIMPORT get_attavgwidth_hook_type get_attavgwidth_hook;
......@@ -148,15 +170,9 @@ extern Oid getBaseType(Oid typid);
extern Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod);
extern int32 get_typavgwidth(Oid typid, int32 typmod);
extern int32 get_attavgwidth(Oid relid, AttrNumber attnum);
extern bool get_attstatsslot(HeapTuple statstuple,
Oid atttype, int32 atttypmod,
int reqkind, Oid reqop,
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 bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
int reqkind, Oid reqop, int flags);
extern void free_attstatsslot(AttStatsSlot *sslot);
extern char *get_namespace_name(Oid nspid);
extern char *get_namespace_name_or_temp(Oid nspid);
extern Oid get_range_subtype(Oid rangeOid);
......
......@@ -72,8 +72,8 @@ typedef struct VariableStatData
/* NB: if statsTuple!=NULL, it must be freed when caller is done */
void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
Oid vartype; /* exposed type of expression */
Oid atttype; /* type to pass to get_attstatsslot */
int32 atttypmod; /* typmod to pass to get_attstatsslot */
Oid atttype; /* actual type (after stripping relabel) */
int32 atttypmod; /* actual typmod (after stripping relabel) */
bool isunique; /* matches unique index or DISTINCT clause */
bool acl_ok; /* result of ACL check on table or column */
} 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