mirror of
https://github.com/postgres/postgres.git
synced 2025-12-15 02:22:24 +03:00
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 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
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user