PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

Definition at line 2215 of file gserialized_estimate.c.

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

References ovdump::args, 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().

Here is the call graph for this function:
Here is the caller graph for this function: