PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ gserialized_gist_sel()

Datum gserialized_gist_sel ( PG_FUNCTION_ARGS  )

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

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