PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

Definition at line 2211 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().

2212 {
2213  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2214  /* Oid operator_oid = PG_GETARG_OID(1); */
2215  List *args = (List *) PG_GETARG_POINTER(2);
2216  /* int varRelid = PG_GETARG_INT32(3); */
2217  int mode = PG_GETARG_INT32(4);
2218 
2219  VariableStatData vardata;
2220  ND_STATS *nd_stats = NULL;
2221 
2222  Node *other;
2223  Var *self;
2224  GBOX search_box;
2225  float8 selectivity = 0;
2226 
2227  POSTGIS_DEBUG(2, "gserialized_gist_sel called");
2228 
2229  /*
2230  * TODO: This is a big one,
2231  * All this statistics code *only* tries to generate a valid
2232  * selectivity for && and &&&. That leaves all the other
2233  * geometry operators with bad stats! The selectivity
2234  * calculation should take account of the incoming operator
2235  * type and do the right thing.
2236  */
2237 
2238  /* Fail if not a binary opclause (probably shouldn't happen) */
2239  if (list_length(args) != 2)
2240  {
2241  POSTGIS_DEBUG(3, "gserialized_gist_sel: not a binary opclause");
2242  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2243  }
2244 
2245  /* Find the constant part */
2246  other = (Node *) linitial(args);
2247  if ( ! IsA(other, Const) )
2248  {
2249  self = (Var *)other;
2250  other = (Node *) lsecond(args);
2251  }
2252  else
2253  {
2254  self = (Var *) lsecond(args);
2255  }
2256 
2257  if ( ! IsA(other, Const) )
2258  {
2259  POSTGIS_DEBUG(3, " no constant arguments - returning a default selectivity");
2260  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2261  }
2262 
2263  /* Convert the constant to a BOX */
2264  if( ! gserialized_datum_get_gbox_p(((Const*)other)->constvalue, &search_box) )
2265  {
2266  POSTGIS_DEBUG(3, "search box is EMPTY");
2267  PG_RETURN_FLOAT8(0.0);
2268  }
2269  POSTGIS_DEBUGF(4, " requested search box is: %s", gbox_to_string(&search_box));
2270 
2271  /* Get pg_statistic row */
2272  examine_variable(root, (Node*)self, 0, &vardata);
2273  if ( vardata.statsTuple ) {
2274  nd_stats = pg_nd_stats_from_tuple(vardata.statsTuple, mode);
2275  }
2276  ReleaseVariableStats(vardata);
2277 
2278  if ( ! nd_stats )
2279  {
2280  POSTGIS_DEBUG(3, " unable to load stats from syscache, not analyzed yet?");
2281  PG_RETURN_FLOAT8(FALLBACK_ND_SEL);
2282  }
2283 
2284  POSTGIS_DEBUGF(4, " got stats:\n%s", nd_stats_to_json(nd_stats));
2285 
2286  /* Do the estimation! */
2287  selectivity = estimate_selectivity(&search_box, nd_stats, mode);
2288  POSTGIS_DEBUGF(3, " returning computed value: %f", selectivity);
2289 
2290  pfree(nd_stats);
2291  PG_RETURN_FLOAT8(selectivity);
2292 }
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:399
#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: