PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

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

2150 {
2151  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2152  /* Oid operator_oid = PG_GETARG_OID(1); */
2153  List *args = (List *) PG_GETARG_POINTER(2);
2154  /* int varRelid = PG_GETARG_INT32(3); */
2155  int mode = PG_GETARG_INT32(4);
2156 
2157  VariableStatData vardata;
2158  ND_STATS *nd_stats = NULL;
2159 
2160  Node *other;
2161  Var *self;
2162  GBOX search_box;
2163  float8 selectivity = 0;
2164 
2165  POSTGIS_DEBUG(2, "gserialized_gist_sel called");
2166 
2167  /*
2168  * TODO: This is a big one,
2169  * All this statistics code *only* tries to generate a valid
2170  * selectivity for && and &&&. That leaves all the other
2171  * geometry operators with bad stats! The selectivity
2172  * calculation should take account of the incoming operator
2173  * type and do the right thing.
2174  */
2175 
2176  /* Fail if not a binary opclause (probably shouldn't happen) */
2177  if (list_length(args) != 2)
2178  {
2179  POSTGIS_DEBUG(3, "gserialized_gist_sel: not a binary opclause");
2180  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2181  }
2182 
2183  /* Find the constant part */
2184  other = (Node *) linitial(args);
2185  if ( ! IsA(other, Const) )
2186  {
2187  self = (Var *)other;
2188  other = (Node *) lsecond(args);
2189  }
2190  else
2191  {
2192  self = (Var *) lsecond(args);
2193  }
2194 
2195  if ( ! IsA(other, Const) )
2196  {
2197  POSTGIS_DEBUG(3, " no constant arguments - returning a default selectivity");
2198  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2199  }
2200 
2201  /* Convert the constant to a BOX */
2202  if( ! gserialized_datum_get_gbox_p(((Const*)other)->constvalue, &search_box) )
2203  {
2204  POSTGIS_DEBUG(3, "search box is EMPTY");
2205  PG_RETURN_FLOAT8(0.0);
2206  }
2207  POSTGIS_DEBUGF(4, " requested search box is: %s", gbox_to_string(&search_box));
2208 
2209  /* Get pg_statistic row */
2210  examine_variable(root, (Node*)self, 0, &vardata);
2211  if ( vardata.statsTuple ) {
2212  nd_stats = pg_nd_stats_from_tuple(vardata.statsTuple, mode);
2213  }
2214  ReleaseVariableStats(vardata);
2215 
2216  if ( ! nd_stats )
2217  {
2218  POSTGIS_DEBUG(3, " unable to load stats from syscache, not analyzed yet?");
2219  PG_RETURN_FLOAT8(FALLBACK_ND_SEL);
2220  }
2221 
2222  POSTGIS_DEBUGF(4, " got stats:\n%s", nd_stats_to_json(nd_stats));
2223 
2224  /* Do the estimation! */
2225  selectivity = estimate_selectivity(&search_box, nd_stats, mode);
2226  POSTGIS_DEBUGF(3, " returning computed value: %f", selectivity);
2227 
2228  pfree(nd_stats);
2229  PG_RETURN_FLOAT8(selectivity);
2230 }
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: