PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

Definition at line 2192 of file gserialized_estimate.c.

References DEFAULT_ND_SEL, estimate_selectivity(), FALLBACK_ND_SEL, gbox_to_string(), nd_stats_to_json(), and pg_nd_stats_from_tuple().

Referenced by gserialized_gist_sel_2d(), and gserialized_gist_sel_nd().

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