PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

Definition at line 2145 of file gserialized_estimate.c.

References ovdump::args, DEFAULT_ND_SEL, estimate_selectivity(), FALLBACK_ND_SEL, gbox_to_string(), gserialized_estimated_extent(), nd_stats_to_json(), PG_FUNCTION_INFO_V1(), and pg_nd_stats_from_tuple().

Referenced by gserialized_gist_sel_2d(), and gserialized_gist_sel_nd().

2146 {
2147  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2148  /* Oid operator_oid = PG_GETARG_OID(1); */
2149  List *args = (List *) PG_GETARG_POINTER(2);
2150  /* int varRelid = PG_GETARG_INT32(3); */
2151  int mode = PG_GETARG_INT32(4);
2152 
2153  VariableStatData vardata;
2154  ND_STATS *nd_stats = NULL;
2155 
2156  Node *other;
2157  Var *self;
2158  GBOX search_box;
2159  float8 selectivity = 0;
2160 
2161  POSTGIS_DEBUG(2, "gserialized_gist_sel called");
2162 
2163  /*
2164  * TODO: This is a big one,
2165  * All this statistics code *only* tries to generate a valid
2166  * selectivity for && and &&&. That leaves all the other
2167  * geometry operators with bad stats! The selectivity
2168  * calculation should take account of the incoming operator
2169  * type and do the right thing.
2170  */
2171 
2172  /* Fail if not a binary opclause (probably shouldn't happen) */
2173  if (list_length(args) != 2)
2174  {
2175  POSTGIS_DEBUG(3, "gserialized_gist_sel: not a binary opclause");
2176  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2177  }
2178 
2179  /* Find the constant part */
2180  other = (Node *) linitial(args);
2181  if ( ! IsA(other, Const) )
2182  {
2183  self = (Var *)other;
2184  other = (Node *) lsecond(args);
2185  }
2186  else
2187  {
2188  self = (Var *) lsecond(args);
2189  }
2190 
2191  if ( ! IsA(other, Const) )
2192  {
2193  POSTGIS_DEBUG(3, " no constant arguments - returning a default selectivity");
2194  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2195  }
2196 
2197  /* Convert the constant to a BOX */
2198  if( ! gserialized_datum_get_gbox_p(((Const*)other)->constvalue, &search_box) )
2199  {
2200  POSTGIS_DEBUG(3, "search box is EMPTY");
2201  PG_RETURN_FLOAT8(0.0);
2202  }
2203  POSTGIS_DEBUGF(4, " requested search box is: %s", gbox_to_string(&search_box));
2204 
2205  /* Get pg_statistic row */
2206  examine_variable(root, (Node*)self, 0, &vardata);
2207  if ( vardata.statsTuple ) {
2208  nd_stats = pg_nd_stats_from_tuple(vardata.statsTuple, mode);
2209  }
2210  ReleaseVariableStats(vardata);
2211 
2212  if ( ! nd_stats )
2213  {
2214  POSTGIS_DEBUG(3, " unable to load stats from syscache, not analyzed yet?");
2215  PG_RETURN_FLOAT8(FALLBACK_ND_SEL);
2216  }
2217 
2218  POSTGIS_DEBUGF(4, " got stats:\n%s", nd_stats_to_json(nd_stats));
2219 
2220  /* Do the estimation! */
2221  selectivity = estimate_selectivity(&search_box, nd_stats, mode);
2222  POSTGIS_DEBUGF(3, " returning computed value: %f", selectivity);
2223 
2224  pfree(nd_stats);
2225  PG_RETURN_FLOAT8(selectivity);
2226 }
args
Definition: ovdump.py:44
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:404
#define FALLBACK_ND_SEL
More modest fallback selectivity factor.
static float8 estimate_selectivity(const GBOX *box, const ND_STATS *nd_stats, int mode)
This function returns an estimate of the selectivity of a search GBOX by looking at data in the ND_ST...
static ND_STATS * pg_nd_stats_from_tuple(HeapTuple stats_tuple, int mode)
static char * nd_stats_to_json(const ND_STATS *nd_stats)
Convert an ND_STATS to a JSON representation for external use.
N-dimensional statistics structure.
#define DEFAULT_ND_SEL
Default geometry selectivity factor.
Here is the call graph for this function:
Here is the caller graph for this function: