PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

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

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