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

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

2079 {
2080  PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
2081  /* Oid operator_oid = PG_GETARG_OID(1); */
2082  List *args = (List *) PG_GETARG_POINTER(2);
2083  /* int varRelid = PG_GETARG_INT32(3); */
2084  int mode = PG_GETARG_INT32(4);
2085 
2086  VariableStatData vardata;
2087  ND_STATS *nd_stats = NULL;
2088 
2089  Node *other;
2090  Var *self;
2091  GBOX search_box;
2092  float8 selectivity = 0;
2093 
2094  POSTGIS_DEBUG(2, "gserialized_gist_sel called");
2095 
2096  /*
2097  * TODO: This is a big one,
2098  * All this statistics code *only* tries to generate a valid
2099  * selectivity for && and &&&. That leaves all the other
2100  * geometry operators with bad stats! The selectivity
2101  * calculation should take account of the incoming operator
2102  * type and do the right thing.
2103  */
2104 
2105  /* Fail if not a binary opclause (probably shouldn't happen) */
2106  if (list_length(args) != 2)
2107  {
2108  POSTGIS_DEBUG(3, "gserialized_gist_sel: not a binary opclause");
2109  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2110  }
2111 
2112  /* Find the constant part */
2113  other = (Node *) linitial(args);
2114  if ( ! IsA(other, Const) )
2115  {
2116  self = (Var *)other;
2117  other = (Node *) lsecond(args);
2118  }
2119  else
2120  {
2121  self = (Var *) lsecond(args);
2122  }
2123 
2124  if ( ! IsA(other, Const) )
2125  {
2126  POSTGIS_DEBUG(3, " no constant arguments - returning a default selectivity");
2127  PG_RETURN_FLOAT8(DEFAULT_ND_SEL);
2128  }
2129 
2130  /* Convert the constant to a BOX */
2131  if( ! gserialized_datum_get_gbox_p(((Const*)other)->constvalue, &search_box) )
2132  {
2133  POSTGIS_DEBUG(3, "search box is EMPTY");
2134  PG_RETURN_FLOAT8(0.0);
2135  }
2136  POSTGIS_DEBUGF(4, " requested search box is: %s", gbox_to_string(&search_box));
2137 
2138  /* Get pg_statistic row */
2139  examine_variable(root, (Node*)self, 0, &vardata);
2140  if ( vardata.statsTuple ) {
2141  nd_stats = pg_nd_stats_from_tuple(vardata.statsTuple, mode);
2142  }
2143  ReleaseVariableStats(vardata);
2144 
2145  if ( ! nd_stats )
2146  {
2147  POSTGIS_DEBUG(3, " unable to load stats from syscache, not analyzed yet?");
2148  PG_RETURN_FLOAT8(FALLBACK_ND_SEL);
2149  }
2150 
2151  POSTGIS_DEBUGF(4, " got stats:\n%s", nd_stats_to_json(nd_stats));
2152 
2153  /* Do the estimation! */
2154  selectivity = estimate_selectivity(&search_box, nd_stats, mode);
2155  POSTGIS_DEBUGF(3, " returning computed value: %f", selectivity);
2156 
2157  pfree(nd_stats);
2158  PG_RETURN_FLOAT8(selectivity);
2159 }
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:369
#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: