Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2693ef0

Browse files
author
Nikita Glukhov
committed
Add stats_form_tuple()
1 parent b36359e commit 2693ef0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7954,3 +7954,58 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
79547954

79557955
*indexPages = index->pages;
79567956
}
7957+
7958+
/*
7959+
* stats_form_tuple - Form pg_statistic tuple from StatsData.
7960+
*
7961+
* If 'data' parameter is NULL, form all-NULL tuple (nullfrac = 1.0).
7962+
*/
7963+
HeapTuple
7964+
stats_form_tuple(StatsData *data)
7965+
{
7966+
Relation rel;
7967+
HeapTuple tuple;
7968+
Datum values[Natts_pg_statistic];
7969+
bool nulls[Natts_pg_statistic];
7970+
int i;
7971+
7972+
for (i = 0; i < Natts_pg_statistic; ++i)
7973+
nulls[i] = false;
7974+
7975+
values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
7976+
values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(0);
7977+
values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
7978+
values[Anum_pg_statistic_stanullfrac - 1] =
7979+
Float4GetDatum(data ? data->nullfrac : 1.0);
7980+
values[Anum_pg_statistic_stawidth - 1] =
7981+
Int32GetDatum(data ? data->width : 0);
7982+
values[Anum_pg_statistic_stadistinct - 1] =
7983+
Float4GetDatum(data ? data->distinct : 0);
7984+
7985+
for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
7986+
{
7987+
StatsSlot *slot = data ? &data->slots[i] : NULL;
7988+
7989+
values[Anum_pg_statistic_stakind1 + i - 1] =
7990+
Int16GetDatum(slot ? slot->kind : 0);
7991+
7992+
values[Anum_pg_statistic_staop1 + i - 1] =
7993+
ObjectIdGetDatum(slot ? slot->opid : InvalidOid);
7994+
7995+
if (slot && DatumGetPointer(slot->numbers))
7996+
values[Anum_pg_statistic_stanumbers1 + i - 1] = slot->numbers;
7997+
else
7998+
nulls[Anum_pg_statistic_stanumbers1 + i - 1] = true;
7999+
8000+
if (slot && DatumGetPointer(slot->values))
8001+
values[Anum_pg_statistic_stavalues1 + i - 1] = slot->values;
8002+
else
8003+
nulls[Anum_pg_statistic_stavalues1 + i - 1] = true;
8004+
}
8005+
8006+
rel = table_open(StatisticRelationId, AccessShareLock);
8007+
tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
8008+
table_close(rel, NoLock);
8009+
8010+
return tuple;
8011+
}

src/include/utils/selfuncs.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define SELFUNCS_H
1717

1818
#include "access/htup.h"
19+
#include "catalog/pg_statistic.h"
1920
#include "fmgr.h"
2021
#include "nodes/pathnodes.h"
2122

@@ -133,6 +134,24 @@ typedef struct
133134
double num_sa_scans; /* # indexscans from ScalarArrayOpExprs */
134135
} GenericCosts;
135136

137+
/* Single pg_statistic slot */
138+
typedef struct StatsSlot
139+
{
140+
int16 kind; /* stakindN: statistic kind (STATISTIC_KIND_XXX) */
141+
Oid opid; /* staopN: associated operator, if needed */
142+
Datum numbers; /* stanumbersN: float4 array of numbers */
143+
Datum values; /* stavaluesN: anyarray of values */
144+
} StatsSlot;
145+
146+
/* Deformed pg_statistic tuple */
147+
typedef struct StatsData
148+
{
149+
float4 nullfrac; /* stanullfrac: fraction of NULL values */
150+
float4 distinct; /* stadistinct: number of distinct non-NULL values */
151+
int32 width; /* stawidth: average width in bytes of non-NULL values */
152+
StatsSlot slots[STATISTIC_NUM_SLOTS]; /* slots for different statistic types */
153+
} StatsData;
154+
136155
/* Hooks for plugins to get control when we ask for stats */
137156
typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
138157
RangeTblEntry *rte,
@@ -231,6 +250,9 @@ extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
231250
double loop_count,
232251
GenericCosts *costs);
233252

253+
extern HeapTuple stats_form_tuple(StatsData *data);
254+
255+
234256
/* Functions in array_selfuncs.c */
235257

236258
extern Selectivity scalararraysel_containment(PlannerInfo *root,

0 commit comments

Comments
 (0)